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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 553 | t->req->cons->shutw(t->req->cons); |
| 554 | t->req->cons->shutr(t->req->cons); |
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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 698 | si->shutr(si); |
| 699 | si->shutw(si); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 745 | si->shutr(si); |
| 746 | si->shutw(si); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 761 | si->shutr(si); |
| 762 | si->shutw(si); |
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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 778 | si->shutr(si); |
| 779 | si->shutw(si); |
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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 842 | si->shutr(si); |
| 843 | si->shutw(si); |
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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 883 | si->shutr(si); |
| 884 | si->shutw(si); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 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) { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1031 | //buffer_shutr(s->req); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1032 | s->req->cons->shutr(s->req->prod); |
| 1033 | } |
| 1034 | if (s->req->flags & BF_WRITE_TIMEOUT) { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1035 | //buffer_shutw(s->req); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 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) { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1042 | //buffer_shutr(s->rep); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1043 | s->rep->cons->shutr(s->rep->prod); |
| 1044 | } |
| 1045 | if (s->rep->flags & BF_WRITE_TIMEOUT) { |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1046 | //buffer_shutw(s->rep); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1058 | //buffer_shutw(s->req); |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 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 */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1064 | //buffer_shutr_now(s->req); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 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 | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1070 | //buffer_shutw(s->rep); |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 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 */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 1076 | //buffer_shutr_now(s->rep); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 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 | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 1139 | |
| 1140 | if (!(s->rep->flags & BF_SHUTW)) |
| 1141 | buffer_check_shutw(s->rep); |
| 1142 | if (!(s->req->flags & BF_SHUTR)) |
| 1143 | buffer_check_shutr(s->req); |
| 1144 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1145 | rqf_cli = s->req->flags; |
| 1146 | rpf_cli = s->rep->flags; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1147 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1148 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1149 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1150 | /* Maybe resync server FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1151 | if (s->req->cons->state != SI_ST_CLO) { |
| 1152 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 1153 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1154 | if (s->req->cons->state == SI_ST_INI && |
| 1155 | (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] | 1156 | s->req->cons->state = SI_ST_REQ; |
| 1157 | do { |
| 1158 | sess_prepare_conn_req(s, &s->si[1]); |
| 1159 | if (s->si[1].state != SI_ST_ASS) |
| 1160 | break; |
| 1161 | if (s->srv && s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1162 | perform_http_redirect(s, &s->si[1]); |
| 1163 | sess_update_stream_int(s, &s->si[1]); |
| 1164 | } while (s->si[1].state == SI_ST_REQ || s->si[1].state == SI_ST_ASS); |
| 1165 | |
| 1166 | /* FIXME: how to process this type of errors ? */ |
| 1167 | if (s->si[1].state == SI_ST_CLO && s->si[1].err_type != SI_ET_NONE) |
| 1168 | return_srv_error(s, s->si[1].err_type); |
| 1169 | resync = 1; |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1170 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1171 | |
| 1172 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1173 | 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] | 1174 | s->be->options & PR_O_FORCE_CLO && |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1175 | s->rep->flags & BF_READ_ACTIVITY) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 1176 | /* We want to force the connection to the server to close, |
| 1177 | * and the server has begun to respond. That's the right |
| 1178 | * time. |
| 1179 | */ |
| 1180 | buffer_shutw_now(s->req); |
| 1181 | } |
| 1182 | |
Willy Tarreau | 0a5d5dd | 2008-11-23 19:31:35 +0100 | [diff] [blame] | 1183 | if (!(s->req->flags & BF_SHUTW)) |
| 1184 | buffer_check_shutw(s->req); |
| 1185 | if (!(s->rep->flags & BF_SHUTR)) |
| 1186 | buffer_check_shutr(s->rep); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 1187 | |
| 1188 | /* When a server-side connection is released, we have to |
| 1189 | * count it and check for pending connections on this server. |
| 1190 | */ |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 1191 | if (s->req->cons->state == SI_ST_DIS) { |
| 1192 | s->req->cons->state = SI_ST_CLO; |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 1193 | if (s->srv) { |
| 1194 | if (s->flags & SN_CURR_SESS) { |
| 1195 | s->flags &= ~SN_CURR_SESS; |
| 1196 | s->srv->cur_sess--; |
| 1197 | } |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 1198 | sess_change_server(s, NULL); |
| 1199 | if (may_dequeue_tasks(s->srv, s->be)) |
| 1200 | process_srv_queue(s->srv); |
| 1201 | } |
| 1202 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1203 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1204 | rqf_srv = s->req->flags; |
| 1205 | rpf_srv = s->rep->flags; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1206 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* we may have to resync because of pending connections */ |
| 1210 | if (resync) |
| 1211 | continue; |
| 1212 | |
| 1213 | /**** Process layer 7 below ****/ |
| 1214 | |
| 1215 | /* Analyse request */ |
| 1216 | if (s->req->flags & BF_MASK_ANALYSER) { |
| 1217 | unsigned int flags = s->req->flags; |
| 1218 | |
| 1219 | if (s->req->prod->state >= SI_ST_EST) { |
| 1220 | /* it's up to the analysers to reset write_ena */ |
| 1221 | buffer_write_ena(s->req); |
| 1222 | if (s->req->analysers) |
| 1223 | process_request(s); |
| 1224 | } |
| 1225 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1226 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1227 | if (s->req->flags != flags) |
| 1228 | resync = 1; |
| 1229 | } |
| 1230 | |
| 1231 | /* Analyse response */ |
| 1232 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 1233 | /* In inject mode, we wake up everytime something has |
| 1234 | * happened on the write side of the buffer. |
| 1235 | */ |
| 1236 | unsigned int flags = s->rep->flags; |
| 1237 | |
| 1238 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 1239 | !(s->rep->flags & BF_FULL)) { |
| 1240 | produce_content(s); |
| 1241 | } |
| 1242 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1243 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1244 | if (s->rep->flags != flags) |
| 1245 | resync = 1; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1246 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1247 | else if (s->rep->flags & BF_MASK_ANALYSER) { |
| 1248 | unsigned int flags = s->rep->flags; |
| 1249 | |
| 1250 | if (s->rep->prod->state >= SI_ST_EST) { |
| 1251 | /* it's up to the analysers to reset write_ena */ |
| 1252 | buffer_write_ena(s->rep); |
| 1253 | if (s->rep->analysers) |
| 1254 | process_response(s); |
| 1255 | } |
| 1256 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1257 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1258 | if (s->rep->flags != flags) |
| 1259 | resync = 1; |
| 1260 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1261 | |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 1262 | /* For the moment, we need to clean the client and server flags that |
| 1263 | * have vanished. This is just a temporary measure though. |
| 1264 | */ |
| 1265 | rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1266 | rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1267 | rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1268 | rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1269 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1270 | |
Willy Tarreau | a37095b | 2008-09-03 11:37:47 +0200 | [diff] [blame] | 1271 | /* This is needed only when debugging is enabled, to indicate |
| 1272 | * client-side or server-side close. Please note that in the unlikely |
| 1273 | * event where both sides would close at once, the sequence is reported |
| 1274 | * on the server side first. |
| 1275 | */ |
| 1276 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1277 | (!(global.mode & MODE_QUIET) || |
| 1278 | (global.mode & MODE_VERBOSE)))) { |
| 1279 | int len; |
| 1280 | |
| 1281 | if (s->si[1].state == SI_ST_CLO && |
| 1282 | s->si[1].prev_state == SI_ST_EST) { |
| 1283 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1284 | s->uniq_id, s->be->id, |
| 1285 | (unsigned short)s->si[0].fd, |
| 1286 | (unsigned short)s->si[1].fd); |
| 1287 | write(1, trash, len); |
| 1288 | } |
| 1289 | |
| 1290 | if (s->si[0].state == SI_ST_CLO && |
| 1291 | s->si[0].prev_state == SI_ST_EST) { |
| 1292 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1293 | s->uniq_id, s->be->id, |
| 1294 | (unsigned short)s->si[0].fd, |
| 1295 | (unsigned short)s->si[1].fd); |
| 1296 | write(1, trash, len); |
| 1297 | } |
| 1298 | } |
| 1299 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1300 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | 8bfa426 | 2008-11-27 09:25:45 +0100 | [diff] [blame] | 1301 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 1302 | |
| 1303 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1304 | session_process_counters(s); |
| 1305 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1306 | if (s->rep->cons->state == SI_ST_EST) |
| 1307 | stream_sock_data_finish(s->rep->cons->fd); |
| 1308 | |
| 1309 | if (s->req->cons->state == SI_ST_EST) |
| 1310 | stream_sock_data_finish(s->req->cons->fd); |
| 1311 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 1312 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 1313 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1314 | s->si[0].prev_state = s->si[0].state; |
| 1315 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1316 | s->si[0].flags = s->si[1].flags = SI_FL_NONE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1317 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 1318 | /* Trick: if a request is being waiting for the server to respond, |
| 1319 | * and if we know the server can timeout, we don't want the timeout |
| 1320 | * to expire on the client side first, but we're still interested |
| 1321 | * in passing data from the client to the server (eg: POST). Thus, |
| 1322 | * we can cancel the client's request timeout if the server's |
| 1323 | * request timeout is set and the server has not yet sent a response. |
| 1324 | */ |
| 1325 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1326 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 1327 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 1328 | s->req->rex = TICK_ETERNITY; |
| 1329 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1330 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1331 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1332 | if (s->req->analysers) |
| 1333 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1334 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 1335 | if (s->si[0].exp) |
| 1336 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1337 | |
| 1338 | if (s->si[1].exp) |
| 1339 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1340 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 1341 | #ifdef DEBUG_FULL |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 1342 | 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", |
| 1343 | 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] | 1344 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1345 | /* restore t to its place in the task list */ |
| 1346 | task_queue(t); |
| 1347 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 1348 | #ifdef DEBUG_DEV |
| 1349 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 1350 | if (!t->expire) |
| 1351 | ABORT_NOW(); |
| 1352 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 1353 | *next = t->expire; |
| 1354 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1357 | s->fe->feconn--; |
| 1358 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1359 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1360 | actconn--; |
| 1361 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 1362 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1363 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1364 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1365 | 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] | 1366 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1367 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1368 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1369 | write(1, trash, len); |
| 1370 | } |
| 1371 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1372 | 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] | 1373 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1374 | |
| 1375 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 1376 | if (s->logs.logwait && |
| 1377 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1378 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 1379 | if (s->fe->to_log & LW_REQ) |
| 1380 | http_sess_log(s); |
| 1381 | else |
| 1382 | tcp_sess_log(s); |
| 1383 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1384 | |
| 1385 | /* the task MUST not be in the run queue anymore */ |
| 1386 | task_delete(t); |
| 1387 | session_free(s); |
| 1388 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1389 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1393 | extern const char sess_term_cond[8]; |
| 1394 | extern const char sess_fin_state[8]; |
| 1395 | extern const char *monthname[12]; |
| 1396 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 1397 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 1398 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 1399 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1400 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 1401 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1403 | /* |
| 1404 | * send a log for the session when we have enough info about it. |
| 1405 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1406 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1407 | static void http_sess_log(struct session *s) |
| 1408 | { |
| 1409 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1410 | struct proxy *fe = s->fe; |
| 1411 | struct proxy *be = s->be; |
| 1412 | struct proxy *prx_log; |
| 1413 | struct http_txn *txn = &s->txn; |
| 1414 | int tolog; |
| 1415 | char *uri, *h; |
| 1416 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1417 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1418 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1419 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1420 | int hdr; |
| 1421 | |
| 1422 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1423 | return; |
| 1424 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1426 | if (s->cli_addr.ss_family == AF_INET) |
| 1427 | inet_ntop(AF_INET, |
| 1428 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1429 | pn, sizeof(pn)); |
| 1430 | else |
| 1431 | inet_ntop(AF_INET6, |
| 1432 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1433 | pn, sizeof(pn)); |
| 1434 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1435 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1436 | |
| 1437 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1438 | tolog = fe->to_log; |
| 1439 | |
| 1440 | h = tmpline; |
| 1441 | if (fe->to_log & LW_REQHDR && |
| 1442 | txn->req.cap && |
| 1443 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1444 | *(h++) = ' '; |
| 1445 | *(h++) = '{'; |
| 1446 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1447 | if (hdr) |
| 1448 | *(h++) = '|'; |
| 1449 | if (txn->req.cap[hdr] != NULL) |
| 1450 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1451 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1452 | } |
| 1453 | *(h++) = '}'; |
| 1454 | } |
| 1455 | |
| 1456 | if (fe->to_log & LW_RSPHDR && |
| 1457 | txn->rsp.cap && |
| 1458 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1459 | *(h++) = ' '; |
| 1460 | *(h++) = '{'; |
| 1461 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1462 | if (hdr) |
| 1463 | *(h++) = '|'; |
| 1464 | if (txn->rsp.cap[hdr] != NULL) |
| 1465 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1466 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1467 | } |
| 1468 | *(h++) = '}'; |
| 1469 | } |
| 1470 | |
| 1471 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1472 | *(h++) = ' '; |
| 1473 | *(h++) = '"'; |
| 1474 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1475 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1476 | '#', url_encode_map, uri); |
| 1477 | *(h++) = '"'; |
| 1478 | } |
| 1479 | *h = '\0'; |
| 1480 | |
| 1481 | svid = (tolog & LW_SVID) ? |
| 1482 | (s->data_source != DATA_SRC_STATS) ? |
| 1483 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1484 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1485 | t_request = -1; |
| 1486 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1487 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1488 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1489 | send_log(prx_log, LOG_INFO, |
| 1490 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 1491 | " %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] | 1492 | " %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] | 1493 | pn, |
| 1494 | (s->cli_addr.ss_family == AF_INET) ? |
| 1495 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1496 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1497 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1498 | 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] | 1499 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1500 | t_request, |
| 1501 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1502 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1503 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1504 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1505 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1506 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1507 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1508 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1509 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1510 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1511 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1512 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1513 | 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] | 1514 | (s->flags & SN_REDISP)?"+":"", |
| 1515 | (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] | 1516 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1517 | |
| 1518 | s->logs.logwait = 0; |
| 1519 | } |
| 1520 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1521 | |
| 1522 | /* |
| 1523 | * Capture headers from message starting at <som> according to header list |
| 1524 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1525 | */ |
| 1526 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1527 | char **cap, struct cap_hdr *cap_hdr) |
| 1528 | { |
| 1529 | char *eol, *sol, *col, *sov; |
| 1530 | int cur_idx; |
| 1531 | struct cap_hdr *h; |
| 1532 | int len; |
| 1533 | |
| 1534 | sol = som + hdr_idx_first_pos(idx); |
| 1535 | cur_idx = hdr_idx_first_idx(idx); |
| 1536 | |
| 1537 | while (cur_idx) { |
| 1538 | eol = sol + idx->v[cur_idx].len; |
| 1539 | |
| 1540 | col = sol; |
| 1541 | while (col < eol && *col != ':') |
| 1542 | col++; |
| 1543 | |
| 1544 | sov = col + 1; |
| 1545 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1546 | sov++; |
| 1547 | |
| 1548 | for (h = cap_hdr; h; h = h->next) { |
| 1549 | if ((h->namelen == col - sol) && |
| 1550 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1551 | if (cap[h->index] == NULL) |
| 1552 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1553 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1554 | |
| 1555 | if (cap[h->index] == NULL) { |
| 1556 | Alert("HTTP capture : out of memory.\n"); |
| 1557 | continue; |
| 1558 | } |
| 1559 | |
| 1560 | len = eol - sov; |
| 1561 | if (len > h->len) |
| 1562 | len = h->len; |
| 1563 | |
| 1564 | memcpy(cap[h->index], sov, len); |
| 1565 | cap[h->index][len]=0; |
| 1566 | } |
| 1567 | } |
| 1568 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1569 | cur_idx = idx->v[cur_idx].next; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1574 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1575 | */ |
| 1576 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1577 | |
| 1578 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1579 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1580 | */ |
| 1581 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1582 | ptr++; \ |
| 1583 | if (likely(ptr < end)) \ |
| 1584 | goto good; \ |
| 1585 | else { \ |
| 1586 | state = (st); \ |
| 1587 | goto http_msg_ood; \ |
| 1588 | } \ |
| 1589 | } while (0) |
| 1590 | |
| 1591 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1592 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1593 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1594 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1595 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1596 | * will give undefined results. |
| 1597 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1598 | * and that msg->sol points to the beginning of the response. |
| 1599 | * If a complete line is found (which implies that at least one CR or LF is |
| 1600 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1601 | * returned indicating an incomplete line (which does not mean that parts have |
| 1602 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1603 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1604 | * upon next call. |
| 1605 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1606 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1607 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1608 | * 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] | 1609 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1610 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1611 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1612 | unsigned int state, const char *ptr, const char *end, |
| 1613 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1614 | { |
| 1615 | __label__ |
| 1616 | http_msg_rpver, |
| 1617 | http_msg_rpver_sp, |
| 1618 | http_msg_rpcode, |
| 1619 | http_msg_rpcode_sp, |
| 1620 | http_msg_rpreason, |
| 1621 | http_msg_rpline_eol, |
| 1622 | http_msg_ood, /* out of data */ |
| 1623 | http_msg_invalid; |
| 1624 | |
| 1625 | switch (state) { |
| 1626 | http_msg_rpver: |
| 1627 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1628 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1629 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1630 | |
| 1631 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1632 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1633 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1634 | } |
| 1635 | goto http_msg_invalid; |
| 1636 | |
| 1637 | http_msg_rpver_sp: |
| 1638 | case HTTP_MSG_RPVER_SP: |
| 1639 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1640 | msg->sl.st.c = ptr - msg_buf; |
| 1641 | goto http_msg_rpcode; |
| 1642 | } |
| 1643 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1644 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1645 | /* so it's a CR/LF, this is invalid */ |
| 1646 | goto http_msg_invalid; |
| 1647 | |
| 1648 | http_msg_rpcode: |
| 1649 | case HTTP_MSG_RPCODE: |
| 1650 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1651 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1652 | |
| 1653 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1654 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1655 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1656 | } |
| 1657 | |
| 1658 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1659 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1660 | http_msg_rsp_reason: |
| 1661 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1662 | msg->sl.st.r = ptr - msg_buf; |
| 1663 | msg->sl.st.r_l = 0; |
| 1664 | goto http_msg_rpline_eol; |
| 1665 | |
| 1666 | http_msg_rpcode_sp: |
| 1667 | case HTTP_MSG_RPCODE_SP: |
| 1668 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1669 | msg->sl.st.r = ptr - msg_buf; |
| 1670 | goto http_msg_rpreason; |
| 1671 | } |
| 1672 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1673 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1674 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1675 | goto http_msg_rsp_reason; |
| 1676 | |
| 1677 | http_msg_rpreason: |
| 1678 | case HTTP_MSG_RPREASON: |
| 1679 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1680 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1681 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1682 | http_msg_rpline_eol: |
| 1683 | /* We have seen the end of line. Note that we do not |
| 1684 | * necessarily have the \n yet, but at least we know that we |
| 1685 | * have EITHER \r OR \n, otherwise the response would not be |
| 1686 | * complete. We can then record the response length and return |
| 1687 | * to the caller which will be able to register it. |
| 1688 | */ |
| 1689 | msg->sl.st.l = ptr - msg->sol; |
| 1690 | return ptr; |
| 1691 | |
| 1692 | #ifdef DEBUG_FULL |
| 1693 | default: |
| 1694 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1695 | exit(1); |
| 1696 | #endif |
| 1697 | } |
| 1698 | |
| 1699 | http_msg_ood: |
| 1700 | /* out of data */ |
| 1701 | if (ret_state) |
| 1702 | *ret_state = state; |
| 1703 | if (ret_ptr) |
| 1704 | *ret_ptr = (char *)ptr; |
| 1705 | return NULL; |
| 1706 | |
| 1707 | http_msg_invalid: |
| 1708 | /* invalid message */ |
| 1709 | if (ret_state) |
| 1710 | *ret_state = HTTP_MSG_ERROR; |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | * This function parses a request line between <ptr> and <end>, starting with |
| 1717 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1718 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1719 | * will give undefined results. |
| 1720 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1721 | * and that msg->sol points to the beginning of the request. |
| 1722 | * If a complete line is found (which implies that at least one CR or LF is |
| 1723 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1724 | * returned indicating an incomplete line (which does not mean that parts have |
| 1725 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1726 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1727 | * upon next call. |
| 1728 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1729 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1731 | * 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] | 1732 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1733 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1734 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1735 | unsigned int state, const char *ptr, const char *end, |
| 1736 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1737 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | __label__ |
| 1739 | http_msg_rqmeth, |
| 1740 | http_msg_rqmeth_sp, |
| 1741 | http_msg_rquri, |
| 1742 | http_msg_rquri_sp, |
| 1743 | http_msg_rqver, |
| 1744 | http_msg_rqline_eol, |
| 1745 | http_msg_ood, /* out of data */ |
| 1746 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1747 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1748 | switch (state) { |
| 1749 | http_msg_rqmeth: |
| 1750 | case HTTP_MSG_RQMETH: |
| 1751 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1752 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1755 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1756 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1757 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1758 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1759 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1760 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1761 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | http_msg_req09_uri: |
| 1763 | msg->sl.rq.u = ptr - msg_buf; |
| 1764 | http_msg_req09_uri_e: |
| 1765 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1766 | http_msg_req09_ver: |
| 1767 | msg->sl.rq.v = ptr - msg_buf; |
| 1768 | msg->sl.rq.v_l = 0; |
| 1769 | goto http_msg_rqline_eol; |
| 1770 | } |
| 1771 | goto http_msg_invalid; |
| 1772 | |
| 1773 | http_msg_rqmeth_sp: |
| 1774 | case HTTP_MSG_RQMETH_SP: |
| 1775 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1776 | msg->sl.rq.u = ptr - msg_buf; |
| 1777 | goto http_msg_rquri; |
| 1778 | } |
| 1779 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1780 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1781 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1782 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1783 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | http_msg_rquri: |
| 1785 | case HTTP_MSG_RQURI: |
| 1786 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1787 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
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 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1790 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1791 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1792 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1793 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1794 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1795 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1796 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | http_msg_rquri_sp: |
| 1798 | case HTTP_MSG_RQURI_SP: |
| 1799 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1800 | msg->sl.rq.v = ptr - msg_buf; |
| 1801 | goto http_msg_rqver; |
| 1802 | } |
| 1803 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1804 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1805 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1806 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1807 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | http_msg_rqver: |
| 1809 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1810 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1811 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1812 | |
| 1813 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1814 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1815 | http_msg_rqline_eol: |
| 1816 | /* We have seen the end of line. Note that we do not |
| 1817 | * necessarily have the \n yet, but at least we know that we |
| 1818 | * have EITHER \r OR \n, otherwise the request would not be |
| 1819 | * complete. We can then record the request length and return |
| 1820 | * to the caller which will be able to register it. |
| 1821 | */ |
| 1822 | msg->sl.rq.l = ptr - msg->sol; |
| 1823 | return ptr; |
| 1824 | } |
| 1825 | |
| 1826 | /* neither an HTTP_VER token nor a CRLF */ |
| 1827 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1828 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1829 | #ifdef DEBUG_FULL |
| 1830 | default: |
| 1831 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1832 | exit(1); |
| 1833 | #endif |
| 1834 | } |
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_ood: |
| 1837 | /* out of data */ |
| 1838 | if (ret_state) |
| 1839 | *ret_state = state; |
| 1840 | if (ret_ptr) |
| 1841 | *ret_ptr = (char *)ptr; |
| 1842 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1843 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1844 | http_msg_invalid: |
| 1845 | /* invalid message */ |
| 1846 | if (ret_state) |
| 1847 | *ret_state = HTTP_MSG_ERROR; |
| 1848 | return NULL; |
| 1849 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1850 | |
| 1851 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1852 | /* |
| 1853 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1854 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1855 | * when data are missing and recalled at the exact same location with no |
| 1856 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1857 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1858 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1859 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1860 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1861 | { |
| 1862 | __label__ |
| 1863 | http_msg_rqbefore, |
| 1864 | http_msg_rqbefore_cr, |
| 1865 | http_msg_rqmeth, |
| 1866 | http_msg_rqline_end, |
| 1867 | http_msg_hdr_first, |
| 1868 | http_msg_hdr_name, |
| 1869 | http_msg_hdr_l1_sp, |
| 1870 | http_msg_hdr_l1_lf, |
| 1871 | http_msg_hdr_l1_lws, |
| 1872 | http_msg_hdr_val, |
| 1873 | http_msg_hdr_l2_lf, |
| 1874 | http_msg_hdr_l2_lws, |
| 1875 | http_msg_complete_header, |
| 1876 | http_msg_last_lf, |
| 1877 | http_msg_ood, /* out of data */ |
| 1878 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1879 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1880 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1881 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1882 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1883 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1884 | ptr = buf->lr; |
| 1885 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1887 | if (unlikely(ptr >= end)) |
| 1888 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1889 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1890 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1891 | /* |
| 1892 | * First, states that are specific to the response only. |
| 1893 | * We check them first so that request and headers are |
| 1894 | * closer to each other (accessed more often). |
| 1895 | */ |
| 1896 | http_msg_rpbefore: |
| 1897 | case HTTP_MSG_RPBEFORE: |
| 1898 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1899 | if (likely(ptr == buf->data)) { |
| 1900 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1901 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1902 | } else { |
| 1903 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1904 | /* only skip empty leading lines, don't remove them */ |
| 1905 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1906 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1907 | #else |
| 1908 | /* Remove empty leading lines, as recommended by |
| 1909 | * RFC2616. This takes a lot of time because we |
| 1910 | * must move all the buffer backwards, but this |
| 1911 | * is rarely needed. The method above will be |
| 1912 | * cleaner when we'll be able to start sending |
| 1913 | * the request from any place in the buffer. |
| 1914 | */ |
| 1915 | buf->lr = ptr; |
| 1916 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1917 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1918 | msg->sol = buf->data; |
| 1919 | ptr = buf->data; |
| 1920 | end = buf->r; |
| 1921 | #endif |
| 1922 | } |
| 1923 | hdr_idx_init(idx); |
| 1924 | state = HTTP_MSG_RPVER; |
| 1925 | goto http_msg_rpver; |
| 1926 | } |
| 1927 | |
| 1928 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1929 | goto http_msg_invalid; |
| 1930 | |
| 1931 | if (unlikely(*ptr == '\n')) |
| 1932 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1933 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1934 | /* stop here */ |
| 1935 | |
| 1936 | http_msg_rpbefore_cr: |
| 1937 | case HTTP_MSG_RPBEFORE_CR: |
| 1938 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1939 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1940 | /* stop here */ |
| 1941 | |
| 1942 | http_msg_rpver: |
| 1943 | case HTTP_MSG_RPVER: |
| 1944 | case HTTP_MSG_RPVER_SP: |
| 1945 | case HTTP_MSG_RPCODE: |
| 1946 | case HTTP_MSG_RPCODE_SP: |
| 1947 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1948 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1949 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1950 | if (unlikely(!ptr)) |
| 1951 | return; |
| 1952 | |
| 1953 | /* we have a full response and we know that we have either a CR |
| 1954 | * or an LF at <ptr>. |
| 1955 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1956 | //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] | 1957 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1958 | |
| 1959 | msg->sol = ptr; |
| 1960 | if (likely(*ptr == '\r')) |
| 1961 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1962 | goto http_msg_rpline_end; |
| 1963 | |
| 1964 | http_msg_rpline_end: |
| 1965 | case HTTP_MSG_RPLINE_END: |
| 1966 | /* msg->sol must point to the first of CR or LF. */ |
| 1967 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1968 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1969 | /* stop here */ |
| 1970 | |
| 1971 | /* |
| 1972 | * Second, states that are specific to the request only |
| 1973 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1974 | http_msg_rqbefore: |
| 1975 | case HTTP_MSG_RQBEFORE: |
| 1976 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1977 | if (likely(ptr == buf->data)) { |
| 1978 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1979 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1980 | } else { |
| 1981 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1982 | /* only skip empty leading lines, don't remove them */ |
| 1983 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1984 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1985 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1986 | /* Remove empty leading lines, as recommended by |
| 1987 | * RFC2616. This takes a lot of time because we |
| 1988 | * must move all the buffer backwards, but this |
| 1989 | * is rarely needed. The method above will be |
| 1990 | * cleaner when we'll be able to start sending |
| 1991 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1992 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1993 | buf->lr = ptr; |
| 1994 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1995 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1996 | msg->sol = buf->data; |
| 1997 | ptr = buf->data; |
| 1998 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1999 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2000 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 2001 | /* we will need this when keep-alive will be supported |
| 2002 | hdr_idx_init(idx); |
| 2003 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 2004 | state = HTTP_MSG_RQMETH; |
| 2005 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2006 | } |
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 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 2009 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2010 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2011 | if (unlikely(*ptr == '\n')) |
| 2012 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 2013 | 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] | 2014 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2015 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2016 | http_msg_rqbefore_cr: |
| 2017 | case HTTP_MSG_RQBEFORE_CR: |
| 2018 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2019 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 2020 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2021 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2022 | http_msg_rqmeth: |
| 2023 | case HTTP_MSG_RQMETH: |
| 2024 | case HTTP_MSG_RQMETH_SP: |
| 2025 | case HTTP_MSG_RQURI: |
| 2026 | case HTTP_MSG_RQURI_SP: |
| 2027 | case HTTP_MSG_RQVER: |
| 2028 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2029 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2030 | if (unlikely(!ptr)) |
| 2031 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2032 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2033 | /* we have a full request and we know that we have either a CR |
| 2034 | * or an LF at <ptr>. |
| 2035 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2036 | //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] | 2037 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2038 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2039 | msg->sol = ptr; |
| 2040 | if (likely(*ptr == '\r')) |
| 2041 | 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] | 2042 | goto http_msg_rqline_end; |
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 | http_msg_rqline_end: |
| 2045 | case HTTP_MSG_RQLINE_END: |
| 2046 | /* check for HTTP/0.9 request : no version information available. |
| 2047 | * msg->sol must point to the first of CR or LF. |
| 2048 | */ |
| 2049 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 2050 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2051 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2052 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2053 | 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] | 2054 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2055 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 2056 | /* |
| 2057 | * Common states below |
| 2058 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2059 | http_msg_hdr_first: |
| 2060 | case HTTP_MSG_HDR_FIRST: |
| 2061 | msg->sol = ptr; |
| 2062 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2063 | goto http_msg_hdr_name; |
| 2064 | } |
| 2065 | |
| 2066 | if (likely(*ptr == '\r')) |
| 2067 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 2068 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2069 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2070 | http_msg_hdr_name: |
| 2071 | case HTTP_MSG_HDR_NAME: |
| 2072 | /* assumes msg->sol points to the first char */ |
| 2073 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 2074 | 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] | 2075 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2076 | if (likely(*ptr == ':')) { |
| 2077 | msg->col = ptr - buf->data; |
| 2078 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 2079 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2081 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2083 | http_msg_hdr_l1_sp: |
| 2084 | case HTTP_MSG_HDR_L1_SP: |
| 2085 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 2086 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 2087 | 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] | 2088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2089 | /* header value can be basically anything except CR/LF */ |
| 2090 | msg->sov = ptr - buf->data; |
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 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2093 | goto http_msg_hdr_val; |
| 2094 | } |
| 2095 | |
| 2096 | if (likely(*ptr == '\r')) |
| 2097 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 2098 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2099 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2100 | http_msg_hdr_l1_lf: |
| 2101 | case HTTP_MSG_HDR_L1_LF: |
| 2102 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2103 | 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] | 2104 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2105 | http_msg_hdr_l1_lws: |
| 2106 | case HTTP_MSG_HDR_L1_LWS: |
| 2107 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 2108 | /* replace HT,CR,LF with spaces */ |
| 2109 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 2110 | buf->data[msg->sov] = ' '; |
| 2111 | goto http_msg_hdr_l1_sp; |
| 2112 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2113 | /* we had a header consisting only in spaces ! */ |
| 2114 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2115 | goto http_msg_complete_header; |
| 2116 | |
| 2117 | http_msg_hdr_val: |
| 2118 | case HTTP_MSG_HDR_VAL: |
| 2119 | /* assumes msg->sol points to the first char, msg->col to the |
| 2120 | * colon, and msg->sov points to the first character of the |
| 2121 | * value. |
| 2122 | */ |
| 2123 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 2124 | 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] | 2125 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2126 | msg->eol = ptr; |
| 2127 | /* Note: we could also copy eol into ->eoh so that we have the |
| 2128 | * real header end in case it ends with lots of LWS, but is this |
| 2129 | * really needed ? |
| 2130 | */ |
| 2131 | if (likely(*ptr == '\r')) |
| 2132 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 2133 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2134 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2135 | http_msg_hdr_l2_lf: |
| 2136 | case HTTP_MSG_HDR_L2_LF: |
| 2137 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2138 | 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] | 2139 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2140 | http_msg_hdr_l2_lws: |
| 2141 | case HTTP_MSG_HDR_L2_LWS: |
| 2142 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 2143 | /* LWS: replace HT,CR,LF with spaces */ |
| 2144 | for (; msg->eol < ptr; msg->eol++) |
| 2145 | *msg->eol = ' '; |
| 2146 | goto http_msg_hdr_val; |
| 2147 | } |
| 2148 | http_msg_complete_header: |
| 2149 | /* |
| 2150 | * It was a new header, so the last one is finished. |
| 2151 | * Assumes msg->sol points to the first char, msg->col to the |
| 2152 | * colon, msg->sov points to the first character of the value |
| 2153 | * and msg->eol to the first CR or LF so we know how the line |
| 2154 | * ends. We insert last header into the index. |
| 2155 | */ |
| 2156 | /* |
| 2157 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 2158 | write(2, msg->sol, msg->eol-msg->sol); |
| 2159 | fprintf(stderr,"\n"); |
| 2160 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2161 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2162 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 2163 | idx, idx->tail) < 0)) |
| 2164 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2165 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2166 | msg->sol = ptr; |
| 2167 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 2168 | goto http_msg_hdr_name; |
| 2169 | } |
| 2170 | |
| 2171 | if (likely(*ptr == '\r')) |
| 2172 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 2173 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2174 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2175 | http_msg_last_lf: |
| 2176 | case HTTP_MSG_LAST_LF: |
| 2177 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 2178 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 2179 | ptr++; |
| 2180 | buf->lr = ptr; |
| 2181 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2182 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2183 | return; |
| 2184 | #ifdef DEBUG_FULL |
| 2185 | default: |
| 2186 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 2187 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2188 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2189 | } |
| 2190 | http_msg_ood: |
| 2191 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2192 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2193 | buf->lr = ptr; |
| 2194 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2195 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2196 | http_msg_invalid: |
| 2197 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2198 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2199 | return; |
| 2200 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2201 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2202 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2203 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2204 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2205 | * 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] | 2206 | * functions. Its behaviour is rather simple : |
| 2207 | * - all enabled analysers are called in turn from the lower to the higher |
| 2208 | * bit. |
| 2209 | * - 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] | 2210 | * 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] | 2211 | * that unprocessed data will not be forwarded. But that probably depends on |
| 2212 | * the protocol. Generally it is not reset in case of errors. |
| 2213 | * - 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] | 2214 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2215 | * analysis). |
| 2216 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 2217 | * reset its bit from the analysers flags in order not to be called anymore. |
| 2218 | * |
| 2219 | * In the future, analysers should be able to indicate that they want to be |
| 2220 | * called after XXX bytes have been received (or transfered), and the min of |
| 2221 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 2222 | * |
| 2223 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2224 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2225 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2226 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2227 | struct buffer *req = t->req; |
| 2228 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2229 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2230 | 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] | 2231 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2232 | t, |
| 2233 | req, |
| 2234 | req->rex, req->wex, |
| 2235 | req->flags, |
| 2236 | req->l, |
| 2237 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2238 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2239 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2240 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2241 | struct tcp_rule *rule; |
| 2242 | int partial; |
| 2243 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 2244 | /* We will abort if we encounter a read error. In theory, we |
| 2245 | * should not abort if we get a close, it might be valid, |
| 2246 | * although very unlikely. FIXME: we'll abort for now, this |
| 2247 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2248 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2249 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2250 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2251 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2252 | if (!(t->flags & SN_ERR_MASK)) |
| 2253 | t->flags |= SN_ERR_CLICL; |
| 2254 | if (!(t->flags & SN_FINST_MASK)) |
| 2255 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2256 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2260 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2261 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2262 | t->fe->failed_req++; |
| 2263 | if (!(t->flags & SN_ERR_MASK)) |
| 2264 | t->flags |= SN_ERR_CLITO; |
| 2265 | if (!(t->flags & SN_FINST_MASK)) |
| 2266 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2267 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /* We don't know whether we have enough data, so must proceed |
| 2271 | * this way : |
| 2272 | * - iterate through all rules in their declaration order |
| 2273 | * - if one rule returns MISS, it means the inspect delay is |
| 2274 | * not over yet, then return immediately, otherwise consider |
| 2275 | * it as a non-match. |
| 2276 | * - if one rule returns OK, then return OK |
| 2277 | * - if one rule returns KO, then return KO |
| 2278 | */ |
| 2279 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2280 | 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] | 2281 | partial = 0; |
| 2282 | else |
| 2283 | partial = ACL_PARTIAL; |
| 2284 | |
| 2285 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 2286 | int ret = ACL_PAT_PASS; |
| 2287 | |
| 2288 | if (rule->cond) { |
| 2289 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 2290 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2291 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2292 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2293 | if (!tick_isset(req->analyse_exp)) |
| 2294 | 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] | 2295 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2296 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2297 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2298 | ret = acl_pass(ret); |
| 2299 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2300 | ret = !ret; |
| 2301 | } |
| 2302 | |
| 2303 | if (ret) { |
| 2304 | /* we have a matching rule. */ |
| 2305 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2306 | buffer_abort(req); |
| 2307 | buffer_abort(rep); |
| 2308 | //FIXME: this delete this |
| 2309 | //fd_delete(t->cli_fd); |
| 2310 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2311 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2312 | t->fe->failed_req++; |
| 2313 | if (!(t->flags & SN_ERR_MASK)) |
| 2314 | t->flags |= SN_ERR_PRXCOND; |
| 2315 | if (!(t->flags & SN_FINST_MASK)) |
| 2316 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2317 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2318 | } |
| 2319 | /* otherwise accept */ |
| 2320 | break; |
| 2321 | } |
| 2322 | } |
| 2323 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2324 | /* if we get there, it means we have no rule which matches, or |
| 2325 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2326 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2327 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2328 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 2329 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2330 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2331 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2332 | /* |
| 2333 | * Now parse the partial (or complete) lines. |
| 2334 | * We will check the request syntax, and also join multi-line |
| 2335 | * headers. An index of all the lines will be elaborated while |
| 2336 | * parsing. |
| 2337 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 2338 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2339 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2340 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2341 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2342 | * req->data + req->eoh = end of processed headers / start of current one |
| 2343 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 2344 | * req->lr = first non-visited byte |
| 2345 | * req->r = end of data |
| 2346 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2347 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2348 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2349 | struct http_txn *txn = &t->txn; |
| 2350 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2351 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2352 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2353 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2354 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2355 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2356 | /* 1: we might have to print this header in debug mode */ |
| 2357 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2358 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2359 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2360 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2361 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2362 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2363 | eol = sol + msg->sl.rq.l; |
| 2364 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2365 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2366 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2367 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2368 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2369 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2370 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2371 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2372 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2373 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2374 | } |
| 2375 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2376 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2377 | |
| 2378 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2379 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2380 | * If not so, we check the FD and buffer states before leaving. |
| 2381 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2382 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2383 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2384 | * |
| 2385 | */ |
| 2386 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2387 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2388 | /* |
| 2389 | * First, let's catch bad requests. |
| 2390 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2391 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2392 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2393 | |
| 2394 | /* 1: Since we are in header mode, if there's no space |
| 2395 | * left for headers, we won't be able to free more |
| 2396 | * later, so the session will never terminate. We |
| 2397 | * must terminate it now. |
| 2398 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2399 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2400 | /* FIXME: check if URI is set and return Status |
| 2401 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2402 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2403 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2404 | } |
| 2405 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2406 | /* 2: have we encountered a read error ? */ |
| 2407 | else if (req->flags & BF_READ_ERROR) { |
| 2408 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2409 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2410 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2411 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2412 | if (!(t->flags & SN_ERR_MASK)) |
| 2413 | t->flags |= SN_ERR_CLICL; |
| 2414 | if (!(t->flags & SN_FINST_MASK)) |
| 2415 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2416 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2420 | 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] | 2421 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2422 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2423 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2424 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2425 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2426 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2427 | if (!(t->flags & SN_ERR_MASK)) |
| 2428 | t->flags |= SN_ERR_CLITO; |
| 2429 | if (!(t->flags & SN_FINST_MASK)) |
| 2430 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2431 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2432 | } |
| 2433 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2434 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2435 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2436 | txn->status = 400; |
| 2437 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2438 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2439 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2440 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2441 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2442 | if (!(t->flags & SN_ERR_MASK)) |
| 2443 | t->flags |= SN_ERR_CLICL; |
| 2444 | if (!(t->flags & SN_FINST_MASK)) |
| 2445 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2446 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2447 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2448 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2449 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2450 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2451 | if (!tick_isset(req->analyse_exp)) |
| 2452 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2453 | |
| 2454 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2455 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | |
| 2459 | /**************************************************************** |
| 2460 | * More interesting part now : we know that we have a complete * |
| 2461 | * request which at least looks like HTTP. We have an indicator * |
| 2462 | * of each header's length, so we can parse them quickly. * |
| 2463 | ****************************************************************/ |
| 2464 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2465 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2466 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2467 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2468 | /* ensure we keep this pointer to the beginning of the message */ |
| 2469 | msg->sol = req->data + msg->som; |
| 2470 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2471 | /* |
| 2472 | * 1: identify the method |
| 2473 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2474 | 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] | 2475 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2476 | /* we can make use of server redirect on GET and HEAD */ |
| 2477 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2478 | t->flags |= SN_REDIRECTABLE; |
| 2479 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2480 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2481 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2482 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2483 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2484 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2485 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 2486 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2487 | !memcmp(&req->data[msg->sl.rq.u], |
| 2488 | t->fe->monitor_uri, |
| 2489 | t->fe->monitor_uri_len))) { |
| 2490 | /* |
| 2491 | * We have found the monitor URI |
| 2492 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2493 | struct acl_cond *cond; |
| 2494 | cur_proxy = t->fe; |
| 2495 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2496 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2497 | |
| 2498 | /* Check if we want to fail this monitor request or not */ |
| 2499 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 2500 | 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] | 2501 | |
| 2502 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2503 | if (cond->pol == ACL_COND_UNLESS) |
| 2504 | ret = !ret; |
| 2505 | |
| 2506 | if (ret) { |
| 2507 | /* we fail this request, let's return 503 service unavail */ |
| 2508 | txn->status = 503; |
| 2509 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 2510 | goto return_prx_cond; |
| 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2515 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2516 | client_retnclose(t, &http_200_chunk); |
| 2517 | goto return_prx_cond; |
| 2518 | } |
| 2519 | |
| 2520 | /* |
| 2521 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2522 | * Note: we cannot log anymore if the request has been |
| 2523 | * classified as invalid. |
| 2524 | */ |
| 2525 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2526 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2527 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2528 | int urilen = msg->sl.rq.l; |
| 2529 | |
| 2530 | if (urilen >= REQURI_LEN) |
| 2531 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2532 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2533 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2534 | |
| 2535 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2536 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2537 | } else { |
| 2538 | Alert("HTTP logging : out of memory.\n"); |
| 2539 | } |
| 2540 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2541 | |
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 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2544 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2545 | int delta; |
| 2546 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2547 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2548 | cur_end = msg->sol + msg->sl.rq.l; |
| 2549 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2550 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2551 | if (msg->sl.rq.u_l == 0) { |
| 2552 | /* if no URI was set, add "/" */ |
| 2553 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2554 | cur_end += delta; |
| 2555 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2556 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2557 | /* add HTTP version */ |
| 2558 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2559 | msg->eoh += delta; |
| 2560 | cur_end += delta; |
| 2561 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2562 | HTTP_MSG_RQMETH, |
| 2563 | msg->sol, cur_end + 1, |
| 2564 | NULL, NULL); |
| 2565 | if (unlikely(!cur_end)) |
| 2566 | goto return_bad_req; |
| 2567 | |
| 2568 | /* we have a full HTTP/1.0 request now and we know that |
| 2569 | * we have either a CR or an LF at <ptr>. |
| 2570 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2571 | 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] | 2572 | } |
| 2573 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2574 | |
| 2575 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2576 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2577 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2578 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2579 | |
| 2580 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2581 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2582 | * As opposed to version 1.2, now they will be evaluated in the |
| 2583 | * filters order and not in the header order. This means that |
| 2584 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2585 | * |
| 2586 | * We can now check whether we want to switch to another |
| 2587 | * backend, in which case we will re-check the backend's |
| 2588 | * filters and various options. In order to support 3-level |
| 2589 | * switching, here's how we should proceed : |
| 2590 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2591 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2592 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2593 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2594 | * There cannot be any switch from there, so ->be cannot be |
| 2595 | * changed anymore. |
| 2596 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2597 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2598 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2599 | * The response path will be able to apply either ->be, or |
| 2600 | * ->be then ->fe filters in order to match the reverse of |
| 2601 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2602 | */ |
| 2603 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2604 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2605 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2606 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2607 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2608 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2609 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2610 | /* first check whether we have some ACLs set to redirect this request */ |
| 2611 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2612 | 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] | 2613 | |
| 2614 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2615 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2616 | ret = !ret; |
| 2617 | |
| 2618 | if (ret) { |
| 2619 | struct chunk rdr = { trash, 0 }; |
| 2620 | const char *msg_fmt; |
| 2621 | |
| 2622 | /* build redirect message */ |
| 2623 | switch(rule->code) { |
| 2624 | case 303: |
| 2625 | rdr.len = strlen(HTTP_303); |
| 2626 | msg_fmt = HTTP_303; |
| 2627 | break; |
| 2628 | case 301: |
| 2629 | rdr.len = strlen(HTTP_301); |
| 2630 | msg_fmt = HTTP_301; |
| 2631 | break; |
| 2632 | case 302: |
| 2633 | default: |
| 2634 | rdr.len = strlen(HTTP_302); |
| 2635 | msg_fmt = HTTP_302; |
| 2636 | break; |
| 2637 | } |
| 2638 | |
| 2639 | if (unlikely(rdr.len > sizeof(trash))) |
| 2640 | goto return_bad_req; |
| 2641 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2642 | |
| 2643 | switch(rule->type) { |
| 2644 | case REDIRECT_TYPE_PREFIX: { |
| 2645 | const char *path; |
| 2646 | int pathlen; |
| 2647 | |
| 2648 | path = http_get_path(txn); |
| 2649 | /* build message using path */ |
| 2650 | if (path) { |
| 2651 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2652 | } else { |
| 2653 | path = "/"; |
| 2654 | pathlen = 1; |
| 2655 | } |
| 2656 | |
| 2657 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2658 | goto return_bad_req; |
| 2659 | |
| 2660 | /* add prefix */ |
| 2661 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2662 | rdr.len += rule->rdr_len; |
| 2663 | |
| 2664 | /* add path */ |
| 2665 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2666 | rdr.len += pathlen; |
| 2667 | break; |
| 2668 | } |
| 2669 | case REDIRECT_TYPE_LOCATION: |
| 2670 | default: |
| 2671 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2672 | goto return_bad_req; |
| 2673 | |
| 2674 | /* add location */ |
| 2675 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2676 | rdr.len += rule->rdr_len; |
| 2677 | break; |
| 2678 | } |
| 2679 | |
| 2680 | /* add end of headers */ |
| 2681 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2682 | rdr.len += 4; |
| 2683 | |
| 2684 | txn->status = rule->code; |
| 2685 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2686 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2687 | client_retnclose(t, &rdr); |
| 2688 | goto return_prx_cond; |
| 2689 | } |
| 2690 | } |
| 2691 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2692 | /* first check whether we have some ACLs set to block this request */ |
| 2693 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2694 | 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] | 2695 | |
| 2696 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2697 | if (cond->pol == ACL_COND_UNLESS) |
| 2698 | ret = !ret; |
| 2699 | |
| 2700 | if (ret) { |
| 2701 | txn->status = 403; |
| 2702 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2703 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2704 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2705 | goto return_prx_cond; |
| 2706 | } |
| 2707 | } |
| 2708 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2709 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2710 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2711 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2712 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2713 | } |
| 2714 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2715 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2716 | /* to ensure correct connection accounting on |
| 2717 | * the backend, we count the connection for the |
| 2718 | * one managing the queue. |
| 2719 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2720 | t->be->beconn++; |
| 2721 | if (t->be->beconn > t->be->beconn_max) |
| 2722 | t->be->beconn_max = t->be->beconn; |
| 2723 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2724 | t->flags |= SN_BE_ASSIGNED; |
| 2725 | } |
| 2726 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2727 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2728 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2729 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2730 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2731 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2732 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2733 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2734 | goto return_prx_cond; |
| 2735 | } |
| 2736 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2737 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2738 | 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] | 2739 | !(t->flags & SN_CONN_CLOSED)) { |
| 2740 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2741 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2742 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2743 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2744 | 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] | 2745 | old_idx = 0; |
| 2746 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2747 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2748 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2749 | cur_ptr = cur_next; |
| 2750 | cur_end = cur_ptr + cur_hdr->len; |
| 2751 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2752 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2753 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2754 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2755 | /* 3 possibilities : |
| 2756 | * - we have already set Connection: close, |
| 2757 | * so we remove this line. |
| 2758 | * - we have not yet set Connection: close, |
| 2759 | * but this line indicates close. We leave |
| 2760 | * it untouched and set the flag. |
| 2761 | * - we have not yet set Connection: close, |
| 2762 | * and this line indicates non-close. We |
| 2763 | * replace it. |
| 2764 | */ |
| 2765 | if (t->flags & SN_CONN_CLOSED) { |
| 2766 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2767 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2768 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2769 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2770 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2771 | cur_hdr->len = 0; |
| 2772 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2773 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2774 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2775 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2776 | cur_next += delta; |
| 2777 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2778 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2779 | } |
| 2780 | t->flags |= SN_CONN_CLOSED; |
| 2781 | } |
| 2782 | } |
| 2783 | old_idx = cur_idx; |
| 2784 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2785 | } |
| 2786 | /* add request headers from the rule sets in the same order */ |
| 2787 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2788 | if (unlikely(http_header_add_tail(req, |
| 2789 | &txn->req, |
| 2790 | &txn->hdr_idx, |
| 2791 | rule_set->req_add[cur_idx])) < 0) |
| 2792 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2793 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2794 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2795 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2796 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2797 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2798 | /* we have to check the URI and auth for this request. |
| 2799 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2800 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2801 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2802 | if (stats_check_uri_auth(t, rule_set)) |
| 2803 | return 1; |
| 2804 | } |
| 2805 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2806 | /* now check whether we have some switching rules for this request */ |
| 2807 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2808 | struct switching_rule *rule; |
| 2809 | |
| 2810 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2811 | int ret; |
| 2812 | |
| 2813 | 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] | 2814 | |
| 2815 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2816 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2817 | ret = !ret; |
| 2818 | |
| 2819 | if (ret) { |
| 2820 | t->be = rule->be.backend; |
| 2821 | t->be->beconn++; |
| 2822 | if (t->be->beconn > t->be->beconn_max) |
| 2823 | t->be->beconn_max = t->be->beconn; |
| 2824 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2825 | |
| 2826 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2827 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2828 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2829 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2830 | t->flags |= SN_BE_ASSIGNED; |
| 2831 | break; |
| 2832 | } |
| 2833 | } |
| 2834 | } |
| 2835 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2836 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2837 | /* No backend was set, but there was a default |
| 2838 | * backend set in the frontend, so we use it and |
| 2839 | * loop again. |
| 2840 | */ |
| 2841 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2842 | t->be->beconn++; |
| 2843 | if (t->be->beconn > t->be->beconn_max) |
| 2844 | t->be->beconn_max = t->be->beconn; |
| 2845 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2846 | |
| 2847 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2848 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2849 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2850 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2851 | t->flags |= SN_BE_ASSIGNED; |
| 2852 | } |
| 2853 | } 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] | 2854 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2855 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2856 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2857 | /* To ensure correct connection accounting on |
| 2858 | * the backend, we count the connection for the |
| 2859 | * one managing the queue. |
| 2860 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2861 | t->be->beconn++; |
| 2862 | if (t->be->beconn > t->be->beconn_max) |
| 2863 | t->be->beconn_max = t->be->beconn; |
| 2864 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2865 | t->flags |= SN_BE_ASSIGNED; |
| 2866 | } |
| 2867 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2868 | /* |
| 2869 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2870 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2871 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2872 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2873 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2874 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2875 | /* |
| 2876 | * If HTTP PROXY is set we simply get remote server address |
| 2877 | * parsing incoming request. |
| 2878 | */ |
| 2879 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2880 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2881 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2882 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2883 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2884 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2885 | * so let's do the same now. |
| 2886 | */ |
| 2887 | |
| 2888 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2889 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2890 | 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] | 2891 | } |
| 2892 | |
| 2893 | |
| 2894 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2895 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2896 | * Note that doing so might move headers in the request, but |
| 2897 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2898 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2899 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2900 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2901 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2902 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2903 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2904 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2905 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2906 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2907 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2908 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2909 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2910 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2911 | /* Add an X-Forwarded-For header unless the source IP is |
| 2912 | * in the 'except' network range. |
| 2913 | */ |
| 2914 | if ((!t->fe->except_mask.s_addr || |
| 2915 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2916 | != t->fe->except_net.s_addr) && |
| 2917 | (!t->be->except_mask.s_addr || |
| 2918 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2919 | != t->be->except_net.s_addr)) { |
| 2920 | int len; |
| 2921 | unsigned char *pn; |
| 2922 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2923 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2924 | /* Note: we rely on the backend to get the header name to be used for |
| 2925 | * x-forwarded-for, because the header is really meant for the backends. |
| 2926 | * However, if the backend did not specify any option, we have to rely |
| 2927 | * on the frontend's header name. |
| 2928 | */ |
| 2929 | if (t->be->fwdfor_hdr_len) { |
| 2930 | len = t->be->fwdfor_hdr_len; |
| 2931 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2932 | } else { |
| 2933 | len = t->fe->fwdfor_hdr_len; |
| 2934 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2935 | } |
| 2936 | 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] | 2937 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2938 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2939 | &txn->hdr_idx, trash, len)) < 0) |
| 2940 | goto return_bad_req; |
| 2941 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2942 | } |
| 2943 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2944 | /* FIXME: for the sake of completeness, we should also support |
| 2945 | * 'except' here, although it is mostly useless in this case. |
| 2946 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2947 | int len; |
| 2948 | char pn[INET6_ADDRSTRLEN]; |
| 2949 | inet_ntop(AF_INET6, |
| 2950 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2951 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2952 | |
| 2953 | /* Note: we rely on the backend to get the header name to be used for |
| 2954 | * x-forwarded-for, because the header is really meant for the backends. |
| 2955 | * However, if the backend did not specify any option, we have to rely |
| 2956 | * on the frontend's header name. |
| 2957 | */ |
| 2958 | if (t->be->fwdfor_hdr_len) { |
| 2959 | len = t->be->fwdfor_hdr_len; |
| 2960 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2961 | } else { |
| 2962 | len = t->fe->fwdfor_hdr_len; |
| 2963 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2964 | } |
| 2965 | len += sprintf(trash + len, ": %s", pn); |
| 2966 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2967 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2968 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2969 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2970 | } |
| 2971 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2972 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2973 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2974 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2975 | * 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] | 2976 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2977 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2978 | ((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] | 2979 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2980 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2981 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2982 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2983 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2984 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2985 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2986 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2987 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2988 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2989 | * (unless headers overflowed the buffer?) |
| 2990 | */ |
| 2991 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2992 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2993 | 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] | 2994 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2995 | /* are there enough bytes here? total == l || r || rlim ? |
| 2996 | * len is unsigned, but eoh is int, |
| 2997 | * how many bytes of body have we received? |
| 2998 | * eoh is the first empty line of the header |
| 2999 | */ |
| 3000 | /* 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] | 3001 | 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] | 3002 | |
| 3003 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 3004 | * We can't assume responsibility for the server's decision, |
| 3005 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 3006 | * We also can't change our mind later, about which server to choose, so round robin. |
| 3007 | */ |
| 3008 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 3009 | struct hdr_ctx ctx; |
| 3010 | ctx.idx = 0; |
| 3011 | /* Expect is allowed in 1.1, look for it */ |
| 3012 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 3013 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3014 | 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] | 3015 | /* We can't reliablly stall and wait for data, because of |
| 3016 | * .NET clients that don't conform to rfc2616; so, no need for |
| 3017 | * the next block to check length expectations. |
| 3018 | * We could send 100 status back to the client, but then we need to |
| 3019 | * re-write headers, and send the message. And this isn't the right |
| 3020 | * place for that action. |
| 3021 | * TODO: support Expect elsewhere and delete this block. |
| 3022 | */ |
| 3023 | goto end_check_maybe_wait_for_body; |
| 3024 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3025 | |
| 3026 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3027 | /* nothing to do, we got enough */ |
| 3028 | } else { |
| 3029 | /* limit implies we are supposed to need this many bytes |
| 3030 | * to find the parameter. Let's see how many bytes we can wait for. |
| 3031 | */ |
| 3032 | long long hint = len; |
| 3033 | struct hdr_ctx ctx; |
| 3034 | ctx.idx = 0; |
| 3035 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3036 | 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] | 3037 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3038 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3039 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3040 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3041 | ctx.idx = 0; |
| 3042 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 3043 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3044 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3045 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3046 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3047 | hint = 0; /* parse failure, untrusted client */ |
| 3048 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3049 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3050 | msg->hdr_content_len = hint; |
| 3051 | else |
| 3052 | hint = 0; /* bad client, sent negative length */ |
| 3053 | } |
| 3054 | } |
| 3055 | /* 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] | 3056 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3057 | hint = t->be->url_param_post_limit; |
| 3058 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3059 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3060 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3061 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3062 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3063 | /* else... There are no body bytes to wait for */ |
| 3064 | } |
| 3065 | } |
| 3066 | } |
| 3067 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3068 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3069 | /************************************************************* |
| 3070 | * OK, that's finished for the headers. We have done what we * |
| 3071 | * could. Let's switch to the DATA state. * |
| 3072 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3073 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3074 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 3075 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3076 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 3077 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3078 | * which may be the same as the connect timeout if unspecified. |
| 3079 | * 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] | 3080 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3081 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3082 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3083 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3084 | /* flush the request so that we can drop the connection early |
| 3085 | * if the client closes first. |
| 3086 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3087 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3088 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3089 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 3090 | if (!req->analyse_exp) |
| 3091 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3092 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3093 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3094 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3095 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3096 | |
| 3097 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3098 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3099 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3100 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 3101 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3102 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3103 | return_prx_cond: |
| 3104 | if (!(t->flags & SN_ERR_MASK)) |
| 3105 | t->flags |= SN_ERR_PRXCOND; |
| 3106 | if (!(t->flags & SN_FINST_MASK)) |
| 3107 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3108 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3109 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3110 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3111 | } |
| 3112 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3113 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 3114 | struct http_txn *txn = &t->txn; |
| 3115 | |
| 3116 | /* This connection is being tarpitted. The CLIENT side has |
| 3117 | * already set the connect expiration date to the right |
| 3118 | * timeout. We just have to check that the client is still |
| 3119 | * there and that the timeout has not expired. |
| 3120 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 3121 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3122 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3123 | return 0; |
| 3124 | |
| 3125 | /* We will set the queue timer to the time spent, just for |
| 3126 | * logging purposes. We fake a 500 server error, so that the |
| 3127 | * attacker will not suspect his connection has been tarpitted. |
| 3128 | * It will not cause trouble to the logs because we can exclude |
| 3129 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3130 | */ |
| 3131 | trace_term(t, TT_HTTP_SRV_2); |
| 3132 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3133 | |
| 3134 | txn->status = 500; |
| 3135 | if (req->flags != BF_READ_ERROR) |
| 3136 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 3137 | |
| 3138 | req->analysers = 0; |
| 3139 | req->analyse_exp = TICK_ETERNITY; |
| 3140 | |
| 3141 | t->fe->failed_req++; |
| 3142 | if (!(t->flags & SN_ERR_MASK)) |
| 3143 | t->flags |= SN_ERR_PRXCOND; |
| 3144 | if (!(t->flags & SN_FINST_MASK)) |
| 3145 | t->flags |= SN_FINST_T; |
| 3146 | return 0; |
| 3147 | } |
| 3148 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3149 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3150 | /* We have to parse the HTTP request body to find any required data. |
| 3151 | * "balance url_param check_post" should have been the only way to get |
| 3152 | * into this. We were brought here after HTTP header analysis, so all |
| 3153 | * related structures are ready. |
| 3154 | */ |
| 3155 | struct http_msg *msg = &t->txn.req; |
| 3156 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 3157 | long long limit = t->be->url_param_post_limit; |
| 3158 | struct hdr_ctx ctx; |
| 3159 | |
| 3160 | ctx.idx = 0; |
| 3161 | |
| 3162 | /* now if we have a length, we'll take the hint */ |
| 3163 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 3164 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 3165 | unsigned int chunk = 0; |
| 3166 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 3167 | char c = msg->sol[body]; |
| 3168 | if (ishex(c)) { |
| 3169 | unsigned int hex = toupper(c) - '0'; |
| 3170 | if (hex > 9) |
| 3171 | hex -= 'A' - '9' - 1; |
| 3172 | chunk = (chunk << 4) | hex; |
| 3173 | } else |
| 3174 | break; |
| 3175 | body++; |
| 3176 | } |
| 3177 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3178 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3179 | |
| 3180 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3181 | 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] | 3182 | |
| 3183 | body += 2; // skip CRLF |
| 3184 | |
| 3185 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 3186 | * 1. how much entity data do we have? new var |
| 3187 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 3188 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 3189 | */ |
| 3190 | |
| 3191 | if (chunk < limit) |
| 3192 | limit = chunk; /* only reading one chunk */ |
| 3193 | } else { |
| 3194 | if (msg->hdr_content_len < limit) |
| 3195 | limit = msg->hdr_content_len; |
| 3196 | } |
| 3197 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3198 | http_body_end: |
| 3199 | /* we leave once we know we have nothing left to do. This means that we have |
| 3200 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 3201 | * buffer closed). |
| 3202 | */ |
| 3203 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 3204 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 3205 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3206 | /* 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] | 3207 | 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] | 3208 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 3209 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3210 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 3211 | else { |
| 3212 | /* Not enough data. We'll re-use the http-request |
| 3213 | * timeout here. Ideally, we should set the timeout |
| 3214 | * relative to the accept() date. We just set the |
| 3215 | * request timeout once at the beginning of the |
| 3216 | * request. |
| 3217 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3218 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 3219 | if (!tick_isset(req->analyse_exp)) |
| 3220 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 3221 | return 0; |
| 3222 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3223 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3224 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3225 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3226 | * probably reduce one day's debugging session. |
| 3227 | */ |
| 3228 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3229 | 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] | 3230 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3231 | __FILE__, __LINE__, req->analysers); |
| 3232 | ABORT_NOW(); |
| 3233 | } |
| 3234 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3235 | 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] | 3236 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3237 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3238 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3239 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3240 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 3241 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3242 | * 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] | 3243 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3244 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3245 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3246 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3247 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3248 | struct buffer *req = t->req; |
| 3249 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3250 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3251 | 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] | 3252 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3253 | t, |
| 3254 | rep, |
| 3255 | rep->rex, rep->wex, |
| 3256 | rep->flags, |
| 3257 | rep->l, |
| 3258 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 3259 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3260 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3261 | /* |
| 3262 | * Now parse the partial (or complete) lines. |
| 3263 | * We will check the response syntax, and also join multi-line |
| 3264 | * headers. An index of all the lines will be elaborated while |
| 3265 | * parsing. |
| 3266 | * |
| 3267 | * For the parsing, we use a 28 states FSM. |
| 3268 | * |
| 3269 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3270 | * rep->data + rep->som = beginning of response |
| 3271 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 3272 | * 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] | 3273 | * rep->lr = first non-visited byte |
| 3274 | * rep->r = end of data |
| 3275 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 3276 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3277 | int cur_idx; |
| 3278 | struct http_msg *msg = &txn->rsp; |
| 3279 | struct proxy *cur_proxy; |
| 3280 | |
| 3281 | if (likely(rep->lr < rep->r)) |
| 3282 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 3283 | |
| 3284 | /* 1: we might have to print this header in debug mode */ |
| 3285 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3286 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 3287 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 3288 | char *eol, *sol; |
| 3289 | |
| 3290 | sol = rep->data + msg->som; |
| 3291 | eol = sol + msg->sl.rq.l; |
| 3292 | debug_hdr("srvrep", t, sol, eol); |
| 3293 | |
| 3294 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 3295 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 3296 | |
| 3297 | while (cur_idx) { |
| 3298 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 3299 | debug_hdr("srvhdr", t, sol, eol); |
| 3300 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 3301 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3302 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3303 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3304 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3305 | /* |
| 3306 | * Now we quickly check if we have found a full valid response. |
| 3307 | * If not so, we check the FD and buffer states before leaving. |
| 3308 | * A full response is indicated by the fact that we have seen |
| 3309 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 3310 | * responses are checked first. |
| 3311 | * |
| 3312 | * Depending on whether the client is still there or not, we |
| 3313 | * may send an error response back or not. Note that normally |
| 3314 | * we should only check for HTTP status there, and check I/O |
| 3315 | * errors somewhere else. |
| 3316 | */ |
| 3317 | |
| 3318 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3319 | /* Invalid response */ |
| 3320 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 3321 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3322 | //buffer_shutr(rep); |
| 3323 | //buffer_shutw(req); |
| 3324 | //fd_delete(req->cons->fd); |
| 3325 | //req->cons->state = SI_ST_CLO; |
| 3326 | buffer_shutr_now(rep); |
| 3327 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3328 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3329 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3330 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3331 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3332 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3333 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3334 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3335 | txn->status = 502; |
| 3336 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3337 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3338 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3339 | if (!(t->flags & SN_FINST_MASK)) |
| 3340 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3341 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3342 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3343 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3344 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3345 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3346 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3347 | /* too large response does not fit in buffer. */ |
| 3348 | else if (rep->flags & BF_FULL) { |
| 3349 | goto hdr_response_bad; |
| 3350 | } |
| 3351 | /* read error */ |
| 3352 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3353 | buffer_shutr_now(rep); |
| 3354 | buffer_shutw_now(req); |
| 3355 | //fd_delete(req->cons->fd); |
| 3356 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3357 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3358 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3359 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3360 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3361 | //} |
| 3362 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3363 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3364 | txn->status = 502; |
| 3365 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3366 | if (!(t->flags & SN_ERR_MASK)) |
| 3367 | t->flags |= SN_ERR_SRVCL; |
| 3368 | if (!(t->flags & SN_FINST_MASK)) |
| 3369 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3370 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3371 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3372 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3373 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3374 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3375 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3376 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3377 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3378 | buffer_shutr_now(rep); |
| 3379 | buffer_shutw_now(req); |
| 3380 | //fd_delete(req->cons->fd); |
| 3381 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3382 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3383 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3384 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3385 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3386 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3387 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3388 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3389 | txn->status = 504; |
| 3390 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 3391 | if (!(t->flags & SN_ERR_MASK)) |
| 3392 | t->flags |= SN_ERR_SRVTO; |
| 3393 | if (!(t->flags & SN_FINST_MASK)) |
| 3394 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3395 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3396 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3397 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3398 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3399 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3400 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 3401 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3402 | buffer_shutr_now(rep); |
| 3403 | buffer_shutw_now(req); |
| 3404 | //fd_delete(req->cons->fd); |
| 3405 | //req->cons->state = SI_ST_CLO; |
| 3406 | if (t->srv) { |
| 3407 | //t->srv->cur_sess--; |
| 3408 | t->srv->failed_resp++; |
| 3409 | //sess_change_server(t, NULL); |
| 3410 | } |
| 3411 | t->be->failed_resp++; |
| 3412 | rep->analysers = 0; |
| 3413 | txn->status = 502; |
| 3414 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3415 | if (!(t->flags & SN_ERR_MASK)) |
| 3416 | t->flags |= SN_ERR_SRVCL; |
| 3417 | if (!(t->flags & SN_FINST_MASK)) |
| 3418 | t->flags |= SN_FINST_H; |
| 3419 | |
| 3420 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3421 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3422 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3423 | return 0; |
| 3424 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3425 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3426 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3427 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3428 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3429 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3430 | /***************************************************************** |
| 3431 | * More interesting part now : we know that we have a complete * |
| 3432 | * response which at least looks like HTTP. We have an indicator * |
| 3433 | * of each header's length, so we can parse them quickly. * |
| 3434 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3435 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3436 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3437 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3438 | /* ensure we keep this pointer to the beginning of the message */ |
| 3439 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3440 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3441 | /* |
| 3442 | * 1: get the status code and check for cacheability. |
| 3443 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3444 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3445 | t->logs.logwait &= ~LW_RESP; |
| 3446 | 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] | 3447 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3448 | switch (txn->status) { |
| 3449 | case 200: |
| 3450 | case 203: |
| 3451 | case 206: |
| 3452 | case 300: |
| 3453 | case 301: |
| 3454 | case 410: |
| 3455 | /* RFC2616 @13.4: |
| 3456 | * "A response received with a status code of |
| 3457 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3458 | * by a cache (...) unless a cache-control |
| 3459 | * directive prohibits caching." |
| 3460 | * |
| 3461 | * RFC2616 @9.5: POST method : |
| 3462 | * "Responses to this method are not cacheable, |
| 3463 | * unless the response includes appropriate |
| 3464 | * Cache-Control or Expires header fields." |
| 3465 | */ |
| 3466 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3467 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3468 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3469 | break; |
| 3470 | default: |
| 3471 | break; |
| 3472 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3473 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3474 | /* |
| 3475 | * 2: we may need to capture headers |
| 3476 | */ |
| 3477 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3478 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3479 | txn->rsp.cap, t->fe->rsp_cap); |
| 3480 | |
| 3481 | /* |
| 3482 | * 3: we will have to evaluate the filters. |
| 3483 | * As opposed to version 1.2, now they will be evaluated in the |
| 3484 | * filters order and not in the header order. This means that |
| 3485 | * each filter has to be validated among all headers. |
| 3486 | * |
| 3487 | * Filters are tried with ->be first, then with ->fe if it is |
| 3488 | * different from ->be. |
| 3489 | */ |
| 3490 | |
| 3491 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3492 | |
| 3493 | cur_proxy = t->be; |
| 3494 | while (1) { |
| 3495 | struct proxy *rule_set = cur_proxy; |
| 3496 | |
| 3497 | /* try headers filters */ |
| 3498 | if (rule_set->rsp_exp != NULL) { |
| 3499 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3500 | return_bad_resp: |
| 3501 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3502 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3503 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3504 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3505 | } |
| 3506 | cur_proxy->failed_resp++; |
| 3507 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3508 | buffer_shutr_now(rep); |
| 3509 | buffer_shutw_now(req); |
| 3510 | //fd_delete(req->cons->fd); |
| 3511 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3512 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3513 | txn->status = 502; |
| 3514 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3515 | if (!(t->flags & SN_ERR_MASK)) |
| 3516 | t->flags |= SN_ERR_PRXCOND; |
| 3517 | if (!(t->flags & SN_FINST_MASK)) |
| 3518 | t->flags |= SN_FINST_H; |
| 3519 | /* We used to have a free connection slot. Since we'll never use it, |
| 3520 | * we have to inform the server that it may be used by another session. |
| 3521 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3522 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 3523 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3524 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3525 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3526 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3527 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3528 | /* has the response been denied ? */ |
| 3529 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3530 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3531 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3532 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3533 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3534 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3535 | cur_proxy->denied_resp++; |
| 3536 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3537 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3538 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3539 | /* We might have to check for "Connection:" */ |
| 3540 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3541 | !(t->flags & SN_CONN_CLOSED)) { |
| 3542 | char *cur_ptr, *cur_end, *cur_next; |
| 3543 | int cur_idx, old_idx, delta, val; |
| 3544 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3545 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3546 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3547 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3548 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3549 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3550 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3551 | cur_ptr = cur_next; |
| 3552 | cur_end = cur_ptr + cur_hdr->len; |
| 3553 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3554 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3555 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3556 | if (val) { |
| 3557 | /* 3 possibilities : |
| 3558 | * - we have already set Connection: close, |
| 3559 | * so we remove this line. |
| 3560 | * - we have not yet set Connection: close, |
| 3561 | * but this line indicates close. We leave |
| 3562 | * it untouched and set the flag. |
| 3563 | * - we have not yet set Connection: close, |
| 3564 | * and this line indicates non-close. We |
| 3565 | * replace it. |
| 3566 | */ |
| 3567 | if (t->flags & SN_CONN_CLOSED) { |
| 3568 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3569 | txn->rsp.eoh += delta; |
| 3570 | cur_next += delta; |
| 3571 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3572 | txn->hdr_idx.used--; |
| 3573 | cur_hdr->len = 0; |
| 3574 | } else { |
| 3575 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3576 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3577 | "close", 5); |
| 3578 | cur_next += delta; |
| 3579 | cur_hdr->len += delta; |
| 3580 | txn->rsp.eoh += delta; |
| 3581 | } |
| 3582 | t->flags |= SN_CONN_CLOSED; |
| 3583 | } |
| 3584 | } |
| 3585 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3586 | } |
| 3587 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3588 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3589 | /* add response headers from the rule sets in the same order */ |
| 3590 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3591 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3592 | rule_set->rsp_add[cur_idx])) < 0) |
| 3593 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3594 | } |
| 3595 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3596 | /* check whether we're already working on the frontend */ |
| 3597 | if (cur_proxy == t->fe) |
| 3598 | break; |
| 3599 | cur_proxy = t->fe; |
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 | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3602 | /* |
| 3603 | * 4: check for server cookie. |
| 3604 | */ |
| 3605 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3606 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3607 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3608 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3609 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3610 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3611 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3612 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3613 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3614 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3615 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3616 | /* |
| 3617 | * 6: add server cookie in the response if needed |
| 3618 | */ |
| 3619 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3620 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3621 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3622 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3623 | /* the server is known, it's not the one the client requested, we have to |
| 3624 | * insert a set-cookie here, except if we want to insert only on POST |
| 3625 | * requests and this one isn't. Note that servers which don't have cookies |
| 3626 | * (eg: some backup servers) will return a full cookie removal request. |
| 3627 | */ |
| 3628 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3629 | t->be->cookie_name, |
| 3630 | 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] | 3631 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3632 | if (t->be->cookie_domain) |
| 3633 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3634 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3635 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3636 | trash, len)) < 0) |
| 3637 | goto return_bad_resp; |
| 3638 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3639 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3640 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3641 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3642 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3643 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3644 | */ |
| 3645 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3646 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3647 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3648 | |
| 3649 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3650 | "Cache-control: private", 22)) < 0) |
| 3651 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3652 | } |
| 3653 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3654 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3655 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3656 | /* |
| 3657 | * 7: check if result will be cacheable with a cookie. |
| 3658 | * We'll block the response if security checks have caught |
| 3659 | * nasty things such as a cacheable cookie. |
| 3660 | */ |
| 3661 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3662 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3663 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3664 | |
| 3665 | /* we're in presence of a cacheable response containing |
| 3666 | * a set-cookie header. We'll block it as requested by |
| 3667 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3668 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3669 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3670 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3671 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3672 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3673 | } |
| 3674 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3675 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3676 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3677 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3678 | send_log(t->be, LOG_ALERT, |
| 3679 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3680 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3681 | goto return_srv_prx_502; |
| 3682 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3683 | |
| 3684 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3685 | * 8: add "Connection: close" if needed and not yet set. |
| 3686 | * 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] | 3687 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3688 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3689 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3690 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3691 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3692 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3693 | "Connection: close", 17)) < 0) |
| 3694 | goto return_bad_resp; |
| 3695 | t->flags |= SN_CONN_CLOSED; |
| 3696 | } |
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 | /************************************************************* |
| 3699 | * OK, that's finished for the headers. We have done what we * |
| 3700 | * could. Let's switch to the DATA state. * |
| 3701 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3702 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3703 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3704 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3705 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3706 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3707 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3708 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3709 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3710 | } |
| 3711 | #endif |
| 3712 | /* if the user wants to log as soon as possible, without counting |
| 3713 | * bytes from the server, then this is the right moment. We have |
| 3714 | * to temporarily assign bytes_out to log what we currently have. |
| 3715 | */ |
| 3716 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3717 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3718 | t->logs.bytes_out = txn->rsp.eoh; |
| 3719 | if (t->fe->to_log & LW_REQ) |
| 3720 | http_sess_log(t); |
| 3721 | else |
| 3722 | tcp_sess_log(t); |
| 3723 | t->logs.bytes_out = 0; |
| 3724 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3725 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3726 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3727 | * otherwise we would not let the client side wake up. |
| 3728 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3729 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3730 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3731 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3732 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3733 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3734 | * probably reduce one day's debugging session. |
| 3735 | */ |
| 3736 | #ifdef DEBUG_DEV |
| 3737 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3738 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3739 | __FILE__, __LINE__, rep->analysers); |
| 3740 | ABORT_NOW(); |
| 3741 | } |
| 3742 | #endif |
| 3743 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3744 | return 0; |
| 3745 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3746 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3747 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3748 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 3749 | * connection was attempted and that the file descriptor is already allocated. |
| 3750 | * We must check for establishment, error and abort. Possible output states |
Willy Tarreau | 74ab2ac | 2008-11-23 17:23:07 +0100 | [diff] [blame] | 3751 | * 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] | 3752 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 3753 | * otherwise 1. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3754 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3755 | 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] | 3756 | { |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3757 | struct buffer *req = si->ob; |
| 3758 | struct buffer *rep = si->ib; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3759 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 3760 | 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] | 3761 | now_ms, __FUNCTION__, |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3762 | cli_stnames[s->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3763 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3764 | req->flags, rep->flags, |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 3765 | req->l, rep->l, |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3766 | fdtab[si->fd].state); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3767 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3768 | |
| 3769 | /* If we got an error, or if nothing happened and the connection timed |
| 3770 | * out, we must give up. The CER state handler will take care of retry |
| 3771 | * attempts and error reports. |
| 3772 | */ |
| 3773 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
| 3774 | si->state = SI_ST_CER; |
| 3775 | fd_delete(si->fd); |
| 3776 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3777 | if (si->err_type) |
| 3778 | return 0; |
| 3779 | |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3780 | si->err_loc = s->srv; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3781 | if (si->flags & SI_FL_ERR) |
| 3782 | si->err_type = SI_ET_CONN_ERR; |
| 3783 | else |
| 3784 | si->err_type = SI_ET_CONN_TO; |
| 3785 | return 0; |
| 3786 | } |
| 3787 | |
| 3788 | /* OK, maybe we want to abort */ |
| 3789 | if (unlikely((req->flags & BF_SHUTW_NOW) || |
| 3790 | (rep->flags & BF_SHUTW) || |
| 3791 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3792 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) || |
| 3793 | s->be->options & PR_O_ABRT_CLOSE)))) { |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 3794 | /* give up */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 3795 | si->shutw(si); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3796 | si->err_type |= SI_ET_CONN_ABRT; |
| 3797 | si->err_loc = s->srv; |
| 3798 | return 1; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3799 | } |
| 3800 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3801 | /* we need to wait a bit more if there was no activity either */ |
| 3802 | if (!(req->flags & BF_WRITE_ACTIVITY)) |
| 3803 | return 1; |
| 3804 | |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3805 | /* OK, this means that a connection succeeded. The caller will be |
| 3806 | * responsible for handling the transition from CON to EST. |
| 3807 | */ |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3808 | s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3809 | si->state = SI_ST_EST; |
| 3810 | si->err_type = SI_ET_NONE; |
| 3811 | si->err_loc = NULL; |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3812 | return 1; |
| 3813 | } |
| 3814 | |
| 3815 | |
| 3816 | /* |
| 3817 | * This function handles the transition between the SI_ST_CON state and the |
| 3818 | * SI_ST_EST state. It must only be called after switching from SI_ST_CON to |
| 3819 | * SI_ST_EST. |
| 3820 | */ |
| 3821 | void sess_establish(struct session *s, struct stream_interface *si) |
| 3822 | { |
| 3823 | struct buffer *req = si->ob; |
| 3824 | struct buffer *rep = si->ib; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3825 | |
| 3826 | if (req->flags & BF_EMPTY) { |
| 3827 | EV_FD_CLR(si->fd, DIR_WR); |
| 3828 | req->wex = TICK_ETERNITY; |
| 3829 | } else { |
| 3830 | EV_FD_SET(si->fd, DIR_WR); |
| 3831 | req->wex = tick_add_ifset(now_ms, s->be->timeout.server); |
| 3832 | if (tick_isset(req->wex)) { |
| 3833 | /* FIXME: to prevent the server from expiring read |
| 3834 | * timeouts during writes, we refresh it. */ |
| 3835 | rep->rex = req->wex; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3836 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3837 | } |
| 3838 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3839 | if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3840 | if (!(rep->flags & BF_HIJACK)) { |
| 3841 | EV_FD_SET(si->fd, DIR_RD); |
| 3842 | rep->rex = tick_add_ifset(now_ms, s->be->timeout.server); |
| 3843 | } |
| 3844 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3845 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3846 | /* if the user wants to log as soon as possible, without counting |
| 3847 | * bytes from the server, then this is the right moment. */ |
| 3848 | if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) { |
| 3849 | s->logs.t_close = s->logs.t_connect; /* to get a valid end date */ |
| 3850 | tcp_sess_log(s); |
| 3851 | } |
| 3852 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3853 | if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) { |
| 3854 | /* TCP splicing supported by both FE and BE */ |
| 3855 | tcp_splice_splicefd(req->prod->fd, si->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3856 | } |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3857 | #endif |
| 3858 | } |
| 3859 | else { |
| 3860 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3861 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3862 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3863 | /* reset hdr_idx which was already initialized by the request. |
| 3864 | * right now, the http parser does it. |
| 3865 | * hdr_idx_init(&s->txn.hdr_idx); |
| 3866 | */ |
| 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 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 3870 | req->wex = TICK_ETERNITY; |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3871 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3872 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3873 | |
| 3874 | /* This function is called with (si->state == SI_ST_CER) meaning that a |
| 3875 | * previous connection attempt has failed and that the file descriptor |
| 3876 | * has already been released. Possible causes include asynchronous error |
| 3877 | * notification and time out. Possible output states are SI_ST_CLO when |
| 3878 | * retries are exhausted, SI_ST_TAR when a delay is wanted before a new |
| 3879 | * connection attempt, SI_ST_ASS when it's wise to retry on the same server, |
| 3880 | * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are |
| 3881 | * marked as in error state. It returns 0. |
| 3882 | */ |
| 3883 | int sess_update_st_cer(struct session *s, struct stream_interface *si) |
| 3884 | { |
Willy Tarreau | 4351b3a | 2008-11-12 01:51:41 +0100 | [diff] [blame] | 3885 | /* we probably have to release last session from the server */ |
| 3886 | if (s->srv) { |
| 3887 | if (s->flags & SN_CURR_SESS) { |
| 3888 | s->flags &= ~SN_CURR_SESS; |
| 3889 | s->srv->cur_sess--; |
| 3890 | } |
| 3891 | sess_change_server(s, NULL); |
| 3892 | } |
| 3893 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3894 | /* ensure that we have enough retries left */ |
| 3895 | s->conn_retries--; |
| 3896 | if (s->conn_retries < 0) { |
| 3897 | if (!si->err_type) { |
| 3898 | si->err_type = SI_ET_CONN_ERR; |
| 3899 | si->err_loc = s->srv; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3900 | } |
| 3901 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3902 | if (s->srv) |
| 3903 | s->srv->failed_conns++; |
| 3904 | s->be->failed_conns++; |
| 3905 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3906 | process_srv_queue(s->srv); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3907 | |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 3908 | si->shutw(si); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3909 | si->ob->flags |= BF_WRITE_ERROR; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3910 | |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame^] | 3911 | si->shutr(si); |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3912 | si->ib->flags |= BF_READ_ERROR; |
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 | si->state = SI_ST_CLO; |
| 3915 | return 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3916 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3917 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3918 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 3919 | * retry on another server for the last retry. In order to achieve this, |
| 3920 | * we must mark the session unassigned, and eventually clear the DIRECT |
| 3921 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 3922 | * redispatch yet, because this will depend on what server is selected. |
| 3923 | */ |
| 3924 | if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) { |
| 3925 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3926 | process_srv_queue(s->srv); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3927 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3928 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3929 | s->prev_srv = s->srv; |
| 3930 | si->state = SI_ST_REQ; |
| 3931 | } else { |
| 3932 | if (s->srv) |
| 3933 | s->srv->retries++; |
| 3934 | s->be->retries++; |
| 3935 | si->state = SI_ST_ASS; |
| 3936 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3937 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3938 | if (si->flags & SI_FL_ERR) { |
| 3939 | /* The error was an asynchronous connection error, and we will |
| 3940 | * likely have to retry connecting to the same server, most |
| 3941 | * likely leading to the same result. To avoid this, we wait |
| 3942 | * one second before retrying. |
| 3943 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3944 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3945 | if (!si->err_type) |
| 3946 | si->err_type = SI_ET_CONN_ERR; |
| 3947 | |
| 3948 | si->state = SI_ST_TAR; |
| 3949 | si->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3950 | return 0; |
| 3951 | } |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 3952 | return 0; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3953 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3954 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3955 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3956 | /* |
| 3957 | * 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] | 3958 | * 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] | 3959 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 3960 | * 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] | 3961 | * 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] | 3962 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3963 | */ |
| 3964 | int produce_content(struct session *s) |
| 3965 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3966 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3967 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3968 | return 1; |
| 3969 | } |
| 3970 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3971 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3972 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3973 | if (ret >= 0) |
| 3974 | return ret; |
| 3975 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3976 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3977 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3978 | /* unknown data source or internal error */ |
| 3979 | s->txn.status = 500; |
| 3980 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3981 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3982 | if (!(s->flags & SN_ERR_MASK)) |
| 3983 | s->flags |= SN_ERR_PRXCOND; |
| 3984 | if (!(s->flags & SN_FINST_MASK)) |
| 3985 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3986 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3987 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3991 | /* Iterate the same filter through all request headers. |
| 3992 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3993 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3994 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3995 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3996 | 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] | 3997 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3998 | char term; |
| 3999 | char *cur_ptr, *cur_end, *cur_next; |
| 4000 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4001 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4002 | struct hdr_idx_elem *cur_hdr; |
| 4003 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4004 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4005 | last_hdr = 0; |
| 4006 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4007 | 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] | 4008 | old_idx = 0; |
| 4009 | |
| 4010 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4011 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4012 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4013 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4014 | (exp->action == ACT_ALLOW || |
| 4015 | exp->action == ACT_DENY || |
| 4016 | exp->action == ACT_TARPIT)) |
| 4017 | return 0; |
| 4018 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4019 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4020 | if (!cur_idx) |
| 4021 | break; |
| 4022 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4023 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4024 | cur_ptr = cur_next; |
| 4025 | cur_end = cur_ptr + cur_hdr->len; |
| 4026 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4027 | |
| 4028 | /* Now we have one header between cur_ptr and cur_end, |
| 4029 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4030 | */ |
| 4031 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4032 | /* The annoying part is that pattern matching needs |
| 4033 | * that we modify the contents to null-terminate all |
| 4034 | * strings before testing them. |
| 4035 | */ |
| 4036 | |
| 4037 | term = *cur_end; |
| 4038 | *cur_end = '\0'; |
| 4039 | |
| 4040 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4041 | switch (exp->action) { |
| 4042 | case ACT_SETBE: |
| 4043 | /* It is not possible to jump a second time. |
| 4044 | * FIXME: should we return an HTTP/500 here so that |
| 4045 | * the admin knows there's a problem ? |
| 4046 | */ |
| 4047 | if (t->be != t->fe) |
| 4048 | break; |
| 4049 | |
| 4050 | /* Swithing Proxy */ |
| 4051 | t->be = (struct proxy *) exp->replace; |
| 4052 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4053 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4054 | * because we have associated req_cap and rsp_cap to the |
| 4055 | * frontend, and the beconn will be updated later. |
| 4056 | */ |
| 4057 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4058 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4059 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4060 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4061 | last_hdr = 1; |
| 4062 | break; |
| 4063 | |
| 4064 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4065 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4066 | last_hdr = 1; |
| 4067 | break; |
| 4068 | |
| 4069 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4070 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4071 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4072 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4073 | break; |
| 4074 | |
| 4075 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4076 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4077 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4078 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4079 | break; |
| 4080 | |
| 4081 | case ACT_REPLACE: |
| 4082 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4083 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4084 | /* FIXME: if the user adds a newline in the replacement, the |
| 4085 | * index will not be recalculated for now, and the new line |
| 4086 | * will not be counted as a new header. |
| 4087 | */ |
| 4088 | |
| 4089 | cur_end += delta; |
| 4090 | cur_next += delta; |
| 4091 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4092 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4093 | break; |
| 4094 | |
| 4095 | case ACT_REMOVE: |
| 4096 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4097 | cur_next += delta; |
| 4098 | |
| 4099 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4100 | txn->req.eoh += delta; |
| 4101 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4102 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4103 | cur_hdr->len = 0; |
| 4104 | cur_end = NULL; /* null-term has been rewritten */ |
| 4105 | break; |
| 4106 | |
| 4107 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4108 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4109 | if (cur_end) |
| 4110 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4111 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4112 | /* keep the link from this header to next one in case of later |
| 4113 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4114 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4115 | old_idx = cur_idx; |
| 4116 | } |
| 4117 | return 0; |
| 4118 | } |
| 4119 | |
| 4120 | |
| 4121 | /* Apply the filter to the request line. |
| 4122 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4123 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4124 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4125 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4126 | */ |
| 4127 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4128 | { |
| 4129 | char term; |
| 4130 | char *cur_ptr, *cur_end; |
| 4131 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4132 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4133 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4134 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4135 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4136 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4137 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4138 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4139 | (exp->action == ACT_ALLOW || |
| 4140 | exp->action == ACT_DENY || |
| 4141 | exp->action == ACT_TARPIT)) |
| 4142 | return 0; |
| 4143 | else if (exp->action == ACT_REMOVE) |
| 4144 | return 0; |
| 4145 | |
| 4146 | done = 0; |
| 4147 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4148 | 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] | 4149 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4150 | |
| 4151 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4152 | |
| 4153 | /* The annoying part is that pattern matching needs |
| 4154 | * that we modify the contents to null-terminate all |
| 4155 | * strings before testing them. |
| 4156 | */ |
| 4157 | |
| 4158 | term = *cur_end; |
| 4159 | *cur_end = '\0'; |
| 4160 | |
| 4161 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4162 | switch (exp->action) { |
| 4163 | case ACT_SETBE: |
| 4164 | /* It is not possible to jump a second time. |
| 4165 | * FIXME: should we return an HTTP/500 here so that |
| 4166 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4167 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4168 | if (t->be != t->fe) |
| 4169 | break; |
| 4170 | |
| 4171 | /* Swithing Proxy */ |
| 4172 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4173 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4174 | /* right now, the backend switch is not too much complicated |
| 4175 | * because we have associated req_cap and rsp_cap to the |
| 4176 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4177 | */ |
| 4178 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4179 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4180 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4181 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4182 | done = 1; |
| 4183 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4184 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4185 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4186 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4187 | done = 1; |
| 4188 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4189 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4190 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4191 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4192 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4193 | done = 1; |
| 4194 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4195 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4196 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4197 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4198 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4199 | done = 1; |
| 4200 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4201 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4202 | case ACT_REPLACE: |
| 4203 | *cur_end = term; /* restore the string terminator */ |
| 4204 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4205 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4206 | /* FIXME: if the user adds a newline in the replacement, the |
| 4207 | * index will not be recalculated for now, and the new line |
| 4208 | * will not be counted as a new header. |
| 4209 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4210 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4211 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4212 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4213 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4214 | 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] | 4215 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4216 | HTTP_MSG_RQMETH, |
| 4217 | cur_ptr, cur_end + 1, |
| 4218 | NULL, NULL); |
| 4219 | if (unlikely(!cur_end)) |
| 4220 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4221 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4222 | /* we have a full request and we know that we have either a CR |
| 4223 | * or an LF at <ptr>. |
| 4224 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4225 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4226 | 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] | 4227 | /* there is no point trying this regex on headers */ |
| 4228 | return 1; |
| 4229 | } |
| 4230 | } |
| 4231 | *cur_end = term; /* restore the string terminator */ |
| 4232 | return done; |
| 4233 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4234 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4235 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4236 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4237 | /* |
| 4238 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4239 | * 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] | 4240 | * unparsable request. Since it can manage the switch to another backend, it |
| 4241 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4242 | */ |
| 4243 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4244 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4245 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4246 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4247 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4248 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4249 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4250 | /* |
| 4251 | * The interleaving of transformations and verdicts |
| 4252 | * makes it difficult to decide to continue or stop |
| 4253 | * the evaluation. |
| 4254 | */ |
| 4255 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4256 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4257 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4258 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4259 | exp = exp->next; |
| 4260 | continue; |
| 4261 | } |
| 4262 | |
| 4263 | /* Apply the filter to the request line. */ |
| 4264 | ret = apply_filter_to_req_line(t, req, exp); |
| 4265 | if (unlikely(ret < 0)) |
| 4266 | return -1; |
| 4267 | |
| 4268 | if (likely(ret == 0)) { |
| 4269 | /* The filter did not match the request, it can be |
| 4270 | * iterated through all headers. |
| 4271 | */ |
| 4272 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4273 | } |
| 4274 | exp = exp->next; |
| 4275 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4276 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4280 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4281 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4282 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4283 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4284 | */ |
| 4285 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4286 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4287 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4288 | char *p1, *p2, *p3, *p4; |
| 4289 | char *del_colon, *del_cookie, *colon; |
| 4290 | int app_cookies; |
| 4291 | |
| 4292 | appsess *asession_temp = NULL; |
| 4293 | appsess local_asession; |
| 4294 | |
| 4295 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4296 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4297 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4298 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4299 | * we start with the start line. |
| 4300 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4301 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4302 | 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] | 4303 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4304 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4305 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4306 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4307 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4308 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4309 | cur_ptr = cur_next; |
| 4310 | cur_end = cur_ptr + cur_hdr->len; |
| 4311 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4312 | |
| 4313 | /* We have one full header between cur_ptr and cur_end, and the |
| 4314 | * next header starts at cur_next. We're only interested in |
| 4315 | * "Cookie:" headers. |
| 4316 | */ |
| 4317 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4318 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4319 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4320 | old_idx = cur_idx; |
| 4321 | continue; |
| 4322 | } |
| 4323 | |
| 4324 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4325 | * attributes whose name begin with a '$', and associate them with |
| 4326 | * the right cookie, if we want to delete this cookie. |
| 4327 | * So there are 3 cases for each cookie read : |
| 4328 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4329 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4330 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4331 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4332 | * "special" cookie. |
| 4333 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4334 | * remove it. If no application cookie persists in the header, we |
| 4335 | * *MUST* delete it |
| 4336 | */ |
| 4337 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4338 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4339 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4340 | /* del_cookie == NULL => nothing to be deleted */ |
| 4341 | del_colon = del_cookie = NULL; |
| 4342 | app_cookies = 0; |
| 4343 | |
| 4344 | while (p1 < cur_end) { |
| 4345 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4346 | while (p1 < cur_end) { |
| 4347 | if (*p1 == ';' || *p1 == ',') |
| 4348 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4349 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4350 | break; |
| 4351 | p1++; |
| 4352 | } |
| 4353 | |
| 4354 | if (p1 == cur_end) |
| 4355 | break; |
| 4356 | |
| 4357 | /* p1 is at the beginning of the cookie name */ |
| 4358 | p2 = p1; |
| 4359 | while (p2 < cur_end && *p2 != '=') |
| 4360 | p2++; |
| 4361 | |
| 4362 | if (p2 == cur_end) |
| 4363 | break; |
| 4364 | |
| 4365 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4366 | if (p3 == cur_end) |
| 4367 | break; |
| 4368 | |
| 4369 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4370 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4371 | p4++; |
| 4372 | |
| 4373 | /* here, we have the cookie name between p1 and p2, |
| 4374 | * and its value between p3 and p4. |
| 4375 | * we can process it : |
| 4376 | * |
| 4377 | * Cookie: NAME=VALUE; |
| 4378 | * | || || | |
| 4379 | * | || || +--> p4 |
| 4380 | * | || |+-------> p3 |
| 4381 | * | || +--------> p2 |
| 4382 | * | |+------------> p1 |
| 4383 | * | +-------------> colon |
| 4384 | * +--------------------> cur_ptr |
| 4385 | */ |
| 4386 | |
| 4387 | if (*p1 == '$') { |
| 4388 | /* skip this one */ |
| 4389 | } |
| 4390 | else { |
| 4391 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4392 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4393 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4394 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4395 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4396 | int log_len = p4 - p1; |
| 4397 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4398 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4399 | Alert("HTTP logging : out of memory.\n"); |
| 4400 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4401 | if (log_len > t->fe->capture_len) |
| 4402 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4403 | memcpy(txn->cli_cookie, p1, log_len); |
| 4404 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4408 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4409 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4410 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4411 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4412 | char *delim; |
| 4413 | |
| 4414 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4415 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4416 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4417 | * |
| 4418 | * Cookie: NAME=SRV~VALUE; |
| 4419 | * | || || | | |
| 4420 | * | || || | +--> p4 |
| 4421 | * | || || +--------> delim |
| 4422 | * | || |+-----------> p3 |
| 4423 | * | || +------------> p2 |
| 4424 | * | |+----------------> p1 |
| 4425 | * | +-----------------> colon |
| 4426 | * +------------------------> cur_ptr |
| 4427 | */ |
| 4428 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4429 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4430 | for (delim = p3; delim < p4; delim++) |
| 4431 | if (*delim == COOKIE_DELIM) |
| 4432 | break; |
| 4433 | } |
| 4434 | else |
| 4435 | delim = p4; |
| 4436 | |
| 4437 | |
| 4438 | /* Here, we'll look for the first running server which supports the cookie. |
| 4439 | * This allows to share a same cookie between several servers, for example |
| 4440 | * to dedicate backup servers to specific servers only. |
| 4441 | * However, to prevent clients from sticking to cookie-less backup server |
| 4442 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4443 | * empty cookies and mark them as invalid. |
| 4444 | */ |
| 4445 | if (delim == p3) |
| 4446 | srv = NULL; |
| 4447 | |
| 4448 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4449 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4450 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4451 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4452 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4453 | txn->flags &= ~TX_CK_MASK; |
| 4454 | txn->flags |= TX_CK_VALID; |
| 4455 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4456 | t->srv = srv; |
| 4457 | break; |
| 4458 | } else { |
| 4459 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4460 | txn->flags &= ~TX_CK_MASK; |
| 4461 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4462 | } |
| 4463 | } |
| 4464 | srv = srv->next; |
| 4465 | } |
| 4466 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4467 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4468 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4469 | txn->flags &= ~TX_CK_MASK; |
| 4470 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | /* depending on the cookie mode, we may have to either : |
| 4474 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4475 | * the server never sees it ; |
| 4476 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4477 | * application cookie so that it does not get accidentely removed later, |
| 4478 | * if we're in cookie prefix mode |
| 4479 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4480 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4481 | int delta; /* negative */ |
| 4482 | |
| 4483 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4484 | p4 += delta; |
| 4485 | cur_end += delta; |
| 4486 | cur_next += delta; |
| 4487 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4488 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4489 | |
| 4490 | del_cookie = del_colon = NULL; |
| 4491 | app_cookies++; /* protect the header from deletion */ |
| 4492 | } |
| 4493 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4494 | (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] | 4495 | del_cookie = p1; |
| 4496 | del_colon = colon; |
| 4497 | } |
| 4498 | } else { |
| 4499 | /* now we know that we must keep this cookie since it's |
| 4500 | * not ours. But if we wanted to delete our cookie |
| 4501 | * earlier, we cannot remove the complete header, but we |
| 4502 | * can remove the previous block itself. |
| 4503 | */ |
| 4504 | app_cookies++; |
| 4505 | |
| 4506 | if (del_cookie != NULL) { |
| 4507 | int delta; /* negative */ |
| 4508 | |
| 4509 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4510 | p4 += delta; |
| 4511 | cur_end += delta; |
| 4512 | cur_next += delta; |
| 4513 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4514 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4515 | del_cookie = del_colon = NULL; |
| 4516 | } |
| 4517 | } |
| 4518 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4519 | if ((t->be->appsession_name != NULL) && |
| 4520 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4521 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4522 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4523 | /* Cool... it's the right one */ |
| 4524 | |
| 4525 | asession_temp = &local_asession; |
| 4526 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4527 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4528 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4529 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4530 | return; |
| 4531 | } |
| 4532 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4533 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4534 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4535 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4536 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4537 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4538 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4539 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4540 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4541 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4542 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4543 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4544 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4545 | return; |
| 4546 | } |
| 4547 | |
| 4548 | asession_temp->sessid = local_asession.sessid; |
| 4549 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4550 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4551 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4552 | } else { |
| 4553 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4554 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4555 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4556 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4557 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4558 | Alert("Found Application Session without matching server.\n"); |
| 4559 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4560 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4561 | while (srv) { |
| 4562 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4563 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4564 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4565 | txn->flags &= ~TX_CK_MASK; |
| 4566 | txn->flags |= TX_CK_VALID; |
| 4567 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4568 | t->srv = srv; |
| 4569 | break; |
| 4570 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4571 | txn->flags &= ~TX_CK_MASK; |
| 4572 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4573 | } |
| 4574 | } |
| 4575 | srv = srv->next; |
| 4576 | }/* end while(srv) */ |
| 4577 | }/* end else if server == NULL */ |
| 4578 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4579 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4580 | asession_temp->request_count++; |
| 4581 | #if defined(DEBUG_HASH) |
| 4582 | Alert("manage_client_side_cookies\n"); |
| 4583 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4584 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4585 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4586 | } |
| 4587 | |
| 4588 | /* we'll have to look for another cookie ... */ |
| 4589 | p1 = p4; |
| 4590 | } /* while (p1 < cur_end) */ |
| 4591 | |
| 4592 | /* There's no more cookie on this line. |
| 4593 | * We may have marked the last one(s) for deletion. |
| 4594 | * We must do this now in two ways : |
| 4595 | * - if there is no app cookie, we simply delete the header ; |
| 4596 | * - if there are app cookies, we must delete the end of the |
| 4597 | * string properly, including the colon/semi-colon before |
| 4598 | * the cookie name. |
| 4599 | */ |
| 4600 | if (del_cookie != NULL) { |
| 4601 | int delta; |
| 4602 | if (app_cookies) { |
| 4603 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4604 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4605 | cur_hdr->len += delta; |
| 4606 | } else { |
| 4607 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4608 | |
| 4609 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4610 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4611 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4612 | cur_hdr->len = 0; |
| 4613 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4614 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4615 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | /* keep the link from this header to next one */ |
| 4619 | old_idx = cur_idx; |
| 4620 | } /* end of cookie processing on this header */ |
| 4621 | } |
| 4622 | |
| 4623 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4624 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4625 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4626 | */ |
| 4627 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4628 | { |
| 4629 | char term; |
| 4630 | char *cur_ptr, *cur_end, *cur_next; |
| 4631 | int cur_idx, old_idx, last_hdr; |
| 4632 | struct http_txn *txn = &t->txn; |
| 4633 | struct hdr_idx_elem *cur_hdr; |
| 4634 | int len, delta; |
| 4635 | |
| 4636 | last_hdr = 0; |
| 4637 | |
| 4638 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4639 | old_idx = 0; |
| 4640 | |
| 4641 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4642 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4643 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4644 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4645 | (exp->action == ACT_ALLOW || |
| 4646 | exp->action == ACT_DENY)) |
| 4647 | return 0; |
| 4648 | |
| 4649 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4650 | if (!cur_idx) |
| 4651 | break; |
| 4652 | |
| 4653 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4654 | cur_ptr = cur_next; |
| 4655 | cur_end = cur_ptr + cur_hdr->len; |
| 4656 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4657 | |
| 4658 | /* Now we have one header between cur_ptr and cur_end, |
| 4659 | * and the next header starts at cur_next. |
| 4660 | */ |
| 4661 | |
| 4662 | /* The annoying part is that pattern matching needs |
| 4663 | * that we modify the contents to null-terminate all |
| 4664 | * strings before testing them. |
| 4665 | */ |
| 4666 | |
| 4667 | term = *cur_end; |
| 4668 | *cur_end = '\0'; |
| 4669 | |
| 4670 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4671 | switch (exp->action) { |
| 4672 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4673 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4674 | last_hdr = 1; |
| 4675 | break; |
| 4676 | |
| 4677 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4678 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4679 | last_hdr = 1; |
| 4680 | break; |
| 4681 | |
| 4682 | case ACT_REPLACE: |
| 4683 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4684 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4685 | /* FIXME: if the user adds a newline in the replacement, the |
| 4686 | * index will not be recalculated for now, and the new line |
| 4687 | * will not be counted as a new header. |
| 4688 | */ |
| 4689 | |
| 4690 | cur_end += delta; |
| 4691 | cur_next += delta; |
| 4692 | cur_hdr->len += delta; |
| 4693 | txn->rsp.eoh += delta; |
| 4694 | break; |
| 4695 | |
| 4696 | case ACT_REMOVE: |
| 4697 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4698 | cur_next += delta; |
| 4699 | |
| 4700 | /* FIXME: this should be a separate function */ |
| 4701 | txn->rsp.eoh += delta; |
| 4702 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4703 | txn->hdr_idx.used--; |
| 4704 | cur_hdr->len = 0; |
| 4705 | cur_end = NULL; /* null-term has been rewritten */ |
| 4706 | break; |
| 4707 | |
| 4708 | } |
| 4709 | } |
| 4710 | if (cur_end) |
| 4711 | *cur_end = term; /* restore the string terminator */ |
| 4712 | |
| 4713 | /* keep the link from this header to next one in case of later |
| 4714 | * removal of next header. |
| 4715 | */ |
| 4716 | old_idx = cur_idx; |
| 4717 | } |
| 4718 | return 0; |
| 4719 | } |
| 4720 | |
| 4721 | |
| 4722 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4723 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4724 | * or -1 if a replacement resulted in an invalid status line. |
| 4725 | */ |
| 4726 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4727 | { |
| 4728 | char term; |
| 4729 | char *cur_ptr, *cur_end; |
| 4730 | int done; |
| 4731 | struct http_txn *txn = &t->txn; |
| 4732 | int len, delta; |
| 4733 | |
| 4734 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4735 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4736 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4737 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4738 | (exp->action == ACT_ALLOW || |
| 4739 | exp->action == ACT_DENY)) |
| 4740 | return 0; |
| 4741 | else if (exp->action == ACT_REMOVE) |
| 4742 | return 0; |
| 4743 | |
| 4744 | done = 0; |
| 4745 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4746 | 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] | 4747 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4748 | |
| 4749 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4750 | |
| 4751 | /* The annoying part is that pattern matching needs |
| 4752 | * that we modify the contents to null-terminate all |
| 4753 | * strings before testing them. |
| 4754 | */ |
| 4755 | |
| 4756 | term = *cur_end; |
| 4757 | *cur_end = '\0'; |
| 4758 | |
| 4759 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4760 | switch (exp->action) { |
| 4761 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4762 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4763 | done = 1; |
| 4764 | break; |
| 4765 | |
| 4766 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4767 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4768 | done = 1; |
| 4769 | break; |
| 4770 | |
| 4771 | case ACT_REPLACE: |
| 4772 | *cur_end = term; /* restore the string terminator */ |
| 4773 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4774 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4775 | /* FIXME: if the user adds a newline in the replacement, the |
| 4776 | * index will not be recalculated for now, and the new line |
| 4777 | * will not be counted as a new header. |
| 4778 | */ |
| 4779 | |
| 4780 | txn->rsp.eoh += delta; |
| 4781 | cur_end += delta; |
| 4782 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4783 | 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] | 4784 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4785 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4786 | cur_ptr, cur_end + 1, |
| 4787 | NULL, NULL); |
| 4788 | if (unlikely(!cur_end)) |
| 4789 | return -1; |
| 4790 | |
| 4791 | /* we have a full respnse and we know that we have either a CR |
| 4792 | * or an LF at <ptr>. |
| 4793 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4794 | 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] | 4795 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4796 | /* there is no point trying this regex on headers */ |
| 4797 | return 1; |
| 4798 | } |
| 4799 | } |
| 4800 | *cur_end = term; /* restore the string terminator */ |
| 4801 | return done; |
| 4802 | } |
| 4803 | |
| 4804 | |
| 4805 | |
| 4806 | /* |
| 4807 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4808 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4809 | * unparsable response. |
| 4810 | */ |
| 4811 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4812 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4813 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4814 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4815 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4816 | int ret; |
| 4817 | |
| 4818 | /* |
| 4819 | * The interleaving of transformations and verdicts |
| 4820 | * makes it difficult to decide to continue or stop |
| 4821 | * the evaluation. |
| 4822 | */ |
| 4823 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4824 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4825 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4826 | exp->action == ACT_PASS)) { |
| 4827 | exp = exp->next; |
| 4828 | continue; |
| 4829 | } |
| 4830 | |
| 4831 | /* Apply the filter to the status line. */ |
| 4832 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4833 | if (unlikely(ret < 0)) |
| 4834 | return -1; |
| 4835 | |
| 4836 | if (likely(ret == 0)) { |
| 4837 | /* The filter did not match the response, it can be |
| 4838 | * iterated through all headers. |
| 4839 | */ |
| 4840 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4841 | } |
| 4842 | exp = exp->next; |
| 4843 | } |
| 4844 | return 0; |
| 4845 | } |
| 4846 | |
| 4847 | |
| 4848 | |
| 4849 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4850 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4851 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4852 | */ |
| 4853 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4854 | { |
| 4855 | struct http_txn *txn = &t->txn; |
| 4856 | char *p1, *p2, *p3, *p4; |
| 4857 | |
| 4858 | appsess *asession_temp = NULL; |
| 4859 | appsess local_asession; |
| 4860 | |
| 4861 | char *cur_ptr, *cur_end, *cur_next; |
| 4862 | int cur_idx, old_idx, delta; |
| 4863 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4864 | /* Iterate through the headers. |
| 4865 | * we start with the start line. |
| 4866 | */ |
| 4867 | old_idx = 0; |
| 4868 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4869 | |
| 4870 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4871 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4872 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4873 | |
| 4874 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4875 | cur_ptr = cur_next; |
| 4876 | cur_end = cur_ptr + cur_hdr->len; |
| 4877 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4878 | |
| 4879 | /* We have one full header between cur_ptr and cur_end, and the |
| 4880 | * next header starts at cur_next. We're only interested in |
| 4881 | * "Cookie:" headers. |
| 4882 | */ |
| 4883 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4884 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4885 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4886 | old_idx = cur_idx; |
| 4887 | continue; |
| 4888 | } |
| 4889 | |
| 4890 | /* 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] | 4891 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4892 | |
| 4893 | |
| 4894 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4895 | if (t->be->cookie_name == NULL && |
| 4896 | t->be->appsession_name == NULL && |
| 4897 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4898 | return; |
| 4899 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4900 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4901 | |
| 4902 | 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] | 4903 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4904 | break; |
| 4905 | |
| 4906 | /* p1 is at the beginning of the cookie name */ |
| 4907 | p2 = p1; |
| 4908 | |
| 4909 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4910 | p2++; |
| 4911 | |
| 4912 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4913 | break; |
| 4914 | |
| 4915 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4916 | if (p3 == cur_end) |
| 4917 | break; |
| 4918 | |
| 4919 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4920 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4921 | p4++; |
| 4922 | |
| 4923 | /* here, we have the cookie name between p1 and p2, |
| 4924 | * and its value between p3 and p4. |
| 4925 | * we can process it. |
| 4926 | */ |
| 4927 | |
| 4928 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4929 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4930 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4931 | (p4 - p1 >= t->be->capture_namelen) && |
| 4932 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4933 | int log_len = p4 - p1; |
| 4934 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4935 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4936 | Alert("HTTP logging : out of memory.\n"); |
| 4937 | } |
| 4938 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4939 | if (log_len > t->be->capture_len) |
| 4940 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4941 | memcpy(txn->srv_cookie, p1, log_len); |
| 4942 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4946 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4947 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4948 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4949 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4950 | |
| 4951 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4952 | * this occurrence because we'll insert another one later. |
| 4953 | * We'll delete it too if the "indirect" option is set and we're in |
| 4954 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4955 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4956 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4957 | /* this header must be deleted */ |
| 4958 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4959 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4960 | txn->hdr_idx.used--; |
| 4961 | cur_hdr->len = 0; |
| 4962 | cur_next += delta; |
| 4963 | txn->rsp.eoh += delta; |
| 4964 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4965 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4966 | } |
| 4967 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4968 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4969 | /* replace bytes p3->p4 with the cookie name associated |
| 4970 | * with this server since we know it. |
| 4971 | */ |
| 4972 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4973 | cur_hdr->len += delta; |
| 4974 | cur_next += delta; |
| 4975 | txn->rsp.eoh += delta; |
| 4976 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4977 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4978 | } |
| 4979 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4980 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4981 | /* insert the cookie name associated with this server |
| 4982 | * before existing cookie, and insert a delimitor between them.. |
| 4983 | */ |
| 4984 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4985 | cur_hdr->len += delta; |
| 4986 | cur_next += delta; |
| 4987 | txn->rsp.eoh += delta; |
| 4988 | |
| 4989 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4990 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4991 | } |
| 4992 | } |
| 4993 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4994 | else if ((t->be->appsession_name != NULL) && |
| 4995 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4996 | |
| 4997 | /* Cool... it's the right one */ |
| 4998 | |
| 4999 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5000 | asession_temp = &local_asession; |
| 5001 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5002 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5003 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5004 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5005 | return; |
| 5006 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5007 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5008 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5009 | asession_temp->serverid = NULL; |
| 5010 | |
| 5011 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5012 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5013 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5014 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5015 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5016 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5017 | return; |
| 5018 | } |
| 5019 | asession_temp->sessid = local_asession.sessid; |
| 5020 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5021 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5022 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5023 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5024 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5025 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5026 | } |
| 5027 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5028 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5029 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5030 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5031 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5032 | return; |
| 5033 | } |
| 5034 | asession_temp->serverid[0] = '\0'; |
| 5035 | } |
| 5036 | |
| 5037 | if (asession_temp->serverid[0] == '\0') |
| 5038 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5039 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5040 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5041 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5042 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5043 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5044 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5045 | #endif |
| 5046 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5047 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5048 | } /* we're now at the end of the cookie value */ |
| 5049 | |
| 5050 | /* keep the link from this header to next one */ |
| 5051 | old_idx = cur_idx; |
| 5052 | } /* end of cookie processing on this header */ |
| 5053 | } |
| 5054 | |
| 5055 | |
| 5056 | |
| 5057 | /* |
| 5058 | * Check if response is cacheable or not. Updates t->flags. |
| 5059 | */ |
| 5060 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5061 | { |
| 5062 | struct http_txn *txn = &t->txn; |
| 5063 | char *p1, *p2; |
| 5064 | |
| 5065 | char *cur_ptr, *cur_end, *cur_next; |
| 5066 | int cur_idx; |
| 5067 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5068 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5069 | return; |
| 5070 | |
| 5071 | /* Iterate through the headers. |
| 5072 | * we start with the start line. |
| 5073 | */ |
| 5074 | cur_idx = 0; |
| 5075 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5076 | |
| 5077 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5078 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5079 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5080 | |
| 5081 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5082 | cur_ptr = cur_next; |
| 5083 | cur_end = cur_ptr + cur_hdr->len; |
| 5084 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5085 | |
| 5086 | /* We have one full header between cur_ptr and cur_end, and the |
| 5087 | * next header starts at cur_next. We're only interested in |
| 5088 | * "Cookie:" headers. |
| 5089 | */ |
| 5090 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5091 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5092 | if (val) { |
| 5093 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5094 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5095 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5096 | return; |
| 5097 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5098 | } |
| 5099 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5100 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5101 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5102 | continue; |
| 5103 | |
| 5104 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5105 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5106 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5107 | |
| 5108 | if (p1 >= cur_end) /* no more info */ |
| 5109 | continue; |
| 5110 | |
| 5111 | /* p1 is at the beginning of the value */ |
| 5112 | p2 = p1; |
| 5113 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5114 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5115 | p2++; |
| 5116 | |
| 5117 | /* we have a complete value between p1 and p2 */ |
| 5118 | if (p2 < cur_end && *p2 == '=') { |
| 5119 | /* we have something of the form no-cache="set-cookie" */ |
| 5120 | if ((cur_end - p1 >= 21) && |
| 5121 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5122 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5123 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5124 | continue; |
| 5125 | } |
| 5126 | |
| 5127 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5128 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5129 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5130 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5131 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5132 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5133 | return; |
| 5134 | } |
| 5135 | |
| 5136 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5137 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5138 | continue; |
| 5139 | } |
| 5140 | } |
| 5141 | } |
| 5142 | |
| 5143 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5144 | /* |
| 5145 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5146 | * If the server is found, it's assigned to the session. |
| 5147 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5148 | 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] | 5149 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5150 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5151 | appsess *asession_temp = NULL; |
| 5152 | appsess local_asession; |
| 5153 | char *request_line; |
| 5154 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5155 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5156 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5157 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5158 | ((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] | 5159 | return; |
| 5160 | |
| 5161 | /* skip ';' */ |
| 5162 | request_line++; |
| 5163 | |
| 5164 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5165 | 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] | 5166 | return; |
| 5167 | |
| 5168 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5169 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5170 | |
| 5171 | /* First try if we already have an appsession */ |
| 5172 | asession_temp = &local_asession; |
| 5173 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5174 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5175 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5176 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5177 | return; |
| 5178 | } |
| 5179 | |
| 5180 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5181 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5182 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5183 | asession_temp->serverid = NULL; |
| 5184 | |
| 5185 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5186 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5187 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5188 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5189 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5190 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5191 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5192 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5193 | return; |
| 5194 | } |
| 5195 | asession_temp->sessid = local_asession.sessid; |
| 5196 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5197 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5198 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5199 | } |
| 5200 | else { |
| 5201 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5202 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5203 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5204 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5205 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5206 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5207 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5208 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5209 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5210 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5211 | #endif |
| 5212 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5213 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5214 | Alert("Found Application Session without matching server.\n"); |
| 5215 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5216 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5217 | while (srv) { |
| 5218 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5219 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5220 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5221 | txn->flags &= ~TX_CK_MASK; |
| 5222 | txn->flags |= TX_CK_VALID; |
| 5223 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5224 | t->srv = srv; |
| 5225 | break; |
| 5226 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5227 | txn->flags &= ~TX_CK_MASK; |
| 5228 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5229 | } |
| 5230 | } |
| 5231 | srv = srv->next; |
| 5232 | } |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5237 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5238 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5239 | * 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] | 5240 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5241 | * 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] | 5242 | * 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] | 5243 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5244 | * |
| 5245 | * Returns 1 if the session's state changes, otherwise 0. |
| 5246 | */ |
| 5247 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5248 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5249 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5250 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5251 | struct user_auth *user; |
| 5252 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5253 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5254 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5255 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5256 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5257 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5258 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5259 | return 0; |
| 5260 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5261 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5262 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5263 | /* the URI is in h */ |
| 5264 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5265 | return 0; |
| 5266 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5267 | h += uri_auth->uri_len; |
| 5268 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5269 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5270 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5271 | break; |
| 5272 | } |
| 5273 | h++; |
| 5274 | } |
| 5275 | |
| 5276 | if (uri_auth->refresh) { |
| 5277 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5278 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5279 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5280 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5281 | break; |
| 5282 | } |
| 5283 | h++; |
| 5284 | } |
| 5285 | } |
| 5286 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5287 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5288 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5289 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5290 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5291 | break; |
| 5292 | } |
| 5293 | h++; |
| 5294 | } |
| 5295 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5296 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5297 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5298 | /* we are in front of a interceptable URI. Let's check |
| 5299 | * if there's an authentication and if it's valid. |
| 5300 | */ |
| 5301 | user = uri_auth->users; |
| 5302 | if (!user) { |
| 5303 | /* no user auth required, it's OK */ |
| 5304 | authenticated = 1; |
| 5305 | } else { |
| 5306 | authenticated = 0; |
| 5307 | |
| 5308 | /* a user list is defined, we have to check. |
| 5309 | * skip 21 chars for "Authorization: Basic ". |
| 5310 | */ |
| 5311 | |
| 5312 | /* FIXME: this should move to an earlier place */ |
| 5313 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5314 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5315 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5316 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5317 | if (len > 14 && |
| 5318 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5319 | txn->auth_hdr.str = h; |
| 5320 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5321 | break; |
| 5322 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5323 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5326 | if (txn->auth_hdr.len < 21 || |
| 5327 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5328 | user = NULL; |
| 5329 | |
| 5330 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5331 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5332 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5333 | user->user_pwd, user->user_len)) { |
| 5334 | authenticated = 1; |
| 5335 | break; |
| 5336 | } |
| 5337 | user = user->next; |
| 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5342 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5343 | |
| 5344 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5345 | msg.str = trash; |
| 5346 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5347 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5348 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5349 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5350 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5351 | if (!(t->flags & SN_ERR_MASK)) |
| 5352 | t->flags |= SN_ERR_PRXCOND; |
| 5353 | if (!(t->flags & SN_FINST_MASK)) |
| 5354 | t->flags |= SN_FINST_R; |
| 5355 | return 1; |
| 5356 | } |
| 5357 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5358 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5359 | * data. |
| 5360 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 5361 | buffer_write_dis(t->req); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 5362 | buffer_shutw_now(t->req); |
| 5363 | buffer_shutr_now(t->rep); |
| 5364 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5365 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5366 | t->data_source = DATA_SRC_STATS; |
| 5367 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5368 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5369 | produce_content(t); |
| 5370 | return 1; |
| 5371 | } |
| 5372 | |
| 5373 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5374 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5375 | * Print a debug line with a header |
| 5376 | */ |
| 5377 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5378 | { |
| 5379 | int len, max; |
| 5380 | 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] | 5381 | 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] | 5382 | max = end - start; |
| 5383 | UBOUND(max, sizeof(trash) - len - 1); |
| 5384 | len += strlcpy2(trash + len, start, max + 1); |
| 5385 | trash[len++] = '\n'; |
| 5386 | write(1, trash, len); |
| 5387 | } |
| 5388 | |
| 5389 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5390 | /************************************************************************/ |
| 5391 | /* The code below is dedicated to ACL parsing and matching */ |
| 5392 | /************************************************************************/ |
| 5393 | |
| 5394 | |
| 5395 | |
| 5396 | |
| 5397 | /* 1. Check on METHOD |
| 5398 | * We use the pre-parsed method if it is known, and store its number as an |
| 5399 | * integer. If it is unknown, we use the pointer and the length. |
| 5400 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5401 | 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] | 5402 | { |
| 5403 | int len, meth; |
| 5404 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5405 | len = strlen(*text); |
| 5406 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5407 | |
| 5408 | pattern->val.i = meth; |
| 5409 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5410 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5411 | if (!pattern->ptr.str) |
| 5412 | return 0; |
| 5413 | pattern->len = len; |
| 5414 | } |
| 5415 | return 1; |
| 5416 | } |
| 5417 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5418 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5419 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5420 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5421 | { |
| 5422 | int meth; |
| 5423 | struct http_txn *txn = l7; |
| 5424 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5425 | if (!txn) |
| 5426 | return 0; |
| 5427 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5428 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5429 | return 0; |
| 5430 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5431 | meth = txn->meth; |
| 5432 | test->i = meth; |
| 5433 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5434 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5435 | /* ensure the indexes are not affected */ |
| 5436 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5437 | test->len = txn->req.sl.rq.m_l; |
| 5438 | test->ptr = txn->req.sol; |
| 5439 | } |
| 5440 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5441 | return 1; |
| 5442 | } |
| 5443 | |
| 5444 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5445 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5446 | int icase; |
| 5447 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5448 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5449 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5450 | |
| 5451 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5452 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5453 | |
| 5454 | /* Other method, we must compare the strings */ |
| 5455 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5456 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5457 | |
| 5458 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5459 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5460 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5461 | return ACL_PAT_FAIL; |
| 5462 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | /* 2. Check on Request/Status Version |
| 5466 | * We simply compare strings here. |
| 5467 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5468 | 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] | 5469 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5470 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5471 | if (!pattern->ptr.str) |
| 5472 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5473 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5474 | return 1; |
| 5475 | } |
| 5476 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5477 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5478 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5479 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5480 | { |
| 5481 | struct http_txn *txn = l7; |
| 5482 | char *ptr; |
| 5483 | int len; |
| 5484 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5485 | if (!txn) |
| 5486 | return 0; |
| 5487 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5488 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5489 | return 0; |
| 5490 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5491 | len = txn->req.sl.rq.v_l; |
| 5492 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5493 | |
| 5494 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5495 | if (len <= 0) |
| 5496 | return 0; |
| 5497 | |
| 5498 | test->ptr = ptr; |
| 5499 | test->len = len; |
| 5500 | |
| 5501 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5502 | return 1; |
| 5503 | } |
| 5504 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5505 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5506 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5507 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5508 | { |
| 5509 | struct http_txn *txn = l7; |
| 5510 | char *ptr; |
| 5511 | int len; |
| 5512 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5513 | if (!txn) |
| 5514 | return 0; |
| 5515 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5516 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5517 | return 0; |
| 5518 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5519 | len = txn->rsp.sl.st.v_l; |
| 5520 | ptr = txn->rsp.sol; |
| 5521 | |
| 5522 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5523 | if (len <= 0) |
| 5524 | return 0; |
| 5525 | |
| 5526 | test->ptr = ptr; |
| 5527 | test->len = len; |
| 5528 | |
| 5529 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5530 | return 1; |
| 5531 | } |
| 5532 | |
| 5533 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5534 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5535 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5536 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5537 | { |
| 5538 | struct http_txn *txn = l7; |
| 5539 | char *ptr; |
| 5540 | int len; |
| 5541 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5542 | if (!txn) |
| 5543 | return 0; |
| 5544 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5545 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5546 | return 0; |
| 5547 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5548 | len = txn->rsp.sl.st.c_l; |
| 5549 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5550 | |
| 5551 | test->i = __strl2ui(ptr, len); |
| 5552 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5553 | return 1; |
| 5554 | } |
| 5555 | |
| 5556 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5557 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5558 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5559 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5560 | { |
| 5561 | struct http_txn *txn = l7; |
| 5562 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5563 | if (!txn) |
| 5564 | return 0; |
| 5565 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5566 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5567 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5568 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5569 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5570 | /* ensure the indexes are not affected */ |
| 5571 | return 0; |
| 5572 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5573 | test->len = txn->req.sl.rq.u_l; |
| 5574 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5575 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5576 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5577 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5578 | return 1; |
| 5579 | } |
| 5580 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5581 | static int |
| 5582 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5583 | struct acl_expr *expr, struct acl_test *test) |
| 5584 | { |
| 5585 | struct http_txn *txn = l7; |
| 5586 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5587 | if (!txn) |
| 5588 | return 0; |
| 5589 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5590 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5591 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5592 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5593 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5594 | /* ensure the indexes are not affected */ |
| 5595 | return 0; |
| 5596 | |
| 5597 | /* Parse HTTP request */ |
| 5598 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5599 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5600 | test->i = AF_INET; |
| 5601 | |
| 5602 | /* |
| 5603 | * If we are parsing url in frontend space, we prepare backend stage |
| 5604 | * to not parse again the same url ! optimization lazyness... |
| 5605 | */ |
| 5606 | if (px->options & PR_O_HTTP_PROXY) |
| 5607 | l4->flags |= SN_ADDR_SET; |
| 5608 | |
| 5609 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5610 | return 1; |
| 5611 | } |
| 5612 | |
| 5613 | static int |
| 5614 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5615 | struct acl_expr *expr, struct acl_test *test) |
| 5616 | { |
| 5617 | struct http_txn *txn = l7; |
| 5618 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5619 | if (!txn) |
| 5620 | return 0; |
| 5621 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5622 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5623 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5624 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5625 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5626 | /* ensure the indexes are not affected */ |
| 5627 | return 0; |
| 5628 | |
| 5629 | /* Same optimization as url_ip */ |
| 5630 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5631 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5632 | |
| 5633 | if (px->options & PR_O_HTTP_PROXY) |
| 5634 | l4->flags |= SN_ADDR_SET; |
| 5635 | |
| 5636 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5637 | return 1; |
| 5638 | } |
| 5639 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5640 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5641 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5642 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5643 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5644 | 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] | 5645 | struct acl_expr *expr, struct acl_test *test) |
| 5646 | { |
| 5647 | struct http_txn *txn = l7; |
| 5648 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5649 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5650 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5651 | if (!txn) |
| 5652 | return 0; |
| 5653 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5654 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5655 | /* search for header from the beginning */ |
| 5656 | ctx->idx = 0; |
| 5657 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5658 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5659 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5660 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5661 | test->len = ctx->vlen; |
| 5662 | test->ptr = (char *)ctx->line + ctx->val; |
| 5663 | return 1; |
| 5664 | } |
| 5665 | |
| 5666 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5667 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5668 | return 0; |
| 5669 | } |
| 5670 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5671 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5672 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5673 | struct acl_expr *expr, struct acl_test *test) |
| 5674 | { |
| 5675 | struct http_txn *txn = l7; |
| 5676 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5677 | if (!txn) |
| 5678 | return 0; |
| 5679 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5680 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5681 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5682 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5683 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5684 | /* ensure the indexes are not affected */ |
| 5685 | return 0; |
| 5686 | |
| 5687 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5688 | } |
| 5689 | |
| 5690 | static int |
| 5691 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5692 | struct acl_expr *expr, struct acl_test *test) |
| 5693 | { |
| 5694 | struct http_txn *txn = l7; |
| 5695 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5696 | if (!txn) |
| 5697 | return 0; |
| 5698 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5699 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5700 | return 0; |
| 5701 | |
| 5702 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5703 | } |
| 5704 | |
| 5705 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5706 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5707 | */ |
| 5708 | static int |
| 5709 | 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] | 5710 | struct acl_expr *expr, struct acl_test *test) |
| 5711 | { |
| 5712 | struct http_txn *txn = l7; |
| 5713 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5714 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5715 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5716 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5717 | if (!txn) |
| 5718 | return 0; |
| 5719 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5720 | ctx.idx = 0; |
| 5721 | cnt = 0; |
| 5722 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5723 | cnt++; |
| 5724 | |
| 5725 | test->i = cnt; |
| 5726 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5727 | return 1; |
| 5728 | } |
| 5729 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5730 | static int |
| 5731 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5732 | struct acl_expr *expr, struct acl_test *test) |
| 5733 | { |
| 5734 | struct http_txn *txn = l7; |
| 5735 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5736 | if (!txn) |
| 5737 | return 0; |
| 5738 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5739 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5740 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5741 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5742 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5743 | /* ensure the indexes are not affected */ |
| 5744 | return 0; |
| 5745 | |
| 5746 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5747 | } |
| 5748 | |
| 5749 | static int |
| 5750 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5751 | struct acl_expr *expr, struct acl_test *test) |
| 5752 | { |
| 5753 | struct http_txn *txn = l7; |
| 5754 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5755 | if (!txn) |
| 5756 | return 0; |
| 5757 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5758 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5759 | return 0; |
| 5760 | |
| 5761 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5762 | } |
| 5763 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5764 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5765 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5766 | * 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] | 5767 | */ |
| 5768 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5769 | 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] | 5770 | struct acl_expr *expr, struct acl_test *test) |
| 5771 | { |
| 5772 | struct http_txn *txn = l7; |
| 5773 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5774 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5775 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5776 | if (!txn) |
| 5777 | return 0; |
| 5778 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5779 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5780 | /* search for header from the beginning */ |
| 5781 | ctx->idx = 0; |
| 5782 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5783 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5784 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5785 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5786 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5787 | return 1; |
| 5788 | } |
| 5789 | |
| 5790 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5791 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5792 | return 0; |
| 5793 | } |
| 5794 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5795 | static int |
| 5796 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5797 | struct acl_expr *expr, struct acl_test *test) |
| 5798 | { |
| 5799 | struct http_txn *txn = l7; |
| 5800 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5801 | if (!txn) |
| 5802 | return 0; |
| 5803 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5804 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5805 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5806 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5807 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5808 | /* ensure the indexes are not affected */ |
| 5809 | return 0; |
| 5810 | |
| 5811 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5812 | } |
| 5813 | |
| 5814 | static int |
| 5815 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5816 | struct acl_expr *expr, struct acl_test *test) |
| 5817 | { |
| 5818 | struct http_txn *txn = l7; |
| 5819 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5820 | if (!txn) |
| 5821 | return 0; |
| 5822 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5823 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5824 | return 0; |
| 5825 | |
| 5826 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5827 | } |
| 5828 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5829 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5830 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5831 | */ |
| 5832 | static int |
| 5833 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5834 | struct acl_expr *expr, struct acl_test *test) |
| 5835 | { |
| 5836 | struct http_txn *txn = l7; |
| 5837 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5838 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5839 | if (!txn) |
| 5840 | return 0; |
| 5841 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5842 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5843 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5844 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5845 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5846 | /* ensure the indexes are not affected */ |
| 5847 | return 0; |
| 5848 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5849 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5850 | ptr = http_get_path(txn); |
| 5851 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5852 | return 0; |
| 5853 | |
| 5854 | /* OK, we got the '/' ! */ |
| 5855 | test->ptr = ptr; |
| 5856 | |
| 5857 | while (ptr < end && *ptr != '?') |
| 5858 | ptr++; |
| 5859 | |
| 5860 | test->len = ptr - test->ptr; |
| 5861 | |
| 5862 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5863 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5864 | return 1; |
| 5865 | } |
| 5866 | |
| 5867 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5868 | |
| 5869 | /************************************************************************/ |
| 5870 | /* All supported keywords must be declared here. */ |
| 5871 | /************************************************************************/ |
| 5872 | |
| 5873 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5874 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5875 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5876 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5877 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5878 | { "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] | 5879 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5880 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5881 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5882 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5883 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5884 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5885 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5886 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5887 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5888 | { "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] | 5889 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5890 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5891 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5892 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5893 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5894 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5895 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5896 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5897 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5898 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5899 | { "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] | 5900 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5901 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5902 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5903 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5904 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5905 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5906 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5907 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5908 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5909 | { "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] | 5910 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5911 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5912 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5913 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5914 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5915 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5916 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5917 | { "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] | 5918 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5919 | { NULL, NULL, NULL, NULL }, |
| 5920 | |
| 5921 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5922 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5923 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5924 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5925 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5926 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5927 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5928 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5929 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5930 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5931 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5932 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5933 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5934 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5935 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5936 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5937 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5938 | |
| 5939 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5940 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5941 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5942 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5943 | { NULL, NULL, NULL, NULL }, |
| 5944 | #endif |
| 5945 | }}; |
| 5946 | |
| 5947 | |
| 5948 | __attribute__((constructor)) |
| 5949 | static void __http_protocol_init(void) |
| 5950 | { |
| 5951 | acl_register_keywords(&acl_kws); |
| 5952 | } |
| 5953 | |
| 5954 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5955 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5956 | * Local variables: |
| 5957 | * c-indent-level: 8 |
| 5958 | * c-basic-offset: 8 |
| 5959 | * End: |
| 5960 | */ |