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> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 53 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 54 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/task.h> |
| 56 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_HAP_TCPSPLICE |
| 58 | #include <libtcpsplice.h> |
| 59 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 61 | #define DEBUG_PARSE_NO_SPEEDUP |
| 62 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 63 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 64 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 65 | * instruction. The first argument is the label for normal operation, and the |
| 66 | * second one is the break/continue instruction in the no_speedup mode. |
| 67 | */ |
| 68 | |
| 69 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 70 | #define QUICK_JUMP(x,y) y |
| 71 | #else |
| 72 | #define QUICK_JUMP(x,y) goto x |
| 73 | #endif |
| 74 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 75 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 76 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 77 | "HTTP/1.0 200 OK\r\n" |
| 78 | "Cache-Control: no-cache\r\n" |
| 79 | "Connection: close\r\n" |
| 80 | "Content-Type: text/html\r\n" |
| 81 | "\r\n" |
| 82 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 83 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | const struct chunk http_200_chunk = { |
| 85 | .str = (char *)&HTTP_200, |
| 86 | .len = sizeof(HTTP_200)-1 |
| 87 | }; |
| 88 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 89 | const char *HTTP_301 = |
| 90 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 91 | "Cache-Control: no-cache\r\n" |
| 92 | "Connection: close\r\n" |
| 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 95 | const char *HTTP_302 = |
| 96 | "HTTP/1.0 302 Found\r\n" |
| 97 | "Cache-Control: no-cache\r\n" |
| 98 | "Connection: close\r\n" |
| 99 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 100 | |
| 101 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 102 | const char *HTTP_303 = |
| 103 | "HTTP/1.0 303 See Other\r\n" |
| 104 | "Cache-Control: no-cache\r\n" |
| 105 | "Connection: close\r\n" |
| 106 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 107 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 109 | const char *HTTP_401_fmt = |
| 110 | "HTTP/1.0 401 Unauthorized\r\n" |
| 111 | "Cache-Control: no-cache\r\n" |
| 112 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 113 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 115 | "\r\n" |
| 116 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 117 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 118 | |
| 119 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 120 | [HTTP_ERR_400] = 400, |
| 121 | [HTTP_ERR_403] = 403, |
| 122 | [HTTP_ERR_408] = 408, |
| 123 | [HTTP_ERR_500] = 500, |
| 124 | [HTTP_ERR_502] = 502, |
| 125 | [HTTP_ERR_503] = 503, |
| 126 | [HTTP_ERR_504] = 504, |
| 127 | }; |
| 128 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 129 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 130 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 131 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 132 | "Cache-Control: no-cache\r\n" |
| 133 | "Connection: close\r\n" |
| 134 | "Content-Type: text/html\r\n" |
| 135 | "\r\n" |
| 136 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 137 | |
| 138 | [HTTP_ERR_403] = |
| 139 | "HTTP/1.0 403 Forbidden\r\n" |
| 140 | "Cache-Control: no-cache\r\n" |
| 141 | "Connection: close\r\n" |
| 142 | "Content-Type: text/html\r\n" |
| 143 | "\r\n" |
| 144 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 145 | |
| 146 | [HTTP_ERR_408] = |
| 147 | "HTTP/1.0 408 Request Time-out\r\n" |
| 148 | "Cache-Control: no-cache\r\n" |
| 149 | "Connection: close\r\n" |
| 150 | "Content-Type: text/html\r\n" |
| 151 | "\r\n" |
| 152 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 153 | |
| 154 | [HTTP_ERR_500] = |
| 155 | "HTTP/1.0 500 Server Error\r\n" |
| 156 | "Cache-Control: no-cache\r\n" |
| 157 | "Connection: close\r\n" |
| 158 | "Content-Type: text/html\r\n" |
| 159 | "\r\n" |
| 160 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 161 | |
| 162 | [HTTP_ERR_502] = |
| 163 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 164 | "Cache-Control: no-cache\r\n" |
| 165 | "Connection: close\r\n" |
| 166 | "Content-Type: text/html\r\n" |
| 167 | "\r\n" |
| 168 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 169 | |
| 170 | [HTTP_ERR_503] = |
| 171 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 172 | "Cache-Control: no-cache\r\n" |
| 173 | "Connection: close\r\n" |
| 174 | "Content-Type: text/html\r\n" |
| 175 | "\r\n" |
| 176 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 177 | |
| 178 | [HTTP_ERR_504] = |
| 179 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 180 | "Cache-Control: no-cache\r\n" |
| 181 | "Connection: close\r\n" |
| 182 | "Content-Type: text/html\r\n" |
| 183 | "\r\n" |
| 184 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 185 | |
| 186 | }; |
| 187 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 188 | /* We must put the messages here since GCC cannot initialize consts depending |
| 189 | * on strlen(). |
| 190 | */ |
| 191 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 192 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 193 | #define FD_SETS_ARE_BITFIELDS |
| 194 | #ifdef FD_SETS_ARE_BITFIELDS |
| 195 | /* |
| 196 | * This map is used with all the FD_* macros to check whether a particular bit |
| 197 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 198 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 199 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 200 | * exclusively to the macros. |
| 201 | */ |
| 202 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 203 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 204 | |
| 205 | #else |
| 206 | #error "Check if your OS uses bitfields for fd_sets" |
| 207 | #endif |
| 208 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 209 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si); |
| 210 | int sess_update_st_cer(struct session *s, struct stream_interface *si); |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 211 | void sess_establish(struct session *s, struct stream_interface *si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 212 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 213 | void init_proto_http() |
| 214 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 215 | int i; |
| 216 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 217 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 218 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 220 | if (!http_err_msgs[msg]) { |
| 221 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 222 | abort(); |
| 223 | } |
| 224 | |
| 225 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 226 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 227 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 228 | |
| 229 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 230 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 231 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 232 | * printable characters above. |
| 233 | */ |
| 234 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 235 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 236 | for (i = 0; i < 32; i++) { |
| 237 | FD_SET(i, hdr_encode_map); |
| 238 | FD_SET(i, url_encode_map); |
| 239 | } |
| 240 | for (i = 127; i < 256; i++) { |
| 241 | FD_SET(i, hdr_encode_map); |
| 242 | FD_SET(i, url_encode_map); |
| 243 | } |
| 244 | |
| 245 | tmp = "\"#{|}"; |
| 246 | while (*tmp) { |
| 247 | FD_SET(*tmp, hdr_encode_map); |
| 248 | tmp++; |
| 249 | } |
| 250 | |
| 251 | tmp = "\"#"; |
| 252 | while (*tmp) { |
| 253 | FD_SET(*tmp, url_encode_map); |
| 254 | tmp++; |
| 255 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 256 | |
| 257 | /* memory allocations */ |
| 258 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 259 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 260 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 261 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 262 | /* |
| 263 | * We have 26 list of methods (1 per first letter), each of which can have |
| 264 | * up to 3 entries (2 valid, 1 null). |
| 265 | */ |
| 266 | struct http_method_desc { |
| 267 | http_meth_t meth; |
| 268 | int len; |
| 269 | const char text[8]; |
| 270 | }; |
| 271 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 272 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 273 | ['C' - 'A'] = { |
| 274 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 275 | }, |
| 276 | ['D' - 'A'] = { |
| 277 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 278 | }, |
| 279 | ['G' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 281 | }, |
| 282 | ['H' - 'A'] = { |
| 283 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 284 | }, |
| 285 | ['P' - 'A'] = { |
| 286 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 287 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 288 | }, |
| 289 | ['T' - 'A'] = { |
| 290 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 291 | }, |
| 292 | /* rest is empty like this : |
| 293 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 294 | */ |
| 295 | }; |
| 296 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 297 | /* 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] | 298 | * 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] | 299 | * RFC2616 for those chars. |
| 300 | */ |
| 301 | |
| 302 | const char http_is_spht[256] = { |
| 303 | [' '] = 1, ['\t'] = 1, |
| 304 | }; |
| 305 | |
| 306 | const char http_is_crlf[256] = { |
| 307 | ['\r'] = 1, ['\n'] = 1, |
| 308 | }; |
| 309 | |
| 310 | const char http_is_lws[256] = { |
| 311 | [' '] = 1, ['\t'] = 1, |
| 312 | ['\r'] = 1, ['\n'] = 1, |
| 313 | }; |
| 314 | |
| 315 | const char http_is_sep[256] = { |
| 316 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 317 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 318 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 319 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 320 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 321 | }; |
| 322 | |
| 323 | const char http_is_ctl[256] = { |
| 324 | [0 ... 31] = 1, |
| 325 | [127] = 1, |
| 326 | }; |
| 327 | |
| 328 | /* |
| 329 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 330 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 331 | * them correctly. Instead, define every non-CTL char's status. |
| 332 | */ |
| 333 | const char http_is_token[256] = { |
| 334 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 335 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 336 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 337 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 338 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 339 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 340 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 341 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 342 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 343 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 344 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 345 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 346 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 347 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 348 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 349 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 350 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 351 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 352 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 353 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 354 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 355 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 356 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 357 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 358 | }; |
| 359 | |
| 360 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 361 | /* |
| 362 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 363 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 364 | */ |
| 365 | const char http_is_ver_token[256] = { |
| 366 | ['.'] = 1, ['/'] = 1, |
| 367 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 368 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 369 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 370 | }; |
| 371 | |
| 372 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 373 | #ifdef DEBUG_FULL |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 374 | static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 375 | #endif |
| 376 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 377 | static void http_sess_log(struct session *s); |
| 378 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 379 | /* |
| 380 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 381 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 382 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 383 | * of headers is automatically adjusted. The number of bytes added is returned |
| 384 | * on success, otherwise <0 is returned indicating an error. |
| 385 | */ |
| 386 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 387 | struct hdr_idx *hdr_idx, const char *text) |
| 388 | { |
| 389 | int bytes, len; |
| 390 | |
| 391 | len = strlen(text); |
| 392 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 393 | if (!bytes) |
| 394 | return -1; |
| 395 | msg->eoh += bytes; |
| 396 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 401 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 402 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 403 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 404 | * of headers is automatically adjusted. The number of bytes added is returned |
| 405 | * on success, otherwise <0 is returned indicating an error. |
| 406 | */ |
| 407 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 408 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 409 | { |
| 410 | int bytes; |
| 411 | |
| 412 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 413 | if (!bytes) |
| 414 | return -1; |
| 415 | msg->eoh += bytes; |
| 416 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 417 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 418 | |
| 419 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 420 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 421 | * If so, returns the position of the first non-space character relative to |
| 422 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 423 | * to return a pointer to the place after the first space. Returns 0 if the |
| 424 | * header name does not match. Checks are case-insensitive. |
| 425 | */ |
| 426 | int http_header_match2(const char *hdr, const char *end, |
| 427 | const char *name, int len) |
| 428 | { |
| 429 | const char *val; |
| 430 | |
| 431 | if (hdr + len >= end) |
| 432 | return 0; |
| 433 | if (hdr[len] != ':') |
| 434 | return 0; |
| 435 | if (strncasecmp(hdr, name, len) != 0) |
| 436 | return 0; |
| 437 | val = hdr + len + 1; |
| 438 | while (val < end && HTTP_IS_SPHT(*val)) |
| 439 | val++; |
| 440 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 441 | return len + 2; /* we may replace starting from second space */ |
| 442 | return val - hdr; |
| 443 | } |
| 444 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 445 | /* Find the end of the header value contained between <s> and <e>. |
| 446 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 447 | * a valid header to return a valid result. |
| 448 | */ |
| 449 | const char *find_hdr_value_end(const char *s, const char *e) |
| 450 | { |
| 451 | int quoted, qdpair; |
| 452 | |
| 453 | quoted = qdpair = 0; |
| 454 | for (; s < e; s++) { |
| 455 | if (qdpair) qdpair = 0; |
| 456 | else if (quoted && *s == '\\') qdpair = 1; |
| 457 | else if (quoted && *s == '"') quoted = 0; |
| 458 | else if (*s == '"') quoted = 1; |
| 459 | else if (*s == ',') return s; |
| 460 | } |
| 461 | return s; |
| 462 | } |
| 463 | |
| 464 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 465 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 466 | * structure holds everything necessary to use the header and find next |
| 467 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 468 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 469 | * 1 when it finds a value, and 0 when there is no more. |
| 470 | */ |
| 471 | int http_find_header2(const char *name, int len, |
| 472 | const char *sol, struct hdr_idx *idx, |
| 473 | struct hdr_ctx *ctx) |
| 474 | { |
| 475 | __label__ return_hdr, next_hdr; |
| 476 | const char *eol, *sov; |
| 477 | int cur_idx; |
| 478 | |
| 479 | if (ctx->idx) { |
| 480 | /* We have previously returned a value, let's search |
| 481 | * another one on the same line. |
| 482 | */ |
| 483 | cur_idx = ctx->idx; |
| 484 | sol = ctx->line; |
| 485 | sov = sol + ctx->val + ctx->vlen; |
| 486 | eol = sol + idx->v[cur_idx].len; |
| 487 | |
| 488 | if (sov >= eol) |
| 489 | /* no more values in this header */ |
| 490 | goto next_hdr; |
| 491 | |
| 492 | /* values remaining for this header, skip the comma */ |
| 493 | sov++; |
| 494 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 495 | sov++; |
| 496 | |
| 497 | goto return_hdr; |
| 498 | } |
| 499 | |
| 500 | /* first request for this header */ |
| 501 | sol += hdr_idx_first_pos(idx); |
| 502 | cur_idx = hdr_idx_first_idx(idx); |
| 503 | |
| 504 | while (cur_idx) { |
| 505 | eol = sol + idx->v[cur_idx].len; |
| 506 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 507 | if (len == 0) { |
| 508 | /* No argument was passed, we want any header. |
| 509 | * To achieve this, we simply build a fake request. */ |
| 510 | while (sol + len < eol && sol[len] != ':') |
| 511 | len++; |
| 512 | name = sol; |
| 513 | } |
| 514 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 515 | if ((len < eol - sol) && |
| 516 | (sol[len] == ':') && |
| 517 | (strncasecmp(sol, name, len) == 0)) { |
| 518 | |
| 519 | sov = sol + len + 1; |
| 520 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 521 | sov++; |
| 522 | return_hdr: |
| 523 | ctx->line = sol; |
| 524 | ctx->idx = cur_idx; |
| 525 | ctx->val = sov - sol; |
| 526 | |
| 527 | eol = find_hdr_value_end(sov, eol); |
| 528 | ctx->vlen = eol - sov; |
| 529 | return 1; |
| 530 | } |
| 531 | next_hdr: |
| 532 | sol = eol + idx->v[cur_idx].cr + 1; |
| 533 | cur_idx = idx->v[cur_idx].next; |
| 534 | } |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | int http_find_header(const char *name, |
| 539 | const char *sol, struct hdr_idx *idx, |
| 540 | struct hdr_ctx *ctx) |
| 541 | { |
| 542 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 543 | } |
| 544 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 545 | /* This function shuts down the buffers on the server side, and sets indicators |
| 546 | * accordingly. The server's fd is supposed to already be closed. Note that if |
| 547 | * <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] | 548 | */ |
| 549 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 550 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 551 | { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 552 | buffer_write_ena(t->rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 553 | buffer_shutw(t->req); |
| 554 | buffer_shutr(t->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 555 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 556 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 557 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 558 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 559 | } |
| 560 | if (!(t->flags & SN_ERR_MASK)) |
| 561 | t->flags |= err; |
| 562 | if (!(t->flags & SN_FINST_MASK)) |
| 563 | t->flags |= finst; |
| 564 | } |
| 565 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 566 | /* This function returns the appropriate error location for the given session |
| 567 | * and message. |
| 568 | */ |
| 569 | |
| 570 | struct chunk *error_message(struct session *s, int msgnum) |
| 571 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 572 | if (s->be->errmsg[msgnum].str) |
| 573 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 574 | else if (s->fe->errmsg[msgnum].str) |
| 575 | return &s->fe->errmsg[msgnum]; |
| 576 | else |
| 577 | return &http_err_chunks[msgnum]; |
| 578 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 579 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 580 | /* |
| 581 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 582 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 583 | */ |
| 584 | static http_meth_t find_http_meth(const char *str, const int len) |
| 585 | { |
| 586 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 587 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | |
| 589 | m = ((unsigned)*str - 'A'); |
| 590 | |
| 591 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 592 | for (h = http_methods[m]; h->len > 0; h++) { |
| 593 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 594 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 595 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 596 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 597 | }; |
| 598 | return HTTP_METH_OTHER; |
| 599 | } |
| 600 | return HTTP_METH_NONE; |
| 601 | |
| 602 | } |
| 603 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 604 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 605 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 606 | * It is returned otherwise. |
| 607 | */ |
| 608 | static char * |
| 609 | http_get_path(struct http_txn *txn) |
| 610 | { |
| 611 | char *ptr, *end; |
| 612 | |
| 613 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 614 | end = ptr + txn->req.sl.rq.u_l; |
| 615 | |
| 616 | if (ptr >= end) |
| 617 | return NULL; |
| 618 | |
| 619 | /* RFC2616, par. 5.1.2 : |
| 620 | * Request-URI = "*" | absuri | abspath | authority |
| 621 | */ |
| 622 | |
| 623 | if (*ptr == '*') |
| 624 | return NULL; |
| 625 | |
| 626 | if (isalpha((unsigned char)*ptr)) { |
| 627 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 628 | ptr++; |
| 629 | while (ptr < end && |
| 630 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 631 | ptr++; |
| 632 | /* skip '://' */ |
| 633 | if (ptr == end || *ptr++ != ':') |
| 634 | return NULL; |
| 635 | if (ptr == end || *ptr++ != '/') |
| 636 | return NULL; |
| 637 | if (ptr == end || *ptr++ != '/') |
| 638 | return NULL; |
| 639 | } |
| 640 | /* skip [user[:passwd]@]host[:[port]] */ |
| 641 | |
| 642 | while (ptr < end && *ptr != '/') |
| 643 | ptr++; |
| 644 | |
| 645 | if (ptr == end) |
| 646 | return NULL; |
| 647 | |
| 648 | /* OK, we got the '/' ! */ |
| 649 | return ptr; |
| 650 | } |
| 651 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 652 | |
| 653 | /* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR. |
| 654 | * Other input states are simply ignored. |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 655 | * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 656 | * Flags must have previously been updated for timeouts and other conditions. |
| 657 | */ |
| 658 | void sess_update_stream_int(struct session *s, struct stream_interface *si) |
| 659 | { |
| 660 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 661 | now_ms, __FUNCTION__, |
| 662 | s, |
| 663 | s->req, s->rep, |
| 664 | s->req->rex, s->rep->wex, |
| 665 | s->req->flags, s->rep->flags, |
| 666 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 667 | |
| 668 | if (si->state == SI_ST_ASS) { |
| 669 | /* Server assigned to connection request, we have to try to connect now */ |
| 670 | int conn_err; |
| 671 | |
| 672 | conn_err = connect_server(s); |
| 673 | if (conn_err == SN_ERR_NONE) { |
| 674 | /* state = SI_ST_CON now */ |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | /* We have received a synchronous error. We might have to |
| 679 | * abort, retry immediately or redispatch. |
| 680 | */ |
| 681 | if (conn_err == SN_ERR_INTERNAL) { |
| 682 | if (!si->err_type) { |
| 683 | si->err_type = SI_ET_CONN_OTHER; |
| 684 | si->err_loc = s->srv; |
| 685 | } |
| 686 | |
| 687 | if (s->srv) |
| 688 | s->srv->cum_sess++; |
| 689 | if (s->srv) |
| 690 | s->srv->failed_conns++; |
| 691 | s->be->failed_conns++; |
| 692 | |
| 693 | /* release other sessions waiting for this server */ |
| 694 | if (may_dequeue_tasks(s->srv, s->be)) |
| 695 | process_srv_queue(s->srv); |
| 696 | |
| 697 | /* Failed and not retryable. */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 698 | buffer_shutr(si->ib); |
| 699 | buffer_shutw(si->ob); |
| 700 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 701 | |
| 702 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 703 | |
| 704 | /* no session was ever accounted for this server */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 705 | si->state = SI_ST_CLO; |
| 706 | return; |
| 707 | } |
| 708 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 709 | /* We are facing a retryable error, but we don't want to run a |
| 710 | * turn-around now, as the problem is likely a source port |
| 711 | * allocation problem, so we want to retry now. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 712 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 713 | si->state = SI_ST_CER; |
| 714 | si->flags &= ~SI_FL_ERR; |
| 715 | sess_update_st_cer(s, si); |
| 716 | /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | else if (si->state == SI_ST_QUE) { |
| 720 | /* connection request was queued, check for any update */ |
| 721 | if (!s->pend_pos) { |
| 722 | /* The connection is not in the queue anymore. Either |
| 723 | * we have a server connection slot available and we |
| 724 | * go directly to the assigned state, or we need to |
| 725 | * load-balance first and go to the INI state. |
| 726 | */ |
| 727 | si->exp = TICK_ETERNITY; |
| 728 | if (unlikely(!(s->flags & SN_ASSIGNED))) |
| 729 | si->state = SI_ST_REQ; |
| 730 | else { |
| 731 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 732 | si->state = SI_ST_ASS; |
| 733 | } |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | /* Connection request still in queue... */ |
| 738 | if (si->flags & SI_FL_EXP) { |
| 739 | /* ... and timeout expired */ |
| 740 | si->exp = TICK_ETERNITY; |
| 741 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 742 | if (s->srv) |
| 743 | s->srv->failed_conns++; |
| 744 | s->be->failed_conns++; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 745 | buffer_shutr(si->ib); |
| 746 | buffer_shutw(si->ob); |
| 747 | si->ob->flags |= BF_WRITE_TIMEOUT; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 748 | if (!si->err_type) |
| 749 | si->err_type = SI_ET_QUEUE_TO; |
| 750 | si->state = SI_ST_CLO; |
| 751 | return; |
| 752 | } |
| 753 | |
| 754 | /* Connection remains in queue, check if we have to abort it */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 755 | if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */ |
| 756 | ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */ |
| 757 | (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 758 | /* give up */ |
| 759 | si->exp = TICK_ETERNITY; |
| 760 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 761 | buffer_shutr(si->ib); |
| 762 | buffer_shutw(si->ob); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 763 | si->err_type |= SI_ET_QUEUE_ABRT; |
| 764 | si->state = SI_ST_CLO; |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | /* Nothing changed */ |
| 769 | return; |
| 770 | } |
| 771 | else if (si->state == SI_ST_TAR) { |
| 772 | /* Connection request might be aborted */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 773 | if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */ |
| 774 | ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */ |
| 775 | (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 776 | /* give up */ |
| 777 | si->exp = TICK_ETERNITY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 778 | buffer_shutr(si->ib); |
| 779 | buffer_shutw(si->ob); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 780 | si->err_type |= SI_ET_CONN_ABRT; |
| 781 | si->state = SI_ST_CLO; |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | if (!(si->flags & SI_FL_EXP)) |
| 786 | return; /* still in turn-around */ |
| 787 | |
| 788 | si->exp = TICK_ETERNITY; |
| 789 | |
| 790 | /* we keep trying on the same server as long as the session is |
| 791 | * marked "assigned". |
| 792 | * FIXME: Should we force a redispatch attempt when the server is down ? |
| 793 | */ |
| 794 | if (s->flags & SN_ASSIGNED) |
| 795 | si->state = SI_ST_ASS; |
| 796 | else |
| 797 | si->state = SI_ST_REQ; |
| 798 | return; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | |
| 803 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 804 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 805 | * left unchanged and will follow normal proxy processing. |
| 806 | */ |
| 807 | static void perform_http_redirect(struct session *s, struct stream_interface *si) |
| 808 | { |
| 809 | struct http_txn *txn; |
| 810 | struct chunk rdr; |
| 811 | char *path; |
| 812 | int len; |
| 813 | |
| 814 | /* 1: create the response header */ |
| 815 | rdr.len = strlen(HTTP_302); |
| 816 | rdr.str = trash; |
| 817 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 818 | |
| 819 | /* 2: add the server's prefix */ |
| 820 | if (rdr.len + s->srv->rdr_len > sizeof(trash)) |
| 821 | return; |
| 822 | |
| 823 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 824 | rdr.len += s->srv->rdr_len; |
| 825 | |
| 826 | /* 3: add the request URI */ |
| 827 | txn = &s->txn; |
| 828 | path = http_get_path(txn); |
| 829 | if (!path) |
| 830 | return; |
| 831 | |
| 832 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 833 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 834 | return; |
| 835 | |
| 836 | memcpy(rdr.str + rdr.len, path, len); |
| 837 | rdr.len += len; |
| 838 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 839 | rdr.len += 4; |
| 840 | |
| 841 | /* prepare to return without error. */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 842 | buffer_shutr(si->ib); |
| 843 | buffer_shutw(si->ob); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 844 | si->err_type = SI_ET_NONE; |
| 845 | si->err_loc = NULL; |
| 846 | si->state = SI_ST_CLO; |
| 847 | |
| 848 | /* send the message */ |
| 849 | srv_close_with_err(s, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 850 | |
| 851 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 852 | if (s->srv) |
| 853 | s->srv->cum_sess++; |
| 854 | } |
| 855 | |
| 856 | |
| 857 | /* This function initiates a server connection request on a stream interface |
| 858 | * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS, |
| 859 | * indicating that a server has been assigned. It may also return SI_ST_QUE, |
| 860 | * or SI_ST_CLO upon error. |
| 861 | */ |
| 862 | static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) { |
| 863 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 864 | now_ms, __FUNCTION__, |
| 865 | s, |
| 866 | s->req, s->rep, |
| 867 | s->req->rex, s->rep->wex, |
| 868 | s->req->flags, s->rep->flags, |
| 869 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 870 | |
| 871 | if (si->state != SI_ST_REQ) |
| 872 | return; |
| 873 | |
| 874 | /* Try to assign a server */ |
| 875 | if (srv_redispatch_connect(s) != 0) { |
| 876 | /* We did not get a server. Either we queued the |
| 877 | * connection request, or we encountered an error. |
| 878 | */ |
| 879 | if (si->state == SI_ST_QUE) |
| 880 | return; |
| 881 | |
| 882 | /* we did not get any server, let's check the cause */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 883 | buffer_shutr(si->ib); |
| 884 | buffer_shutw(si->ob); |
| 885 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 886 | if (!si->err_type) |
| 887 | si->err_type = SI_ET_CONN_OTHER; |
| 888 | si->state = SI_ST_CLO; |
| 889 | return; |
| 890 | } |
| 891 | |
| 892 | /* The server is assigned */ |
| 893 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 894 | si->state = SI_ST_ASS; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | /* Return the error message corresponding to err_type. It is assumed |
| 899 | * that the server side is closed. Note that err_type is actually a |
| 900 | * bitmask, where almost only aborts may be cumulated with other |
| 901 | * values. We consider that aborted operations are more important |
| 902 | * than timeouts or errors due to the fact that nobody else in the |
| 903 | * logs might explain incomplete retries. All others should avoid |
| 904 | * being cumulated. It should normally not be possible to have multiple |
| 905 | * aborts at once, but just in case, the first one in sequence is reported. |
| 906 | */ |
| 907 | void return_srv_error(struct session *s, int err_type) |
| 908 | { |
| 909 | s->req->wex = TICK_ETERNITY; |
| 910 | |
| 911 | if (err_type & SI_ET_QUEUE_ABRT) |
| 912 | srv_close_with_err(s, SN_ERR_CLICL, SN_FINST_Q, |
| 913 | 503, error_message(s, HTTP_ERR_503)); |
| 914 | else if (err_type & SI_ET_CONN_ABRT) |
| 915 | srv_close_with_err(s, SN_ERR_CLICL, SN_FINST_C, |
| 916 | 503, error_message(s, HTTP_ERR_503)); |
| 917 | else if (err_type & SI_ET_QUEUE_TO) |
| 918 | srv_close_with_err(s, SN_ERR_SRVTO, SN_FINST_Q, |
| 919 | 503, error_message(s, HTTP_ERR_503)); |
| 920 | else if (err_type & SI_ET_QUEUE_ERR) |
| 921 | srv_close_with_err(s, SN_ERR_SRVCL, SN_FINST_Q, |
| 922 | 503, error_message(s, HTTP_ERR_503)); |
| 923 | else if (err_type & SI_ET_CONN_TO) |
| 924 | srv_close_with_err(s, SN_ERR_SRVTO, SN_FINST_C, |
| 925 | 503, error_message(s, HTTP_ERR_503)); |
| 926 | else if (err_type & SI_ET_CONN_ERR) |
| 927 | srv_close_with_err(s, SN_ERR_SRVCL, SN_FINST_C, |
| 928 | 503, error_message(s, HTTP_ERR_503)); |
| 929 | else /* SI_ET_CONN_OTHER and others */ |
| 930 | srv_close_with_err(s, SN_ERR_INTERNAL, SN_FINST_C, |
| 931 | 500, error_message(s, HTTP_ERR_500)); |
| 932 | } |
| 933 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 934 | /* Processes the client, server, request and response jobs of a session task, |
| 935 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 936 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 937 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 938 | * nothing too many times, the request and response buffers flags are monitored |
| 939 | * and each function is called only if at least another function has changed at |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 940 | * least one flag it is interested in. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 941 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 942 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 943 | { |
| 944 | struct session *s = t->context; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 945 | int resync; |
| 946 | unsigned int rqf_cli, rpf_cli; |
| 947 | unsigned int rqf_srv, rpf_srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 949 | //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__, |
| 950 | // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 951 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 952 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 953 | * stream interfaces when their timeouts have expired. |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 954 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 955 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 956 | stream_int_check_timeouts(&s->si[0]); |
| 957 | stream_int_check_timeouts(&s->si[1]); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 958 | } |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 959 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 960 | /* 1b: check for low-level errors reported at the stream interface. |
| 961 | * First we check if it's a retryable error (in which case we don't |
| 962 | * want to tell the buffer). Otherwise we report the error one level |
| 963 | * upper by setting flags into the buffers. Note that the side towards |
| 964 | * the client cannot have connect (hence retryable) errors. |
| 965 | */ |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 966 | if (s->si[0].state == SI_ST_EST) { |
| 967 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 968 | s->si[0].state = SI_ST_DIS; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 969 | fd_delete(s->si[0].fd); |
| 970 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 971 | } |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 972 | } |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 973 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 974 | if (s->si[1].state == SI_ST_EST) { |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 975 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 976 | s->si[1].state = SI_ST_DIS; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 977 | fd_delete(s->si[1].fd); |
| 978 | stream_int_report_error(&s->si[1]); |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 979 | /////////// FIXME: the following must move somewhere else |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 980 | s->be->failed_resp++; |
| 981 | if (s->srv) |
| 982 | s->srv->failed_resp++; |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 983 | } |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 984 | } |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 985 | else if (s->si[1].state >= SI_ST_QUE && s->si[1].state <= SI_ST_CON) { |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 986 | /* Maybe we were trying to establish a connection on the server side ? */ |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 987 | if (s->si[1].state == SI_ST_CON) { |
| 988 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 989 | sess_update_st_cer(s, &s->si[1]); |
| 990 | else if (s->si[1].state == SI_ST_EST) |
| 991 | sess_establish(s, &s->si[1]); |
| 992 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 993 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 994 | /* now try to complete any initiated connection setup */ |
| 995 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 996 | do { |
| 997 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 998 | * to give a chance to step 2 to perform a redirect if needed. |
| 999 | */ |
| 1000 | sess_update_stream_int(s, &s->si[1]); |
| 1001 | if (s->si[1].state == SI_ST_REQ) |
| 1002 | sess_prepare_conn_req(s, &s->si[1]); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1003 | |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 1004 | ///// FIXME: redirect should be handled later |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1005 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 1006 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1007 | perform_http_redirect(s, &s->si[1]); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1008 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1009 | } while (s->si[1].state == SI_ST_ASS); |
| 1010 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 1013 | /////// FIXME: do that later |
| 1014 | /* FIXME: we might have got errors above, and we should process them below */ |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 1015 | if ((s->si[1].state == SI_ST_DIS || s->si[1].state == SI_ST_CLO) && |
| 1016 | s->si[1].prev_state != SI_ST_CLO && s->si[1].err_type != SI_ET_NONE) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1017 | return_srv_error(s, s->si[1].err_type); |
| 1018 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1019 | |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 1020 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 1021 | * buffers and/or stream interfaces when their timeouts have expired. |
| 1022 | */ |
| 1023 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 1024 | buffer_check_timeouts(s->req); |
| 1025 | buffer_check_timeouts(s->rep); |
| 1026 | } |
| 1027 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1028 | /* 1c: Manage buffer timeouts. */ |
| 1029 | if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 1030 | if (s->req->flags & BF_READ_TIMEOUT) { |
| 1031 | buffer_shutr(s->req); |
| 1032 | s->req->cons->shutr(s->req->prod); |
| 1033 | } |
| 1034 | if (s->req->flags & BF_WRITE_TIMEOUT) { |
| 1035 | buffer_shutw(s->req); |
| 1036 | s->req->cons->shutw(s->req->cons); |
| 1037 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1040 | if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 1041 | if (s->rep->flags & BF_READ_TIMEOUT) { |
| 1042 | buffer_shutr(s->rep); |
| 1043 | s->rep->cons->shutr(s->rep->prod); |
| 1044 | } |
| 1045 | if (s->rep->flags & BF_WRITE_TIMEOUT) { |
| 1046 | buffer_shutw(s->rep); |
| 1047 | s->rep->cons->shutw(s->rep->cons); |
| 1048 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
Willy Tarreau | d7704b5 | 2008-09-04 11:51:16 +0200 | [diff] [blame] | 1051 | /* 2: Check if we need to close the write side. This can only happen |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 1052 | * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear |
| 1053 | * from low level, and neither HIJACK nor SHUTW can disappear from low |
| 1054 | * level. |
| 1055 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1056 | if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) || |
| 1057 | unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) { |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 1058 | buffer_shutw(s->req); |
| 1059 | s->req->cons->shutw(s->req->cons); |
| 1060 | } |
| 1061 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 1062 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW)) { |
| 1063 | /* write closed on server side, let's forward it to the client */ |
| 1064 | buffer_shutr_now(s->req); |
| 1065 | s->req->prod->shutr(s->req->prod); |
| 1066 | } |
| 1067 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1068 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) || |
| 1069 | unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW)) { |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 1070 | buffer_shutw(s->rep); |
| 1071 | s->rep->cons->shutw(s->rep->cons); |
| 1072 | } |
| 1073 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 1074 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW)) { |
| 1075 | /* write closed on client side, let's forward it to the server */ |
| 1076 | buffer_shutr_now(s->rep); |
| 1077 | s->rep->prod->shutr(s->rep->prod); |
| 1078 | } |
| 1079 | |
Willy Tarreau | d7704b5 | 2008-09-04 11:51:16 +0200 | [diff] [blame] | 1080 | /* 3: When a server-side connection is released, we have to |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1081 | * count it and check for pending connections on this server. |
| 1082 | */ |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 1083 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 1084 | s->req->cons->state = SI_ST_CLO; |
| 1085 | if (s->srv) { |
| 1086 | if (s->flags & SN_CURR_SESS) { |
| 1087 | s->flags &= ~SN_CURR_SESS; |
| 1088 | s->srv->cur_sess--; |
| 1089 | } |
| 1090 | sess_change_server(s, NULL); |
| 1091 | if (may_dequeue_tasks(s->srv, s->be)) |
| 1092 | process_srv_queue(s->srv); |
| 1093 | } |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 1096 | /* nothing special to be done on client side */ |
| 1097 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 1098 | s->req->prod->state = SI_ST_CLO; |
| 1099 | |
| 1100 | /* |
| 1101 | * Note: all transient states (REQ, CER, DIS) have been eliminated at |
| 1102 | * this point. |
| 1103 | */ |
| 1104 | |
| 1105 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1106 | /* Dirty trick: force one first pass everywhere */ |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1107 | rqf_cli = rqf_srv = ~s->req->flags; |
| 1108 | rpf_cli = rpf_srv = ~s->rep->flags; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 1109 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1110 | /* well, SI_ST_EST state is already handled properly */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1111 | if (s->req->prod->state == SI_ST_EST) { |
| 1112 | rqf_cli = s->req->flags; |
| 1113 | rpf_cli = s->rep->flags; |
| 1114 | } |
| 1115 | |
| 1116 | if (s->req->cons->state == SI_ST_EST) { |
| 1117 | rqf_srv = s->req->flags; |
| 1118 | rpf_srv = s->rep->flags; |
| 1119 | } |
| 1120 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1121 | do { |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1122 | DPRINTF(stderr,"[%u] %s: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n", |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1123 | now_ms, __FUNCTION__, |
| 1124 | t, |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1125 | s, s->flags, |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1126 | s->req, s->rep, |
| 1127 | s->req->rex, s->rep->wex, |
| 1128 | s->req->flags, s->rep->flags, |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1129 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 1130 | s->rep->cons->err_type, s->req->cons->err_type, |
| 1131 | s->conn_retries); |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1132 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1133 | resync = 0; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1134 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1135 | /* Maybe resync client FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1136 | if (s->rep->cons->state != SI_ST_CLO) { |
| 1137 | if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) || |
| 1138 | ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1139 | stream_sock_data_update(s->rep->cons->fd); |
| 1140 | rqf_cli = s->req->flags; |
| 1141 | rpf_cli = s->rep->flags; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1142 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1143 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1144 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1145 | /* Maybe resync server FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1146 | if (s->req->cons->state != SI_ST_CLO) { |
| 1147 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 1148 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1149 | if (s->req->cons->state == SI_ST_INI && |
| 1150 | (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA) { |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1151 | s->req->cons->state = SI_ST_REQ; |
| 1152 | do { |
| 1153 | sess_prepare_conn_req(s, &s->si[1]); |
| 1154 | if (s->si[1].state != SI_ST_ASS) |
| 1155 | break; |
| 1156 | if (s->srv && s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1157 | perform_http_redirect(s, &s->si[1]); |
| 1158 | sess_update_stream_int(s, &s->si[1]); |
| 1159 | } while (s->si[1].state == SI_ST_REQ || s->si[1].state == SI_ST_ASS); |
| 1160 | |
| 1161 | /* FIXME: how to process this type of errors ? */ |
| 1162 | if (s->si[1].state == SI_ST_CLO && s->si[1].err_type != SI_ET_NONE) |
| 1163 | return_srv_error(s, s->si[1].err_type); |
| 1164 | resync = 1; |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1165 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1166 | |
| 1167 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1168 | if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) && |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 1169 | s->be->options & PR_O_FORCE_CLO && |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1170 | s->rep->flags & BF_READ_ACTIVITY) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 1171 | /* We want to force the connection to the server to close, |
| 1172 | * and the server has begun to respond. That's the right |
| 1173 | * time. |
| 1174 | */ |
| 1175 | buffer_shutw_now(s->req); |
| 1176 | } |
| 1177 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1178 | stream_sock_data_update(s->req->cons->fd); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 1179 | |
| 1180 | /* When a server-side connection is released, we have to |
| 1181 | * count it and check for pending connections on this server. |
| 1182 | */ |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 1183 | if (s->req->cons->state == SI_ST_DIS) { |
| 1184 | s->req->cons->state = SI_ST_CLO; |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 1185 | if (s->srv) { |
| 1186 | if (s->flags & SN_CURR_SESS) { |
| 1187 | s->flags &= ~SN_CURR_SESS; |
| 1188 | s->srv->cur_sess--; |
| 1189 | } |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 1190 | sess_change_server(s, NULL); |
| 1191 | if (may_dequeue_tasks(s->srv, s->be)) |
| 1192 | process_srv_queue(s->srv); |
| 1193 | } |
| 1194 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1195 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1196 | rqf_srv = s->req->flags; |
| 1197 | rpf_srv = s->rep->flags; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1198 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /* we may have to resync because of pending connections */ |
| 1202 | if (resync) |
| 1203 | continue; |
| 1204 | |
| 1205 | /**** Process layer 7 below ****/ |
| 1206 | |
| 1207 | /* Analyse request */ |
| 1208 | if (s->req->flags & BF_MASK_ANALYSER) { |
| 1209 | unsigned int flags = s->req->flags; |
| 1210 | |
| 1211 | if (s->req->prod->state >= SI_ST_EST) { |
| 1212 | /* it's up to the analysers to reset write_ena */ |
| 1213 | buffer_write_ena(s->req); |
| 1214 | if (s->req->analysers) |
| 1215 | process_request(s); |
| 1216 | } |
| 1217 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1218 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1219 | if (s->req->flags != flags) |
| 1220 | resync = 1; |
| 1221 | } |
| 1222 | |
| 1223 | /* Analyse response */ |
| 1224 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 1225 | /* In inject mode, we wake up everytime something has |
| 1226 | * happened on the write side of the buffer. |
| 1227 | */ |
| 1228 | unsigned int flags = s->rep->flags; |
| 1229 | |
| 1230 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 1231 | !(s->rep->flags & BF_FULL)) { |
| 1232 | produce_content(s); |
| 1233 | } |
| 1234 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1235 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1236 | if (s->rep->flags != flags) |
| 1237 | resync = 1; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1238 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1239 | else if (s->rep->flags & BF_MASK_ANALYSER) { |
| 1240 | unsigned int flags = s->rep->flags; |
| 1241 | |
| 1242 | if (s->rep->prod->state >= SI_ST_EST) { |
| 1243 | /* it's up to the analysers to reset write_ena */ |
| 1244 | buffer_write_ena(s->rep); |
| 1245 | if (s->rep->analysers) |
| 1246 | process_response(s); |
| 1247 | } |
| 1248 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1249 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1250 | if (s->rep->flags != flags) |
| 1251 | resync = 1; |
| 1252 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1253 | |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1254 | /* For the moment, we need to clean the client and server flags that |
| 1255 | * have vanished. This is just a temporary measure though. |
| 1256 | */ |
| 1257 | rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1258 | rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1259 | rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1260 | rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1261 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1262 | |
Willy Tarreau | a37095b | 2008-09-03 11:37:47 +0200 | [diff] [blame] | 1263 | /* This is needed only when debugging is enabled, to indicate |
| 1264 | * client-side or server-side close. Please note that in the unlikely |
| 1265 | * event where both sides would close at once, the sequence is reported |
| 1266 | * on the server side first. |
| 1267 | */ |
| 1268 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1269 | (!(global.mode & MODE_QUIET) || |
| 1270 | (global.mode & MODE_VERBOSE)))) { |
| 1271 | int len; |
| 1272 | |
| 1273 | if (s->si[1].state == SI_ST_CLO && |
| 1274 | s->si[1].prev_state == SI_ST_EST) { |
| 1275 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1276 | s->uniq_id, s->be->id, |
| 1277 | (unsigned short)s->si[0].fd, |
| 1278 | (unsigned short)s->si[1].fd); |
| 1279 | write(1, trash, len); |
| 1280 | } |
| 1281 | |
| 1282 | if (s->si[0].state == SI_ST_CLO && |
| 1283 | s->si[0].prev_state == SI_ST_EST) { |
| 1284 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1285 | s->uniq_id, s->be->id, |
| 1286 | (unsigned short)s->si[0].fd, |
| 1287 | (unsigned short)s->si[1].fd); |
| 1288 | write(1, trash, len); |
| 1289 | } |
| 1290 | } |
| 1291 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1292 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1293 | (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] | 1294 | |
| 1295 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1296 | session_process_counters(s); |
| 1297 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1298 | if (s->rep->cons->state == SI_ST_EST) |
| 1299 | stream_sock_data_finish(s->rep->cons->fd); |
| 1300 | |
| 1301 | if (s->req->cons->state == SI_ST_EST) |
| 1302 | stream_sock_data_finish(s->req->cons->fd); |
| 1303 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 1304 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1305 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1306 | s->si[0].prev_state = s->si[0].state; |
| 1307 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1308 | s->si[0].flags = s->si[1].flags = SI_FL_NONE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1309 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 1310 | /* Trick: if a request is being waiting for the server to respond, |
| 1311 | * and if we know the server can timeout, we don't want the timeout |
| 1312 | * to expire on the client side first, but we're still interested |
| 1313 | * in passing data from the client to the server (eg: POST). Thus, |
| 1314 | * we can cancel the client's request timeout if the server's |
| 1315 | * request timeout is set and the server has not yet sent a response. |
| 1316 | */ |
| 1317 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1318 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 1319 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 1320 | s->req->rex = TICK_ETERNITY; |
| 1321 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1322 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1323 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1324 | if (s->req->analysers) |
| 1325 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1326 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 1327 | if (s->si[0].exp) |
| 1328 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1329 | |
| 1330 | if (s->si[1].exp) |
| 1331 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1332 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 1333 | #ifdef DEBUG_FULL |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1334 | fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u, cs=%d, ss=%d\n", |
| 1335 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex, s->si[0].state, s->si[1].state); |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 1336 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1337 | /* restore t to its place in the task list */ |
| 1338 | task_queue(t); |
| 1339 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 1340 | #ifdef DEBUG_DEV |
| 1341 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 1342 | if (!t->expire) |
| 1343 | ABORT_NOW(); |
| 1344 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 1345 | *next = t->expire; |
| 1346 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1349 | s->fe->feconn--; |
| 1350 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1351 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1352 | actconn--; |
| 1353 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 1354 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1355 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1356 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1357 | 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] | 1358 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1359 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1360 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1361 | write(1, trash, len); |
| 1362 | } |
| 1363 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1364 | 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] | 1365 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1366 | |
| 1367 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 1368 | if (s->logs.logwait && |
| 1369 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1370 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 1371 | if (s->fe->to_log & LW_REQ) |
| 1372 | http_sess_log(s); |
| 1373 | else |
| 1374 | tcp_sess_log(s); |
| 1375 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1376 | |
| 1377 | /* the task MUST not be in the run queue anymore */ |
| 1378 | task_delete(t); |
| 1379 | session_free(s); |
| 1380 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1381 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1385 | extern const char sess_term_cond[8]; |
| 1386 | extern const char sess_fin_state[8]; |
| 1387 | extern const char *monthname[12]; |
| 1388 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 1389 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 1390 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 1391 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1392 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 1393 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1395 | /* |
| 1396 | * send a log for the session when we have enough info about it. |
| 1397 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1399 | static void http_sess_log(struct session *s) |
| 1400 | { |
| 1401 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1402 | struct proxy *fe = s->fe; |
| 1403 | struct proxy *be = s->be; |
| 1404 | struct proxy *prx_log; |
| 1405 | struct http_txn *txn = &s->txn; |
| 1406 | int tolog; |
| 1407 | char *uri, *h; |
| 1408 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1409 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1410 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1411 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1412 | int hdr; |
| 1413 | |
| 1414 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1415 | return; |
| 1416 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1418 | if (s->cli_addr.ss_family == AF_INET) |
| 1419 | inet_ntop(AF_INET, |
| 1420 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1421 | pn, sizeof(pn)); |
| 1422 | else |
| 1423 | inet_ntop(AF_INET6, |
| 1424 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1425 | pn, sizeof(pn)); |
| 1426 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1427 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1428 | |
| 1429 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1430 | tolog = fe->to_log; |
| 1431 | |
| 1432 | h = tmpline; |
| 1433 | if (fe->to_log & LW_REQHDR && |
| 1434 | txn->req.cap && |
| 1435 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1436 | *(h++) = ' '; |
| 1437 | *(h++) = '{'; |
| 1438 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1439 | if (hdr) |
| 1440 | *(h++) = '|'; |
| 1441 | if (txn->req.cap[hdr] != NULL) |
| 1442 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1443 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1444 | } |
| 1445 | *(h++) = '}'; |
| 1446 | } |
| 1447 | |
| 1448 | if (fe->to_log & LW_RSPHDR && |
| 1449 | txn->rsp.cap && |
| 1450 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1451 | *(h++) = ' '; |
| 1452 | *(h++) = '{'; |
| 1453 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1454 | if (hdr) |
| 1455 | *(h++) = '|'; |
| 1456 | if (txn->rsp.cap[hdr] != NULL) |
| 1457 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1458 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1459 | } |
| 1460 | *(h++) = '}'; |
| 1461 | } |
| 1462 | |
| 1463 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1464 | *(h++) = ' '; |
| 1465 | *(h++) = '"'; |
| 1466 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1467 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1468 | '#', url_encode_map, uri); |
| 1469 | *(h++) = '"'; |
| 1470 | } |
| 1471 | *h = '\0'; |
| 1472 | |
| 1473 | svid = (tolog & LW_SVID) ? |
| 1474 | (s->data_source != DATA_SRC_STATS) ? |
| 1475 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1476 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1477 | t_request = -1; |
| 1478 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1479 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1480 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1481 | send_log(prx_log, LOG_INFO, |
| 1482 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 1483 | " %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] | 1484 | " %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] | 1485 | pn, |
| 1486 | (s->cli_addr.ss_family == AF_INET) ? |
| 1487 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1488 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1489 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1490 | 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] | 1491 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1492 | t_request, |
| 1493 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1494 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1495 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1496 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1497 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1498 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1499 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1500 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1501 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1502 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1503 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1504 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1505 | 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] | 1506 | (s->flags & SN_REDISP)?"+":"", |
| 1507 | (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] | 1508 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1509 | |
| 1510 | s->logs.logwait = 0; |
| 1511 | } |
| 1512 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* |
| 1515 | * Capture headers from message starting at <som> according to header list |
| 1516 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1517 | */ |
| 1518 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1519 | char **cap, struct cap_hdr *cap_hdr) |
| 1520 | { |
| 1521 | char *eol, *sol, *col, *sov; |
| 1522 | int cur_idx; |
| 1523 | struct cap_hdr *h; |
| 1524 | int len; |
| 1525 | |
| 1526 | sol = som + hdr_idx_first_pos(idx); |
| 1527 | cur_idx = hdr_idx_first_idx(idx); |
| 1528 | |
| 1529 | while (cur_idx) { |
| 1530 | eol = sol + idx->v[cur_idx].len; |
| 1531 | |
| 1532 | col = sol; |
| 1533 | while (col < eol && *col != ':') |
| 1534 | col++; |
| 1535 | |
| 1536 | sov = col + 1; |
| 1537 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1538 | sov++; |
| 1539 | |
| 1540 | for (h = cap_hdr; h; h = h->next) { |
| 1541 | if ((h->namelen == col - sol) && |
| 1542 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1543 | if (cap[h->index] == NULL) |
| 1544 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1545 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1546 | |
| 1547 | if (cap[h->index] == NULL) { |
| 1548 | Alert("HTTP capture : out of memory.\n"); |
| 1549 | continue; |
| 1550 | } |
| 1551 | |
| 1552 | len = eol - sov; |
| 1553 | if (len > h->len) |
| 1554 | len = h->len; |
| 1555 | |
| 1556 | memcpy(cap[h->index], sov, len); |
| 1557 | cap[h->index][len]=0; |
| 1558 | } |
| 1559 | } |
| 1560 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1561 | cur_idx = idx->v[cur_idx].next; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1566 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1567 | */ |
| 1568 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1569 | |
| 1570 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1571 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1572 | */ |
| 1573 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1574 | ptr++; \ |
| 1575 | if (likely(ptr < end)) \ |
| 1576 | goto good; \ |
| 1577 | else { \ |
| 1578 | state = (st); \ |
| 1579 | goto http_msg_ood; \ |
| 1580 | } \ |
| 1581 | } while (0) |
| 1582 | |
| 1583 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1584 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1585 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1586 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1587 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1588 | * will give undefined results. |
| 1589 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1590 | * and that msg->sol points to the beginning of the response. |
| 1591 | * If a complete line is found (which implies that at least one CR or LF is |
| 1592 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1593 | * returned indicating an incomplete line (which does not mean that parts have |
| 1594 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1595 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1596 | * upon next call. |
| 1597 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1598 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1599 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1600 | * 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] | 1601 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1602 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1603 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1604 | unsigned int state, const char *ptr, const char *end, |
| 1605 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1606 | { |
| 1607 | __label__ |
| 1608 | http_msg_rpver, |
| 1609 | http_msg_rpver_sp, |
| 1610 | http_msg_rpcode, |
| 1611 | http_msg_rpcode_sp, |
| 1612 | http_msg_rpreason, |
| 1613 | http_msg_rpline_eol, |
| 1614 | http_msg_ood, /* out of data */ |
| 1615 | http_msg_invalid; |
| 1616 | |
| 1617 | switch (state) { |
| 1618 | http_msg_rpver: |
| 1619 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1620 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1621 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1622 | |
| 1623 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1624 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1625 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1626 | } |
| 1627 | goto http_msg_invalid; |
| 1628 | |
| 1629 | http_msg_rpver_sp: |
| 1630 | case HTTP_MSG_RPVER_SP: |
| 1631 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1632 | msg->sl.st.c = ptr - msg_buf; |
| 1633 | goto http_msg_rpcode; |
| 1634 | } |
| 1635 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1636 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1637 | /* so it's a CR/LF, this is invalid */ |
| 1638 | goto http_msg_invalid; |
| 1639 | |
| 1640 | http_msg_rpcode: |
| 1641 | case HTTP_MSG_RPCODE: |
| 1642 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1643 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1644 | |
| 1645 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1646 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1647 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1648 | } |
| 1649 | |
| 1650 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1651 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1652 | http_msg_rsp_reason: |
| 1653 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1654 | msg->sl.st.r = ptr - msg_buf; |
| 1655 | msg->sl.st.r_l = 0; |
| 1656 | goto http_msg_rpline_eol; |
| 1657 | |
| 1658 | http_msg_rpcode_sp: |
| 1659 | case HTTP_MSG_RPCODE_SP: |
| 1660 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1661 | msg->sl.st.r = ptr - msg_buf; |
| 1662 | goto http_msg_rpreason; |
| 1663 | } |
| 1664 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1665 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1666 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1667 | goto http_msg_rsp_reason; |
| 1668 | |
| 1669 | http_msg_rpreason: |
| 1670 | case HTTP_MSG_RPREASON: |
| 1671 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1672 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1673 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1674 | http_msg_rpline_eol: |
| 1675 | /* We have seen the end of line. Note that we do not |
| 1676 | * necessarily have the \n yet, but at least we know that we |
| 1677 | * have EITHER \r OR \n, otherwise the response would not be |
| 1678 | * complete. We can then record the response length and return |
| 1679 | * to the caller which will be able to register it. |
| 1680 | */ |
| 1681 | msg->sl.st.l = ptr - msg->sol; |
| 1682 | return ptr; |
| 1683 | |
| 1684 | #ifdef DEBUG_FULL |
| 1685 | default: |
| 1686 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1687 | exit(1); |
| 1688 | #endif |
| 1689 | } |
| 1690 | |
| 1691 | http_msg_ood: |
| 1692 | /* out of data */ |
| 1693 | if (ret_state) |
| 1694 | *ret_state = state; |
| 1695 | if (ret_ptr) |
| 1696 | *ret_ptr = (char *)ptr; |
| 1697 | return NULL; |
| 1698 | |
| 1699 | http_msg_invalid: |
| 1700 | /* invalid message */ |
| 1701 | if (ret_state) |
| 1702 | *ret_state = HTTP_MSG_ERROR; |
| 1703 | return NULL; |
| 1704 | } |
| 1705 | |
| 1706 | |
| 1707 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | * This function parses a request line between <ptr> and <end>, starting with |
| 1709 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1710 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1711 | * will give undefined results. |
| 1712 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1713 | * and that msg->sol points to the beginning of the request. |
| 1714 | * If a complete line is found (which implies that at least one CR or LF is |
| 1715 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1716 | * returned indicating an incomplete line (which does not mean that parts have |
| 1717 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1718 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1719 | * upon next call. |
| 1720 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1721 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1723 | * 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] | 1724 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1725 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1726 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1727 | unsigned int state, const char *ptr, const char *end, |
| 1728 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1729 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | __label__ |
| 1731 | http_msg_rqmeth, |
| 1732 | http_msg_rqmeth_sp, |
| 1733 | http_msg_rquri, |
| 1734 | http_msg_rquri_sp, |
| 1735 | http_msg_rqver, |
| 1736 | http_msg_rqline_eol, |
| 1737 | http_msg_ood, /* out of data */ |
| 1738 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1739 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | switch (state) { |
| 1741 | http_msg_rqmeth: |
| 1742 | case HTTP_MSG_RQMETH: |
| 1743 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1744 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1745 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1747 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1749 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1752 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1753 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | http_msg_req09_uri: |
| 1755 | msg->sl.rq.u = ptr - msg_buf; |
| 1756 | http_msg_req09_uri_e: |
| 1757 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1758 | http_msg_req09_ver: |
| 1759 | msg->sl.rq.v = ptr - msg_buf; |
| 1760 | msg->sl.rq.v_l = 0; |
| 1761 | goto http_msg_rqline_eol; |
| 1762 | } |
| 1763 | goto http_msg_invalid; |
| 1764 | |
| 1765 | http_msg_rqmeth_sp: |
| 1766 | case HTTP_MSG_RQMETH_SP: |
| 1767 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1768 | msg->sl.rq.u = ptr - msg_buf; |
| 1769 | goto http_msg_rquri; |
| 1770 | } |
| 1771 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1772 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1773 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1774 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1775 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | http_msg_rquri: |
| 1777 | case HTTP_MSG_RQURI: |
| 1778 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1779 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1780 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1781 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1782 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1783 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1784 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1785 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1787 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1788 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1789 | http_msg_rquri_sp: |
| 1790 | case HTTP_MSG_RQURI_SP: |
| 1791 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1792 | msg->sl.rq.v = ptr - msg_buf; |
| 1793 | goto http_msg_rqver; |
| 1794 | } |
| 1795 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1796 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1797 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1798 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1799 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1800 | http_msg_rqver: |
| 1801 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1802 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1804 | |
| 1805 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1806 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1807 | http_msg_rqline_eol: |
| 1808 | /* We have seen the end of line. Note that we do not |
| 1809 | * necessarily have the \n yet, but at least we know that we |
| 1810 | * have EITHER \r OR \n, otherwise the request would not be |
| 1811 | * complete. We can then record the request length and return |
| 1812 | * to the caller which will be able to register it. |
| 1813 | */ |
| 1814 | msg->sl.rq.l = ptr - msg->sol; |
| 1815 | return ptr; |
| 1816 | } |
| 1817 | |
| 1818 | /* neither an HTTP_VER token nor a CRLF */ |
| 1819 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1820 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1821 | #ifdef DEBUG_FULL |
| 1822 | default: |
| 1823 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1824 | exit(1); |
| 1825 | #endif |
| 1826 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1827 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | http_msg_ood: |
| 1829 | /* out of data */ |
| 1830 | if (ret_state) |
| 1831 | *ret_state = state; |
| 1832 | if (ret_ptr) |
| 1833 | *ret_ptr = (char *)ptr; |
| 1834 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1835 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1836 | http_msg_invalid: |
| 1837 | /* invalid message */ |
| 1838 | if (ret_state) |
| 1839 | *ret_state = HTTP_MSG_ERROR; |
| 1840 | return NULL; |
| 1841 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1842 | |
| 1843 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1844 | /* |
| 1845 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1846 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1847 | * when data are missing and recalled at the exact same location with no |
| 1848 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1849 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1850 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1851 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1852 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1853 | { |
| 1854 | __label__ |
| 1855 | http_msg_rqbefore, |
| 1856 | http_msg_rqbefore_cr, |
| 1857 | http_msg_rqmeth, |
| 1858 | http_msg_rqline_end, |
| 1859 | http_msg_hdr_first, |
| 1860 | http_msg_hdr_name, |
| 1861 | http_msg_hdr_l1_sp, |
| 1862 | http_msg_hdr_l1_lf, |
| 1863 | http_msg_hdr_l1_lws, |
| 1864 | http_msg_hdr_val, |
| 1865 | http_msg_hdr_l2_lf, |
| 1866 | http_msg_hdr_l2_lws, |
| 1867 | http_msg_complete_header, |
| 1868 | http_msg_last_lf, |
| 1869 | http_msg_ood, /* out of data */ |
| 1870 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1871 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1872 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1873 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1874 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1875 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1876 | ptr = buf->lr; |
| 1877 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1878 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1879 | if (unlikely(ptr >= end)) |
| 1880 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1881 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1882 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1883 | /* |
| 1884 | * First, states that are specific to the response only. |
| 1885 | * We check them first so that request and headers are |
| 1886 | * closer to each other (accessed more often). |
| 1887 | */ |
| 1888 | http_msg_rpbefore: |
| 1889 | case HTTP_MSG_RPBEFORE: |
| 1890 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1891 | if (likely(ptr == buf->data)) { |
| 1892 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1893 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1894 | } else { |
| 1895 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1896 | /* only skip empty leading lines, don't remove them */ |
| 1897 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1898 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1899 | #else |
| 1900 | /* Remove empty leading lines, as recommended by |
| 1901 | * RFC2616. This takes a lot of time because we |
| 1902 | * must move all the buffer backwards, but this |
| 1903 | * is rarely needed. The method above will be |
| 1904 | * cleaner when we'll be able to start sending |
| 1905 | * the request from any place in the buffer. |
| 1906 | */ |
| 1907 | buf->lr = ptr; |
| 1908 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1909 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1910 | msg->sol = buf->data; |
| 1911 | ptr = buf->data; |
| 1912 | end = buf->r; |
| 1913 | #endif |
| 1914 | } |
| 1915 | hdr_idx_init(idx); |
| 1916 | state = HTTP_MSG_RPVER; |
| 1917 | goto http_msg_rpver; |
| 1918 | } |
| 1919 | |
| 1920 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1921 | goto http_msg_invalid; |
| 1922 | |
| 1923 | if (unlikely(*ptr == '\n')) |
| 1924 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1925 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1926 | /* stop here */ |
| 1927 | |
| 1928 | http_msg_rpbefore_cr: |
| 1929 | case HTTP_MSG_RPBEFORE_CR: |
| 1930 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1931 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1932 | /* stop here */ |
| 1933 | |
| 1934 | http_msg_rpver: |
| 1935 | case HTTP_MSG_RPVER: |
| 1936 | case HTTP_MSG_RPVER_SP: |
| 1937 | case HTTP_MSG_RPCODE: |
| 1938 | case HTTP_MSG_RPCODE_SP: |
| 1939 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1940 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1941 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1942 | if (unlikely(!ptr)) |
| 1943 | return; |
| 1944 | |
| 1945 | /* we have a full response and we know that we have either a CR |
| 1946 | * or an LF at <ptr>. |
| 1947 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1948 | //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] | 1949 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1950 | |
| 1951 | msg->sol = ptr; |
| 1952 | if (likely(*ptr == '\r')) |
| 1953 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1954 | goto http_msg_rpline_end; |
| 1955 | |
| 1956 | http_msg_rpline_end: |
| 1957 | case HTTP_MSG_RPLINE_END: |
| 1958 | /* msg->sol must point to the first of CR or LF. */ |
| 1959 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1960 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1961 | /* stop here */ |
| 1962 | |
| 1963 | /* |
| 1964 | * Second, states that are specific to the request only |
| 1965 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1966 | http_msg_rqbefore: |
| 1967 | case HTTP_MSG_RQBEFORE: |
| 1968 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1969 | if (likely(ptr == buf->data)) { |
| 1970 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1971 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1972 | } else { |
| 1973 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1974 | /* only skip empty leading lines, don't remove them */ |
| 1975 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1976 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1977 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1978 | /* Remove empty leading lines, as recommended by |
| 1979 | * RFC2616. This takes a lot of time because we |
| 1980 | * must move all the buffer backwards, but this |
| 1981 | * is rarely needed. The method above will be |
| 1982 | * cleaner when we'll be able to start sending |
| 1983 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1984 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1985 | buf->lr = ptr; |
| 1986 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1987 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1988 | msg->sol = buf->data; |
| 1989 | ptr = buf->data; |
| 1990 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1991 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1992 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1993 | /* we will need this when keep-alive will be supported |
| 1994 | hdr_idx_init(idx); |
| 1995 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1996 | state = HTTP_MSG_RQMETH; |
| 1997 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1998 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1999 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2000 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 2001 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2002 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2003 | if (unlikely(*ptr == '\n')) |
| 2004 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 2005 | 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] | 2006 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2007 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2008 | http_msg_rqbefore_cr: |
| 2009 | case HTTP_MSG_RQBEFORE_CR: |
| 2010 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2011 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 2012 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2013 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2014 | http_msg_rqmeth: |
| 2015 | case HTTP_MSG_RQMETH: |
| 2016 | case HTTP_MSG_RQMETH_SP: |
| 2017 | case HTTP_MSG_RQURI: |
| 2018 | case HTTP_MSG_RQURI_SP: |
| 2019 | case HTTP_MSG_RQVER: |
| 2020 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2021 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2022 | if (unlikely(!ptr)) |
| 2023 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2024 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2025 | /* we have a full request and we know that we have either a CR |
| 2026 | * or an LF at <ptr>. |
| 2027 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2028 | //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] | 2029 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2030 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2031 | msg->sol = ptr; |
| 2032 | if (likely(*ptr == '\r')) |
| 2033 | 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] | 2034 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2035 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2036 | http_msg_rqline_end: |
| 2037 | case HTTP_MSG_RQLINE_END: |
| 2038 | /* check for HTTP/0.9 request : no version information available. |
| 2039 | * msg->sol must point to the first of CR or LF. |
| 2040 | */ |
| 2041 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 2042 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2043 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2044 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2045 | 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] | 2046 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2047 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 2048 | /* |
| 2049 | * Common states below |
| 2050 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2051 | http_msg_hdr_first: |
| 2052 | case HTTP_MSG_HDR_FIRST: |
| 2053 | msg->sol = ptr; |
| 2054 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2055 | goto http_msg_hdr_name; |
| 2056 | } |
| 2057 | |
| 2058 | if (likely(*ptr == '\r')) |
| 2059 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 2060 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2061 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2062 | http_msg_hdr_name: |
| 2063 | case HTTP_MSG_HDR_NAME: |
| 2064 | /* assumes msg->sol points to the first char */ |
| 2065 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 2066 | 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] | 2067 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2068 | if (likely(*ptr == ':')) { |
| 2069 | msg->col = ptr - buf->data; |
| 2070 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 2071 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2072 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2073 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2075 | http_msg_hdr_l1_sp: |
| 2076 | case HTTP_MSG_HDR_L1_SP: |
| 2077 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 2078 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 2079 | 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] | 2080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2081 | /* header value can be basically anything except CR/LF */ |
| 2082 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2083 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2084 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2085 | goto http_msg_hdr_val; |
| 2086 | } |
| 2087 | |
| 2088 | if (likely(*ptr == '\r')) |
| 2089 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 2090 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2091 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2092 | http_msg_hdr_l1_lf: |
| 2093 | case HTTP_MSG_HDR_L1_LF: |
| 2094 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2095 | 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] | 2096 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2097 | http_msg_hdr_l1_lws: |
| 2098 | case HTTP_MSG_HDR_L1_LWS: |
| 2099 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 2100 | /* replace HT,CR,LF with spaces */ |
| 2101 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 2102 | buf->data[msg->sov] = ' '; |
| 2103 | goto http_msg_hdr_l1_sp; |
| 2104 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2105 | /* we had a header consisting only in spaces ! */ |
| 2106 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2107 | goto http_msg_complete_header; |
| 2108 | |
| 2109 | http_msg_hdr_val: |
| 2110 | case HTTP_MSG_HDR_VAL: |
| 2111 | /* assumes msg->sol points to the first char, msg->col to the |
| 2112 | * colon, and msg->sov points to the first character of the |
| 2113 | * value. |
| 2114 | */ |
| 2115 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 2116 | 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] | 2117 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2118 | msg->eol = ptr; |
| 2119 | /* Note: we could also copy eol into ->eoh so that we have the |
| 2120 | * real header end in case it ends with lots of LWS, but is this |
| 2121 | * really needed ? |
| 2122 | */ |
| 2123 | if (likely(*ptr == '\r')) |
| 2124 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 2125 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2126 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2127 | http_msg_hdr_l2_lf: |
| 2128 | case HTTP_MSG_HDR_L2_LF: |
| 2129 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2130 | 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] | 2131 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2132 | http_msg_hdr_l2_lws: |
| 2133 | case HTTP_MSG_HDR_L2_LWS: |
| 2134 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 2135 | /* LWS: replace HT,CR,LF with spaces */ |
| 2136 | for (; msg->eol < ptr; msg->eol++) |
| 2137 | *msg->eol = ' '; |
| 2138 | goto http_msg_hdr_val; |
| 2139 | } |
| 2140 | http_msg_complete_header: |
| 2141 | /* |
| 2142 | * It was a new header, so the last one is finished. |
| 2143 | * Assumes msg->sol points to the first char, msg->col to the |
| 2144 | * colon, msg->sov points to the first character of the value |
| 2145 | * and msg->eol to the first CR or LF so we know how the line |
| 2146 | * ends. We insert last header into the index. |
| 2147 | */ |
| 2148 | /* |
| 2149 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 2150 | write(2, msg->sol, msg->eol-msg->sol); |
| 2151 | fprintf(stderr,"\n"); |
| 2152 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2153 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2154 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 2155 | idx, idx->tail) < 0)) |
| 2156 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2157 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2158 | msg->sol = ptr; |
| 2159 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2160 | goto http_msg_hdr_name; |
| 2161 | } |
| 2162 | |
| 2163 | if (likely(*ptr == '\r')) |
| 2164 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 2165 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2166 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2167 | http_msg_last_lf: |
| 2168 | case HTTP_MSG_LAST_LF: |
| 2169 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 2170 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2171 | ptr++; |
| 2172 | buf->lr = ptr; |
| 2173 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2174 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2175 | return; |
| 2176 | #ifdef DEBUG_FULL |
| 2177 | default: |
| 2178 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 2179 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2180 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2181 | } |
| 2182 | http_msg_ood: |
| 2183 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2184 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2185 | buf->lr = ptr; |
| 2186 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2187 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2188 | http_msg_invalid: |
| 2189 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2190 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2191 | return; |
| 2192 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2193 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2194 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2195 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2196 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2197 | * 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] | 2198 | * functions. Its behaviour is rather simple : |
| 2199 | * - all enabled analysers are called in turn from the lower to the higher |
| 2200 | * bit. |
| 2201 | * - if an analyser does not have enough data, it must return without calling |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2202 | * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2203 | * that unprocessed data will not be forwarded. But that probably depends on |
| 2204 | * the protocol. Generally it is not reset in case of errors. |
| 2205 | * - if an analyser has enough data, it just has to pass on to the next |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2206 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2207 | * analysis). |
| 2208 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 2209 | * reset its bit from the analysers flags in order not to be called anymore. |
| 2210 | * |
| 2211 | * In the future, analysers should be able to indicate that they want to be |
| 2212 | * called after XXX bytes have been received (or transfered), and the min of |
| 2213 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 2214 | * |
| 2215 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2216 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2217 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2218 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2219 | struct buffer *req = t->req; |
| 2220 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2221 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2222 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2223 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2224 | t, |
| 2225 | req, |
| 2226 | req->rex, req->wex, |
| 2227 | req->flags, |
| 2228 | req->l, |
| 2229 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2230 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2231 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2232 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2233 | struct tcp_rule *rule; |
| 2234 | int partial; |
| 2235 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 2236 | /* We will abort if we encounter a read error. In theory, we |
| 2237 | * should not abort if we get a close, it might be valid, |
| 2238 | * although very unlikely. FIXME: we'll abort for now, this |
| 2239 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2240 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2241 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2242 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2243 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2244 | if (!(t->flags & SN_ERR_MASK)) |
| 2245 | t->flags |= SN_ERR_CLICL; |
| 2246 | if (!(t->flags & SN_FINST_MASK)) |
| 2247 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2248 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2252 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2253 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2254 | t->fe->failed_req++; |
| 2255 | if (!(t->flags & SN_ERR_MASK)) |
| 2256 | t->flags |= SN_ERR_CLITO; |
| 2257 | if (!(t->flags & SN_FINST_MASK)) |
| 2258 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2259 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | /* We don't know whether we have enough data, so must proceed |
| 2263 | * this way : |
| 2264 | * - iterate through all rules in their declaration order |
| 2265 | * - if one rule returns MISS, it means the inspect delay is |
| 2266 | * not over yet, then return immediately, otherwise consider |
| 2267 | * it as a non-match. |
| 2268 | * - if one rule returns OK, then return OK |
| 2269 | * - if one rule returns KO, then return KO |
| 2270 | */ |
| 2271 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2272 | if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2273 | partial = 0; |
| 2274 | else |
| 2275 | partial = ACL_PARTIAL; |
| 2276 | |
| 2277 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 2278 | int ret = ACL_PAT_PASS; |
| 2279 | |
| 2280 | if (rule->cond) { |
| 2281 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 2282 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2283 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2284 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2285 | if (!tick_isset(req->analyse_exp)) |
| 2286 | 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] | 2287 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2288 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2289 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2290 | ret = acl_pass(ret); |
| 2291 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2292 | ret = !ret; |
| 2293 | } |
| 2294 | |
| 2295 | if (ret) { |
| 2296 | /* we have a matching rule. */ |
| 2297 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2298 | buffer_abort(req); |
| 2299 | buffer_abort(rep); |
| 2300 | //FIXME: this delete this |
| 2301 | //fd_delete(t->cli_fd); |
| 2302 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2303 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2304 | t->fe->failed_req++; |
| 2305 | if (!(t->flags & SN_ERR_MASK)) |
| 2306 | t->flags |= SN_ERR_PRXCOND; |
| 2307 | if (!(t->flags & SN_FINST_MASK)) |
| 2308 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2309 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2310 | } |
| 2311 | /* otherwise accept */ |
| 2312 | break; |
| 2313 | } |
| 2314 | } |
| 2315 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2316 | /* if we get there, it means we have no rule which matches, or |
| 2317 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2318 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2319 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2320 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2321 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2322 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2323 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2324 | /* |
| 2325 | * Now parse the partial (or complete) lines. |
| 2326 | * We will check the request syntax, and also join multi-line |
| 2327 | * headers. An index of all the lines will be elaborated while |
| 2328 | * parsing. |
| 2329 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 2330 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2331 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2332 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2333 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2334 | * req->data + req->eoh = end of processed headers / start of current one |
| 2335 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 2336 | * req->lr = first non-visited byte |
| 2337 | * req->r = end of data |
| 2338 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2339 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2340 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2341 | struct http_txn *txn = &t->txn; |
| 2342 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2343 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2344 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2345 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2346 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2347 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2348 | /* 1: we might have to print this header in debug mode */ |
| 2349 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2350 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2351 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2352 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2353 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2354 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2355 | eol = sol + msg->sl.rq.l; |
| 2356 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2357 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2358 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2359 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2360 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2361 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2362 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2363 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2364 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2365 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2366 | } |
| 2367 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2368 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2369 | |
| 2370 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2371 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2372 | * If not so, we check the FD and buffer states before leaving. |
| 2373 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2374 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2375 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2376 | * |
| 2377 | */ |
| 2378 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2379 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2380 | /* |
| 2381 | * First, let's catch bad requests. |
| 2382 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2383 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2384 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2385 | |
| 2386 | /* 1: Since we are in header mode, if there's no space |
| 2387 | * left for headers, we won't be able to free more |
| 2388 | * later, so the session will never terminate. We |
| 2389 | * must terminate it now. |
| 2390 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2391 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2392 | /* FIXME: check if URI is set and return Status |
| 2393 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2394 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2395 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2396 | } |
| 2397 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2398 | /* 2: have we encountered a read error ? */ |
| 2399 | else if (req->flags & BF_READ_ERROR) { |
| 2400 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2401 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2402 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2403 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2404 | if (!(t->flags & SN_ERR_MASK)) |
| 2405 | t->flags |= SN_ERR_CLICL; |
| 2406 | if (!(t->flags & SN_FINST_MASK)) |
| 2407 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2408 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2412 | 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] | 2413 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2414 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2415 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2416 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2417 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2418 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2419 | if (!(t->flags & SN_ERR_MASK)) |
| 2420 | t->flags |= SN_ERR_CLITO; |
| 2421 | if (!(t->flags & SN_FINST_MASK)) |
| 2422 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2423 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2424 | } |
| 2425 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2426 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2427 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2428 | txn->status = 400; |
| 2429 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2430 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2431 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2432 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2433 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2434 | if (!(t->flags & SN_ERR_MASK)) |
| 2435 | t->flags |= SN_ERR_CLICL; |
| 2436 | if (!(t->flags & SN_FINST_MASK)) |
| 2437 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2438 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2439 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2440 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2441 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2442 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2443 | if (!tick_isset(req->analyse_exp)) |
| 2444 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2445 | |
| 2446 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2447 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | |
| 2451 | /**************************************************************** |
| 2452 | * More interesting part now : we know that we have a complete * |
| 2453 | * request which at least looks like HTTP. We have an indicator * |
| 2454 | * of each header's length, so we can parse them quickly. * |
| 2455 | ****************************************************************/ |
| 2456 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2457 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2458 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2459 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2460 | /* ensure we keep this pointer to the beginning of the message */ |
| 2461 | msg->sol = req->data + msg->som; |
| 2462 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2463 | /* |
| 2464 | * 1: identify the method |
| 2465 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2466 | 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] | 2467 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2468 | /* we can make use of server redirect on GET and HEAD */ |
| 2469 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2470 | t->flags |= SN_REDIRECTABLE; |
| 2471 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2472 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2473 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2474 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2475 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2476 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2477 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 2478 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2479 | !memcmp(&req->data[msg->sl.rq.u], |
| 2480 | t->fe->monitor_uri, |
| 2481 | t->fe->monitor_uri_len))) { |
| 2482 | /* |
| 2483 | * We have found the monitor URI |
| 2484 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2485 | struct acl_cond *cond; |
| 2486 | cur_proxy = t->fe; |
| 2487 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2488 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2489 | |
| 2490 | /* Check if we want to fail this monitor request or not */ |
| 2491 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 2492 | 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] | 2493 | |
| 2494 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2495 | if (cond->pol == ACL_COND_UNLESS) |
| 2496 | ret = !ret; |
| 2497 | |
| 2498 | if (ret) { |
| 2499 | /* we fail this request, let's return 503 service unavail */ |
| 2500 | txn->status = 503; |
| 2501 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 2502 | goto return_prx_cond; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2507 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2508 | client_retnclose(t, &http_200_chunk); |
| 2509 | goto return_prx_cond; |
| 2510 | } |
| 2511 | |
| 2512 | /* |
| 2513 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2514 | * Note: we cannot log anymore if the request has been |
| 2515 | * classified as invalid. |
| 2516 | */ |
| 2517 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2518 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2519 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2520 | int urilen = msg->sl.rq.l; |
| 2521 | |
| 2522 | if (urilen >= REQURI_LEN) |
| 2523 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2524 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2525 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2526 | |
| 2527 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2528 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2529 | } else { |
| 2530 | Alert("HTTP logging : out of memory.\n"); |
| 2531 | } |
| 2532 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2533 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2534 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2535 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2536 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2537 | int delta; |
| 2538 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2539 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2540 | cur_end = msg->sol + msg->sl.rq.l; |
| 2541 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2542 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2543 | if (msg->sl.rq.u_l == 0) { |
| 2544 | /* if no URI was set, add "/" */ |
| 2545 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2546 | cur_end += delta; |
| 2547 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2548 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2549 | /* add HTTP version */ |
| 2550 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2551 | msg->eoh += delta; |
| 2552 | cur_end += delta; |
| 2553 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2554 | HTTP_MSG_RQMETH, |
| 2555 | msg->sol, cur_end + 1, |
| 2556 | NULL, NULL); |
| 2557 | if (unlikely(!cur_end)) |
| 2558 | goto return_bad_req; |
| 2559 | |
| 2560 | /* we have a full HTTP/1.0 request now and we know that |
| 2561 | * we have either a CR or an LF at <ptr>. |
| 2562 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2563 | 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] | 2564 | } |
| 2565 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2566 | |
| 2567 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2568 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2569 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2570 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2571 | |
| 2572 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2573 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2574 | * As opposed to version 1.2, now they will be evaluated in the |
| 2575 | * filters order and not in the header order. This means that |
| 2576 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2577 | * |
| 2578 | * We can now check whether we want to switch to another |
| 2579 | * backend, in which case we will re-check the backend's |
| 2580 | * filters and various options. In order to support 3-level |
| 2581 | * switching, here's how we should proceed : |
| 2582 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2583 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2584 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2585 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2586 | * There cannot be any switch from there, so ->be cannot be |
| 2587 | * changed anymore. |
| 2588 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2589 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2590 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2591 | * The response path will be able to apply either ->be, or |
| 2592 | * ->be then ->fe filters in order to match the reverse of |
| 2593 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2594 | */ |
| 2595 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2596 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2597 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2598 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2599 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2600 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2601 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2602 | /* first check whether we have some ACLs set to redirect this request */ |
| 2603 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2604 | 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] | 2605 | |
| 2606 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2607 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2608 | ret = !ret; |
| 2609 | |
| 2610 | if (ret) { |
| 2611 | struct chunk rdr = { trash, 0 }; |
| 2612 | const char *msg_fmt; |
| 2613 | |
| 2614 | /* build redirect message */ |
| 2615 | switch(rule->code) { |
| 2616 | case 303: |
| 2617 | rdr.len = strlen(HTTP_303); |
| 2618 | msg_fmt = HTTP_303; |
| 2619 | break; |
| 2620 | case 301: |
| 2621 | rdr.len = strlen(HTTP_301); |
| 2622 | msg_fmt = HTTP_301; |
| 2623 | break; |
| 2624 | case 302: |
| 2625 | default: |
| 2626 | rdr.len = strlen(HTTP_302); |
| 2627 | msg_fmt = HTTP_302; |
| 2628 | break; |
| 2629 | } |
| 2630 | |
| 2631 | if (unlikely(rdr.len > sizeof(trash))) |
| 2632 | goto return_bad_req; |
| 2633 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2634 | |
| 2635 | switch(rule->type) { |
| 2636 | case REDIRECT_TYPE_PREFIX: { |
| 2637 | const char *path; |
| 2638 | int pathlen; |
| 2639 | |
| 2640 | path = http_get_path(txn); |
| 2641 | /* build message using path */ |
| 2642 | if (path) { |
| 2643 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2644 | } else { |
| 2645 | path = "/"; |
| 2646 | pathlen = 1; |
| 2647 | } |
| 2648 | |
| 2649 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2650 | goto return_bad_req; |
| 2651 | |
| 2652 | /* add prefix */ |
| 2653 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2654 | rdr.len += rule->rdr_len; |
| 2655 | |
| 2656 | /* add path */ |
| 2657 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2658 | rdr.len += pathlen; |
| 2659 | break; |
| 2660 | } |
| 2661 | case REDIRECT_TYPE_LOCATION: |
| 2662 | default: |
| 2663 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2664 | goto return_bad_req; |
| 2665 | |
| 2666 | /* add location */ |
| 2667 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2668 | rdr.len += rule->rdr_len; |
| 2669 | break; |
| 2670 | } |
| 2671 | |
| 2672 | /* add end of headers */ |
| 2673 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2674 | rdr.len += 4; |
| 2675 | |
| 2676 | txn->status = rule->code; |
| 2677 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2678 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2679 | client_retnclose(t, &rdr); |
| 2680 | goto return_prx_cond; |
| 2681 | } |
| 2682 | } |
| 2683 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2684 | /* first check whether we have some ACLs set to block this request */ |
| 2685 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2686 | 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] | 2687 | |
| 2688 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2689 | if (cond->pol == ACL_COND_UNLESS) |
| 2690 | ret = !ret; |
| 2691 | |
| 2692 | if (ret) { |
| 2693 | txn->status = 403; |
| 2694 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2695 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2696 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2697 | goto return_prx_cond; |
| 2698 | } |
| 2699 | } |
| 2700 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2701 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2702 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2703 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2704 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2705 | } |
| 2706 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2707 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2708 | /* to ensure correct connection accounting on |
| 2709 | * the backend, we count the connection for the |
| 2710 | * one managing the queue. |
| 2711 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2712 | t->be->beconn++; |
| 2713 | if (t->be->beconn > t->be->beconn_max) |
| 2714 | t->be->beconn_max = t->be->beconn; |
| 2715 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2716 | t->flags |= SN_BE_ASSIGNED; |
| 2717 | } |
| 2718 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2719 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2720 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2721 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2722 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2723 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2724 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2725 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2726 | goto return_prx_cond; |
| 2727 | } |
| 2728 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2729 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2730 | 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] | 2731 | !(t->flags & SN_CONN_CLOSED)) { |
| 2732 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2733 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2734 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2735 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2736 | 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] | 2737 | old_idx = 0; |
| 2738 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2739 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2740 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2741 | cur_ptr = cur_next; |
| 2742 | cur_end = cur_ptr + cur_hdr->len; |
| 2743 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2744 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2745 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2746 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2747 | /* 3 possibilities : |
| 2748 | * - we have already set Connection: close, |
| 2749 | * so we remove this line. |
| 2750 | * - we have not yet set Connection: close, |
| 2751 | * but this line indicates close. We leave |
| 2752 | * it untouched and set the flag. |
| 2753 | * - we have not yet set Connection: close, |
| 2754 | * and this line indicates non-close. We |
| 2755 | * replace it. |
| 2756 | */ |
| 2757 | if (t->flags & SN_CONN_CLOSED) { |
| 2758 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2759 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2760 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2761 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2762 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2763 | cur_hdr->len = 0; |
| 2764 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2765 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2766 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2767 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2768 | cur_next += delta; |
| 2769 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2770 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2771 | } |
| 2772 | t->flags |= SN_CONN_CLOSED; |
| 2773 | } |
| 2774 | } |
| 2775 | old_idx = cur_idx; |
| 2776 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2777 | } |
| 2778 | /* add request headers from the rule sets in the same order */ |
| 2779 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2780 | if (unlikely(http_header_add_tail(req, |
| 2781 | &txn->req, |
| 2782 | &txn->hdr_idx, |
| 2783 | rule_set->req_add[cur_idx])) < 0) |
| 2784 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2785 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2786 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2787 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2788 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2789 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2790 | /* we have to check the URI and auth for this request. |
| 2791 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2792 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2793 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2794 | if (stats_check_uri_auth(t, rule_set)) |
| 2795 | return 1; |
| 2796 | } |
| 2797 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2798 | /* now check whether we have some switching rules for this request */ |
| 2799 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2800 | struct switching_rule *rule; |
| 2801 | |
| 2802 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2803 | int ret; |
| 2804 | |
| 2805 | 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] | 2806 | |
| 2807 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2808 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2809 | ret = !ret; |
| 2810 | |
| 2811 | if (ret) { |
| 2812 | t->be = rule->be.backend; |
| 2813 | t->be->beconn++; |
| 2814 | if (t->be->beconn > t->be->beconn_max) |
| 2815 | t->be->beconn_max = t->be->beconn; |
| 2816 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2817 | |
| 2818 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2819 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2820 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2821 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2822 | t->flags |= SN_BE_ASSIGNED; |
| 2823 | break; |
| 2824 | } |
| 2825 | } |
| 2826 | } |
| 2827 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2828 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2829 | /* No backend was set, but there was a default |
| 2830 | * backend set in the frontend, so we use it and |
| 2831 | * loop again. |
| 2832 | */ |
| 2833 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2834 | t->be->beconn++; |
| 2835 | if (t->be->beconn > t->be->beconn_max) |
| 2836 | t->be->beconn_max = t->be->beconn; |
| 2837 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2838 | |
| 2839 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2840 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2841 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2842 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2843 | t->flags |= SN_BE_ASSIGNED; |
| 2844 | } |
| 2845 | } 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] | 2846 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2847 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2848 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2849 | /* To ensure correct connection accounting on |
| 2850 | * the backend, we count the connection for the |
| 2851 | * one managing the queue. |
| 2852 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2853 | t->be->beconn++; |
| 2854 | if (t->be->beconn > t->be->beconn_max) |
| 2855 | t->be->beconn_max = t->be->beconn; |
| 2856 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2857 | t->flags |= SN_BE_ASSIGNED; |
| 2858 | } |
| 2859 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2860 | /* |
| 2861 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2862 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2863 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2864 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2865 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2866 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2867 | /* |
| 2868 | * If HTTP PROXY is set we simply get remote server address |
| 2869 | * parsing incoming request. |
| 2870 | */ |
| 2871 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2872 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2873 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2874 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2875 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2876 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2877 | * so let's do the same now. |
| 2878 | */ |
| 2879 | |
| 2880 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2881 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2882 | 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] | 2883 | } |
| 2884 | |
| 2885 | |
| 2886 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2887 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2888 | * Note that doing so might move headers in the request, but |
| 2889 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2890 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2891 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2892 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2893 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2894 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2895 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2896 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2897 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2898 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2899 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2900 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2901 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2902 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2903 | /* Add an X-Forwarded-For header unless the source IP is |
| 2904 | * in the 'except' network range. |
| 2905 | */ |
| 2906 | if ((!t->fe->except_mask.s_addr || |
| 2907 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2908 | != t->fe->except_net.s_addr) && |
| 2909 | (!t->be->except_mask.s_addr || |
| 2910 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2911 | != t->be->except_net.s_addr)) { |
| 2912 | int len; |
| 2913 | unsigned char *pn; |
| 2914 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2915 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2916 | /* Note: we rely on the backend to get the header name to be used for |
| 2917 | * x-forwarded-for, because the header is really meant for the backends. |
| 2918 | * However, if the backend did not specify any option, we have to rely |
| 2919 | * on the frontend's header name. |
| 2920 | */ |
| 2921 | if (t->be->fwdfor_hdr_len) { |
| 2922 | len = t->be->fwdfor_hdr_len; |
| 2923 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2924 | } else { |
| 2925 | len = t->fe->fwdfor_hdr_len; |
| 2926 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2927 | } |
| 2928 | 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] | 2929 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2930 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2931 | &txn->hdr_idx, trash, len)) < 0) |
| 2932 | goto return_bad_req; |
| 2933 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2934 | } |
| 2935 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2936 | /* FIXME: for the sake of completeness, we should also support |
| 2937 | * 'except' here, although it is mostly useless in this case. |
| 2938 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2939 | int len; |
| 2940 | char pn[INET6_ADDRSTRLEN]; |
| 2941 | inet_ntop(AF_INET6, |
| 2942 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2943 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2944 | |
| 2945 | /* Note: we rely on the backend to get the header name to be used for |
| 2946 | * x-forwarded-for, because the header is really meant for the backends. |
| 2947 | * However, if the backend did not specify any option, we have to rely |
| 2948 | * on the frontend's header name. |
| 2949 | */ |
| 2950 | if (t->be->fwdfor_hdr_len) { |
| 2951 | len = t->be->fwdfor_hdr_len; |
| 2952 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2953 | } else { |
| 2954 | len = t->fe->fwdfor_hdr_len; |
| 2955 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2956 | } |
| 2957 | len += sprintf(trash + len, ": %s", pn); |
| 2958 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2959 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2960 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2961 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2962 | } |
| 2963 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2964 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2965 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2966 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2967 | * 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] | 2968 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2969 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2970 | ((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] | 2971 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2972 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2973 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2974 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2975 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2976 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2977 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2978 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2979 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2980 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2981 | * (unless headers overflowed the buffer?) |
| 2982 | */ |
| 2983 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2984 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2985 | 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] | 2986 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2987 | /* are there enough bytes here? total == l || r || rlim ? |
| 2988 | * len is unsigned, but eoh is int, |
| 2989 | * how many bytes of body have we received? |
| 2990 | * eoh is the first empty line of the header |
| 2991 | */ |
| 2992 | /* 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] | 2993 | 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] | 2994 | |
| 2995 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2996 | * We can't assume responsibility for the server's decision, |
| 2997 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2998 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2999 | */ |
| 3000 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 3001 | struct hdr_ctx ctx; |
| 3002 | ctx.idx = 0; |
| 3003 | /* Expect is allowed in 1.1, look for it */ |
| 3004 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 3005 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3006 | 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] | 3007 | /* We can't reliablly stall and wait for data, because of |
| 3008 | * .NET clients that don't conform to rfc2616; so, no need for |
| 3009 | * the next block to check length expectations. |
| 3010 | * We could send 100 status back to the client, but then we need to |
| 3011 | * re-write headers, and send the message. And this isn't the right |
| 3012 | * place for that action. |
| 3013 | * TODO: support Expect elsewhere and delete this block. |
| 3014 | */ |
| 3015 | goto end_check_maybe_wait_for_body; |
| 3016 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3017 | |
| 3018 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3019 | /* nothing to do, we got enough */ |
| 3020 | } else { |
| 3021 | /* limit implies we are supposed to need this many bytes |
| 3022 | * to find the parameter. Let's see how many bytes we can wait for. |
| 3023 | */ |
| 3024 | long long hint = len; |
| 3025 | struct hdr_ctx ctx; |
| 3026 | ctx.idx = 0; |
| 3027 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3028 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3029 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3030 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3031 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3032 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3033 | ctx.idx = 0; |
| 3034 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 3035 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3036 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3037 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3038 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3039 | hint = 0; /* parse failure, untrusted client */ |
| 3040 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3041 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3042 | msg->hdr_content_len = hint; |
| 3043 | else |
| 3044 | hint = 0; /* bad client, sent negative length */ |
| 3045 | } |
| 3046 | } |
| 3047 | /* 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] | 3048 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3049 | hint = t->be->url_param_post_limit; |
| 3050 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3051 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3052 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3053 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3054 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3055 | /* else... There are no body bytes to wait for */ |
| 3056 | } |
| 3057 | } |
| 3058 | } |
| 3059 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3061 | /************************************************************* |
| 3062 | * OK, that's finished for the headers. We have done what we * |
| 3063 | * could. Let's switch to the DATA state. * |
| 3064 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3065 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3066 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 3067 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3068 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 3069 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3070 | * which may be the same as the connect timeout if unspecified. |
| 3071 | * 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] | 3072 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3073 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3074 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3075 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3076 | /* flush the request so that we can drop the connection early |
| 3077 | * if the client closes first. |
| 3078 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3079 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3080 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3081 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 3082 | if (!req->analyse_exp) |
| 3083 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3084 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3085 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3086 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3087 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3088 | |
| 3089 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3090 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3091 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3092 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 3093 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3094 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3095 | return_prx_cond: |
| 3096 | if (!(t->flags & SN_ERR_MASK)) |
| 3097 | t->flags |= SN_ERR_PRXCOND; |
| 3098 | if (!(t->flags & SN_FINST_MASK)) |
| 3099 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3100 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3101 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3102 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3103 | } |
| 3104 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3105 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 3106 | struct http_txn *txn = &t->txn; |
| 3107 | |
| 3108 | /* This connection is being tarpitted. The CLIENT side has |
| 3109 | * already set the connect expiration date to the right |
| 3110 | * timeout. We just have to check that the client is still |
| 3111 | * there and that the timeout has not expired. |
| 3112 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 3113 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3114 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3115 | return 0; |
| 3116 | |
| 3117 | /* We will set the queue timer to the time spent, just for |
| 3118 | * logging purposes. We fake a 500 server error, so that the |
| 3119 | * attacker will not suspect his connection has been tarpitted. |
| 3120 | * It will not cause trouble to the logs because we can exclude |
| 3121 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3122 | */ |
| 3123 | trace_term(t, TT_HTTP_SRV_2); |
| 3124 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3125 | |
| 3126 | txn->status = 500; |
| 3127 | if (req->flags != BF_READ_ERROR) |
| 3128 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 3129 | |
| 3130 | req->analysers = 0; |
| 3131 | req->analyse_exp = TICK_ETERNITY; |
| 3132 | |
| 3133 | t->fe->failed_req++; |
| 3134 | if (!(t->flags & SN_ERR_MASK)) |
| 3135 | t->flags |= SN_ERR_PRXCOND; |
| 3136 | if (!(t->flags & SN_FINST_MASK)) |
| 3137 | t->flags |= SN_FINST_T; |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3141 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3142 | /* We have to parse the HTTP request body to find any required data. |
| 3143 | * "balance url_param check_post" should have been the only way to get |
| 3144 | * into this. We were brought here after HTTP header analysis, so all |
| 3145 | * related structures are ready. |
| 3146 | */ |
| 3147 | struct http_msg *msg = &t->txn.req; |
| 3148 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 3149 | long long limit = t->be->url_param_post_limit; |
| 3150 | struct hdr_ctx ctx; |
| 3151 | |
| 3152 | ctx.idx = 0; |
| 3153 | |
| 3154 | /* now if we have a length, we'll take the hint */ |
| 3155 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 3156 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 3157 | unsigned int chunk = 0; |
| 3158 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 3159 | char c = msg->sol[body]; |
| 3160 | if (ishex(c)) { |
| 3161 | unsigned int hex = toupper(c) - '0'; |
| 3162 | if (hex > 9) |
| 3163 | hex -= 'A' - '9' - 1; |
| 3164 | chunk = (chunk << 4) | hex; |
| 3165 | } else |
| 3166 | break; |
| 3167 | body++; |
| 3168 | } |
| 3169 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3170 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3171 | |
| 3172 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3173 | 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] | 3174 | |
| 3175 | body += 2; // skip CRLF |
| 3176 | |
| 3177 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 3178 | * 1. how much entity data do we have? new var |
| 3179 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 3180 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 3181 | */ |
| 3182 | |
| 3183 | if (chunk < limit) |
| 3184 | limit = chunk; /* only reading one chunk */ |
| 3185 | } else { |
| 3186 | if (msg->hdr_content_len < limit) |
| 3187 | limit = msg->hdr_content_len; |
| 3188 | } |
| 3189 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3190 | http_body_end: |
| 3191 | /* we leave once we know we have nothing left to do. This means that we have |
| 3192 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 3193 | * buffer closed). |
| 3194 | */ |
| 3195 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 3196 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 3197 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3198 | /* 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] | 3199 | 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] | 3200 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 3201 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3202 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 3203 | else { |
| 3204 | /* Not enough data. We'll re-use the http-request |
| 3205 | * timeout here. Ideally, we should set the timeout |
| 3206 | * relative to the accept() date. We just set the |
| 3207 | * request timeout once at the beginning of the |
| 3208 | * request. |
| 3209 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3210 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 3211 | if (!tick_isset(req->analyse_exp)) |
| 3212 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 3213 | return 0; |
| 3214 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3215 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3216 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3217 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3218 | * probably reduce one day's debugging session. |
| 3219 | */ |
| 3220 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3221 | 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] | 3222 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3223 | __FILE__, __LINE__, req->analysers); |
| 3224 | ABORT_NOW(); |
| 3225 | } |
| 3226 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3227 | 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] | 3228 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3229 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3230 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3231 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3232 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 3233 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3234 | * 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] | 3235 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3236 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3237 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3238 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3239 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3240 | struct buffer *req = t->req; |
| 3241 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3242 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3243 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3244 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3245 | t, |
| 3246 | rep, |
| 3247 | rep->rex, rep->wex, |
| 3248 | rep->flags, |
| 3249 | rep->l, |
| 3250 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 3251 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3252 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3253 | /* |
| 3254 | * Now parse the partial (or complete) lines. |
| 3255 | * We will check the response syntax, and also join multi-line |
| 3256 | * headers. An index of all the lines will be elaborated while |
| 3257 | * parsing. |
| 3258 | * |
| 3259 | * For the parsing, we use a 28 states FSM. |
| 3260 | * |
| 3261 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3262 | * rep->data + rep->som = beginning of response |
| 3263 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 3264 | * 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] | 3265 | * rep->lr = first non-visited byte |
| 3266 | * rep->r = end of data |
| 3267 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 3268 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3269 | int cur_idx; |
| 3270 | struct http_msg *msg = &txn->rsp; |
| 3271 | struct proxy *cur_proxy; |
| 3272 | |
| 3273 | if (likely(rep->lr < rep->r)) |
| 3274 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 3275 | |
| 3276 | /* 1: we might have to print this header in debug mode */ |
| 3277 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3278 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 3279 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 3280 | char *eol, *sol; |
| 3281 | |
| 3282 | sol = rep->data + msg->som; |
| 3283 | eol = sol + msg->sl.rq.l; |
| 3284 | debug_hdr("srvrep", t, sol, eol); |
| 3285 | |
| 3286 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 3287 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 3288 | |
| 3289 | while (cur_idx) { |
| 3290 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 3291 | debug_hdr("srvhdr", t, sol, eol); |
| 3292 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 3293 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3294 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3295 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3296 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3297 | /* |
| 3298 | * Now we quickly check if we have found a full valid response. |
| 3299 | * If not so, we check the FD and buffer states before leaving. |
| 3300 | * A full response is indicated by the fact that we have seen |
| 3301 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 3302 | * responses are checked first. |
| 3303 | * |
| 3304 | * Depending on whether the client is still there or not, we |
| 3305 | * may send an error response back or not. Note that normally |
| 3306 | * we should only check for HTTP status there, and check I/O |
| 3307 | * errors somewhere else. |
| 3308 | */ |
| 3309 | |
| 3310 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3311 | /* Invalid response */ |
| 3312 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 3313 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3314 | //buffer_shutr(rep); |
| 3315 | //buffer_shutw(req); |
| 3316 | //fd_delete(req->cons->fd); |
| 3317 | //req->cons->state = SI_ST_CLO; |
| 3318 | buffer_shutr_now(rep); |
| 3319 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3320 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3321 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3322 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3323 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3324 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3325 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3326 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3327 | txn->status = 502; |
| 3328 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3329 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3330 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3331 | if (!(t->flags & SN_FINST_MASK)) |
| 3332 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3333 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3334 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3335 | // process_srv_queue(t->srv); |
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 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3338 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3339 | /* too large response does not fit in buffer. */ |
| 3340 | else if (rep->flags & BF_FULL) { |
| 3341 | goto hdr_response_bad; |
| 3342 | } |
| 3343 | /* read error */ |
| 3344 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3345 | buffer_shutr_now(rep); |
| 3346 | buffer_shutw_now(req); |
| 3347 | //fd_delete(req->cons->fd); |
| 3348 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3349 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3350 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3351 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3352 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3353 | //} |
| 3354 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3355 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3356 | txn->status = 502; |
| 3357 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3358 | if (!(t->flags & SN_ERR_MASK)) |
| 3359 | t->flags |= SN_ERR_SRVCL; |
| 3360 | if (!(t->flags & SN_FINST_MASK)) |
| 3361 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3362 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3363 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3364 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3365 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3366 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3367 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3368 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3369 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3370 | buffer_shutr_now(rep); |
| 3371 | buffer_shutw_now(req); |
| 3372 | //fd_delete(req->cons->fd); |
| 3373 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3374 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3375 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3376 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3377 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3378 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3379 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3380 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3381 | txn->status = 504; |
| 3382 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 3383 | if (!(t->flags & SN_ERR_MASK)) |
| 3384 | t->flags |= SN_ERR_SRVTO; |
| 3385 | if (!(t->flags & SN_FINST_MASK)) |
| 3386 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3387 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3388 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3389 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3390 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3391 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3392 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 3393 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3394 | buffer_shutr_now(rep); |
| 3395 | buffer_shutw_now(req); |
| 3396 | //fd_delete(req->cons->fd); |
| 3397 | //req->cons->state = SI_ST_CLO; |
| 3398 | if (t->srv) { |
| 3399 | //t->srv->cur_sess--; |
| 3400 | t->srv->failed_resp++; |
| 3401 | //sess_change_server(t, NULL); |
| 3402 | } |
| 3403 | t->be->failed_resp++; |
| 3404 | rep->analysers = 0; |
| 3405 | txn->status = 502; |
| 3406 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3407 | if (!(t->flags & SN_ERR_MASK)) |
| 3408 | t->flags |= SN_ERR_SRVCL; |
| 3409 | if (!(t->flags & SN_FINST_MASK)) |
| 3410 | t->flags |= SN_FINST_H; |
| 3411 | |
| 3412 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3413 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3414 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3415 | return 0; |
| 3416 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3417 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3418 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3419 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3420 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3421 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3422 | /***************************************************************** |
| 3423 | * More interesting part now : we know that we have a complete * |
| 3424 | * response which at least looks like HTTP. We have an indicator * |
| 3425 | * of each header's length, so we can parse them quickly. * |
| 3426 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3427 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3428 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3429 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3430 | /* ensure we keep this pointer to the beginning of the message */ |
| 3431 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3432 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3433 | /* |
| 3434 | * 1: get the status code and check for cacheability. |
| 3435 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3436 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3437 | t->logs.logwait &= ~LW_RESP; |
| 3438 | 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] | 3439 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3440 | switch (txn->status) { |
| 3441 | case 200: |
| 3442 | case 203: |
| 3443 | case 206: |
| 3444 | case 300: |
| 3445 | case 301: |
| 3446 | case 410: |
| 3447 | /* RFC2616 @13.4: |
| 3448 | * "A response received with a status code of |
| 3449 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3450 | * by a cache (...) unless a cache-control |
| 3451 | * directive prohibits caching." |
| 3452 | * |
| 3453 | * RFC2616 @9.5: POST method : |
| 3454 | * "Responses to this method are not cacheable, |
| 3455 | * unless the response includes appropriate |
| 3456 | * Cache-Control or Expires header fields." |
| 3457 | */ |
| 3458 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3459 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3460 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3461 | break; |
| 3462 | default: |
| 3463 | break; |
| 3464 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3465 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3466 | /* |
| 3467 | * 2: we may need to capture headers |
| 3468 | */ |
| 3469 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3470 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3471 | txn->rsp.cap, t->fe->rsp_cap); |
| 3472 | |
| 3473 | /* |
| 3474 | * 3: we will have to evaluate the filters. |
| 3475 | * As opposed to version 1.2, now they will be evaluated in the |
| 3476 | * filters order and not in the header order. This means that |
| 3477 | * each filter has to be validated among all headers. |
| 3478 | * |
| 3479 | * Filters are tried with ->be first, then with ->fe if it is |
| 3480 | * different from ->be. |
| 3481 | */ |
| 3482 | |
| 3483 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3484 | |
| 3485 | cur_proxy = t->be; |
| 3486 | while (1) { |
| 3487 | struct proxy *rule_set = cur_proxy; |
| 3488 | |
| 3489 | /* try headers filters */ |
| 3490 | if (rule_set->rsp_exp != NULL) { |
| 3491 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3492 | return_bad_resp: |
| 3493 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3494 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3495 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3496 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3497 | } |
| 3498 | cur_proxy->failed_resp++; |
| 3499 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3500 | buffer_shutr_now(rep); |
| 3501 | buffer_shutw_now(req); |
| 3502 | //fd_delete(req->cons->fd); |
| 3503 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3504 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3505 | txn->status = 502; |
| 3506 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3507 | if (!(t->flags & SN_ERR_MASK)) |
| 3508 | t->flags |= SN_ERR_PRXCOND; |
| 3509 | if (!(t->flags & SN_FINST_MASK)) |
| 3510 | t->flags |= SN_FINST_H; |
| 3511 | /* We used to have a free connection slot. Since we'll never use it, |
| 3512 | * we have to inform the server that it may be used by another session. |
| 3513 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3514 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 3515 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3516 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3517 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3518 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3519 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3520 | /* has the response been denied ? */ |
| 3521 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3522 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3523 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3524 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3525 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3526 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3527 | cur_proxy->denied_resp++; |
| 3528 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3529 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3530 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3531 | /* We might have to check for "Connection:" */ |
| 3532 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3533 | !(t->flags & SN_CONN_CLOSED)) { |
| 3534 | char *cur_ptr, *cur_end, *cur_next; |
| 3535 | int cur_idx, old_idx, delta, val; |
| 3536 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3537 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3538 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3539 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3540 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3541 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3542 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3543 | cur_ptr = cur_next; |
| 3544 | cur_end = cur_ptr + cur_hdr->len; |
| 3545 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3546 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3547 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3548 | if (val) { |
| 3549 | /* 3 possibilities : |
| 3550 | * - we have already set Connection: close, |
| 3551 | * so we remove this line. |
| 3552 | * - we have not yet set Connection: close, |
| 3553 | * but this line indicates close. We leave |
| 3554 | * it untouched and set the flag. |
| 3555 | * - we have not yet set Connection: close, |
| 3556 | * and this line indicates non-close. We |
| 3557 | * replace it. |
| 3558 | */ |
| 3559 | if (t->flags & SN_CONN_CLOSED) { |
| 3560 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3561 | txn->rsp.eoh += delta; |
| 3562 | cur_next += delta; |
| 3563 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3564 | txn->hdr_idx.used--; |
| 3565 | cur_hdr->len = 0; |
| 3566 | } else { |
| 3567 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3568 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3569 | "close", 5); |
| 3570 | cur_next += delta; |
| 3571 | cur_hdr->len += delta; |
| 3572 | txn->rsp.eoh += delta; |
| 3573 | } |
| 3574 | t->flags |= SN_CONN_CLOSED; |
| 3575 | } |
| 3576 | } |
| 3577 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3578 | } |
| 3579 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3580 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3581 | /* add response headers from the rule sets in the same order */ |
| 3582 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3583 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3584 | rule_set->rsp_add[cur_idx])) < 0) |
| 3585 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3586 | } |
| 3587 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3588 | /* check whether we're already working on the frontend */ |
| 3589 | if (cur_proxy == t->fe) |
| 3590 | break; |
| 3591 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3592 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3593 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3594 | /* |
| 3595 | * 4: check for server cookie. |
| 3596 | */ |
| 3597 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3598 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3599 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3600 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3601 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3602 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3603 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3604 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3605 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3606 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3607 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3608 | /* |
| 3609 | * 6: add server cookie in the response if needed |
| 3610 | */ |
| 3611 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3612 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3613 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3614 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3615 | /* the server is known, it's not the one the client requested, we have to |
| 3616 | * insert a set-cookie here, except if we want to insert only on POST |
| 3617 | * requests and this one isn't. Note that servers which don't have cookies |
| 3618 | * (eg: some backup servers) will return a full cookie removal request. |
| 3619 | */ |
| 3620 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3621 | t->be->cookie_name, |
| 3622 | 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] | 3623 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3624 | if (t->be->cookie_domain) |
| 3625 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3626 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3627 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3628 | trash, len)) < 0) |
| 3629 | goto return_bad_resp; |
| 3630 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3631 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3632 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3633 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3634 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3635 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3636 | */ |
| 3637 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3638 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3639 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3640 | |
| 3641 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3642 | "Cache-control: private", 22)) < 0) |
| 3643 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3644 | } |
| 3645 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3646 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3647 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3648 | /* |
| 3649 | * 7: check if result will be cacheable with a cookie. |
| 3650 | * We'll block the response if security checks have caught |
| 3651 | * nasty things such as a cacheable cookie. |
| 3652 | */ |
| 3653 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3654 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3655 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3656 | |
| 3657 | /* we're in presence of a cacheable response containing |
| 3658 | * a set-cookie header. We'll block it as requested by |
| 3659 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3660 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3661 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3662 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3663 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3664 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3665 | } |
| 3666 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3667 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3668 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3669 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3670 | send_log(t->be, LOG_ALERT, |
| 3671 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3672 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3673 | goto return_srv_prx_502; |
| 3674 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3675 | |
| 3676 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3677 | * 8: add "Connection: close" if needed and not yet set. |
| 3678 | * 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] | 3679 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3680 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3681 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3682 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3683 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3684 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3685 | "Connection: close", 17)) < 0) |
| 3686 | goto return_bad_resp; |
| 3687 | t->flags |= SN_CONN_CLOSED; |
| 3688 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3689 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3690 | /************************************************************* |
| 3691 | * OK, that's finished for the headers. We have done what we * |
| 3692 | * could. Let's switch to the DATA state. * |
| 3693 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3694 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3695 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3696 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3697 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3698 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3699 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3700 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3701 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3702 | } |
| 3703 | #endif |
| 3704 | /* if the user wants to log as soon as possible, without counting |
| 3705 | * bytes from the server, then this is the right moment. We have |
| 3706 | * to temporarily assign bytes_out to log what we currently have. |
| 3707 | */ |
| 3708 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3709 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3710 | t->logs.bytes_out = txn->rsp.eoh; |
| 3711 | if (t->fe->to_log & LW_REQ) |
| 3712 | http_sess_log(t); |
| 3713 | else |
| 3714 | tcp_sess_log(t); |
| 3715 | t->logs.bytes_out = 0; |
| 3716 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3717 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3718 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3719 | * otherwise we would not let the client side wake up. |
| 3720 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3721 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3722 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3723 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3724 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3725 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3726 | * probably reduce one day's debugging session. |
| 3727 | */ |
| 3728 | #ifdef DEBUG_DEV |
| 3729 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3730 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3731 | __FILE__, __LINE__, rep->analysers); |
| 3732 | ABORT_NOW(); |
| 3733 | } |
| 3734 | #endif |
| 3735 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3736 | return 0; |
| 3737 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3738 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3739 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3740 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 3741 | * connection was attempted and that the file descriptor is already allocated. |
| 3742 | * We must check for establishment, error and abort. Possible output states |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 3743 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3744 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 3745 | * otherwise 1. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3746 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3747 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3748 | { |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3749 | struct buffer *req = si->ob; |
| 3750 | struct buffer *rep = si->ib; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3751 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 3752 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d, fds=%d\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3753 | now_ms, __FUNCTION__, |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3754 | cli_stnames[s->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3755 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3756 | req->flags, rep->flags, |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 3757 | req->l, rep->l, |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3758 | fdtab[si->fd].state); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3759 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3760 | |
| 3761 | /* If we got an error, or if nothing happened and the connection timed |
| 3762 | * out, we must give up. The CER state handler will take care of retry |
| 3763 | * attempts and error reports. |
| 3764 | */ |
| 3765 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
| 3766 | si->state = SI_ST_CER; |
| 3767 | fd_delete(si->fd); |
| 3768 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3769 | if (si->err_type) |
| 3770 | return 0; |
| 3771 | |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3772 | si->err_loc = s->srv; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3773 | if (si->flags & SI_FL_ERR) |
| 3774 | si->err_type = SI_ET_CONN_ERR; |
| 3775 | else |
| 3776 | si->err_type = SI_ET_CONN_TO; |
| 3777 | return 0; |
| 3778 | } |
| 3779 | |
| 3780 | /* OK, maybe we want to abort */ |
| 3781 | if (unlikely((req->flags & BF_SHUTW_NOW) || |
| 3782 | (rep->flags & BF_SHUTW) || |
| 3783 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3784 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) || |
| 3785 | s->be->options & PR_O_ABRT_CLOSE)))) { |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 3786 | /* give up */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3787 | req->wex = TICK_ETERNITY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3788 | fd_delete(si->fd); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3789 | buffer_shutw(req); |
| 3790 | buffer_shutr(rep); |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame^] | 3791 | si->state = SI_ST_DIS; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3792 | si->err_type |= SI_ET_CONN_ABRT; |
| 3793 | si->err_loc = s->srv; |
| 3794 | return 1; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3795 | } |
| 3796 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3797 | /* we need to wait a bit more if there was no activity either */ |
| 3798 | if (!(req->flags & BF_WRITE_ACTIVITY)) |
| 3799 | return 1; |
| 3800 | |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3801 | /* OK, this means that a connection succeeded. The caller will be |
| 3802 | * responsible for handling the transition from CON to EST. |
| 3803 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3804 | s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3805 | si->state = SI_ST_EST; |
| 3806 | si->err_type = SI_ET_NONE; |
| 3807 | si->err_loc = NULL; |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3808 | return 1; |
| 3809 | } |
| 3810 | |
| 3811 | |
| 3812 | /* |
| 3813 | * This function handles the transition between the SI_ST_CON state and the |
| 3814 | * SI_ST_EST state. It must only be called after switching from SI_ST_CON to |
| 3815 | * SI_ST_EST. |
| 3816 | */ |
| 3817 | void sess_establish(struct session *s, struct stream_interface *si) |
| 3818 | { |
| 3819 | struct buffer *req = si->ob; |
| 3820 | struct buffer *rep = si->ib; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3821 | |
| 3822 | if (req->flags & BF_EMPTY) { |
| 3823 | EV_FD_CLR(si->fd, DIR_WR); |
| 3824 | req->wex = TICK_ETERNITY; |
| 3825 | } else { |
| 3826 | EV_FD_SET(si->fd, DIR_WR); |
| 3827 | req->wex = tick_add_ifset(now_ms, s->be->timeout.server); |
| 3828 | if (tick_isset(req->wex)) { |
| 3829 | /* FIXME: to prevent the server from expiring read |
| 3830 | * timeouts during writes, we refresh it. */ |
| 3831 | rep->rex = req->wex; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3832 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3833 | } |
| 3834 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3835 | if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3836 | if (!(rep->flags & BF_HIJACK)) { |
| 3837 | EV_FD_SET(si->fd, DIR_RD); |
| 3838 | rep->rex = tick_add_ifset(now_ms, s->be->timeout.server); |
| 3839 | } |
| 3840 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3841 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3842 | /* if the user wants to log as soon as possible, without counting |
| 3843 | * bytes from the server, then this is the right moment. */ |
| 3844 | if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) { |
| 3845 | s->logs.t_close = s->logs.t_connect; /* to get a valid end date */ |
| 3846 | tcp_sess_log(s); |
| 3847 | } |
| 3848 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3849 | if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
| 3850 | /* TCP splicing supported by both FE and BE */ |
| 3851 | tcp_splice_splicefd(req->prod->fd, si->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3852 | } |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3853 | #endif |
| 3854 | } |
| 3855 | else { |
| 3856 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3857 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3858 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3859 | /* reset hdr_idx which was already initialized by the request. |
| 3860 | * right now, the http parser does it. |
| 3861 | * hdr_idx_init(&s->txn.hdr_idx); |
| 3862 | */ |
| 3863 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3864 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3865 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 3866 | req->wex = TICK_ETERNITY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3867 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3868 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3869 | |
| 3870 | /* This function is called with (si->state == SI_ST_CER) meaning that a |
| 3871 | * previous connection attempt has failed and that the file descriptor |
| 3872 | * has already been released. Possible causes include asynchronous error |
| 3873 | * notification and time out. Possible output states are SI_ST_CLO when |
| 3874 | * retries are exhausted, SI_ST_TAR when a delay is wanted before a new |
| 3875 | * connection attempt, SI_ST_ASS when it's wise to retry on the same server, |
| 3876 | * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are |
| 3877 | * marked as in error state. It returns 0. |
| 3878 | */ |
| 3879 | int sess_update_st_cer(struct session *s, struct stream_interface *si) |
| 3880 | { |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3881 | /* we probably have to release last session from the server */ |
| 3882 | if (s->srv) { |
| 3883 | if (s->flags & SN_CURR_SESS) { |
| 3884 | s->flags &= ~SN_CURR_SESS; |
| 3885 | s->srv->cur_sess--; |
| 3886 | } |
| 3887 | sess_change_server(s, NULL); |
| 3888 | } |
| 3889 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3890 | /* ensure that we have enough retries left */ |
| 3891 | s->conn_retries--; |
| 3892 | if (s->conn_retries < 0) { |
| 3893 | if (!si->err_type) { |
| 3894 | si->err_type = SI_ET_CONN_ERR; |
| 3895 | si->err_loc = s->srv; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3896 | } |
| 3897 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3898 | if (s->srv) |
| 3899 | s->srv->failed_conns++; |
| 3900 | s->be->failed_conns++; |
| 3901 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3902 | process_srv_queue(s->srv); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3903 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3904 | buffer_shutw(si->ob); |
| 3905 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3906 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3907 | buffer_shutr(si->ib); |
| 3908 | si->ib->flags |= BF_READ_ERROR; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3909 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3910 | si->state = SI_ST_CLO; |
| 3911 | return 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3912 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3913 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3914 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 3915 | * retry on another server for the last retry. In order to achieve this, |
| 3916 | * we must mark the session unassigned, and eventually clear the DIRECT |
| 3917 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 3918 | * redispatch yet, because this will depend on what server is selected. |
| 3919 | */ |
| 3920 | if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) { |
| 3921 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3922 | process_srv_queue(s->srv); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3923 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3924 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3925 | s->prev_srv = s->srv; |
| 3926 | si->state = SI_ST_REQ; |
| 3927 | } else { |
| 3928 | if (s->srv) |
| 3929 | s->srv->retries++; |
| 3930 | s->be->retries++; |
| 3931 | si->state = SI_ST_ASS; |
| 3932 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3933 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3934 | if (si->flags & SI_FL_ERR) { |
| 3935 | /* The error was an asynchronous connection error, and we will |
| 3936 | * likely have to retry connecting to the same server, most |
| 3937 | * likely leading to the same result. To avoid this, we wait |
| 3938 | * one second before retrying. |
| 3939 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3940 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3941 | if (!si->err_type) |
| 3942 | si->err_type = SI_ET_CONN_ERR; |
| 3943 | |
| 3944 | si->state = SI_ST_TAR; |
| 3945 | si->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3946 | return 0; |
| 3947 | } |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3948 | return 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3949 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3950 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3951 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3952 | /* |
| 3953 | * 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] | 3954 | * called with client socket shut down on input. Right now, only statistics can |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3955 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 3956 | * buffer, 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] | 3957 | * 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] | 3958 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3959 | */ |
| 3960 | int produce_content(struct session *s) |
| 3961 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3962 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3963 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3964 | return 1; |
| 3965 | } |
| 3966 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3967 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3968 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3969 | if (ret >= 0) |
| 3970 | return ret; |
| 3971 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3972 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3973 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3974 | /* unknown data source or internal error */ |
| 3975 | s->txn.status = 500; |
| 3976 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3977 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3978 | if (!(s->flags & SN_ERR_MASK)) |
| 3979 | s->flags |= SN_ERR_PRXCOND; |
| 3980 | if (!(s->flags & SN_FINST_MASK)) |
| 3981 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3982 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3983 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3987 | /* Iterate the same filter through all request headers. |
| 3988 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3989 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3990 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3991 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3992 | 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] | 3993 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3994 | char term; |
| 3995 | char *cur_ptr, *cur_end, *cur_next; |
| 3996 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3997 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3998 | struct hdr_idx_elem *cur_hdr; |
| 3999 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4000 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4001 | last_hdr = 0; |
| 4002 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4003 | 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] | 4004 | old_idx = 0; |
| 4005 | |
| 4006 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4007 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4008 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4009 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4010 | (exp->action == ACT_ALLOW || |
| 4011 | exp->action == ACT_DENY || |
| 4012 | exp->action == ACT_TARPIT)) |
| 4013 | return 0; |
| 4014 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4015 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4016 | if (!cur_idx) |
| 4017 | break; |
| 4018 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4019 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4020 | cur_ptr = cur_next; |
| 4021 | cur_end = cur_ptr + cur_hdr->len; |
| 4022 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4023 | |
| 4024 | /* Now we have one header between cur_ptr and cur_end, |
| 4025 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4026 | */ |
| 4027 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4028 | /* The annoying part is that pattern matching needs |
| 4029 | * that we modify the contents to null-terminate all |
| 4030 | * strings before testing them. |
| 4031 | */ |
| 4032 | |
| 4033 | term = *cur_end; |
| 4034 | *cur_end = '\0'; |
| 4035 | |
| 4036 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4037 | switch (exp->action) { |
| 4038 | case ACT_SETBE: |
| 4039 | /* It is not possible to jump a second time. |
| 4040 | * FIXME: should we return an HTTP/500 here so that |
| 4041 | * the admin knows there's a problem ? |
| 4042 | */ |
| 4043 | if (t->be != t->fe) |
| 4044 | break; |
| 4045 | |
| 4046 | /* Swithing Proxy */ |
| 4047 | t->be = (struct proxy *) exp->replace; |
| 4048 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4049 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4050 | * because we have associated req_cap and rsp_cap to the |
| 4051 | * frontend, and the beconn will be updated later. |
| 4052 | */ |
| 4053 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4054 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4055 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4056 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4057 | last_hdr = 1; |
| 4058 | break; |
| 4059 | |
| 4060 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4061 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4062 | last_hdr = 1; |
| 4063 | break; |
| 4064 | |
| 4065 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4066 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4067 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4068 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4069 | break; |
| 4070 | |
| 4071 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4072 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4073 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4074 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4075 | break; |
| 4076 | |
| 4077 | case ACT_REPLACE: |
| 4078 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4079 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4080 | /* FIXME: if the user adds a newline in the replacement, the |
| 4081 | * index will not be recalculated for now, and the new line |
| 4082 | * will not be counted as a new header. |
| 4083 | */ |
| 4084 | |
| 4085 | cur_end += delta; |
| 4086 | cur_next += delta; |
| 4087 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4088 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4089 | break; |
| 4090 | |
| 4091 | case ACT_REMOVE: |
| 4092 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4093 | cur_next += delta; |
| 4094 | |
| 4095 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4096 | txn->req.eoh += delta; |
| 4097 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4098 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4099 | cur_hdr->len = 0; |
| 4100 | cur_end = NULL; /* null-term has been rewritten */ |
| 4101 | break; |
| 4102 | |
| 4103 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4104 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4105 | if (cur_end) |
| 4106 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4107 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4108 | /* keep the link from this header to next one in case of later |
| 4109 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4110 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4111 | old_idx = cur_idx; |
| 4112 | } |
| 4113 | return 0; |
| 4114 | } |
| 4115 | |
| 4116 | |
| 4117 | /* Apply the filter to the request line. |
| 4118 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4119 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4120 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4121 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4122 | */ |
| 4123 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4124 | { |
| 4125 | char term; |
| 4126 | char *cur_ptr, *cur_end; |
| 4127 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4128 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4129 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4130 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4131 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4132 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4133 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4134 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4135 | (exp->action == ACT_ALLOW || |
| 4136 | exp->action == ACT_DENY || |
| 4137 | exp->action == ACT_TARPIT)) |
| 4138 | return 0; |
| 4139 | else if (exp->action == ACT_REMOVE) |
| 4140 | return 0; |
| 4141 | |
| 4142 | done = 0; |
| 4143 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4144 | 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] | 4145 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4146 | |
| 4147 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4148 | |
| 4149 | /* The annoying part is that pattern matching needs |
| 4150 | * that we modify the contents to null-terminate all |
| 4151 | * strings before testing them. |
| 4152 | */ |
| 4153 | |
| 4154 | term = *cur_end; |
| 4155 | *cur_end = '\0'; |
| 4156 | |
| 4157 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4158 | switch (exp->action) { |
| 4159 | case ACT_SETBE: |
| 4160 | /* It is not possible to jump a second time. |
| 4161 | * FIXME: should we return an HTTP/500 here so that |
| 4162 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4163 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4164 | if (t->be != t->fe) |
| 4165 | break; |
| 4166 | |
| 4167 | /* Swithing Proxy */ |
| 4168 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4169 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4170 | /* right now, the backend switch is not too much complicated |
| 4171 | * because we have associated req_cap and rsp_cap to the |
| 4172 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4173 | */ |
| 4174 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4175 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4176 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4177 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4178 | done = 1; |
| 4179 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4180 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4181 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4182 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4183 | done = 1; |
| 4184 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4185 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4186 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4187 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4188 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4189 | done = 1; |
| 4190 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4191 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4192 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4193 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4194 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4195 | done = 1; |
| 4196 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4197 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4198 | case ACT_REPLACE: |
| 4199 | *cur_end = term; /* restore the string terminator */ |
| 4200 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4201 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4202 | /* FIXME: if the user adds a newline in the replacement, the |
| 4203 | * index will not be recalculated for now, and the new line |
| 4204 | * will not be counted as a new header. |
| 4205 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4206 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4207 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4208 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4209 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4210 | 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] | 4211 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4212 | HTTP_MSG_RQMETH, |
| 4213 | cur_ptr, cur_end + 1, |
| 4214 | NULL, NULL); |
| 4215 | if (unlikely(!cur_end)) |
| 4216 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4217 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4218 | /* we have a full request and we know that we have either a CR |
| 4219 | * or an LF at <ptr>. |
| 4220 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4221 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4222 | 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] | 4223 | /* there is no point trying this regex on headers */ |
| 4224 | return 1; |
| 4225 | } |
| 4226 | } |
| 4227 | *cur_end = term; /* restore the string terminator */ |
| 4228 | return done; |
| 4229 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4230 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4231 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4232 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4233 | /* |
| 4234 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4235 | * 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] | 4236 | * unparsable request. Since it can manage the switch to another backend, it |
| 4237 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4238 | */ |
| 4239 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4240 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4241 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4242 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4243 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4244 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4245 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4246 | /* |
| 4247 | * The interleaving of transformations and verdicts |
| 4248 | * makes it difficult to decide to continue or stop |
| 4249 | * the evaluation. |
| 4250 | */ |
| 4251 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4252 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4253 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4254 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4255 | exp = exp->next; |
| 4256 | continue; |
| 4257 | } |
| 4258 | |
| 4259 | /* Apply the filter to the request line. */ |
| 4260 | ret = apply_filter_to_req_line(t, req, exp); |
| 4261 | if (unlikely(ret < 0)) |
| 4262 | return -1; |
| 4263 | |
| 4264 | if (likely(ret == 0)) { |
| 4265 | /* The filter did not match the request, it can be |
| 4266 | * iterated through all headers. |
| 4267 | */ |
| 4268 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4269 | } |
| 4270 | exp = exp->next; |
| 4271 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4272 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4276 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4277 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4278 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4279 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4280 | */ |
| 4281 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4282 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4283 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4284 | char *p1, *p2, *p3, *p4; |
| 4285 | char *del_colon, *del_cookie, *colon; |
| 4286 | int app_cookies; |
| 4287 | |
| 4288 | appsess *asession_temp = NULL; |
| 4289 | appsess local_asession; |
| 4290 | |
| 4291 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4292 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4293 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4294 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4295 | * we start with the start line. |
| 4296 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4297 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4298 | 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] | 4299 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4300 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4301 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4302 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4303 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4304 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4305 | cur_ptr = cur_next; |
| 4306 | cur_end = cur_ptr + cur_hdr->len; |
| 4307 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4308 | |
| 4309 | /* We have one full header between cur_ptr and cur_end, and the |
| 4310 | * next header starts at cur_next. We're only interested in |
| 4311 | * "Cookie:" headers. |
| 4312 | */ |
| 4313 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4314 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4315 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4316 | old_idx = cur_idx; |
| 4317 | continue; |
| 4318 | } |
| 4319 | |
| 4320 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4321 | * attributes whose name begin with a '$', and associate them with |
| 4322 | * the right cookie, if we want to delete this cookie. |
| 4323 | * So there are 3 cases for each cookie read : |
| 4324 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4325 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4326 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4327 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4328 | * "special" cookie. |
| 4329 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4330 | * remove it. If no application cookie persists in the header, we |
| 4331 | * *MUST* delete it |
| 4332 | */ |
| 4333 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4334 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4335 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4336 | /* del_cookie == NULL => nothing to be deleted */ |
| 4337 | del_colon = del_cookie = NULL; |
| 4338 | app_cookies = 0; |
| 4339 | |
| 4340 | while (p1 < cur_end) { |
| 4341 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4342 | while (p1 < cur_end) { |
| 4343 | if (*p1 == ';' || *p1 == ',') |
| 4344 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4345 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4346 | break; |
| 4347 | p1++; |
| 4348 | } |
| 4349 | |
| 4350 | if (p1 == cur_end) |
| 4351 | break; |
| 4352 | |
| 4353 | /* p1 is at the beginning of the cookie name */ |
| 4354 | p2 = p1; |
| 4355 | while (p2 < cur_end && *p2 != '=') |
| 4356 | p2++; |
| 4357 | |
| 4358 | if (p2 == cur_end) |
| 4359 | break; |
| 4360 | |
| 4361 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4362 | if (p3 == cur_end) |
| 4363 | break; |
| 4364 | |
| 4365 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4366 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4367 | p4++; |
| 4368 | |
| 4369 | /* here, we have the cookie name between p1 and p2, |
| 4370 | * and its value between p3 and p4. |
| 4371 | * we can process it : |
| 4372 | * |
| 4373 | * Cookie: NAME=VALUE; |
| 4374 | * | || || | |
| 4375 | * | || || +--> p4 |
| 4376 | * | || |+-------> p3 |
| 4377 | * | || +--------> p2 |
| 4378 | * | |+------------> p1 |
| 4379 | * | +-------------> colon |
| 4380 | * +--------------------> cur_ptr |
| 4381 | */ |
| 4382 | |
| 4383 | if (*p1 == '$') { |
| 4384 | /* skip this one */ |
| 4385 | } |
| 4386 | else { |
| 4387 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4388 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4389 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4390 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4391 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4392 | int log_len = p4 - p1; |
| 4393 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4394 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4395 | Alert("HTTP logging : out of memory.\n"); |
| 4396 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4397 | if (log_len > t->fe->capture_len) |
| 4398 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4399 | memcpy(txn->cli_cookie, p1, log_len); |
| 4400 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4401 | } |
| 4402 | } |
| 4403 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4404 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4405 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4406 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4407 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4408 | char *delim; |
| 4409 | |
| 4410 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4411 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4412 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4413 | * |
| 4414 | * Cookie: NAME=SRV~VALUE; |
| 4415 | * | || || | | |
| 4416 | * | || || | +--> p4 |
| 4417 | * | || || +--------> delim |
| 4418 | * | || |+-----------> p3 |
| 4419 | * | || +------------> p2 |
| 4420 | * | |+----------------> p1 |
| 4421 | * | +-----------------> colon |
| 4422 | * +------------------------> cur_ptr |
| 4423 | */ |
| 4424 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4425 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4426 | for (delim = p3; delim < p4; delim++) |
| 4427 | if (*delim == COOKIE_DELIM) |
| 4428 | break; |
| 4429 | } |
| 4430 | else |
| 4431 | delim = p4; |
| 4432 | |
| 4433 | |
| 4434 | /* Here, we'll look for the first running server which supports the cookie. |
| 4435 | * This allows to share a same cookie between several servers, for example |
| 4436 | * to dedicate backup servers to specific servers only. |
| 4437 | * However, to prevent clients from sticking to cookie-less backup server |
| 4438 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4439 | * empty cookies and mark them as invalid. |
| 4440 | */ |
| 4441 | if (delim == p3) |
| 4442 | srv = NULL; |
| 4443 | |
| 4444 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4445 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4446 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4447 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4448 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4449 | txn->flags &= ~TX_CK_MASK; |
| 4450 | txn->flags |= TX_CK_VALID; |
| 4451 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4452 | t->srv = srv; |
| 4453 | break; |
| 4454 | } else { |
| 4455 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4456 | txn->flags &= ~TX_CK_MASK; |
| 4457 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4458 | } |
| 4459 | } |
| 4460 | srv = srv->next; |
| 4461 | } |
| 4462 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4463 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4464 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4465 | txn->flags &= ~TX_CK_MASK; |
| 4466 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | /* depending on the cookie mode, we may have to either : |
| 4470 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4471 | * the server never sees it ; |
| 4472 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4473 | * application cookie so that it does not get accidentely removed later, |
| 4474 | * if we're in cookie prefix mode |
| 4475 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4476 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4477 | int delta; /* negative */ |
| 4478 | |
| 4479 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4480 | p4 += delta; |
| 4481 | cur_end += delta; |
| 4482 | cur_next += delta; |
| 4483 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4484 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4485 | |
| 4486 | del_cookie = del_colon = NULL; |
| 4487 | app_cookies++; /* protect the header from deletion */ |
| 4488 | } |
| 4489 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4490 | (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] | 4491 | del_cookie = p1; |
| 4492 | del_colon = colon; |
| 4493 | } |
| 4494 | } else { |
| 4495 | /* now we know that we must keep this cookie since it's |
| 4496 | * not ours. But if we wanted to delete our cookie |
| 4497 | * earlier, we cannot remove the complete header, but we |
| 4498 | * can remove the previous block itself. |
| 4499 | */ |
| 4500 | app_cookies++; |
| 4501 | |
| 4502 | if (del_cookie != NULL) { |
| 4503 | int delta; /* negative */ |
| 4504 | |
| 4505 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4506 | p4 += delta; |
| 4507 | cur_end += delta; |
| 4508 | cur_next += delta; |
| 4509 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4510 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4511 | del_cookie = del_colon = NULL; |
| 4512 | } |
| 4513 | } |
| 4514 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4515 | if ((t->be->appsession_name != NULL) && |
| 4516 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4517 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4518 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4519 | /* Cool... it's the right one */ |
| 4520 | |
| 4521 | asession_temp = &local_asession; |
| 4522 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4523 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4524 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4525 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4526 | return; |
| 4527 | } |
| 4528 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4529 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4530 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4531 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4532 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4533 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4534 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4535 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4536 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4537 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4538 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4539 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4540 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4541 | return; |
| 4542 | } |
| 4543 | |
| 4544 | asession_temp->sessid = local_asession.sessid; |
| 4545 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4546 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4547 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4548 | } else { |
| 4549 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4550 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4551 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4552 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4553 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4554 | Alert("Found Application Session without matching server.\n"); |
| 4555 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4556 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4557 | while (srv) { |
| 4558 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4559 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4560 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4561 | txn->flags &= ~TX_CK_MASK; |
| 4562 | txn->flags |= TX_CK_VALID; |
| 4563 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4564 | t->srv = srv; |
| 4565 | break; |
| 4566 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4567 | txn->flags &= ~TX_CK_MASK; |
| 4568 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4569 | } |
| 4570 | } |
| 4571 | srv = srv->next; |
| 4572 | }/* end while(srv) */ |
| 4573 | }/* end else if server == NULL */ |
| 4574 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4575 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4576 | asession_temp->request_count++; |
| 4577 | #if defined(DEBUG_HASH) |
| 4578 | Alert("manage_client_side_cookies\n"); |
| 4579 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4580 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4581 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4582 | } |
| 4583 | |
| 4584 | /* we'll have to look for another cookie ... */ |
| 4585 | p1 = p4; |
| 4586 | } /* while (p1 < cur_end) */ |
| 4587 | |
| 4588 | /* There's no more cookie on this line. |
| 4589 | * We may have marked the last one(s) for deletion. |
| 4590 | * We must do this now in two ways : |
| 4591 | * - if there is no app cookie, we simply delete the header ; |
| 4592 | * - if there are app cookies, we must delete the end of the |
| 4593 | * string properly, including the colon/semi-colon before |
| 4594 | * the cookie name. |
| 4595 | */ |
| 4596 | if (del_cookie != NULL) { |
| 4597 | int delta; |
| 4598 | if (app_cookies) { |
| 4599 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4600 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4601 | cur_hdr->len += delta; |
| 4602 | } else { |
| 4603 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4604 | |
| 4605 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4606 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4607 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4608 | cur_hdr->len = 0; |
| 4609 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4610 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4611 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | /* keep the link from this header to next one */ |
| 4615 | old_idx = cur_idx; |
| 4616 | } /* end of cookie processing on this header */ |
| 4617 | } |
| 4618 | |
| 4619 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4620 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4621 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4622 | */ |
| 4623 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4624 | { |
| 4625 | char term; |
| 4626 | char *cur_ptr, *cur_end, *cur_next; |
| 4627 | int cur_idx, old_idx, last_hdr; |
| 4628 | struct http_txn *txn = &t->txn; |
| 4629 | struct hdr_idx_elem *cur_hdr; |
| 4630 | int len, delta; |
| 4631 | |
| 4632 | last_hdr = 0; |
| 4633 | |
| 4634 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4635 | old_idx = 0; |
| 4636 | |
| 4637 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4638 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4639 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4640 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4641 | (exp->action == ACT_ALLOW || |
| 4642 | exp->action == ACT_DENY)) |
| 4643 | return 0; |
| 4644 | |
| 4645 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4646 | if (!cur_idx) |
| 4647 | break; |
| 4648 | |
| 4649 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4650 | cur_ptr = cur_next; |
| 4651 | cur_end = cur_ptr + cur_hdr->len; |
| 4652 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4653 | |
| 4654 | /* Now we have one header between cur_ptr and cur_end, |
| 4655 | * and the next header starts at cur_next. |
| 4656 | */ |
| 4657 | |
| 4658 | /* The annoying part is that pattern matching needs |
| 4659 | * that we modify the contents to null-terminate all |
| 4660 | * strings before testing them. |
| 4661 | */ |
| 4662 | |
| 4663 | term = *cur_end; |
| 4664 | *cur_end = '\0'; |
| 4665 | |
| 4666 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4667 | switch (exp->action) { |
| 4668 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4669 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4670 | last_hdr = 1; |
| 4671 | break; |
| 4672 | |
| 4673 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4674 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4675 | last_hdr = 1; |
| 4676 | break; |
| 4677 | |
| 4678 | case ACT_REPLACE: |
| 4679 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4680 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4681 | /* FIXME: if the user adds a newline in the replacement, the |
| 4682 | * index will not be recalculated for now, and the new line |
| 4683 | * will not be counted as a new header. |
| 4684 | */ |
| 4685 | |
| 4686 | cur_end += delta; |
| 4687 | cur_next += delta; |
| 4688 | cur_hdr->len += delta; |
| 4689 | txn->rsp.eoh += delta; |
| 4690 | break; |
| 4691 | |
| 4692 | case ACT_REMOVE: |
| 4693 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4694 | cur_next += delta; |
| 4695 | |
| 4696 | /* FIXME: this should be a separate function */ |
| 4697 | txn->rsp.eoh += delta; |
| 4698 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4699 | txn->hdr_idx.used--; |
| 4700 | cur_hdr->len = 0; |
| 4701 | cur_end = NULL; /* null-term has been rewritten */ |
| 4702 | break; |
| 4703 | |
| 4704 | } |
| 4705 | } |
| 4706 | if (cur_end) |
| 4707 | *cur_end = term; /* restore the string terminator */ |
| 4708 | |
| 4709 | /* keep the link from this header to next one in case of later |
| 4710 | * removal of next header. |
| 4711 | */ |
| 4712 | old_idx = cur_idx; |
| 4713 | } |
| 4714 | return 0; |
| 4715 | } |
| 4716 | |
| 4717 | |
| 4718 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4719 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4720 | * or -1 if a replacement resulted in an invalid status line. |
| 4721 | */ |
| 4722 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4723 | { |
| 4724 | char term; |
| 4725 | char *cur_ptr, *cur_end; |
| 4726 | int done; |
| 4727 | struct http_txn *txn = &t->txn; |
| 4728 | int len, delta; |
| 4729 | |
| 4730 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4731 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4732 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4733 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4734 | (exp->action == ACT_ALLOW || |
| 4735 | exp->action == ACT_DENY)) |
| 4736 | return 0; |
| 4737 | else if (exp->action == ACT_REMOVE) |
| 4738 | return 0; |
| 4739 | |
| 4740 | done = 0; |
| 4741 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4742 | 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] | 4743 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4744 | |
| 4745 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4746 | |
| 4747 | /* The annoying part is that pattern matching needs |
| 4748 | * that we modify the contents to null-terminate all |
| 4749 | * strings before testing them. |
| 4750 | */ |
| 4751 | |
| 4752 | term = *cur_end; |
| 4753 | *cur_end = '\0'; |
| 4754 | |
| 4755 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4756 | switch (exp->action) { |
| 4757 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4758 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4759 | done = 1; |
| 4760 | break; |
| 4761 | |
| 4762 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4763 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4764 | done = 1; |
| 4765 | break; |
| 4766 | |
| 4767 | case ACT_REPLACE: |
| 4768 | *cur_end = term; /* restore the string terminator */ |
| 4769 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4770 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4771 | /* FIXME: if the user adds a newline in the replacement, the |
| 4772 | * index will not be recalculated for now, and the new line |
| 4773 | * will not be counted as a new header. |
| 4774 | */ |
| 4775 | |
| 4776 | txn->rsp.eoh += delta; |
| 4777 | cur_end += delta; |
| 4778 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4779 | 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] | 4780 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4781 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4782 | cur_ptr, cur_end + 1, |
| 4783 | NULL, NULL); |
| 4784 | if (unlikely(!cur_end)) |
| 4785 | return -1; |
| 4786 | |
| 4787 | /* we have a full respnse and we know that we have either a CR |
| 4788 | * or an LF at <ptr>. |
| 4789 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4790 | 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] | 4791 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4792 | /* there is no point trying this regex on headers */ |
| 4793 | return 1; |
| 4794 | } |
| 4795 | } |
| 4796 | *cur_end = term; /* restore the string terminator */ |
| 4797 | return done; |
| 4798 | } |
| 4799 | |
| 4800 | |
| 4801 | |
| 4802 | /* |
| 4803 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4804 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4805 | * unparsable response. |
| 4806 | */ |
| 4807 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4808 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4809 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4810 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4811 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4812 | int ret; |
| 4813 | |
| 4814 | /* |
| 4815 | * The interleaving of transformations and verdicts |
| 4816 | * makes it difficult to decide to continue or stop |
| 4817 | * the evaluation. |
| 4818 | */ |
| 4819 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4820 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4821 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4822 | exp->action == ACT_PASS)) { |
| 4823 | exp = exp->next; |
| 4824 | continue; |
| 4825 | } |
| 4826 | |
| 4827 | /* Apply the filter to the status line. */ |
| 4828 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4829 | if (unlikely(ret < 0)) |
| 4830 | return -1; |
| 4831 | |
| 4832 | if (likely(ret == 0)) { |
| 4833 | /* The filter did not match the response, it can be |
| 4834 | * iterated through all headers. |
| 4835 | */ |
| 4836 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4837 | } |
| 4838 | exp = exp->next; |
| 4839 | } |
| 4840 | return 0; |
| 4841 | } |
| 4842 | |
| 4843 | |
| 4844 | |
| 4845 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4846 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4847 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4848 | */ |
| 4849 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4850 | { |
| 4851 | struct http_txn *txn = &t->txn; |
| 4852 | char *p1, *p2, *p3, *p4; |
| 4853 | |
| 4854 | appsess *asession_temp = NULL; |
| 4855 | appsess local_asession; |
| 4856 | |
| 4857 | char *cur_ptr, *cur_end, *cur_next; |
| 4858 | int cur_idx, old_idx, delta; |
| 4859 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4860 | /* Iterate through the headers. |
| 4861 | * we start with the start line. |
| 4862 | */ |
| 4863 | old_idx = 0; |
| 4864 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4865 | |
| 4866 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4867 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4868 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4869 | |
| 4870 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4871 | cur_ptr = cur_next; |
| 4872 | cur_end = cur_ptr + cur_hdr->len; |
| 4873 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4874 | |
| 4875 | /* We have one full header between cur_ptr and cur_end, and the |
| 4876 | * next header starts at cur_next. We're only interested in |
| 4877 | * "Cookie:" headers. |
| 4878 | */ |
| 4879 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4880 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4881 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4882 | old_idx = cur_idx; |
| 4883 | continue; |
| 4884 | } |
| 4885 | |
| 4886 | /* 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] | 4887 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4888 | |
| 4889 | |
| 4890 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4891 | if (t->be->cookie_name == NULL && |
| 4892 | t->be->appsession_name == NULL && |
| 4893 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4894 | return; |
| 4895 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4896 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4897 | |
| 4898 | 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] | 4899 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4900 | break; |
| 4901 | |
| 4902 | /* p1 is at the beginning of the cookie name */ |
| 4903 | p2 = p1; |
| 4904 | |
| 4905 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4906 | p2++; |
| 4907 | |
| 4908 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4909 | break; |
| 4910 | |
| 4911 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4912 | if (p3 == cur_end) |
| 4913 | break; |
| 4914 | |
| 4915 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4916 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4917 | p4++; |
| 4918 | |
| 4919 | /* here, we have the cookie name between p1 and p2, |
| 4920 | * and its value between p3 and p4. |
| 4921 | * we can process it. |
| 4922 | */ |
| 4923 | |
| 4924 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4925 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4926 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4927 | (p4 - p1 >= t->be->capture_namelen) && |
| 4928 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4929 | int log_len = p4 - p1; |
| 4930 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4931 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4932 | Alert("HTTP logging : out of memory.\n"); |
| 4933 | } |
| 4934 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4935 | if (log_len > t->be->capture_len) |
| 4936 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4937 | memcpy(txn->srv_cookie, p1, log_len); |
| 4938 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4942 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4943 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4944 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4945 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4946 | |
| 4947 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4948 | * this occurrence because we'll insert another one later. |
| 4949 | * We'll delete it too if the "indirect" option is set and we're in |
| 4950 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4951 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4952 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4953 | /* this header must be deleted */ |
| 4954 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4955 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4956 | txn->hdr_idx.used--; |
| 4957 | cur_hdr->len = 0; |
| 4958 | cur_next += delta; |
| 4959 | txn->rsp.eoh += delta; |
| 4960 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4961 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4962 | } |
| 4963 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4964 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4965 | /* replace bytes p3->p4 with the cookie name associated |
| 4966 | * with this server since we know it. |
| 4967 | */ |
| 4968 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4969 | cur_hdr->len += delta; |
| 4970 | cur_next += delta; |
| 4971 | txn->rsp.eoh += delta; |
| 4972 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4973 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4974 | } |
| 4975 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4976 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4977 | /* insert the cookie name associated with this server |
| 4978 | * before existing cookie, and insert a delimitor between them.. |
| 4979 | */ |
| 4980 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4981 | cur_hdr->len += delta; |
| 4982 | cur_next += delta; |
| 4983 | txn->rsp.eoh += delta; |
| 4984 | |
| 4985 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4986 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4987 | } |
| 4988 | } |
| 4989 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4990 | else if ((t->be->appsession_name != NULL) && |
| 4991 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4992 | |
| 4993 | /* Cool... it's the right one */ |
| 4994 | |
| 4995 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4996 | asession_temp = &local_asession; |
| 4997 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4998 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4999 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5000 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5001 | return; |
| 5002 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5003 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5004 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5005 | asession_temp->serverid = NULL; |
| 5006 | |
| 5007 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5008 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5009 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5010 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5011 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5012 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5013 | return; |
| 5014 | } |
| 5015 | asession_temp->sessid = local_asession.sessid; |
| 5016 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5017 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5018 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5019 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5020 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5021 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5022 | } |
| 5023 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5024 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5025 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5026 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5027 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5028 | return; |
| 5029 | } |
| 5030 | asession_temp->serverid[0] = '\0'; |
| 5031 | } |
| 5032 | |
| 5033 | if (asession_temp->serverid[0] == '\0') |
| 5034 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5035 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5036 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5037 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5038 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5039 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5040 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5041 | #endif |
| 5042 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5043 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5044 | } /* we're now at the end of the cookie value */ |
| 5045 | |
| 5046 | /* keep the link from this header to next one */ |
| 5047 | old_idx = cur_idx; |
| 5048 | } /* end of cookie processing on this header */ |
| 5049 | } |
| 5050 | |
| 5051 | |
| 5052 | |
| 5053 | /* |
| 5054 | * Check if response is cacheable or not. Updates t->flags. |
| 5055 | */ |
| 5056 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5057 | { |
| 5058 | struct http_txn *txn = &t->txn; |
| 5059 | char *p1, *p2; |
| 5060 | |
| 5061 | char *cur_ptr, *cur_end, *cur_next; |
| 5062 | int cur_idx; |
| 5063 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5064 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5065 | return; |
| 5066 | |
| 5067 | /* Iterate through the headers. |
| 5068 | * we start with the start line. |
| 5069 | */ |
| 5070 | cur_idx = 0; |
| 5071 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5072 | |
| 5073 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5074 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5075 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5076 | |
| 5077 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5078 | cur_ptr = cur_next; |
| 5079 | cur_end = cur_ptr + cur_hdr->len; |
| 5080 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5081 | |
| 5082 | /* We have one full header between cur_ptr and cur_end, and the |
| 5083 | * next header starts at cur_next. We're only interested in |
| 5084 | * "Cookie:" headers. |
| 5085 | */ |
| 5086 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5087 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5088 | if (val) { |
| 5089 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5090 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5091 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5092 | return; |
| 5093 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5094 | } |
| 5095 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5096 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5097 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5098 | continue; |
| 5099 | |
| 5100 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5101 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5102 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5103 | |
| 5104 | if (p1 >= cur_end) /* no more info */ |
| 5105 | continue; |
| 5106 | |
| 5107 | /* p1 is at the beginning of the value */ |
| 5108 | p2 = p1; |
| 5109 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5110 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5111 | p2++; |
| 5112 | |
| 5113 | /* we have a complete value between p1 and p2 */ |
| 5114 | if (p2 < cur_end && *p2 == '=') { |
| 5115 | /* we have something of the form no-cache="set-cookie" */ |
| 5116 | if ((cur_end - p1 >= 21) && |
| 5117 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5118 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5119 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5120 | continue; |
| 5121 | } |
| 5122 | |
| 5123 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5124 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5125 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5126 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5127 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5128 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5129 | return; |
| 5130 | } |
| 5131 | |
| 5132 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5133 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5134 | continue; |
| 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5140 | /* |
| 5141 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5142 | * If the server is found, it's assigned to the session. |
| 5143 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5144 | 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] | 5145 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5146 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5147 | appsess *asession_temp = NULL; |
| 5148 | appsess local_asession; |
| 5149 | char *request_line; |
| 5150 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5151 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5152 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5153 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5154 | ((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] | 5155 | return; |
| 5156 | |
| 5157 | /* skip ';' */ |
| 5158 | request_line++; |
| 5159 | |
| 5160 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5161 | 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] | 5162 | return; |
| 5163 | |
| 5164 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5165 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5166 | |
| 5167 | /* First try if we already have an appsession */ |
| 5168 | asession_temp = &local_asession; |
| 5169 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5170 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5171 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5172 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5173 | return; |
| 5174 | } |
| 5175 | |
| 5176 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5177 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5178 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5179 | asession_temp->serverid = NULL; |
| 5180 | |
| 5181 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5182 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5183 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5184 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5185 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5186 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5187 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5188 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5189 | return; |
| 5190 | } |
| 5191 | asession_temp->sessid = local_asession.sessid; |
| 5192 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5193 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5194 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5195 | } |
| 5196 | else { |
| 5197 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5198 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5199 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5200 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5201 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5202 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5203 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5204 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5205 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5206 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5207 | #endif |
| 5208 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5209 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5210 | Alert("Found Application Session without matching server.\n"); |
| 5211 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5212 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5213 | while (srv) { |
| 5214 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5215 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5216 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5217 | txn->flags &= ~TX_CK_MASK; |
| 5218 | txn->flags |= TX_CK_VALID; |
| 5219 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5220 | t->srv = srv; |
| 5221 | break; |
| 5222 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5223 | txn->flags &= ~TX_CK_MASK; |
| 5224 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5225 | } |
| 5226 | } |
| 5227 | srv = srv->next; |
| 5228 | } |
| 5229 | } |
| 5230 | } |
| 5231 | |
| 5232 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5233 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5234 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5235 | * 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] | 5236 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5237 | * 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] | 5238 | * 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] | 5239 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5240 | * |
| 5241 | * Returns 1 if the session's state changes, otherwise 0. |
| 5242 | */ |
| 5243 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5244 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5245 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5246 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5247 | struct user_auth *user; |
| 5248 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5249 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5250 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5251 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5252 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5253 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5254 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5255 | return 0; |
| 5256 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5257 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5258 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5259 | /* the URI is in h */ |
| 5260 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5261 | return 0; |
| 5262 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5263 | h += uri_auth->uri_len; |
| 5264 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5265 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5266 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5267 | break; |
| 5268 | } |
| 5269 | h++; |
| 5270 | } |
| 5271 | |
| 5272 | if (uri_auth->refresh) { |
| 5273 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5274 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5275 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5276 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5277 | break; |
| 5278 | } |
| 5279 | h++; |
| 5280 | } |
| 5281 | } |
| 5282 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5283 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5284 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5285 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5286 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5287 | break; |
| 5288 | } |
| 5289 | h++; |
| 5290 | } |
| 5291 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5292 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5293 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5294 | /* we are in front of a interceptable URI. Let's check |
| 5295 | * if there's an authentication and if it's valid. |
| 5296 | */ |
| 5297 | user = uri_auth->users; |
| 5298 | if (!user) { |
| 5299 | /* no user auth required, it's OK */ |
| 5300 | authenticated = 1; |
| 5301 | } else { |
| 5302 | authenticated = 0; |
| 5303 | |
| 5304 | /* a user list is defined, we have to check. |
| 5305 | * skip 21 chars for "Authorization: Basic ". |
| 5306 | */ |
| 5307 | |
| 5308 | /* FIXME: this should move to an earlier place */ |
| 5309 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5310 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5311 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5312 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5313 | if (len > 14 && |
| 5314 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5315 | txn->auth_hdr.str = h; |
| 5316 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5317 | break; |
| 5318 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5319 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5320 | } |
| 5321 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5322 | if (txn->auth_hdr.len < 21 || |
| 5323 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5324 | user = NULL; |
| 5325 | |
| 5326 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5327 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5328 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5329 | user->user_pwd, user->user_len)) { |
| 5330 | authenticated = 1; |
| 5331 | break; |
| 5332 | } |
| 5333 | user = user->next; |
| 5334 | } |
| 5335 | } |
| 5336 | |
| 5337 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5338 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5339 | |
| 5340 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5341 | msg.str = trash; |
| 5342 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5343 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5344 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5345 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5346 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5347 | if (!(t->flags & SN_ERR_MASK)) |
| 5348 | t->flags |= SN_ERR_PRXCOND; |
| 5349 | if (!(t->flags & SN_FINST_MASK)) |
| 5350 | t->flags |= SN_FINST_R; |
| 5351 | return 1; |
| 5352 | } |
| 5353 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5354 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5355 | * data. |
| 5356 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 5357 | buffer_write_dis(t->req); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 5358 | buffer_shutw_now(t->req); |
| 5359 | buffer_shutr_now(t->rep); |
| 5360 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5361 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5362 | t->data_source = DATA_SRC_STATS; |
| 5363 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5364 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5365 | produce_content(t); |
| 5366 | return 1; |
| 5367 | } |
| 5368 | |
| 5369 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5370 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5371 | * Print a debug line with a header |
| 5372 | */ |
| 5373 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5374 | { |
| 5375 | int len, max; |
| 5376 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5377 | dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5378 | max = end - start; |
| 5379 | UBOUND(max, sizeof(trash) - len - 1); |
| 5380 | len += strlcpy2(trash + len, start, max + 1); |
| 5381 | trash[len++] = '\n'; |
| 5382 | write(1, trash, len); |
| 5383 | } |
| 5384 | |
| 5385 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5386 | /************************************************************************/ |
| 5387 | /* The code below is dedicated to ACL parsing and matching */ |
| 5388 | /************************************************************************/ |
| 5389 | |
| 5390 | |
| 5391 | |
| 5392 | |
| 5393 | /* 1. Check on METHOD |
| 5394 | * We use the pre-parsed method if it is known, and store its number as an |
| 5395 | * integer. If it is unknown, we use the pointer and the length. |
| 5396 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5397 | 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] | 5398 | { |
| 5399 | int len, meth; |
| 5400 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5401 | len = strlen(*text); |
| 5402 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5403 | |
| 5404 | pattern->val.i = meth; |
| 5405 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5406 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5407 | if (!pattern->ptr.str) |
| 5408 | return 0; |
| 5409 | pattern->len = len; |
| 5410 | } |
| 5411 | return 1; |
| 5412 | } |
| 5413 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5414 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5415 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5416 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5417 | { |
| 5418 | int meth; |
| 5419 | struct http_txn *txn = l7; |
| 5420 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5421 | if (!txn) |
| 5422 | return 0; |
| 5423 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5424 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5425 | return 0; |
| 5426 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5427 | meth = txn->meth; |
| 5428 | test->i = meth; |
| 5429 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5430 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5431 | /* ensure the indexes are not affected */ |
| 5432 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5433 | test->len = txn->req.sl.rq.m_l; |
| 5434 | test->ptr = txn->req.sol; |
| 5435 | } |
| 5436 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5437 | return 1; |
| 5438 | } |
| 5439 | |
| 5440 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5441 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5442 | int icase; |
| 5443 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5444 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5445 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5446 | |
| 5447 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5448 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5449 | |
| 5450 | /* Other method, we must compare the strings */ |
| 5451 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5452 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5453 | |
| 5454 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5455 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5456 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5457 | return ACL_PAT_FAIL; |
| 5458 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | /* 2. Check on Request/Status Version |
| 5462 | * We simply compare strings here. |
| 5463 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5464 | 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] | 5465 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5466 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5467 | if (!pattern->ptr.str) |
| 5468 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5469 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5470 | return 1; |
| 5471 | } |
| 5472 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5473 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5474 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5475 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5476 | { |
| 5477 | struct http_txn *txn = l7; |
| 5478 | char *ptr; |
| 5479 | int len; |
| 5480 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5481 | if (!txn) |
| 5482 | return 0; |
| 5483 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5484 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5485 | return 0; |
| 5486 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5487 | len = txn->req.sl.rq.v_l; |
| 5488 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5489 | |
| 5490 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5491 | if (len <= 0) |
| 5492 | return 0; |
| 5493 | |
| 5494 | test->ptr = ptr; |
| 5495 | test->len = len; |
| 5496 | |
| 5497 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5498 | return 1; |
| 5499 | } |
| 5500 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5501 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5502 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5503 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5504 | { |
| 5505 | struct http_txn *txn = l7; |
| 5506 | char *ptr; |
| 5507 | int len; |
| 5508 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5509 | if (!txn) |
| 5510 | return 0; |
| 5511 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5512 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5513 | return 0; |
| 5514 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5515 | len = txn->rsp.sl.st.v_l; |
| 5516 | ptr = txn->rsp.sol; |
| 5517 | |
| 5518 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5519 | if (len <= 0) |
| 5520 | return 0; |
| 5521 | |
| 5522 | test->ptr = ptr; |
| 5523 | test->len = len; |
| 5524 | |
| 5525 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5526 | return 1; |
| 5527 | } |
| 5528 | |
| 5529 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5530 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5531 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5532 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5533 | { |
| 5534 | struct http_txn *txn = l7; |
| 5535 | char *ptr; |
| 5536 | int len; |
| 5537 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5538 | if (!txn) |
| 5539 | return 0; |
| 5540 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5541 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5542 | return 0; |
| 5543 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5544 | len = txn->rsp.sl.st.c_l; |
| 5545 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5546 | |
| 5547 | test->i = __strl2ui(ptr, len); |
| 5548 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5549 | return 1; |
| 5550 | } |
| 5551 | |
| 5552 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5553 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5554 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5555 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5556 | { |
| 5557 | struct http_txn *txn = l7; |
| 5558 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5559 | if (!txn) |
| 5560 | return 0; |
| 5561 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5562 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5563 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5564 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5565 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5566 | /* ensure the indexes are not affected */ |
| 5567 | return 0; |
| 5568 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5569 | test->len = txn->req.sl.rq.u_l; |
| 5570 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5571 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5572 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5573 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5574 | return 1; |
| 5575 | } |
| 5576 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5577 | static int |
| 5578 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5579 | struct acl_expr *expr, struct acl_test *test) |
| 5580 | { |
| 5581 | struct http_txn *txn = l7; |
| 5582 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5583 | if (!txn) |
| 5584 | return 0; |
| 5585 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5586 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5587 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5588 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5589 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5590 | /* ensure the indexes are not affected */ |
| 5591 | return 0; |
| 5592 | |
| 5593 | /* Parse HTTP request */ |
| 5594 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5595 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5596 | test->i = AF_INET; |
| 5597 | |
| 5598 | /* |
| 5599 | * If we are parsing url in frontend space, we prepare backend stage |
| 5600 | * to not parse again the same url ! optimization lazyness... |
| 5601 | */ |
| 5602 | if (px->options & PR_O_HTTP_PROXY) |
| 5603 | l4->flags |= SN_ADDR_SET; |
| 5604 | |
| 5605 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5606 | return 1; |
| 5607 | } |
| 5608 | |
| 5609 | static int |
| 5610 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5611 | struct acl_expr *expr, struct acl_test *test) |
| 5612 | { |
| 5613 | struct http_txn *txn = l7; |
| 5614 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5615 | if (!txn) |
| 5616 | return 0; |
| 5617 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5618 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5619 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5620 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5621 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5622 | /* ensure the indexes are not affected */ |
| 5623 | return 0; |
| 5624 | |
| 5625 | /* Same optimization as url_ip */ |
| 5626 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5627 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5628 | |
| 5629 | if (px->options & PR_O_HTTP_PROXY) |
| 5630 | l4->flags |= SN_ADDR_SET; |
| 5631 | |
| 5632 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5633 | return 1; |
| 5634 | } |
| 5635 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5636 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5637 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5638 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5639 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5640 | 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] | 5641 | struct acl_expr *expr, struct acl_test *test) |
| 5642 | { |
| 5643 | struct http_txn *txn = l7; |
| 5644 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5645 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5646 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5647 | if (!txn) |
| 5648 | return 0; |
| 5649 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5650 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5651 | /* search for header from the beginning */ |
| 5652 | ctx->idx = 0; |
| 5653 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5654 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5655 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5656 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5657 | test->len = ctx->vlen; |
| 5658 | test->ptr = (char *)ctx->line + ctx->val; |
| 5659 | return 1; |
| 5660 | } |
| 5661 | |
| 5662 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5663 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5664 | return 0; |
| 5665 | } |
| 5666 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5667 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5668 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5669 | struct acl_expr *expr, struct acl_test *test) |
| 5670 | { |
| 5671 | struct http_txn *txn = l7; |
| 5672 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5673 | if (!txn) |
| 5674 | return 0; |
| 5675 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5676 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5677 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5678 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5679 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5680 | /* ensure the indexes are not affected */ |
| 5681 | return 0; |
| 5682 | |
| 5683 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5684 | } |
| 5685 | |
| 5686 | static int |
| 5687 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5688 | struct acl_expr *expr, struct acl_test *test) |
| 5689 | { |
| 5690 | struct http_txn *txn = l7; |
| 5691 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5692 | if (!txn) |
| 5693 | return 0; |
| 5694 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5695 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5696 | return 0; |
| 5697 | |
| 5698 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5699 | } |
| 5700 | |
| 5701 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5702 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5703 | */ |
| 5704 | static int |
| 5705 | 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] | 5706 | struct acl_expr *expr, struct acl_test *test) |
| 5707 | { |
| 5708 | struct http_txn *txn = l7; |
| 5709 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5710 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5711 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5712 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5713 | if (!txn) |
| 5714 | return 0; |
| 5715 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5716 | ctx.idx = 0; |
| 5717 | cnt = 0; |
| 5718 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5719 | cnt++; |
| 5720 | |
| 5721 | test->i = cnt; |
| 5722 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5723 | return 1; |
| 5724 | } |
| 5725 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5726 | static int |
| 5727 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5728 | struct acl_expr *expr, struct acl_test *test) |
| 5729 | { |
| 5730 | struct http_txn *txn = l7; |
| 5731 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5732 | if (!txn) |
| 5733 | return 0; |
| 5734 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5735 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5736 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5737 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5738 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5739 | /* ensure the indexes are not affected */ |
| 5740 | return 0; |
| 5741 | |
| 5742 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5743 | } |
| 5744 | |
| 5745 | static int |
| 5746 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5747 | struct acl_expr *expr, struct acl_test *test) |
| 5748 | { |
| 5749 | struct http_txn *txn = l7; |
| 5750 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5751 | if (!txn) |
| 5752 | return 0; |
| 5753 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5754 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5755 | return 0; |
| 5756 | |
| 5757 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5758 | } |
| 5759 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5760 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5761 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5762 | * 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] | 5763 | */ |
| 5764 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5765 | 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] | 5766 | struct acl_expr *expr, struct acl_test *test) |
| 5767 | { |
| 5768 | struct http_txn *txn = l7; |
| 5769 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5770 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5771 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5772 | if (!txn) |
| 5773 | return 0; |
| 5774 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5775 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5776 | /* search for header from the beginning */ |
| 5777 | ctx->idx = 0; |
| 5778 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5779 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5780 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5781 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5782 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5783 | return 1; |
| 5784 | } |
| 5785 | |
| 5786 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5787 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5788 | return 0; |
| 5789 | } |
| 5790 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5791 | static int |
| 5792 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5793 | struct acl_expr *expr, struct acl_test *test) |
| 5794 | { |
| 5795 | struct http_txn *txn = l7; |
| 5796 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5797 | if (!txn) |
| 5798 | return 0; |
| 5799 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5800 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5801 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5802 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5803 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5804 | /* ensure the indexes are not affected */ |
| 5805 | return 0; |
| 5806 | |
| 5807 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5808 | } |
| 5809 | |
| 5810 | static int |
| 5811 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5812 | struct acl_expr *expr, struct acl_test *test) |
| 5813 | { |
| 5814 | struct http_txn *txn = l7; |
| 5815 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5816 | if (!txn) |
| 5817 | return 0; |
| 5818 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5819 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5820 | return 0; |
| 5821 | |
| 5822 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5823 | } |
| 5824 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5825 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5826 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5827 | */ |
| 5828 | static int |
| 5829 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5830 | struct acl_expr *expr, struct acl_test *test) |
| 5831 | { |
| 5832 | struct http_txn *txn = l7; |
| 5833 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5834 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5835 | if (!txn) |
| 5836 | return 0; |
| 5837 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5838 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5839 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5840 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5841 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5842 | /* ensure the indexes are not affected */ |
| 5843 | return 0; |
| 5844 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5845 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5846 | ptr = http_get_path(txn); |
| 5847 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5848 | return 0; |
| 5849 | |
| 5850 | /* OK, we got the '/' ! */ |
| 5851 | test->ptr = ptr; |
| 5852 | |
| 5853 | while (ptr < end && *ptr != '?') |
| 5854 | ptr++; |
| 5855 | |
| 5856 | test->len = ptr - test->ptr; |
| 5857 | |
| 5858 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5859 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5860 | return 1; |
| 5861 | } |
| 5862 | |
| 5863 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5864 | |
| 5865 | /************************************************************************/ |
| 5866 | /* All supported keywords must be declared here. */ |
| 5867 | /************************************************************************/ |
| 5868 | |
| 5869 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5870 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5871 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5872 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5873 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5874 | { "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] | 5875 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5876 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5877 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5878 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5879 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5880 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5881 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5882 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5883 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5884 | { "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] | 5885 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5886 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5887 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5888 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5889 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5890 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5891 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5892 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5893 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5894 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5895 | { "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] | 5896 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5897 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5898 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5899 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5900 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5901 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5902 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5903 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5904 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5905 | { "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] | 5906 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5907 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5908 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5909 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5910 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5911 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5912 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5913 | { "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] | 5914 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5915 | { NULL, NULL, NULL, NULL }, |
| 5916 | |
| 5917 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5918 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5919 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5920 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5921 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5922 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5923 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5924 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5925 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5926 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5927 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5928 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5929 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5930 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5931 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5932 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5933 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5934 | |
| 5935 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5936 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5937 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5938 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5939 | { NULL, NULL, NULL, NULL }, |
| 5940 | #endif |
| 5941 | }}; |
| 5942 | |
| 5943 | |
| 5944 | __attribute__((constructor)) |
| 5945 | static void __http_protocol_init(void) |
| 5946 | { |
| 5947 | acl_register_keywords(&acl_kws); |
| 5948 | } |
| 5949 | |
| 5950 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5951 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5952 | * Local variables: |
| 5953 | * c-indent-level: 8 |
| 5954 | * c-basic-offset: 8 |
| 5955 | * End: |
| 5956 | */ |