Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 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> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 44 | #include <proto/checks.h> |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 45 | #include <proto/client.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 46 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/fd.h> |
| 48 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 49 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 50 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 52 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 54 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 56 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 57 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/task.h> |
| 59 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 60 | const char HTTP_100[] = |
| 61 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 62 | |
| 63 | const struct chunk http_100_chunk = { |
| 64 | .str = (char *)&HTTP_100, |
| 65 | .len = sizeof(HTTP_100)-1 |
| 66 | }; |
| 67 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 68 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 69 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 70 | "HTTP/1.0 200 OK\r\n" |
| 71 | "Cache-Control: no-cache\r\n" |
| 72 | "Connection: close\r\n" |
| 73 | "Content-Type: text/html\r\n" |
| 74 | "\r\n" |
| 75 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 76 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 77 | const struct chunk http_200_chunk = { |
| 78 | .str = (char *)&HTTP_200, |
| 79 | .len = sizeof(HTTP_200)-1 |
| 80 | }; |
| 81 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 82 | const char *HTTP_301 = |
| 83 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 84 | "Cache-Control: no-cache\r\n" |
| 85 | "Connection: close\r\n" |
| 86 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 87 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 88 | const char *HTTP_302 = |
| 89 | "HTTP/1.0 302 Found\r\n" |
| 90 | "Cache-Control: no-cache\r\n" |
| 91 | "Connection: close\r\n" |
| 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
| 94 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 95 | const char *HTTP_303 = |
| 96 | "HTTP/1.0 303 See Other\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 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 102 | const char *HTTP_401_fmt = |
| 103 | "HTTP/1.0 401 Unauthorized\r\n" |
| 104 | "Cache-Control: no-cache\r\n" |
| 105 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 106 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 108 | "\r\n" |
| 109 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 110 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 111 | |
| 112 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 113 | [HTTP_ERR_400] = 400, |
| 114 | [HTTP_ERR_403] = 403, |
| 115 | [HTTP_ERR_408] = 408, |
| 116 | [HTTP_ERR_500] = 500, |
| 117 | [HTTP_ERR_502] = 502, |
| 118 | [HTTP_ERR_503] = 503, |
| 119 | [HTTP_ERR_504] = 504, |
| 120 | }; |
| 121 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 122 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 123 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 124 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 125 | "Cache-Control: no-cache\r\n" |
| 126 | "Connection: close\r\n" |
| 127 | "Content-Type: text/html\r\n" |
| 128 | "\r\n" |
| 129 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 130 | |
| 131 | [HTTP_ERR_403] = |
| 132 | "HTTP/1.0 403 Forbidden\r\n" |
| 133 | "Cache-Control: no-cache\r\n" |
| 134 | "Connection: close\r\n" |
| 135 | "Content-Type: text/html\r\n" |
| 136 | "\r\n" |
| 137 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 138 | |
| 139 | [HTTP_ERR_408] = |
| 140 | "HTTP/1.0 408 Request Time-out\r\n" |
| 141 | "Cache-Control: no-cache\r\n" |
| 142 | "Connection: close\r\n" |
| 143 | "Content-Type: text/html\r\n" |
| 144 | "\r\n" |
| 145 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 146 | |
| 147 | [HTTP_ERR_500] = |
| 148 | "HTTP/1.0 500 Server Error\r\n" |
| 149 | "Cache-Control: no-cache\r\n" |
| 150 | "Connection: close\r\n" |
| 151 | "Content-Type: text/html\r\n" |
| 152 | "\r\n" |
| 153 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 154 | |
| 155 | [HTTP_ERR_502] = |
| 156 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 157 | "Cache-Control: no-cache\r\n" |
| 158 | "Connection: close\r\n" |
| 159 | "Content-Type: text/html\r\n" |
| 160 | "\r\n" |
| 161 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 162 | |
| 163 | [HTTP_ERR_503] = |
| 164 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 165 | "Cache-Control: no-cache\r\n" |
| 166 | "Connection: close\r\n" |
| 167 | "Content-Type: text/html\r\n" |
| 168 | "\r\n" |
| 169 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 170 | |
| 171 | [HTTP_ERR_504] = |
| 172 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 173 | "Cache-Control: no-cache\r\n" |
| 174 | "Connection: close\r\n" |
| 175 | "Content-Type: text/html\r\n" |
| 176 | "\r\n" |
| 177 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 178 | |
| 179 | }; |
| 180 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 181 | /* We must put the messages here since GCC cannot initialize consts depending |
| 182 | * on strlen(). |
| 183 | */ |
| 184 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 185 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 186 | #define FD_SETS_ARE_BITFIELDS |
| 187 | #ifdef FD_SETS_ARE_BITFIELDS |
| 188 | /* |
| 189 | * This map is used with all the FD_* macros to check whether a particular bit |
| 190 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 191 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 192 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 193 | * exclusively to the macros. |
| 194 | */ |
| 195 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 196 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 197 | |
| 198 | #else |
| 199 | #error "Check if your OS uses bitfields for fd_sets" |
| 200 | #endif |
| 201 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 202 | void init_proto_http() |
| 203 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 204 | int i; |
| 205 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 206 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 207 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 208 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 209 | if (!http_err_msgs[msg]) { |
| 210 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 211 | abort(); |
| 212 | } |
| 213 | |
| 214 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 215 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 216 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 217 | |
| 218 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 219 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 220 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 221 | * printable characters above. |
| 222 | */ |
| 223 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 224 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 225 | for (i = 0; i < 32; i++) { |
| 226 | FD_SET(i, hdr_encode_map); |
| 227 | FD_SET(i, url_encode_map); |
| 228 | } |
| 229 | for (i = 127; i < 256; i++) { |
| 230 | FD_SET(i, hdr_encode_map); |
| 231 | FD_SET(i, url_encode_map); |
| 232 | } |
| 233 | |
| 234 | tmp = "\"#{|}"; |
| 235 | while (*tmp) { |
| 236 | FD_SET(*tmp, hdr_encode_map); |
| 237 | tmp++; |
| 238 | } |
| 239 | |
| 240 | tmp = "\"#"; |
| 241 | while (*tmp) { |
| 242 | FD_SET(*tmp, url_encode_map); |
| 243 | tmp++; |
| 244 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 245 | |
| 246 | /* memory allocations */ |
| 247 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 248 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 249 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 250 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 251 | /* |
| 252 | * We have 26 list of methods (1 per first letter), each of which can have |
| 253 | * up to 3 entries (2 valid, 1 null). |
| 254 | */ |
| 255 | struct http_method_desc { |
| 256 | http_meth_t meth; |
| 257 | int len; |
| 258 | const char text[8]; |
| 259 | }; |
| 260 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 261 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 262 | ['C' - 'A'] = { |
| 263 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 264 | }, |
| 265 | ['D' - 'A'] = { |
| 266 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 267 | }, |
| 268 | ['G' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 270 | }, |
| 271 | ['H' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 273 | }, |
| 274 | ['P' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 276 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 277 | }, |
| 278 | ['T' - 'A'] = { |
| 279 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 280 | }, |
| 281 | /* rest is empty like this : |
| 282 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 283 | */ |
| 284 | }; |
| 285 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 286 | /* 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] | 287 | * 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] | 288 | * RFC2616 for those chars. |
| 289 | */ |
| 290 | |
| 291 | const char http_is_spht[256] = { |
| 292 | [' '] = 1, ['\t'] = 1, |
| 293 | }; |
| 294 | |
| 295 | const char http_is_crlf[256] = { |
| 296 | ['\r'] = 1, ['\n'] = 1, |
| 297 | }; |
| 298 | |
| 299 | const char http_is_lws[256] = { |
| 300 | [' '] = 1, ['\t'] = 1, |
| 301 | ['\r'] = 1, ['\n'] = 1, |
| 302 | }; |
| 303 | |
| 304 | const char http_is_sep[256] = { |
| 305 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 306 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 307 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 308 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 309 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 310 | }; |
| 311 | |
| 312 | const char http_is_ctl[256] = { |
| 313 | [0 ... 31] = 1, |
| 314 | [127] = 1, |
| 315 | }; |
| 316 | |
| 317 | /* |
| 318 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 319 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 320 | * them correctly. Instead, define every non-CTL char's status. |
| 321 | */ |
| 322 | const char http_is_token[256] = { |
| 323 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 324 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 325 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 326 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 327 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 328 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 329 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 330 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 331 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 332 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 333 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 334 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 335 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 336 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 337 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 338 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 339 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 340 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 341 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 342 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 343 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 344 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 345 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 346 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 347 | }; |
| 348 | |
| 349 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 350 | /* |
| 351 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 352 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 353 | */ |
| 354 | const char http_is_ver_token[256] = { |
| 355 | ['.'] = 1, ['/'] = 1, |
| 356 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 357 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 358 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 359 | }; |
| 360 | |
| 361 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 362 | /* |
| 363 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 364 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 365 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 366 | * of headers is automatically adjusted. The number of bytes added is returned |
| 367 | * on success, otherwise <0 is returned indicating an error. |
| 368 | */ |
| 369 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 370 | struct hdr_idx *hdr_idx, const char *text) |
| 371 | { |
| 372 | int bytes, len; |
| 373 | |
| 374 | len = strlen(text); |
| 375 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 376 | if (!bytes) |
| 377 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 378 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 379 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 384 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 385 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 386 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 387 | * of headers is automatically adjusted. The number of bytes added is returned |
| 388 | * on success, otherwise <0 is returned indicating an error. |
| 389 | */ |
| 390 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 391 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 392 | { |
| 393 | int bytes; |
| 394 | |
| 395 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 396 | if (!bytes) |
| 397 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 398 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 399 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 400 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 401 | |
| 402 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 403 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 404 | * If so, returns the position of the first non-space character relative to |
| 405 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 406 | * to return a pointer to the place after the first space. Returns 0 if the |
| 407 | * header name does not match. Checks are case-insensitive. |
| 408 | */ |
| 409 | int http_header_match2(const char *hdr, const char *end, |
| 410 | const char *name, int len) |
| 411 | { |
| 412 | const char *val; |
| 413 | |
| 414 | if (hdr + len >= end) |
| 415 | return 0; |
| 416 | if (hdr[len] != ':') |
| 417 | return 0; |
| 418 | if (strncasecmp(hdr, name, len) != 0) |
| 419 | return 0; |
| 420 | val = hdr + len + 1; |
| 421 | while (val < end && HTTP_IS_SPHT(*val)) |
| 422 | val++; |
| 423 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 424 | return len + 2; /* we may replace starting from second space */ |
| 425 | return val - hdr; |
| 426 | } |
| 427 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 428 | /* Find the end of the header value contained between <s> and <e>. |
| 429 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 430 | * a valid header to return a valid result. |
| 431 | */ |
| 432 | const char *find_hdr_value_end(const char *s, const char *e) |
| 433 | { |
| 434 | int quoted, qdpair; |
| 435 | |
| 436 | quoted = qdpair = 0; |
| 437 | for (; s < e; s++) { |
| 438 | if (qdpair) qdpair = 0; |
| 439 | else if (quoted && *s == '\\') qdpair = 1; |
| 440 | else if (quoted && *s == '"') quoted = 0; |
| 441 | else if (*s == '"') quoted = 1; |
| 442 | else if (*s == ',') return s; |
| 443 | } |
| 444 | return s; |
| 445 | } |
| 446 | |
| 447 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 448 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 449 | * structure holds everything necessary to use the header and find next |
| 450 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 451 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 452 | * 1 when it finds a value, and 0 when there is no more. |
| 453 | */ |
| 454 | int http_find_header2(const char *name, int len, |
| 455 | const char *sol, struct hdr_idx *idx, |
| 456 | struct hdr_ctx *ctx) |
| 457 | { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 458 | const char *eol, *sov; |
| 459 | int cur_idx; |
| 460 | |
| 461 | if (ctx->idx) { |
| 462 | /* We have previously returned a value, let's search |
| 463 | * another one on the same line. |
| 464 | */ |
| 465 | cur_idx = ctx->idx; |
| 466 | sol = ctx->line; |
| 467 | sov = sol + ctx->val + ctx->vlen; |
| 468 | eol = sol + idx->v[cur_idx].len; |
| 469 | |
| 470 | if (sov >= eol) |
| 471 | /* no more values in this header */ |
| 472 | goto next_hdr; |
| 473 | |
| 474 | /* values remaining for this header, skip the comma */ |
| 475 | sov++; |
| 476 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 477 | sov++; |
| 478 | |
| 479 | goto return_hdr; |
| 480 | } |
| 481 | |
| 482 | /* first request for this header */ |
| 483 | sol += hdr_idx_first_pos(idx); |
| 484 | cur_idx = hdr_idx_first_idx(idx); |
| 485 | |
| 486 | while (cur_idx) { |
| 487 | eol = sol + idx->v[cur_idx].len; |
| 488 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 489 | if (len == 0) { |
| 490 | /* No argument was passed, we want any header. |
| 491 | * To achieve this, we simply build a fake request. */ |
| 492 | while (sol + len < eol && sol[len] != ':') |
| 493 | len++; |
| 494 | name = sol; |
| 495 | } |
| 496 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | if ((len < eol - sol) && |
| 498 | (sol[len] == ':') && |
| 499 | (strncasecmp(sol, name, len) == 0)) { |
| 500 | |
| 501 | sov = sol + len + 1; |
| 502 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 503 | sov++; |
| 504 | return_hdr: |
| 505 | ctx->line = sol; |
| 506 | ctx->idx = cur_idx; |
| 507 | ctx->val = sov - sol; |
| 508 | |
| 509 | eol = find_hdr_value_end(sov, eol); |
| 510 | ctx->vlen = eol - sov; |
| 511 | return 1; |
| 512 | } |
| 513 | next_hdr: |
| 514 | sol = eol + idx->v[cur_idx].cr + 1; |
| 515 | cur_idx = idx->v[cur_idx].next; |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | int http_find_header(const char *name, |
| 521 | const char *sol, struct hdr_idx *idx, |
| 522 | struct hdr_ctx *ctx) |
| 523 | { |
| 524 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 525 | } |
| 526 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 527 | /* This function handles a server error at the stream interface level. The |
| 528 | * stream interface is assumed to be already in a closed state. An optional |
| 529 | * message is copied into the input buffer, and an HTTP status code stored. |
| 530 | * The error flags are set to the values in arguments. Any pending request |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 531 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 532 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 533 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 534 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 535 | { |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 536 | buffer_erase(si->ob); |
| 537 | buffer_erase(si->ib); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 538 | buffer_auto_close(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 539 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 540 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 541 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 542 | } |
| 543 | if (!(t->flags & SN_ERR_MASK)) |
| 544 | t->flags |= err; |
| 545 | if (!(t->flags & SN_FINST_MASK)) |
| 546 | t->flags |= finst; |
| 547 | } |
| 548 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 549 | /* This function returns the appropriate error location for the given session |
| 550 | * and message. |
| 551 | */ |
| 552 | |
| 553 | struct chunk *error_message(struct session *s, int msgnum) |
| 554 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 555 | if (s->be->errmsg[msgnum].str) |
| 556 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 557 | else if (s->fe->errmsg[msgnum].str) |
| 558 | return &s->fe->errmsg[msgnum]; |
| 559 | else |
| 560 | return &http_err_chunks[msgnum]; |
| 561 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 562 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 563 | /* |
| 564 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 565 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 566 | */ |
| 567 | static http_meth_t find_http_meth(const char *str, const int len) |
| 568 | { |
| 569 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 570 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 571 | |
| 572 | m = ((unsigned)*str - 'A'); |
| 573 | |
| 574 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 575 | for (h = http_methods[m]; h->len > 0; h++) { |
| 576 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 577 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 578 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 579 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 580 | }; |
| 581 | return HTTP_METH_OTHER; |
| 582 | } |
| 583 | return HTTP_METH_NONE; |
| 584 | |
| 585 | } |
| 586 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 587 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 588 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 589 | * It is returned otherwise. |
| 590 | */ |
| 591 | static char * |
| 592 | http_get_path(struct http_txn *txn) |
| 593 | { |
| 594 | char *ptr, *end; |
| 595 | |
| 596 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 597 | end = ptr + txn->req.sl.rq.u_l; |
| 598 | |
| 599 | if (ptr >= end) |
| 600 | return NULL; |
| 601 | |
| 602 | /* RFC2616, par. 5.1.2 : |
| 603 | * Request-URI = "*" | absuri | abspath | authority |
| 604 | */ |
| 605 | |
| 606 | if (*ptr == '*') |
| 607 | return NULL; |
| 608 | |
| 609 | if (isalpha((unsigned char)*ptr)) { |
| 610 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 611 | ptr++; |
| 612 | while (ptr < end && |
| 613 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 614 | ptr++; |
| 615 | /* skip '://' */ |
| 616 | if (ptr == end || *ptr++ != ':') |
| 617 | return NULL; |
| 618 | if (ptr == end || *ptr++ != '/') |
| 619 | return NULL; |
| 620 | if (ptr == end || *ptr++ != '/') |
| 621 | return NULL; |
| 622 | } |
| 623 | /* skip [user[:passwd]@]host[:[port]] */ |
| 624 | |
| 625 | while (ptr < end && *ptr != '/') |
| 626 | ptr++; |
| 627 | |
| 628 | if (ptr == end) |
| 629 | return NULL; |
| 630 | |
| 631 | /* OK, we got the '/' ! */ |
| 632 | return ptr; |
| 633 | } |
| 634 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 635 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 636 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 637 | * left unchanged and will follow normal proxy processing. |
| 638 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 639 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 640 | { |
| 641 | struct http_txn *txn; |
| 642 | struct chunk rdr; |
| 643 | char *path; |
| 644 | int len; |
| 645 | |
| 646 | /* 1: create the response header */ |
| 647 | rdr.len = strlen(HTTP_302); |
| 648 | rdr.str = trash; |
| 649 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 650 | |
| 651 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 652 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 653 | return; |
| 654 | |
| 655 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 656 | rdr.len += s->srv->rdr_len; |
| 657 | |
| 658 | /* 3: add the request URI */ |
| 659 | txn = &s->txn; |
| 660 | path = http_get_path(txn); |
| 661 | if (!path) |
| 662 | return; |
| 663 | |
| 664 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 665 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 666 | return; |
| 667 | |
| 668 | memcpy(rdr.str + rdr.len, path, len); |
| 669 | rdr.len += len; |
| 670 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 671 | rdr.len += 4; |
| 672 | |
| 673 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 674 | si->shutr(si); |
| 675 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 676 | si->err_type = SI_ET_NONE; |
| 677 | si->err_loc = NULL; |
| 678 | si->state = SI_ST_CLO; |
| 679 | |
| 680 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 681 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 682 | |
| 683 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 684 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 685 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 688 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 689 | * that the server side is closed. Note that err_type is actually a |
| 690 | * bitmask, where almost only aborts may be cumulated with other |
| 691 | * values. We consider that aborted operations are more important |
| 692 | * than timeouts or errors due to the fact that nobody else in the |
| 693 | * logs might explain incomplete retries. All others should avoid |
| 694 | * being cumulated. It should normally not be possible to have multiple |
| 695 | * aborts at once, but just in case, the first one in sequence is reported. |
| 696 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 697 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 698 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 699 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 700 | |
| 701 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 702 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 703 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 704 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 705 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 706 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 707 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 708 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 709 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 710 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 711 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 712 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 713 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 714 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 715 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 716 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 717 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 718 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 719 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 720 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 721 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 724 | extern const char sess_term_cond[8]; |
| 725 | extern const char sess_fin_state[8]; |
| 726 | extern const char *monthname[12]; |
| 727 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 728 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 729 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 730 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 731 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 732 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 733 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 734 | void http_sess_clflog(struct session *s) |
| 735 | { |
| 736 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 737 | struct proxy *fe = s->fe; |
| 738 | struct proxy *be = s->be; |
| 739 | struct proxy *prx_log; |
| 740 | struct http_txn *txn = &s->txn; |
| 741 | int tolog, level, err; |
| 742 | char *uri, *h; |
| 743 | char *svid; |
| 744 | struct tm tm; |
| 745 | static char tmpline[MAX_SYSLOG_LEN]; |
| 746 | int hdr; |
| 747 | size_t w; |
| 748 | int t_request; |
| 749 | |
| 750 | prx_log = fe; |
| 751 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 752 | (s->conn_retries != be->conn_retries) || |
| 753 | txn->status >= 500; |
| 754 | |
| 755 | if (s->cli_addr.ss_family == AF_INET) |
| 756 | inet_ntop(AF_INET, |
| 757 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 758 | pn, sizeof(pn)); |
| 759 | else |
| 760 | inet_ntop(AF_INET6, |
| 761 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 762 | pn, sizeof(pn)); |
| 763 | |
| 764 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 765 | |
| 766 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 767 | tolog = fe->to_log; |
| 768 | |
| 769 | h = tmpline; |
| 770 | |
| 771 | w = snprintf(h, sizeof(tmpline), |
| 772 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 773 | pn, |
| 774 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 775 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 776 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 777 | goto trunc; |
| 778 | h += w; |
| 779 | |
| 780 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 781 | goto trunc; |
| 782 | |
| 783 | *(h++) = ' '; |
| 784 | *(h++) = '\"'; |
| 785 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 786 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 787 | '#', url_encode_map, uri); |
| 788 | *(h++) = '\"'; |
| 789 | |
| 790 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 791 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 792 | goto trunc; |
| 793 | h += w; |
| 794 | |
| 795 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 796 | goto trunc; |
| 797 | memcpy(h, " \"-\" \"-\"", 8); |
| 798 | h += 8; |
| 799 | |
| 800 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 801 | " %d %03d", |
| 802 | (s->cli_addr.ss_family == AF_INET) ? |
| 803 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 804 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 805 | (int)s->logs.accept_date.tv_usec/1000); |
| 806 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 807 | goto trunc; |
| 808 | h += w; |
| 809 | |
| 810 | w = strlen(fe->id); |
| 811 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 812 | goto trunc; |
| 813 | *(h++) = ' '; |
| 814 | *(h++) = '\"'; |
| 815 | memcpy(h, fe->id, w); |
| 816 | h += w; |
| 817 | *(h++) = '\"'; |
| 818 | |
| 819 | w = strlen(be->id); |
| 820 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 821 | goto trunc; |
| 822 | *(h++) = ' '; |
| 823 | *(h++) = '\"'; |
| 824 | memcpy(h, be->id, w); |
| 825 | h += w; |
| 826 | *(h++) = '\"'; |
| 827 | |
| 828 | svid = (tolog & LW_SVID) ? |
| 829 | (s->data_source != DATA_SRC_STATS) ? |
| 830 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 831 | |
| 832 | w = strlen(svid); |
| 833 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 834 | goto trunc; |
| 835 | *(h++) = ' '; |
| 836 | *(h++) = '\"'; |
| 837 | memcpy(h, svid, w); |
| 838 | h += w; |
| 839 | *(h++) = '\"'; |
| 840 | |
| 841 | t_request = -1; |
| 842 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 843 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 844 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 845 | " %d %ld %ld %ld %ld", |
| 846 | t_request, |
| 847 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 848 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 849 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 850 | s->logs.t_close); |
| 851 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 852 | goto trunc; |
| 853 | h += w; |
| 854 | |
| 855 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 856 | goto trunc; |
| 857 | *(h++) = ' '; |
| 858 | *(h++) = '\"'; |
| 859 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 860 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 861 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 862 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 863 | *(h++) = '\"'; |
| 864 | |
| 865 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 866 | " %d %d %d %d %d %ld %ld", |
| 867 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 868 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 869 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 870 | |
| 871 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 872 | goto trunc; |
| 873 | h += w; |
| 874 | |
| 875 | if (txn->cli_cookie) { |
| 876 | w = strlen(txn->cli_cookie); |
| 877 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 878 | goto trunc; |
| 879 | *(h++) = ' '; |
| 880 | *(h++) = '\"'; |
| 881 | memcpy(h, txn->cli_cookie, w); |
| 882 | h += w; |
| 883 | *(h++) = '\"'; |
| 884 | } else { |
| 885 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 886 | goto trunc; |
| 887 | memcpy(h, " \"-\"", 4); |
| 888 | h += 4; |
| 889 | } |
| 890 | |
| 891 | if (txn->srv_cookie) { |
| 892 | w = strlen(txn->srv_cookie); |
| 893 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 894 | goto trunc; |
| 895 | *(h++) = ' '; |
| 896 | *(h++) = '\"'; |
| 897 | memcpy(h, txn->srv_cookie, w); |
| 898 | h += w; |
| 899 | *(h++) = '\"'; |
| 900 | } else { |
| 901 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 902 | goto trunc; |
| 903 | memcpy(h, " \"-\"", 4); |
| 904 | h += 4; |
| 905 | } |
| 906 | |
| 907 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 908 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 909 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 910 | goto trunc; |
| 911 | *(h++) = ' '; |
| 912 | *(h++) = '\"'; |
| 913 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 914 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 915 | *(h++) = '\"'; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 920 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 921 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 922 | goto trunc; |
| 923 | *(h++) = ' '; |
| 924 | *(h++) = '\"'; |
| 925 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 926 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 927 | *(h++) = '\"'; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | trunc: |
| 932 | *h = '\0'; |
| 933 | |
| 934 | level = LOG_INFO; |
| 935 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 936 | level = LOG_ERR; |
| 937 | |
| 938 | send_log(prx_log, level, "%s\n", tmpline); |
| 939 | |
| 940 | s->logs.logwait = 0; |
| 941 | } |
| 942 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 943 | /* |
| 944 | * send a log for the session when we have enough info about it. |
| 945 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 946 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 947 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 948 | { |
| 949 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 950 | struct proxy *fe = s->fe; |
| 951 | struct proxy *be = s->be; |
| 952 | struct proxy *prx_log; |
| 953 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 954 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 955 | char *uri, *h; |
| 956 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 957 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 958 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 959 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 960 | int hdr; |
| 961 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 962 | /* if we don't want to log normal traffic, return now */ |
| 963 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 964 | (s->conn_retries != be->conn_retries) || |
| 965 | txn->status >= 500; |
| 966 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 967 | return; |
| 968 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 969 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 970 | return; |
| 971 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 972 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 973 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 974 | return http_sess_clflog(s); |
| 975 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 976 | if (s->cli_addr.ss_family == AF_INET) |
| 977 | inet_ntop(AF_INET, |
| 978 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 979 | pn, sizeof(pn)); |
| 980 | else |
| 981 | inet_ntop(AF_INET6, |
| 982 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 983 | pn, sizeof(pn)); |
| 984 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 985 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 986 | |
| 987 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 988 | tolog = fe->to_log; |
| 989 | |
| 990 | h = tmpline; |
| 991 | if (fe->to_log & LW_REQHDR && |
| 992 | txn->req.cap && |
| 993 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 994 | *(h++) = ' '; |
| 995 | *(h++) = '{'; |
| 996 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 997 | if (hdr) |
| 998 | *(h++) = '|'; |
| 999 | if (txn->req.cap[hdr] != NULL) |
| 1000 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1001 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1002 | } |
| 1003 | *(h++) = '}'; |
| 1004 | } |
| 1005 | |
| 1006 | if (fe->to_log & LW_RSPHDR && |
| 1007 | txn->rsp.cap && |
| 1008 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1009 | *(h++) = ' '; |
| 1010 | *(h++) = '{'; |
| 1011 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1012 | if (hdr) |
| 1013 | *(h++) = '|'; |
| 1014 | if (txn->rsp.cap[hdr] != NULL) |
| 1015 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1016 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1017 | } |
| 1018 | *(h++) = '}'; |
| 1019 | } |
| 1020 | |
| 1021 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1022 | *(h++) = ' '; |
| 1023 | *(h++) = '"'; |
| 1024 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1025 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1026 | '#', url_encode_map, uri); |
| 1027 | *(h++) = '"'; |
| 1028 | } |
| 1029 | *h = '\0'; |
| 1030 | |
| 1031 | svid = (tolog & LW_SVID) ? |
| 1032 | (s->data_source != DATA_SRC_STATS) ? |
| 1033 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1034 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1035 | t_request = -1; |
| 1036 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1037 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1038 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1039 | level = LOG_INFO; |
| 1040 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1041 | level = LOG_ERR; |
| 1042 | |
| 1043 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1044 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1045 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1046 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n", |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1047 | pn, |
| 1048 | (s->cli_addr.ss_family == AF_INET) ? |
| 1049 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1050 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1051 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1052 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1053 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1054 | t_request, |
| 1055 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1056 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1057 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1058 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1059 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1060 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1061 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1062 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1063 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1064 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1065 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1066 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1067 | 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] | 1068 | (s->flags & SN_REDISP)?"+":"", |
| 1069 | (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] | 1070 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1071 | |
| 1072 | s->logs.logwait = 0; |
| 1073 | } |
| 1074 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1075 | |
| 1076 | /* |
| 1077 | * Capture headers from message starting at <som> according to header list |
| 1078 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1079 | */ |
| 1080 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1081 | char **cap, struct cap_hdr *cap_hdr) |
| 1082 | { |
| 1083 | char *eol, *sol, *col, *sov; |
| 1084 | int cur_idx; |
| 1085 | struct cap_hdr *h; |
| 1086 | int len; |
| 1087 | |
| 1088 | sol = som + hdr_idx_first_pos(idx); |
| 1089 | cur_idx = hdr_idx_first_idx(idx); |
| 1090 | |
| 1091 | while (cur_idx) { |
| 1092 | eol = sol + idx->v[cur_idx].len; |
| 1093 | |
| 1094 | col = sol; |
| 1095 | while (col < eol && *col != ':') |
| 1096 | col++; |
| 1097 | |
| 1098 | sov = col + 1; |
| 1099 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1100 | sov++; |
| 1101 | |
| 1102 | for (h = cap_hdr; h; h = h->next) { |
| 1103 | if ((h->namelen == col - sol) && |
| 1104 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1105 | if (cap[h->index] == NULL) |
| 1106 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1107 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1108 | |
| 1109 | if (cap[h->index] == NULL) { |
| 1110 | Alert("HTTP capture : out of memory.\n"); |
| 1111 | continue; |
| 1112 | } |
| 1113 | |
| 1114 | len = eol - sov; |
| 1115 | if (len > h->len) |
| 1116 | len = h->len; |
| 1117 | |
| 1118 | memcpy(cap[h->index], sov, len); |
| 1119 | cap[h->index][len]=0; |
| 1120 | } |
| 1121 | } |
| 1122 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1123 | cur_idx = idx->v[cur_idx].next; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1128 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1129 | */ |
| 1130 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1131 | |
| 1132 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1133 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1134 | */ |
| 1135 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1136 | ptr++; \ |
| 1137 | if (likely(ptr < end)) \ |
| 1138 | goto good; \ |
| 1139 | else { \ |
| 1140 | state = (st); \ |
| 1141 | goto http_msg_ood; \ |
| 1142 | } \ |
| 1143 | } while (0) |
| 1144 | |
| 1145 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1146 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1147 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1148 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1149 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1150 | * will give undefined results. |
| 1151 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1152 | * and that msg->sol points to the beginning of the response. |
| 1153 | * If a complete line is found (which implies that at least one CR or LF is |
| 1154 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1155 | * returned indicating an incomplete line (which does not mean that parts have |
| 1156 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1157 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1158 | * upon next call. |
| 1159 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1160 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1161 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1162 | * 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] | 1163 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1164 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1165 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1166 | unsigned int state, const char *ptr, const char *end, |
| 1167 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1168 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1169 | switch (state) { |
| 1170 | http_msg_rpver: |
| 1171 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1172 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1173 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1174 | |
| 1175 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1176 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1177 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1178 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1179 | state = HTTP_MSG_ERROR; |
| 1180 | break; |
| 1181 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1182 | http_msg_rpver_sp: |
| 1183 | case HTTP_MSG_RPVER_SP: |
| 1184 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1185 | msg->sl.st.c = ptr - msg_buf; |
| 1186 | goto http_msg_rpcode; |
| 1187 | } |
| 1188 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1189 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1190 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1191 | state = HTTP_MSG_ERROR; |
| 1192 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1193 | |
| 1194 | http_msg_rpcode: |
| 1195 | case HTTP_MSG_RPCODE: |
| 1196 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1197 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1198 | |
| 1199 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1200 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1201 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1202 | } |
| 1203 | |
| 1204 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1205 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1206 | http_msg_rsp_reason: |
| 1207 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1208 | msg->sl.st.r = ptr - msg_buf; |
| 1209 | msg->sl.st.r_l = 0; |
| 1210 | goto http_msg_rpline_eol; |
| 1211 | |
| 1212 | http_msg_rpcode_sp: |
| 1213 | case HTTP_MSG_RPCODE_SP: |
| 1214 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1215 | msg->sl.st.r = ptr - msg_buf; |
| 1216 | goto http_msg_rpreason; |
| 1217 | } |
| 1218 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1219 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1220 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1221 | goto http_msg_rsp_reason; |
| 1222 | |
| 1223 | http_msg_rpreason: |
| 1224 | case HTTP_MSG_RPREASON: |
| 1225 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1226 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1227 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1228 | http_msg_rpline_eol: |
| 1229 | /* We have seen the end of line. Note that we do not |
| 1230 | * necessarily have the \n yet, but at least we know that we |
| 1231 | * have EITHER \r OR \n, otherwise the response would not be |
| 1232 | * complete. We can then record the response length and return |
| 1233 | * to the caller which will be able to register it. |
| 1234 | */ |
| 1235 | msg->sl.st.l = ptr - msg->sol; |
| 1236 | return ptr; |
| 1237 | |
| 1238 | #ifdef DEBUG_FULL |
| 1239 | default: |
| 1240 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1241 | exit(1); |
| 1242 | #endif |
| 1243 | } |
| 1244 | |
| 1245 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1246 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1247 | if (ret_state) |
| 1248 | *ret_state = state; |
| 1249 | if (ret_ptr) |
| 1250 | *ret_ptr = (char *)ptr; |
| 1251 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | |
| 1255 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1256 | * This function parses a request line between <ptr> and <end>, starting with |
| 1257 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1258 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1259 | * will give undefined results. |
| 1260 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1261 | * and that msg->sol points to the beginning of the request. |
| 1262 | * If a complete line is found (which implies that at least one CR or LF is |
| 1263 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1264 | * returned indicating an incomplete line (which does not mean that parts have |
| 1265 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1266 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1267 | * upon next call. |
| 1268 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1269 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1270 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1271 | * 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] | 1272 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1273 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1274 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1275 | unsigned int state, const char *ptr, const char *end, |
| 1276 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1277 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1278 | switch (state) { |
| 1279 | http_msg_rqmeth: |
| 1280 | case HTTP_MSG_RQMETH: |
| 1281 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1282 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1285 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1286 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1287 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1289 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1290 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1291 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1292 | http_msg_req09_uri: |
| 1293 | msg->sl.rq.u = ptr - msg_buf; |
| 1294 | http_msg_req09_uri_e: |
| 1295 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1296 | http_msg_req09_ver: |
| 1297 | msg->sl.rq.v = ptr - msg_buf; |
| 1298 | msg->sl.rq.v_l = 0; |
| 1299 | goto http_msg_rqline_eol; |
| 1300 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1301 | state = HTTP_MSG_ERROR; |
| 1302 | break; |
| 1303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1304 | http_msg_rqmeth_sp: |
| 1305 | case HTTP_MSG_RQMETH_SP: |
| 1306 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1307 | msg->sl.rq.u = ptr - msg_buf; |
| 1308 | goto http_msg_rquri; |
| 1309 | } |
| 1310 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1311 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1312 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1313 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1314 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1315 | http_msg_rquri: |
| 1316 | case HTTP_MSG_RQURI: |
| 1317 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1318 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1319 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1321 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1322 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1323 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1324 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1325 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1326 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1327 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | http_msg_rquri_sp: |
| 1329 | case HTTP_MSG_RQURI_SP: |
| 1330 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1331 | msg->sl.rq.v = ptr - msg_buf; |
| 1332 | goto http_msg_rqver; |
| 1333 | } |
| 1334 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1335 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1336 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1337 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1338 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1339 | http_msg_rqver: |
| 1340 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1341 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1342 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1343 | |
| 1344 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1345 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1346 | http_msg_rqline_eol: |
| 1347 | /* We have seen the end of line. Note that we do not |
| 1348 | * necessarily have the \n yet, but at least we know that we |
| 1349 | * have EITHER \r OR \n, otherwise the request would not be |
| 1350 | * complete. We can then record the request length and return |
| 1351 | * to the caller which will be able to register it. |
| 1352 | */ |
| 1353 | msg->sl.rq.l = ptr - msg->sol; |
| 1354 | return ptr; |
| 1355 | } |
| 1356 | |
| 1357 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1358 | state = HTTP_MSG_ERROR; |
| 1359 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1360 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1361 | #ifdef DEBUG_FULL |
| 1362 | default: |
| 1363 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1364 | exit(1); |
| 1365 | #endif |
| 1366 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1367 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1368 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1369 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | if (ret_state) |
| 1371 | *ret_state = state; |
| 1372 | if (ret_ptr) |
| 1373 | *ret_ptr = (char *)ptr; |
| 1374 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1375 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1376 | |
| 1377 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1378 | /* |
| 1379 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1380 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1381 | * when data are missing and recalled at the exact same location with no |
| 1382 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1383 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1384 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1385 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1386 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1387 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1388 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1390 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1391 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | ptr = buf->lr; |
| 1393 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | if (unlikely(ptr >= end)) |
| 1396 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1397 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1399 | /* |
| 1400 | * First, states that are specific to the response only. |
| 1401 | * We check them first so that request and headers are |
| 1402 | * closer to each other (accessed more often). |
| 1403 | */ |
| 1404 | http_msg_rpbefore: |
| 1405 | case HTTP_MSG_RPBEFORE: |
| 1406 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1407 | #if !defined(PARSE_PRESERVE_EMPTY_LINES) |
| 1408 | if (likely(ptr != buf->data)) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1409 | /* Remove empty leading lines, as recommended by |
| 1410 | * RFC2616. This takes a lot of time because we |
| 1411 | * must move all the buffer backwards, but this |
| 1412 | * is rarely needed. The method above will be |
| 1413 | * cleaner when we'll be able to start sending |
| 1414 | * the request from any place in the buffer. |
| 1415 | */ |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1416 | ptr += buffer_replace2(buf, buf->lr, ptr, NULL, 0); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1417 | end = buf->r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1418 | } |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1419 | #endif |
| 1420 | msg->sol = ptr; |
| 1421 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1422 | hdr_idx_init(idx); |
| 1423 | state = HTTP_MSG_RPVER; |
| 1424 | goto http_msg_rpver; |
| 1425 | } |
| 1426 | |
| 1427 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1428 | goto http_msg_invalid; |
| 1429 | |
| 1430 | if (unlikely(*ptr == '\n')) |
| 1431 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1432 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1433 | /* stop here */ |
| 1434 | |
| 1435 | http_msg_rpbefore_cr: |
| 1436 | case HTTP_MSG_RPBEFORE_CR: |
| 1437 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1438 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1439 | /* stop here */ |
| 1440 | |
| 1441 | http_msg_rpver: |
| 1442 | case HTTP_MSG_RPVER: |
| 1443 | case HTTP_MSG_RPVER_SP: |
| 1444 | case HTTP_MSG_RPCODE: |
| 1445 | case HTTP_MSG_RPCODE_SP: |
| 1446 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1447 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1448 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1449 | if (unlikely(!ptr)) |
| 1450 | return; |
| 1451 | |
| 1452 | /* we have a full response and we know that we have either a CR |
| 1453 | * or an LF at <ptr>. |
| 1454 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1455 | //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] | 1456 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1457 | |
| 1458 | msg->sol = ptr; |
| 1459 | if (likely(*ptr == '\r')) |
| 1460 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1461 | goto http_msg_rpline_end; |
| 1462 | |
| 1463 | http_msg_rpline_end: |
| 1464 | case HTTP_MSG_RPLINE_END: |
| 1465 | /* msg->sol must point to the first of CR or LF. */ |
| 1466 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1467 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1468 | /* stop here */ |
| 1469 | |
| 1470 | /* |
| 1471 | * Second, states that are specific to the request only |
| 1472 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | http_msg_rqbefore: |
| 1474 | case HTTP_MSG_RQBEFORE: |
| 1475 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1476 | if (likely(ptr == buf->data)) { |
| 1477 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1478 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1479 | } else { |
| 1480 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1481 | /* only skip empty leading lines, don't remove them */ |
| 1482 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1483 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1484 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | /* Remove empty leading lines, as recommended by |
| 1486 | * RFC2616. This takes a lot of time because we |
| 1487 | * must move all the buffer backwards, but this |
| 1488 | * is rarely needed. The method above will be |
| 1489 | * cleaner when we'll be able to start sending |
| 1490 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1491 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | buf->lr = ptr; |
| 1493 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1494 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1495 | msg->sol = buf->data; |
| 1496 | ptr = buf->data; |
| 1497 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1498 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1499 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1500 | /* we will need this when keep-alive will be supported |
| 1501 | hdr_idx_init(idx); |
| 1502 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1503 | state = HTTP_MSG_RQMETH; |
| 1504 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1505 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1506 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1508 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1509 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | if (unlikely(*ptr == '\n')) |
| 1511 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1512 | 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] | 1513 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1514 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1515 | http_msg_rqbefore_cr: |
| 1516 | case HTTP_MSG_RQBEFORE_CR: |
| 1517 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1518 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1519 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1520 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | http_msg_rqmeth: |
| 1522 | case HTTP_MSG_RQMETH: |
| 1523 | case HTTP_MSG_RQMETH_SP: |
| 1524 | case HTTP_MSG_RQURI: |
| 1525 | case HTTP_MSG_RQURI_SP: |
| 1526 | case HTTP_MSG_RQVER: |
| 1527 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1528 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1529 | if (unlikely(!ptr)) |
| 1530 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1531 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1532 | /* we have a full request and we know that we have either a CR |
| 1533 | * or an LF at <ptr>. |
| 1534 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1535 | //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] | 1536 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1537 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | msg->sol = ptr; |
| 1539 | if (likely(*ptr == '\r')) |
| 1540 | 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] | 1541 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1542 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1543 | http_msg_rqline_end: |
| 1544 | case HTTP_MSG_RQLINE_END: |
| 1545 | /* check for HTTP/0.9 request : no version information available. |
| 1546 | * msg->sol must point to the first of CR or LF. |
| 1547 | */ |
| 1548 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1549 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1550 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1552 | 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] | 1553 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1554 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1555 | /* |
| 1556 | * Common states below |
| 1557 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1558 | http_msg_hdr_first: |
| 1559 | case HTTP_MSG_HDR_FIRST: |
| 1560 | msg->sol = ptr; |
| 1561 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1562 | goto http_msg_hdr_name; |
| 1563 | } |
| 1564 | |
| 1565 | if (likely(*ptr == '\r')) |
| 1566 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1567 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | http_msg_hdr_name: |
| 1570 | case HTTP_MSG_HDR_NAME: |
| 1571 | /* assumes msg->sol points to the first char */ |
| 1572 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1573 | 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] | 1574 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | if (likely(*ptr == ':')) { |
| 1576 | msg->col = ptr - buf->data; |
| 1577 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1578 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1580 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1581 | goto http_msg_invalid; |
| 1582 | |
| 1583 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1584 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1585 | |
| 1586 | /* and we still accept this non-token character */ |
| 1587 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | http_msg_hdr_l1_sp: |
| 1590 | case HTTP_MSG_HDR_L1_SP: |
| 1591 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1592 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1593 | 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] | 1594 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1595 | /* header value can be basically anything except CR/LF */ |
| 1596 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1597 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1598 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1599 | goto http_msg_hdr_val; |
| 1600 | } |
| 1601 | |
| 1602 | if (likely(*ptr == '\r')) |
| 1603 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1604 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1605 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1606 | http_msg_hdr_l1_lf: |
| 1607 | case HTTP_MSG_HDR_L1_LF: |
| 1608 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1609 | 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] | 1610 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1611 | http_msg_hdr_l1_lws: |
| 1612 | case HTTP_MSG_HDR_L1_LWS: |
| 1613 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1614 | /* replace HT,CR,LF with spaces */ |
| 1615 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1616 | buf->data[msg->sov] = ' '; |
| 1617 | goto http_msg_hdr_l1_sp; |
| 1618 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1619 | /* we had a header consisting only in spaces ! */ |
| 1620 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | goto http_msg_complete_header; |
| 1622 | |
| 1623 | http_msg_hdr_val: |
| 1624 | case HTTP_MSG_HDR_VAL: |
| 1625 | /* assumes msg->sol points to the first char, msg->col to the |
| 1626 | * colon, and msg->sov points to the first character of the |
| 1627 | * value. |
| 1628 | */ |
| 1629 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1630 | 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] | 1631 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | msg->eol = ptr; |
| 1633 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1634 | * real header end in case it ends with lots of LWS, but is this |
| 1635 | * really needed ? |
| 1636 | */ |
| 1637 | if (likely(*ptr == '\r')) |
| 1638 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1639 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1640 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1641 | http_msg_hdr_l2_lf: |
| 1642 | case HTTP_MSG_HDR_L2_LF: |
| 1643 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1644 | 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] | 1645 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1646 | http_msg_hdr_l2_lws: |
| 1647 | case HTTP_MSG_HDR_L2_LWS: |
| 1648 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1649 | /* LWS: replace HT,CR,LF with spaces */ |
| 1650 | for (; msg->eol < ptr; msg->eol++) |
| 1651 | *msg->eol = ' '; |
| 1652 | goto http_msg_hdr_val; |
| 1653 | } |
| 1654 | http_msg_complete_header: |
| 1655 | /* |
| 1656 | * It was a new header, so the last one is finished. |
| 1657 | * Assumes msg->sol points to the first char, msg->col to the |
| 1658 | * colon, msg->sov points to the first character of the value |
| 1659 | * and msg->eol to the first CR or LF so we know how the line |
| 1660 | * ends. We insert last header into the index. |
| 1661 | */ |
| 1662 | /* |
| 1663 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1664 | write(2, msg->sol, msg->eol-msg->sol); |
| 1665 | fprintf(stderr,"\n"); |
| 1666 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1667 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1669 | idx, idx->tail) < 0)) |
| 1670 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1671 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1672 | msg->sol = ptr; |
| 1673 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1674 | goto http_msg_hdr_name; |
| 1675 | } |
| 1676 | |
| 1677 | if (likely(*ptr == '\r')) |
| 1678 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1679 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1680 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | http_msg_last_lf: |
| 1682 | case HTTP_MSG_LAST_LF: |
| 1683 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1684 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1685 | ptr++; |
| 1686 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1687 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1688 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1689 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | return; |
| 1691 | #ifdef DEBUG_FULL |
| 1692 | default: |
| 1693 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1694 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1695 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | } |
| 1697 | http_msg_ood: |
| 1698 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1699 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | buf->lr = ptr; |
| 1701 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | http_msg_invalid: |
| 1704 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1705 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1706 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1707 | return; |
| 1708 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1709 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1710 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1711 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1712 | * nothing is done and 1 is returned. |
| 1713 | */ |
| 1714 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1715 | { |
| 1716 | int delta; |
| 1717 | char *cur_end; |
| 1718 | |
| 1719 | if (msg->sl.rq.v_l != 0) |
| 1720 | return 1; |
| 1721 | |
| 1722 | msg->sol = req->data + msg->som; |
| 1723 | cur_end = msg->sol + msg->sl.rq.l; |
| 1724 | delta = 0; |
| 1725 | |
| 1726 | if (msg->sl.rq.u_l == 0) { |
| 1727 | /* if no URI was set, add "/" */ |
| 1728 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1729 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1730 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1731 | } |
| 1732 | /* add HTTP version */ |
| 1733 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1734 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1735 | cur_end += delta; |
| 1736 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1737 | HTTP_MSG_RQMETH, |
| 1738 | msg->sol, cur_end + 1, |
| 1739 | NULL, NULL); |
| 1740 | if (unlikely(!cur_end)) |
| 1741 | return 0; |
| 1742 | |
| 1743 | /* we have a full HTTP/1.0 request now and we know that |
| 1744 | * we have either a CR or an LF at <ptr>. |
| 1745 | */ |
| 1746 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1747 | return 1; |
| 1748 | } |
| 1749 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 1750 | /* Parse the Connection: headaer of an HTTP request, and set the transaction |
| 1751 | * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag |
| 1752 | * is also set so that we don't parse a second time. If some dangerous values |
| 1753 | * are encountered, we leave the status to indicate that the request might be |
| 1754 | * interpreted as keep-alive, but we also set the connection flags to indicate |
| 1755 | * that we WANT it to be a close, so that the header will be fixed. This |
| 1756 | * function should only be called when we know we're interested in checking |
| 1757 | * the request (not a CONNECT, and FE or BE mangles the header). |
| 1758 | */ |
| 1759 | void http_parse_connection_header(struct http_txn *txn) |
| 1760 | { |
| 1761 | struct http_msg *msg = &txn->req; |
| 1762 | struct hdr_ctx ctx; |
| 1763 | int conn_cl, conn_ka; |
| 1764 | |
| 1765 | if (txn->flags & TX_CON_HDR_PARS) |
| 1766 | return; |
| 1767 | |
| 1768 | conn_cl = 0; |
| 1769 | conn_ka = 0; |
| 1770 | ctx.idx = 0; |
| 1771 | |
| 1772 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 1773 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 1774 | conn_cl = 1; |
| 1775 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 1776 | conn_ka = 1; |
| 1777 | } |
| 1778 | |
| 1779 | /* Determine if the client wishes keep-alive or close. |
| 1780 | * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections |
| 1781 | * are persistent unless "Connection: close" is explicitly specified. |
| 1782 | * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections. |
| 1783 | * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless |
| 1784 | * they explicitly specify "Connection: Keep-Alive", regardless of any |
| 1785 | * optional "Keep-Alive" header. |
| 1786 | * Note that if we find a request with both "Connection: close" and |
| 1787 | * "Connection: Keep-Alive", we indicate we want a close but don't have |
| 1788 | * it, so that it can be enforced later. |
| 1789 | */ |
| 1790 | |
| 1791 | if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */ |
| 1792 | if (conn_cl) { |
| 1793 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1794 | if (!conn_ka) |
| 1795 | txn->flags |= TX_REQ_CONN_CLO; |
| 1796 | } |
| 1797 | } else { /* HTTP/1.0 */ |
| 1798 | if (!conn_ka) |
| 1799 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO; |
| 1800 | else if (conn_cl) |
| 1801 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1802 | } |
| 1803 | txn->flags |= TX_CON_HDR_PARS; |
| 1804 | } |
| 1805 | |
| 1806 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1807 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1808 | * processing can continue on next analysers, or zero if it either needs more |
| 1809 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 1810 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 1811 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1812 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1813 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1814 | int http_wait_for_request(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1816 | /* |
| 1817 | * We will parse the partial (or complete) lines. |
| 1818 | * We will check the request syntax, and also join multi-line |
| 1819 | * headers. An index of all the lines will be elaborated while |
| 1820 | * parsing. |
| 1821 | * |
| 1822 | * For the parsing, we use a 28 states FSM. |
| 1823 | * |
| 1824 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 1825 | * req->data + msg->som = beginning of request |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1826 | * req->data + req->eoh = end of processed headers / start of current one |
| 1827 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1828 | * req->lr = first non-visited byte |
| 1829 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1830 | * |
| 1831 | * At end of parsing, we may perform a capture of the error (if any), and |
| 1832 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 1833 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1834 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1835 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1836 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1837 | int cur_idx; |
| 1838 | struct http_txn *txn = &s->txn; |
| 1839 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1840 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1841 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1842 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 1843 | now_ms, __FUNCTION__, |
| 1844 | s, |
| 1845 | req, |
| 1846 | req->rex, req->wex, |
| 1847 | req->flags, |
| 1848 | req->l, |
| 1849 | req->analysers); |
| 1850 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1851 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1852 | s->srv_error = http_return_srv_error; |
| 1853 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1854 | if (likely(req->lr < req->r)) |
| 1855 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1856 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1857 | /* 1: we might have to print this header in debug mode */ |
| 1858 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1859 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1860 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1861 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1862 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1863 | sol = req->data + msg->som; |
| 1864 | eol = sol + msg->sl.rq.l; |
| 1865 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1866 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1867 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1868 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1869 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1870 | while (cur_idx) { |
| 1871 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1872 | debug_hdr("clihdr", s, sol, eol); |
| 1873 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1874 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1875 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1878 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1879 | /* |
| 1880 | * Now we quickly check if we have found a full valid request. |
| 1881 | * If not so, we check the FD and buffer states before leaving. |
| 1882 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1883 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1884 | * requests are checked first. |
| 1885 | * |
| 1886 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1887 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 1888 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1889 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1890 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1891 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1892 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 1893 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1894 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1895 | /* 1: Since we are in header mode, if there's no space |
| 1896 | * left for headers, we won't be able to free more |
| 1897 | * later, so the session will never terminate. We |
| 1898 | * must terminate it now. |
| 1899 | */ |
| 1900 | if (unlikely(req->flags & BF_FULL)) { |
| 1901 | /* FIXME: check if URI is set and return Status |
| 1902 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1903 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1904 | goto return_bad_req; |
| 1905 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1906 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1907 | /* 2: have we encountered a read error ? */ |
| 1908 | else if (req->flags & BF_READ_ERROR) { |
| 1909 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1910 | if (msg->err_pos >= 0) |
| 1911 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1912 | msg->msg_state = HTTP_MSG_ERROR; |
| 1913 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1914 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1915 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1916 | if (s->listener->counters) |
| 1917 | s->listener->counters->failed_req++; |
| 1918 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1919 | if (!(s->flags & SN_ERR_MASK)) |
| 1920 | s->flags |= SN_ERR_CLICL; |
| 1921 | if (!(s->flags & SN_FINST_MASK)) |
| 1922 | s->flags |= SN_FINST_R; |
| 1923 | return 0; |
| 1924 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1925 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1926 | /* 3: has the read timeout expired ? */ |
| 1927 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
| 1928 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1929 | if (msg->err_pos >= 0) |
| 1930 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1931 | txn->status = 408; |
| 1932 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 1933 | msg->msg_state = HTTP_MSG_ERROR; |
| 1934 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1935 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1936 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1937 | if (s->listener->counters) |
| 1938 | s->listener->counters->failed_req++; |
| 1939 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1940 | if (!(s->flags & SN_ERR_MASK)) |
| 1941 | s->flags |= SN_ERR_CLITO; |
| 1942 | if (!(s->flags & SN_FINST_MASK)) |
| 1943 | s->flags |= SN_FINST_R; |
| 1944 | return 0; |
| 1945 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1946 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1947 | /* 4: have we encountered a close ? */ |
| 1948 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1949 | if (msg->err_pos >= 0) |
| 1950 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1951 | txn->status = 400; |
| 1952 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 1953 | msg->msg_state = HTTP_MSG_ERROR; |
| 1954 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1955 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1956 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1957 | if (s->listener->counters) |
| 1958 | s->listener->counters->failed_req++; |
| 1959 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1960 | if (!(s->flags & SN_ERR_MASK)) |
| 1961 | s->flags |= SN_ERR_CLICL; |
| 1962 | if (!(s->flags & SN_FINST_MASK)) |
| 1963 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1964 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1965 | } |
| 1966 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1967 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1968 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 1969 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1970 | /* just set the request timeout once at the beginning of the request */ |
| 1971 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 1972 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1973 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1974 | /* we're not ready yet */ |
| 1975 | return 0; |
| 1976 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1977 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1978 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1979 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1980 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 1981 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 1982 | * points to the CRLF of the request line. req->lr points to the first |
| 1983 | * byte after the last LF. msg->col and msg->sov point to the first |
| 1984 | * byte of data. msg->eol cannot be trusted because it may have been |
| 1985 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1986 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1987 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1988 | /* Maybe we found in invalid header name while we were configured not |
| 1989 | * to block on that, so we have to capture it now. |
| 1990 | */ |
| 1991 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1992 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 1993 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1994 | /* ensure we keep this pointer to the beginning of the message */ |
| 1995 | msg->sol = req->data + msg->som; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1996 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1997 | /* |
| 1998 | * 1: identify the method |
| 1999 | */ |
| 2000 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 2001 | |
| 2002 | /* we can make use of server redirect on GET and HEAD */ |
| 2003 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2004 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2005 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2006 | /* |
| 2007 | * 2: check if the URI matches the monitor_uri. |
| 2008 | * We have to do this for every request which gets in, because |
| 2009 | * the monitor-uri is defined by the frontend. |
| 2010 | */ |
| 2011 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2012 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2013 | !memcmp(&req->data[msg->sl.rq.u], |
| 2014 | s->fe->monitor_uri, |
| 2015 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2016 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2017 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2018 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2019 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2020 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2021 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2022 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2023 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2024 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2025 | int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2026 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2027 | ret = acl_pass(ret); |
| 2028 | if (cond->pol == ACL_COND_UNLESS) |
| 2029 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2030 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2031 | if (ret) { |
| 2032 | /* we fail this request, let's return 503 service unavail */ |
| 2033 | txn->status = 503; |
| 2034 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2035 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2036 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2037 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2038 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2039 | /* nothing to fail, let's reply normaly */ |
| 2040 | txn->status = 200; |
| 2041 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2042 | goto return_prx_cond; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2047 | * Note: we cannot log anymore if the request has been |
| 2048 | * classified as invalid. |
| 2049 | */ |
| 2050 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2051 | /* we have a complete HTTP request that we must log */ |
| 2052 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2053 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2054 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2055 | if (urilen >= REQURI_LEN) |
| 2056 | urilen = REQURI_LEN - 1; |
| 2057 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2058 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2059 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2060 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2061 | s->do_log(s); |
| 2062 | } else { |
| 2063 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2064 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2065 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2066 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2067 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2068 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2069 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2070 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2071 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2072 | if ((msg->sl.rq.v_l == 8) && |
| 2073 | ((req->data[msg->som + msg->sl.rq.v + 5] > '1') || |
| 2074 | ((req->data[msg->som + msg->sl.rq.v + 5] == '1') && |
| 2075 | (req->data[msg->som + msg->sl.rq.v + 7] >= '1')))) |
| 2076 | txn->flags |= TX_REQ_VER_11; |
| 2077 | |
| 2078 | /* "connection" has not been parsed yet */ |
| 2079 | txn->flags &= ~TX_CON_HDR_PARS; |
| 2080 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2081 | /* 5: we may need to capture headers */ |
| 2082 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
| 2083 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
| 2084 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2085 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2086 | /* 6: determine if we have a transfer-encoding or content-length. |
| 2087 | * RFC2616 #4.4 states that : |
| 2088 | * - If a Transfer-Encoding header field is present and has any value |
| 2089 | * other than "identity", then the transfer-length is defined by use |
| 2090 | * of the "chunked" transfer-coding ; |
| 2091 | * |
| 2092 | * - If a Content-Length header field is present, its decimal value in |
| 2093 | * OCTETs represents both the entity-length and the transfer-length. |
| 2094 | * If a message is received with both a Transfer-Encoding header |
| 2095 | * field and a Content-Length header field, the latter MUST be ignored. |
| 2096 | * |
| 2097 | * - If a request contains a message-body and a Content-Length is not |
| 2098 | * given, the server SHOULD respond with 400 (bad request) if it |
| 2099 | * cannot determine the length of the message, or with 411 (length |
| 2100 | * required) if it wishes to insist on receiving a valid Content-Length. |
| 2101 | */ |
| 2102 | |
| 2103 | /* FIXME: chunked encoding is HTTP/1.1 only */ |
| 2104 | ctx.idx = 0; |
| 2105 | while (http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2106 | if (ctx.vlen == 8 && strncasecmp(ctx.line + ctx.val, "identity", 8) == 0) |
| 2107 | continue; |
| 2108 | txn->flags |= TX_REQ_TE_CHNK; |
| 2109 | break; |
| 2110 | } |
| 2111 | |
| 2112 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 2113 | ctx.idx = 0; |
| 2114 | while (!(txn->flags & TX_REQ_TE_CHNK) && |
| 2115 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2116 | signed long long cl; |
| 2117 | |
| 2118 | if (!ctx.vlen) |
| 2119 | goto return_bad_req; |
| 2120 | |
| 2121 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2122 | goto return_bad_req; /* parse failure */ |
| 2123 | |
| 2124 | if (cl < 0) |
| 2125 | goto return_bad_req; |
| 2126 | |
| 2127 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2128 | goto return_bad_req; /* already specified, was different */ |
| 2129 | |
| 2130 | txn->flags |= TX_REQ_CNT_LEN; |
| 2131 | msg->hdr_content_len = cl; |
| 2132 | } |
| 2133 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2134 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2135 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2136 | req->analyse_exp = TICK_ETERNITY; |
| 2137 | return 1; |
| 2138 | |
| 2139 | return_bad_req: |
| 2140 | /* We centralize bad requests processing here */ |
| 2141 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2142 | /* we detected a parsing error. We want to archive this request |
| 2143 | * in the dedicated proxy area for later troubleshooting. |
| 2144 | */ |
| 2145 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2146 | } |
| 2147 | |
| 2148 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2149 | txn->status = 400; |
| 2150 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2151 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2152 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2153 | if (s->listener->counters) |
| 2154 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2155 | |
| 2156 | return_prx_cond: |
| 2157 | if (!(s->flags & SN_ERR_MASK)) |
| 2158 | s->flags |= SN_ERR_PRXCOND; |
| 2159 | if (!(s->flags & SN_FINST_MASK)) |
| 2160 | s->flags |= SN_FINST_R; |
| 2161 | |
| 2162 | req->analysers = 0; |
| 2163 | req->analyse_exp = TICK_ETERNITY; |
| 2164 | return 0; |
| 2165 | } |
| 2166 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2167 | /* This stream analyser runs all HTTP request processing which is common to |
| 2168 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2169 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2170 | * It returns 1 if the processing can continue on next analysers, or zero if it |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2171 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2172 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2173 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2174 | int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2175 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2176 | struct http_txn *txn = &s->txn; |
| 2177 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2178 | struct acl_cond *cond; |
| 2179 | struct redirect_rule *rule; |
| 2180 | int cur_idx; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2181 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2182 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2183 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2184 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2185 | return 0; |
| 2186 | } |
| 2187 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2188 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2189 | req->analyse_exp = TICK_ETERNITY; |
| 2190 | |
| 2191 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2192 | now_ms, __FUNCTION__, |
| 2193 | s, |
| 2194 | req, |
| 2195 | req->rex, req->wex, |
| 2196 | req->flags, |
| 2197 | req->l, |
| 2198 | req->analysers); |
| 2199 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2200 | /* first check whether we have some ACLs set to block this request */ |
| 2201 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2202 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2203 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2204 | ret = acl_pass(ret); |
| 2205 | if (cond->pol == ACL_COND_UNLESS) |
| 2206 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2207 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2208 | if (ret) { |
| 2209 | txn->status = 403; |
| 2210 | /* let's log the request time */ |
| 2211 | s->logs.tv_request = now; |
| 2212 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2213 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2214 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2215 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2216 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2217 | /* try headers filters */ |
| 2218 | if (px->req_exp != NULL) { |
| 2219 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2220 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2221 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2222 | /* has the request been denied ? */ |
| 2223 | if (txn->flags & TX_CLDENY) { |
| 2224 | /* no need to go further */ |
| 2225 | txn->status = 403; |
| 2226 | /* let's log the request time */ |
| 2227 | s->logs.tv_request = now; |
| 2228 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2229 | goto return_prx_cond; |
| 2230 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2231 | |
| 2232 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2233 | * which may be the same as the connect timeout if unspecified. |
| 2234 | * If unset, then set it to zero because we really want it to |
| 2235 | * eventually expire. We build the tarpit as an analyser. |
| 2236 | */ |
| 2237 | if (txn->flags & TX_CLTARPIT) { |
| 2238 | buffer_erase(s->req); |
| 2239 | /* wipe the request out so that we can drop the connection early |
| 2240 | * if the client closes first. |
| 2241 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2242 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2243 | req->analysers = 0; /* remove switching rules etc... */ |
| 2244 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2245 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2246 | if (!req->analyse_exp) |
| 2247 | req->analyse_exp = tick_add(now_ms, 0); |
| 2248 | return 1; |
| 2249 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2250 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2251 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2252 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2253 | * only change if both the request and the config reference something else. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2254 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2255 | |
| 2256 | if ((s->txn.meth != HTTP_METH_CONNECT) && |
| 2257 | ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 2258 | int tmp = TX_CON_WANT_TUN; |
| 2259 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2260 | tmp = TX_CON_WANT_KAL; |
| 2261 | /* FIXME: for now, we don't support server-close mode */ |
| 2262 | if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) |
| 2263 | tmp = TX_CON_WANT_CLO; |
| 2264 | |
| 2265 | if (!(txn->flags & TX_CON_HDR_PARS)) |
| 2266 | http_parse_connection_header(txn); |
| 2267 | |
| 2268 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2269 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2270 | } |
| 2271 | |
| 2272 | /* We're really certain of the connection mode (tunnel, close, keep-alive) |
| 2273 | * once we know the backend, because the tunnel mode can be implied by the |
| 2274 | * lack of any close/keepalive options in both the FE and the BE. Since |
| 2275 | * this information can evolve with time, we proceed by trying to make the |
| 2276 | * header status match the desired status. For this, we'll have to adjust |
| 2277 | * the "Connection" header. The test for persistent connections has already |
| 2278 | * been performed, so we only enter here if there is a risk the connection |
| 2279 | * is considered as persistent and we want it to be closed on the server |
| 2280 | * side. It would be nice if we could enter this place only when a |
| 2281 | * Connection header exists. Note that a CONNECT method will not enter |
| 2282 | * here. |
| 2283 | */ |
| 2284 | if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2285 | char *cur_ptr, *cur_end, *cur_next; |
| 2286 | int old_idx, delta, val; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2287 | int must_delete; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2288 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2289 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2290 | must_delete = !(txn->flags & TX_REQ_VER_11); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2291 | 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] | 2292 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2293 | for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2294 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2295 | cur_ptr = cur_next; |
| 2296 | cur_end = cur_ptr + cur_hdr->len; |
| 2297 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2298 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2299 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2300 | if (!val) |
| 2301 | continue; |
| 2302 | |
| 2303 | /* 3 possibilities : |
| 2304 | * - we have already set "Connection: close" or we're in |
| 2305 | * HTTP/1.0, so we remove this line. |
| 2306 | * - we have not yet set "Connection: close", but this line |
| 2307 | * indicates close. We leave it untouched and set the flag. |
| 2308 | * - we have not yet set "Connection: close", and this line |
| 2309 | * indicates non-close. We replace it and set the flag. |
| 2310 | */ |
| 2311 | if (must_delete) { |
| 2312 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 2313 | http_msg_move_end(&txn->req, delta); |
| 2314 | cur_next += delta; |
| 2315 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2316 | txn->hdr_idx.used--; |
| 2317 | cur_hdr->len = 0; |
| 2318 | txn->flags |= TX_REQ_CONN_CLO; |
| 2319 | } else { |
| 2320 | if (cur_end - cur_ptr - val != 5 || |
| 2321 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2322 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2323 | "close", 5); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2324 | cur_next += delta; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2325 | cur_hdr->len += delta; |
| 2326 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2327 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2328 | txn->flags |= TX_REQ_CONN_CLO; |
| 2329 | must_delete = 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2330 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2331 | } /* for loop */ |
| 2332 | } /* if must close keep-alive */ |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2333 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2334 | /* add request headers from the rule sets in the same order */ |
| 2335 | for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) { |
| 2336 | if (unlikely(http_header_add_tail(req, |
| 2337 | &txn->req, |
| 2338 | &txn->hdr_idx, |
| 2339 | px->req_add[cur_idx])) < 0) |
| 2340 | goto return_bad_req; |
| 2341 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2342 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2343 | /* check if stats URI was requested, and if an auth is needed */ |
| 2344 | if (px->uri_auth != NULL && |
| 2345 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2346 | /* we have to check the URI and auth for this request. |
| 2347 | * FIXME!!! that one is rather dangerous, we want to |
| 2348 | * make it follow standard rules (eg: clear req->analysers). |
| 2349 | */ |
| 2350 | if (stats_check_uri_auth(s, px)) { |
| 2351 | req->analysers = 0; |
| 2352 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2354 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2355 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2356 | /* check whether we have some ACLs set to redirect this request */ |
| 2357 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 2358 | int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2359 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2360 | ret = acl_pass(ret); |
| 2361 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2362 | ret = !ret; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2363 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2364 | if (ret) { |
| 2365 | struct chunk rdr = { trash, 0 }; |
| 2366 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2367 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2368 | /* build redirect message */ |
| 2369 | switch(rule->code) { |
| 2370 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2371 | msg_fmt = HTTP_303; |
| 2372 | break; |
| 2373 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2374 | msg_fmt = HTTP_301; |
| 2375 | break; |
| 2376 | case 302: |
| 2377 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2378 | msg_fmt = HTTP_302; |
| 2379 | break; |
| 2380 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2381 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2382 | if (unlikely(chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2383 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2384 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2385 | switch(rule->type) { |
| 2386 | case REDIRECT_TYPE_PREFIX: { |
| 2387 | const char *path; |
| 2388 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2389 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2390 | path = http_get_path(txn); |
| 2391 | /* build message using path */ |
| 2392 | if (path) { |
| 2393 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2394 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2395 | int qs = 0; |
| 2396 | while (qs < pathlen) { |
| 2397 | if (path[qs] == '?') { |
| 2398 | pathlen = qs; |
| 2399 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2400 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2401 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2402 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2403 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2404 | } else { |
| 2405 | path = "/"; |
| 2406 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2407 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2408 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2409 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2410 | goto return_bad_req; |
| 2411 | |
| 2412 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2413 | * add anything, otherwise it makes it hard for the user to |
| 2414 | * configure a self-redirection. |
| 2415 | */ |
| 2416 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2417 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2418 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2419 | } |
| 2420 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2421 | /* add path */ |
| 2422 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2423 | rdr.len += pathlen; |
| 2424 | break; |
| 2425 | } |
| 2426 | case REDIRECT_TYPE_LOCATION: |
| 2427 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2428 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2429 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2430 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2431 | /* add location */ |
| 2432 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2433 | rdr.len += rule->rdr_len; |
| 2434 | break; |
| 2435 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2436 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2437 | if (rule->cookie_len) { |
| 2438 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2439 | rdr.len += 14; |
| 2440 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2441 | rdr.len += rule->cookie_len; |
| 2442 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2443 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2444 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2445 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2446 | /* add end of headers */ |
| 2447 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2448 | rdr.len += 4; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2449 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2450 | txn->status = rule->code; |
| 2451 | /* let's log the request time */ |
| 2452 | s->logs.tv_request = now; |
| 2453 | stream_int_retnclose(req->prod, &rdr); |
| 2454 | goto return_prx_cond; |
| 2455 | } |
| 2456 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2457 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2458 | /* We can shut read side if "connection: close" && no-data && !tunnel && !abort_on_close */ |
| 2459 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO && |
| 2460 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 2461 | (txn->meth != HTTP_METH_CONNECT) && |
| 2462 | !(s->be->options & PR_O_ABRT_CLOSE)) |
Willy Tarreau | 349a0f6 | 2009-10-18 21:17:42 +0200 | [diff] [blame] | 2463 | req->flags |= BF_DONT_READ; |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 2464 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2465 | /* that's OK for us now, let's move on to next analysers */ |
| 2466 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2467 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2468 | return_bad_req: |
| 2469 | /* We centralize bad requests processing here */ |
| 2470 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2471 | /* we detected a parsing error. We want to archive this request |
| 2472 | * in the dedicated proxy area for later troubleshooting. |
| 2473 | */ |
| 2474 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2475 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2476 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2477 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2478 | txn->status = 400; |
| 2479 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2480 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2481 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2482 | if (s->listener->counters) |
| 2483 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2484 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2485 | return_prx_cond: |
| 2486 | if (!(s->flags & SN_ERR_MASK)) |
| 2487 | s->flags |= SN_ERR_PRXCOND; |
| 2488 | if (!(s->flags & SN_FINST_MASK)) |
| 2489 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2490 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2491 | req->analysers = 0; |
| 2492 | req->analyse_exp = TICK_ETERNITY; |
| 2493 | return 0; |
| 2494 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2495 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2496 | /* This function performs all the processing enabled for the current request. |
| 2497 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2498 | * needs more data, encounters an error, or wants to immediately abort the |
| 2499 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 2500 | */ |
| 2501 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 2502 | { |
| 2503 | struct http_txn *txn = &s->txn; |
| 2504 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2505 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2506 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2507 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2508 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2509 | return 0; |
| 2510 | } |
| 2511 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2512 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2513 | now_ms, __FUNCTION__, |
| 2514 | s, |
| 2515 | req, |
| 2516 | req->rex, req->wex, |
| 2517 | req->flags, |
| 2518 | req->l, |
| 2519 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2520 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2521 | /* |
| 2522 | * Right now, we know that we have processed the entire headers |
| 2523 | * and that unwanted requests have been filtered out. We can do |
| 2524 | * whatever we want with the remaining request. Also, now we |
| 2525 | * may have separate values for ->fe, ->be. |
| 2526 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2527 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2528 | /* |
| 2529 | * If HTTP PROXY is set we simply get remote server address |
| 2530 | * parsing incoming request. |
| 2531 | */ |
| 2532 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
| 2533 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
| 2534 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2535 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2536 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2537 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2538 | * Note that doing so might move headers in the request, but |
| 2539 | * the fields will stay coherent and the URI will not move. |
| 2540 | * This should only be performed in the backend. |
| 2541 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2542 | if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2543 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 2544 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2545 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2546 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2547 | * 8: the appsession cookie was looked up very early in 1.2, |
| 2548 | * so let's do the same now. |
| 2549 | */ |
| 2550 | |
| 2551 | /* It needs to look into the URI */ |
| 2552 | if ((s->sessid == NULL) && s->be->appsession_name) { |
| 2553 | get_srv_from_appsession(s, &req->data[msg->som + msg->sl.rq.u], msg->sl.rq.u_l); |
| 2554 | } |
| 2555 | |
| 2556 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2557 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2558 | * asks for it. |
| 2559 | */ |
| 2560 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 2561 | if (s->cli_addr.ss_family == AF_INET) { |
| 2562 | /* Add an X-Forwarded-For header unless the source IP is |
| 2563 | * in the 'except' network range. |
| 2564 | */ |
| 2565 | if ((!s->fe->except_mask.s_addr || |
| 2566 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 2567 | != s->fe->except_net.s_addr) && |
| 2568 | (!s->be->except_mask.s_addr || |
| 2569 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 2570 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2571 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | unsigned char *pn; |
| 2573 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2574 | |
| 2575 | /* Note: we rely on the backend to get the header name to be used for |
| 2576 | * x-forwarded-for, because the header is really meant for the backends. |
| 2577 | * However, if the backend did not specify any option, we have to rely |
| 2578 | * on the frontend's header name. |
| 2579 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2580 | if (s->be->fwdfor_hdr_len) { |
| 2581 | len = s->be->fwdfor_hdr_len; |
| 2582 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2583 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2584 | len = s->fe->fwdfor_hdr_len; |
| 2585 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 2586 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2587 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 2588 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2589 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2590 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2591 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2592 | } |
| 2593 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2594 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 2595 | /* FIXME: for the sake of completeness, we should also support |
| 2596 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2597 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2598 | int len; |
| 2599 | char pn[INET6_ADDRSTRLEN]; |
| 2600 | inet_ntop(AF_INET6, |
| 2601 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2602 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | /* Note: we rely on the backend to get the header name to be used for |
| 2605 | * x-forwarded-for, because the header is really meant for the backends. |
| 2606 | * However, if the backend did not specify any option, we have to rely |
| 2607 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2608 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2609 | if (s->be->fwdfor_hdr_len) { |
| 2610 | len = s->be->fwdfor_hdr_len; |
| 2611 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 2612 | } else { |
| 2613 | len = s->fe->fwdfor_hdr_len; |
| 2614 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2615 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2616 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2617 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2618 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2619 | &txn->hdr_idx, trash, len)) < 0) |
| 2620 | goto return_bad_req; |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2625 | * 10: add X-Original-To if either the frontend or the backend |
| 2626 | * asks for it. |
| 2627 | */ |
| 2628 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 2629 | |
| 2630 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 2631 | if (s->cli_addr.ss_family == AF_INET) { |
| 2632 | /* Add an X-Original-To header unless the destination IP is |
| 2633 | * in the 'except' network range. |
| 2634 | */ |
| 2635 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 2636 | get_frt_addr(s); |
| 2637 | |
| 2638 | if ((!s->fe->except_mask_to.s_addr || |
| 2639 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 2640 | != s->fe->except_to.s_addr) && |
| 2641 | (!s->be->except_mask_to.s_addr || |
| 2642 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 2643 | != s->be->except_to.s_addr)) { |
| 2644 | int len; |
| 2645 | unsigned char *pn; |
| 2646 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 2647 | |
| 2648 | /* Note: we rely on the backend to get the header name to be used for |
| 2649 | * x-original-to, because the header is really meant for the backends. |
| 2650 | * However, if the backend did not specify any option, we have to rely |
| 2651 | * on the frontend's header name. |
| 2652 | */ |
| 2653 | if (s->be->orgto_hdr_len) { |
| 2654 | len = s->be->orgto_hdr_len; |
| 2655 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 2656 | } else { |
| 2657 | len = s->fe->orgto_hdr_len; |
| 2658 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 2659 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2660 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 2661 | |
| 2662 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2663 | &txn->hdr_idx, trash, len)) < 0) |
| 2664 | goto return_bad_req; |
| 2665 | } |
| 2666 | } |
| 2667 | } |
| 2668 | |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2669 | /* 11: add "Connection: close" if needed and not yet set. */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2670 | if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) { |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2671 | if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2672 | "Connection: close", 17)) < 0) |
| 2673 | goto return_bad_req; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 2674 | txn->flags |= TX_REQ_CONN_CLO; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2676 | |
| 2677 | /* If we have no server assigned yet and we're balancing on url_param |
| 2678 | * with a POST request, we may be interested in checking the body for |
| 2679 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2680 | */ |
| 2681 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2682 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2683 | s->be->url_param_post_limit != 0 && |
| 2684 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2685 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2686 | buffer_dont_connect(req); |
| 2687 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2688 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2689 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2690 | /************************************************************* |
| 2691 | * OK, that's finished for the headers. We have done what we * |
| 2692 | * could. Let's switch to the DATA state. * |
| 2693 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2694 | req->analyse_exp = TICK_ETERNITY; |
| 2695 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2696 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 2697 | buffer_set_rlim(req, req->size); /* no more rewrite needed */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2698 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2699 | /* OK let's go on with the BODY now */ |
| 2700 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2701 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2702 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2703 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2704 | /* we detected a parsing error. We want to archive this request |
| 2705 | * in the dedicated proxy area for later troubleshooting. |
| 2706 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2707 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2708 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2709 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2710 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2711 | txn->status = 400; |
| 2712 | req->analysers = 0; |
| 2713 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2714 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2715 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2716 | if (s->listener->counters) |
| 2717 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2718 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2719 | if (!(s->flags & SN_ERR_MASK)) |
| 2720 | s->flags |= SN_ERR_PRXCOND; |
| 2721 | if (!(s->flags & SN_FINST_MASK)) |
| 2722 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2723 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2724 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2725 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2726 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 2727 | * returns zero, at the beginning because it prevents any other processing |
| 2728 | * from occurring, and at the end because it terminates the request. |
| 2729 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2730 | int http_process_tarpit(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2731 | { |
| 2732 | struct http_txn *txn = &s->txn; |
| 2733 | |
| 2734 | /* This connection is being tarpitted. The CLIENT side has |
| 2735 | * already set the connect expiration date to the right |
| 2736 | * timeout. We just have to check that the client is still |
| 2737 | * there and that the timeout has not expired. |
| 2738 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2739 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2740 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 2741 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2742 | return 0; |
| 2743 | |
| 2744 | /* We will set the queue timer to the time spent, just for |
| 2745 | * logging purposes. We fake a 500 server error, so that the |
| 2746 | * attacker will not suspect his connection has been tarpitted. |
| 2747 | * It will not cause trouble to the logs because we can exclude |
| 2748 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2749 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2750 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 2751 | |
| 2752 | txn->status = 500; |
| 2753 | if (req->flags != BF_READ_ERROR) |
| 2754 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 2755 | |
| 2756 | req->analysers = 0; |
| 2757 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2758 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2759 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2760 | if (s->listener->counters) |
| 2761 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2762 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2763 | if (!(s->flags & SN_ERR_MASK)) |
| 2764 | s->flags |= SN_ERR_PRXCOND; |
| 2765 | if (!(s->flags & SN_FINST_MASK)) |
| 2766 | s->flags |= SN_FINST_T; |
| 2767 | return 0; |
| 2768 | } |
| 2769 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2770 | /* This function is an analyser which processes the HTTP request body. It looks |
| 2771 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 2772 | * must only be called after the standard HTTP request processing has occurred, |
| 2773 | * because it expects the request to be parsed. It returns zero if it needs to |
| 2774 | * read more data, or 1 once it has completed its analysis. |
| 2775 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2776 | int http_process_request_body(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2777 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2778 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2779 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2780 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2781 | |
| 2782 | /* We have to parse the HTTP request body to find any required data. |
| 2783 | * "balance url_param check_post" should have been the only way to get |
| 2784 | * into this. We were brought here after HTTP header analysis, so all |
| 2785 | * related structures are ready. |
| 2786 | */ |
| 2787 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2788 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 2789 | goto missing_data; |
| 2790 | |
| 2791 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2792 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2793 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2794 | */ |
| 2795 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2796 | struct hdr_ctx ctx; |
| 2797 | ctx.idx = 0; |
| 2798 | /* Expect is allowed in 1.1, look for it */ |
| 2799 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 2800 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 2801 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 2802 | } |
| 2803 | } |
| 2804 | msg->msg_state = HTTP_MSG_100_SENT; |
| 2805 | } |
| 2806 | |
| 2807 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 2808 | if (txn->flags & TX_REQ_TE_CHNK) |
| 2809 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2810 | else |
| 2811 | msg->msg_state = HTTP_MSG_DATA; |
| 2812 | } |
| 2813 | |
| 2814 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
| 2815 | unsigned long body = msg->col; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2816 | unsigned int chunk = 0; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2817 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2818 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2819 | int c = hex2i(msg->sol[body]); |
| 2820 | if (c < 0) |
| 2821 | goto return_bad_req; |
| 2822 | chunk = (chunk << 4) + c; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2823 | body++; |
| 2824 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2825 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2826 | /* now we want CRLF */ |
| 2827 | if (body + 2 >= req->l) /* we want CRLF too */ |
| 2828 | goto missing_data; /* end of buffer? data missing! */ |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2829 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2830 | if ((msg->sol[body++] != '\r') || (msg->sol[body++] != '\n')) |
| 2831 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2832 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2833 | msg->sov = body; |
| 2834 | txn->req.hdr_content_len = chunk; |
| 2835 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2838 | /* Now we're in HTTP_MSG_DATA state. |
| 2839 | * We have the first non-header byte in msg->col, which is either the |
| 2840 | * beginning of the chunk size or of the data. The first data byte is in |
| 2841 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 2842 | * We're waiting for at least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2843 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2844 | |
| 2845 | if (msg->hdr_content_len < limit) |
| 2846 | limit = msg->hdr_content_len; |
| 2847 | |
| 2848 | if (req->l - msg->sov >= limit) /* we have enough bytes now */ |
| 2849 | goto http_end; |
| 2850 | |
| 2851 | missing_data: |
| 2852 | /* we get here if we need to wait for more data */ |
| 2853 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 2854 | txn->status = 408; |
| 2855 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2856 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2857 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2858 | |
| 2859 | /* we get here if we need to wait for more data */ |
| 2860 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2861 | /* Not enough data. We'll re-use the http-request |
| 2862 | * timeout here. Ideally, we should set the timeout |
| 2863 | * relative to the accept() date. We just set the |
| 2864 | * request timeout once at the beginning of the |
| 2865 | * request. |
| 2866 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2867 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2868 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 2869 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2870 | return 0; |
| 2871 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 2872 | |
| 2873 | http_end: |
| 2874 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 2875 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2876 | req->analysers &= ~an_bit; |
| 2877 | req->analyse_exp = TICK_ETERNITY; |
| 2878 | return 1; |
| 2879 | |
| 2880 | return_bad_req: /* let's centralize all bad requests */ |
| 2881 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2882 | txn->status = 400; |
| 2883 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2884 | |
| 2885 | return_err_msg: |
| 2886 | req->analysers = 0; |
| 2887 | s->fe->counters.failed_req++; |
| 2888 | if (s->listener->counters) |
| 2889 | s->listener->counters->failed_req++; |
| 2890 | |
| 2891 | if (!(s->flags & SN_ERR_MASK)) |
| 2892 | s->flags |= SN_ERR_PRXCOND; |
| 2893 | if (!(s->flags & SN_FINST_MASK)) |
| 2894 | s->flags |= SN_FINST_R; |
| 2895 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2896 | } |
| 2897 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2898 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 2899 | * processing can continue on next analysers, or zero if it either needs more |
| 2900 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 2901 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 2902 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2903 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2904 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2905 | int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2906 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2907 | struct http_txn *txn = &s->txn; |
| 2908 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 2909 | struct hdr_ctx ctx; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2910 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 2911 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2912 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2913 | 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] | 2914 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2915 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2916 | rep, |
| 2917 | rep->rex, rep->wex, |
| 2918 | rep->flags, |
| 2919 | rep->l, |
| 2920 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2921 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2922 | /* |
| 2923 | * Now parse the partial (or complete) lines. |
| 2924 | * We will check the response syntax, and also join multi-line |
| 2925 | * headers. An index of all the lines will be elaborated while |
| 2926 | * parsing. |
| 2927 | * |
| 2928 | * For the parsing, we use a 28 states FSM. |
| 2929 | * |
| 2930 | * Here is the information we currently have : |
| 2931 | * rep->data + rep->som = beginning of response |
| 2932 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2933 | * rep->data + rep->eol = end of current header or line (LF or CRLF) |
| 2934 | * rep->lr = first non-visited byte |
| 2935 | * rep->r = end of data |
| 2936 | */ |
| 2937 | |
| 2938 | if (likely(rep->lr < rep->r)) |
| 2939 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2940 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2941 | /* 1: we might have to print this header in debug mode */ |
| 2942 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2943 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2944 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2945 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2946 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2947 | sol = rep->data + msg->som; |
| 2948 | eol = sol + msg->sl.rq.l; |
| 2949 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2950 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2951 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2952 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2953 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2954 | while (cur_idx) { |
| 2955 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2956 | debug_hdr("srvhdr", s, sol, eol); |
| 2957 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2958 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2959 | } |
| 2960 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2961 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2962 | /* |
| 2963 | * Now we quickly check if we have found a full valid response. |
| 2964 | * If not so, we check the FD and buffer states before leaving. |
| 2965 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2966 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2967 | * responses are checked first. |
| 2968 | * |
| 2969 | * Depending on whether the client is still there or not, we |
| 2970 | * may send an error response back or not. Note that normally |
| 2971 | * we should only check for HTTP status there, and check I/O |
| 2972 | * errors somewhere else. |
| 2973 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2974 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2975 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2976 | /* Invalid response */ |
| 2977 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2978 | /* we detected a parsing error. We want to archive this response |
| 2979 | * in the dedicated proxy area for later troubleshooting. |
| 2980 | */ |
| 2981 | hdr_response_bad: |
| 2982 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 2983 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 2984 | |
| 2985 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 2986 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2987 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 2988 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 2989 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 2990 | |
| 2991 | rep->analysers = 0; |
| 2992 | txn->status = 502; |
| 2993 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 2994 | |
| 2995 | if (!(s->flags & SN_ERR_MASK)) |
| 2996 | s->flags |= SN_ERR_PRXCOND; |
| 2997 | if (!(s->flags & SN_FINST_MASK)) |
| 2998 | s->flags |= SN_FINST_H; |
| 2999 | |
| 3000 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3001 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3002 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3003 | /* too large response does not fit in buffer. */ |
| 3004 | else if (rep->flags & BF_FULL) { |
| 3005 | goto hdr_response_bad; |
| 3006 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3007 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3008 | /* read error */ |
| 3009 | else if (rep->flags & BF_READ_ERROR) { |
| 3010 | if (msg->err_pos >= 0) |
| 3011 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3012 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3013 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3014 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3015 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3016 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 3017 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3018 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3019 | rep->analysers = 0; |
| 3020 | txn->status = 502; |
| 3021 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3022 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3023 | if (!(s->flags & SN_ERR_MASK)) |
| 3024 | s->flags |= SN_ERR_SRVCL; |
| 3025 | if (!(s->flags & SN_FINST_MASK)) |
| 3026 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3027 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3028 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3029 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3030 | /* read timeout : return a 504 to the client. */ |
| 3031 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 3032 | if (msg->err_pos >= 0) |
| 3033 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3034 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3035 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3036 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3037 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3038 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 3039 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3040 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3041 | rep->analysers = 0; |
| 3042 | txn->status = 504; |
| 3043 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3044 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3045 | if (!(s->flags & SN_ERR_MASK)) |
| 3046 | s->flags |= SN_ERR_SRVTO; |
| 3047 | if (!(s->flags & SN_FINST_MASK)) |
| 3048 | s->flags |= SN_FINST_H; |
| 3049 | return 0; |
| 3050 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3051 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3052 | /* close from server */ |
| 3053 | else if (rep->flags & BF_SHUTR) { |
| 3054 | if (msg->err_pos >= 0) |
| 3055 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3056 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3057 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3058 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3059 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3060 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 3061 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3062 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3063 | rep->analysers = 0; |
| 3064 | txn->status = 502; |
| 3065 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3066 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3067 | if (!(s->flags & SN_ERR_MASK)) |
| 3068 | s->flags |= SN_ERR_SRVCL; |
| 3069 | if (!(s->flags & SN_FINST_MASK)) |
| 3070 | s->flags |= SN_FINST_H; |
| 3071 | return 0; |
| 3072 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3073 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3074 | /* write error to client (we don't send any message then) */ |
| 3075 | else if (rep->flags & BF_WRITE_ERROR) { |
| 3076 | if (msg->err_pos >= 0) |
| 3077 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3078 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3079 | s->be->counters.failed_resp++; |
| 3080 | rep->analysers = 0; |
| 3081 | |
| 3082 | if (!(s->flags & SN_ERR_MASK)) |
| 3083 | s->flags |= SN_ERR_CLICL; |
| 3084 | if (!(s->flags & SN_FINST_MASK)) |
| 3085 | s->flags |= SN_FINST_H; |
| 3086 | |
| 3087 | /* process_session() will take care of the error */ |
| 3088 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3089 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3090 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3091 | buffer_dont_close(rep); |
| 3092 | return 0; |
| 3093 | } |
| 3094 | |
| 3095 | /* More interesting part now : we know that we have a complete |
| 3096 | * response which at least looks like HTTP. We have an indicator |
| 3097 | * of each header's length, so we can parse them quickly. |
| 3098 | */ |
| 3099 | |
| 3100 | if (unlikely(msg->err_pos >= 0)) |
| 3101 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 3102 | |
| 3103 | /* ensure we keep this pointer to the beginning of the message */ |
| 3104 | msg->sol = rep->data + msg->som; |
| 3105 | |
| 3106 | /* |
| 3107 | * 1: get the status code |
| 3108 | */ |
| 3109 | n = rep->data[msg->sl.st.c] - '0'; |
| 3110 | if (n < 1 || n > 5) |
| 3111 | n = 0; |
| 3112 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3113 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 3114 | /* check if the response is HTTP/1.1 or above */ |
| 3115 | if ((msg->sl.st.v_l == 8) && |
| 3116 | ((rep->data[msg->som + 5] > '1') || |
| 3117 | ((rep->data[msg->som + 5] == '1') && |
| 3118 | (rep->data[msg->som + 7] >= '1')))) |
| 3119 | txn->flags |= TX_RES_VER_11; |
| 3120 | |
| 3121 | /* "connection" has not been parsed yet */ |
| 3122 | txn->flags &= ~TX_CON_HDR_PARS; |
| 3123 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3124 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
| 3125 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3126 | if (txn->status >= 100 && txn->status < 500) |
| 3127 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 3128 | else |
| 3129 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 3130 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3131 | /* |
| 3132 | * 2: check for cacheability. |
| 3133 | */ |
| 3134 | |
| 3135 | switch (txn->status) { |
| 3136 | case 200: |
| 3137 | case 203: |
| 3138 | case 206: |
| 3139 | case 300: |
| 3140 | case 301: |
| 3141 | case 410: |
| 3142 | /* RFC2616 @13.4: |
| 3143 | * "A response received with a status code of |
| 3144 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3145 | * by a cache (...) unless a cache-control |
| 3146 | * directive prohibits caching." |
| 3147 | * |
| 3148 | * RFC2616 @9.5: POST method : |
| 3149 | * "Responses to this method are not cacheable, |
| 3150 | * unless the response includes appropriate |
| 3151 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3152 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3153 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3154 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3155 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3156 | break; |
| 3157 | default: |
| 3158 | break; |
| 3159 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3160 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3161 | /* |
| 3162 | * 3: we may need to capture headers |
| 3163 | */ |
| 3164 | s->logs.logwait &= ~LW_RESP; |
| 3165 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
| 3166 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3167 | txn->rsp.cap, s->fe->rsp_cap); |
| 3168 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3169 | /* 4: determine if we have a transfer-encoding or content-length. |
| 3170 | * RFC2616 #4.4 states that : |
| 3171 | * - Any response which "MUST NOT" include a message-body (such as the |
| 3172 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 3173 | * always terminated by the first empty line after the header fields, |
| 3174 | * regardless of the entity-header fields present in the message. |
| 3175 | * |
| 3176 | * - If a Transfer-Encoding header field is present and has any value |
| 3177 | * other than "identity", then the transfer-length is defined by use |
| 3178 | * of the "chunked" transfer-coding ; |
| 3179 | * |
| 3180 | * - If a Content-Length header field is present, its decimal value in |
| 3181 | * OCTETs represents both the entity-length and the transfer-length. |
| 3182 | * If a message is received with both a Transfer-Encoding header |
| 3183 | * field and a Content-Length header field, the latter MUST be ignored. |
| 3184 | */ |
| 3185 | |
| 3186 | /* Skip parsing if no content length is possible. The response flags |
| 3187 | * remain 0 as well as the hdr_content_len, which may or may not mirror |
| 3188 | * the real header value. |
| 3189 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 3190 | */ |
| 3191 | if (txn->meth == HTTP_METH_HEAD || |
| 3192 | (txn->status >= 100 && txn->status < 200) || |
| 3193 | txn->status == 204 || txn->status == 304) |
| 3194 | goto skip_content_length; |
| 3195 | |
| 3196 | /* FIXME: chunked encoding is HTTP/1.1 only */ |
| 3197 | ctx.idx = 0; |
| 3198 | while (http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3199 | if (ctx.vlen == 8 && strncasecmp(ctx.line + ctx.val, "identity", 8) == 0) |
| 3200 | continue; |
| 3201 | txn->flags |= TX_RES_TE_CHNK; |
| 3202 | break; |
| 3203 | } |
| 3204 | |
| 3205 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 3206 | ctx.idx = 0; |
| 3207 | while (!(txn->flags & TX_RES_TE_CHNK) && |
| 3208 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3209 | signed long long cl; |
| 3210 | |
| 3211 | if (!ctx.vlen) |
| 3212 | goto hdr_response_bad; |
| 3213 | |
| 3214 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 3215 | goto hdr_response_bad; /* parse failure */ |
| 3216 | |
| 3217 | if (cl < 0) |
| 3218 | goto hdr_response_bad; |
| 3219 | |
| 3220 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 3221 | goto hdr_response_bad; /* already specified, was different */ |
| 3222 | |
| 3223 | txn->flags |= TX_RES_CNT_LEN; |
| 3224 | msg->hdr_content_len = cl; |
| 3225 | } |
| 3226 | |
| 3227 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3228 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3229 | /* end of job, return OK */ |
| 3230 | rep->analysers &= ~an_bit; |
| 3231 | rep->analyse_exp = TICK_ETERNITY; |
| 3232 | return 1; |
| 3233 | } |
| 3234 | |
| 3235 | /* This function performs all the processing enabled for the current response. |
| 3236 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 3237 | * called again after other functions. It relies on buffers flags, and updates |
| 3238 | * t->rep->analysers. It might make sense to explode it into several other |
| 3239 | * functions. It works like process_request (see indications above). |
| 3240 | */ |
| 3241 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 3242 | { |
| 3243 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3244 | struct http_msg *msg = &txn->rsp; |
| 3245 | struct proxy *cur_proxy; |
| 3246 | int cur_idx; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 3247 | int conn_ka = 0, conn_cl = 0; |
| 3248 | int must_close = 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3249 | |
| 3250 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3251 | now_ms, __FUNCTION__, |
| 3252 | t, |
| 3253 | rep, |
| 3254 | rep->rex, rep->wex, |
| 3255 | rep->flags, |
| 3256 | rep->l, |
| 3257 | rep->analysers); |
| 3258 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3259 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3260 | return 0; |
| 3261 | |
| 3262 | rep->analysers &= ~an_bit; |
| 3263 | rep->analyse_exp = TICK_ETERNITY; |
| 3264 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 3265 | /* Now we have to check if we need to modify the Connection header. |
| 3266 | * This is more difficult on the response than it is on the request, |
| 3267 | * because we can have two different HTTP versions and we don't know |
| 3268 | * how the client will interprete a response. For instance, let's say |
| 3269 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 3270 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 3271 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 3272 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 3273 | * the lack of header as a keep-alive. Thus we will use two flags |
| 3274 | * indicating how a request MAY be understood by the client. In case |
| 3275 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 3276 | * ambiguous cases such as both close and keepalive are seen, then we |
| 3277 | * will fall back to explicit close. Note that we won't take risks with |
| 3278 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
| 3279 | */ |
| 3280 | |
| 3281 | if ((txn->meth != HTTP_METH_CONNECT) && |
| 3282 | (txn->status >= 200) && |
| 3283 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN && |
| 3284 | !(txn->flags & TX_CON_HDR_PARS)) { |
| 3285 | int may_keep = 0, may_close = 0; /* how it may be understood */ |
| 3286 | struct hdr_ctx ctx; |
| 3287 | |
| 3288 | ctx.idx = 0; |
| 3289 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3290 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 3291 | conn_cl = 1; |
| 3292 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 3293 | conn_ka = 1; |
| 3294 | } |
| 3295 | |
| 3296 | if (conn_cl) { |
| 3297 | /* close header present */ |
| 3298 | may_close = 1; |
| 3299 | if (conn_ka) /* we have both close and keep-alive */ |
| 3300 | may_keep = 1; |
| 3301 | } |
| 3302 | else if (conn_ka) { |
| 3303 | /* keep-alive alone */ |
| 3304 | may_keep = 1; |
| 3305 | } |
| 3306 | else { |
| 3307 | /* no close nor keep-alive header */ |
| 3308 | if (txn->flags & TX_RES_VER_11) |
| 3309 | may_keep = 1; |
| 3310 | else |
| 3311 | may_close = 1; |
| 3312 | |
| 3313 | if (txn->flags & TX_REQ_VER_11) |
| 3314 | may_keep = 1; |
| 3315 | else |
| 3316 | may_close = 1; |
| 3317 | } |
| 3318 | |
| 3319 | /* let's update the transaction status to reflect any close. |
| 3320 | * Note that ambiguous cases with keep & close will also be |
| 3321 | * handled. |
| 3322 | */ |
| 3323 | if (may_close) |
| 3324 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3325 | |
| 3326 | /* Now we must adjust the response header : |
| 3327 | * - set "close" if may_keep and WANT_CLO |
| 3328 | * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK) |
| 3329 | * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length |
| 3330 | * |
| 3331 | * Until we support the server-close mode, we'll only support the set "close". |
| 3332 | */ |
| 3333 | if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) |
| 3334 | must_close = 1; |
| 3335 | |
| 3336 | txn->flags |= TX_CON_HDR_PARS; |
| 3337 | } |
| 3338 | |
| 3339 | /* We might have to check for "Connection:" if the server |
| 3340 | * returns a connection status that is not compatible with |
| 3341 | * the client's or with the config. |
| 3342 | */ |
| 3343 | if ((txn->status >= 200) && must_close && (conn_cl|conn_ka)) { |
| 3344 | char *cur_ptr, *cur_end, *cur_next; |
| 3345 | int cur_idx, old_idx, delta, val; |
| 3346 | int must_delete; |
| 3347 | struct hdr_idx_elem *cur_hdr; |
| 3348 | |
| 3349 | /* we just have to remove the headers if both sides are 1.0 */ |
| 3350 | must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11); |
| 3351 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3352 | |
| 3353 | for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) { |
| 3354 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3355 | cur_ptr = cur_next; |
| 3356 | cur_end = cur_ptr + cur_hdr->len; |
| 3357 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3358 | |
| 3359 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3360 | if (!val) |
| 3361 | continue; |
| 3362 | |
| 3363 | /* 3 possibilities : |
| 3364 | * - we have already set "Connection: close" or we're in |
| 3365 | * HTTP/1.0, so we remove this line. |
| 3366 | * - we have not yet set "Connection: close", but this line |
| 3367 | * indicates close. We leave it untouched and set the flag. |
| 3368 | * - we have not yet set "Connection: close", and this line |
| 3369 | * indicates non-close. We replace it and set the flag. |
| 3370 | */ |
| 3371 | if (must_delete) { |
| 3372 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3373 | http_msg_move_end(&txn->rsp, delta); |
| 3374 | cur_next += delta; |
| 3375 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3376 | txn->hdr_idx.used--; |
| 3377 | cur_hdr->len = 0; |
| 3378 | must_close = 0; |
| 3379 | } else { |
| 3380 | if (cur_end - cur_ptr - val != 5 || |
| 3381 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3382 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3383 | "close", 5); |
| 3384 | cur_next += delta; |
| 3385 | cur_hdr->len += delta; |
| 3386 | http_msg_move_end(&txn->rsp, delta); |
| 3387 | } |
| 3388 | must_delete = 1; |
| 3389 | must_close = 0; |
| 3390 | } |
| 3391 | } /* for loop */ |
| 3392 | } /* if must close keep-alive */ |
| 3393 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3394 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3395 | /* |
| 3396 | * 3: we will have to evaluate the filters. |
| 3397 | * As opposed to version 1.2, now they will be evaluated in the |
| 3398 | * filters order and not in the header order. This means that |
| 3399 | * each filter has to be validated among all headers. |
| 3400 | * |
| 3401 | * Filters are tried with ->be first, then with ->fe if it is |
| 3402 | * different from ->be. |
| 3403 | */ |
| 3404 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3405 | cur_proxy = t->be; |
| 3406 | while (1) { |
| 3407 | struct proxy *rule_set = cur_proxy; |
| 3408 | |
| 3409 | /* try headers filters */ |
| 3410 | if (rule_set->rsp_exp != NULL) { |
| 3411 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3412 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3413 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3414 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3415 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 3416 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3417 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3418 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3419 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3420 | txn->status = 502; |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 3421 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3422 | if (!(t->flags & SN_ERR_MASK)) |
| 3423 | t->flags |= SN_ERR_PRXCOND; |
| 3424 | if (!(t->flags & SN_FINST_MASK)) |
| 3425 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3426 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3427 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +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 | /* has the response been denied ? */ |
| 3431 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3432 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3433 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3434 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3435 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3436 | if (t->listener->counters) |
| 3437 | t->listener->counters->denied_resp++; |
| 3438 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3439 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3440 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3441 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3442 | /* add response headers from the rule sets in the same order */ |
| 3443 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3444 | if (txn->status < 200) |
| 3445 | break; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3446 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3447 | rule_set->rsp_add[cur_idx])) < 0) |
| 3448 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3449 | } |
| 3450 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3451 | /* check whether we're already working on the frontend */ |
| 3452 | if (cur_proxy == t->fe) |
| 3453 | break; |
| 3454 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3455 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3456 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3457 | /* |
| 3458 | * 4: check for server cookie. |
| 3459 | */ |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3460 | if ((t->be->cookie_name || t->be->appsession_name || t->fe->capture_name |
| 3461 | || (t->be->options & PR_O_CHK_CACHE)) && txn->status >= 200) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3462 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3463 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3464 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3465 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3466 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3467 | */ |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3468 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0 && txn->status >= 200) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3469 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3470 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3471 | /* |
| 3472 | * 6: add server cookie in the response if needed |
| 3473 | */ |
| 3474 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3475 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 3476 | txn->status >= 200) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3477 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3478 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3479 | /* the server is known, it's not the one the client requested, we have to |
| 3480 | * insert a set-cookie here, except if we want to insert only on POST |
| 3481 | * requests and this one isn't. Note that servers which don't have cookies |
| 3482 | * (eg: some backup servers) will return a full cookie removal request. |
| 3483 | */ |
| 3484 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3485 | t->be->cookie_name, |
| 3486 | 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] | 3487 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3488 | if (t->be->cookie_domain) |
| 3489 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3490 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3491 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3492 | trash, len)) < 0) |
| 3493 | goto return_bad_resp; |
| 3494 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3495 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3496 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3497 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3498 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3499 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3500 | */ |
| 3501 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3502 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3503 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3504 | |
| 3505 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3506 | "Cache-control: private", 22)) < 0) |
| 3507 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3508 | } |
| 3509 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3510 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3511 | /* |
| 3512 | * 7: check if result will be cacheable with a cookie. |
| 3513 | * We'll block the response if security checks have caught |
| 3514 | * nasty things such as a cacheable cookie. |
| 3515 | */ |
| 3516 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3517 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3518 | (t->be->options & PR_O_CHK_CACHE) && |
| 3519 | txn->status >= 200) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3520 | |
| 3521 | /* we're in presence of a cacheable response containing |
| 3522 | * a set-cookie header. We'll block it as requested by |
| 3523 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3524 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3525 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3526 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3527 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3528 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3529 | if (t->listener->counters) |
| 3530 | t->listener->counters->denied_resp++; |
| 3531 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3532 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3533 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3534 | send_log(t->be, LOG_ALERT, |
| 3535 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3536 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3537 | goto return_srv_prx_502; |
| 3538 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3539 | |
| 3540 | /* |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 3541 | * 8: add "Connection: close" if needed and not yet set. This is |
| 3542 | * only needed for 1.1 responses since we know there is no other |
| 3543 | * Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3544 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 3545 | if (txn->status >= 200 && must_close && (txn->flags & TX_RES_VER_11)) { |
| 3546 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3547 | "Connection: close", 17)) < 0) |
| 3548 | goto return_bad_resp; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame^] | 3549 | must_close = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3550 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3551 | |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3552 | /* |
| 3553 | * 9: we may be facing a 1xx response (100 continue, 101 switching protocols), |
| 3554 | * in which case this is not the right response, and we're waiting for the |
| 3555 | * next one. Let's allow this response to go to the client and wait for the |
| 3556 | * next one. |
| 3557 | */ |
| 3558 | if (txn->status < 200) { |
| 3559 | hdr_idx_init(&txn->hdr_idx); |
| 3560 | buffer_forward(rep, rep->lr - (rep->data + msg->som)); |
| 3561 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 3562 | txn->status = 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3563 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 3564 | return 1; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3565 | } |
| 3566 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3567 | /************************************************************* |
| 3568 | * OK, that's finished for the headers. We have done what we * |
| 3569 | * could. Let's switch to the DATA state. * |
| 3570 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3571 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 3572 | buffer_set_rlim(rep, rep->size); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3573 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3574 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3575 | /* if the user wants to log as soon as possible, without counting |
| 3576 | * bytes from the server, then this is the right moment. We have |
| 3577 | * to temporarily assign bytes_out to log what we currently have. |
| 3578 | */ |
| 3579 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3580 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3581 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 3582 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3583 | t->logs.bytes_out = 0; |
| 3584 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3585 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3586 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3587 | * otherwise we would not let the client side wake up. |
| 3588 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3589 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3590 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3591 | } |
| 3592 | return 0; |
| 3593 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3594 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3595 | /* Iterate the same filter through all request headers. |
| 3596 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3597 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3598 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3599 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3600 | 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] | 3601 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3602 | char term; |
| 3603 | char *cur_ptr, *cur_end, *cur_next; |
| 3604 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3605 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3606 | struct hdr_idx_elem *cur_hdr; |
| 3607 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3608 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3609 | last_hdr = 0; |
| 3610 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3611 | 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] | 3612 | old_idx = 0; |
| 3613 | |
| 3614 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3615 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3616 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3617 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3618 | (exp->action == ACT_ALLOW || |
| 3619 | exp->action == ACT_DENY || |
| 3620 | exp->action == ACT_TARPIT)) |
| 3621 | return 0; |
| 3622 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3623 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3624 | if (!cur_idx) |
| 3625 | break; |
| 3626 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3627 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3628 | cur_ptr = cur_next; |
| 3629 | cur_end = cur_ptr + cur_hdr->len; |
| 3630 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3631 | |
| 3632 | /* Now we have one header between cur_ptr and cur_end, |
| 3633 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3634 | */ |
| 3635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3636 | /* The annoying part is that pattern matching needs |
| 3637 | * that we modify the contents to null-terminate all |
| 3638 | * strings before testing them. |
| 3639 | */ |
| 3640 | |
| 3641 | term = *cur_end; |
| 3642 | *cur_end = '\0'; |
| 3643 | |
| 3644 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3645 | switch (exp->action) { |
| 3646 | case ACT_SETBE: |
| 3647 | /* It is not possible to jump a second time. |
| 3648 | * FIXME: should we return an HTTP/500 here so that |
| 3649 | * the admin knows there's a problem ? |
| 3650 | */ |
| 3651 | if (t->be != t->fe) |
| 3652 | break; |
| 3653 | |
| 3654 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3655 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3656 | last_hdr = 1; |
| 3657 | break; |
| 3658 | |
| 3659 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3660 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3661 | last_hdr = 1; |
| 3662 | break; |
| 3663 | |
| 3664 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3665 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3666 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3667 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3668 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3669 | if (t->listener->counters) |
| 3670 | t->listener->counters->denied_resp++; |
| 3671 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3672 | break; |
| 3673 | |
| 3674 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3675 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3676 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3677 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3678 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3679 | if (t->listener->counters) |
| 3680 | t->listener->counters->denied_resp++; |
| 3681 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3682 | break; |
| 3683 | |
| 3684 | case ACT_REPLACE: |
| 3685 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3686 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3687 | /* FIXME: if the user adds a newline in the replacement, the |
| 3688 | * index will not be recalculated for now, and the new line |
| 3689 | * will not be counted as a new header. |
| 3690 | */ |
| 3691 | |
| 3692 | cur_end += delta; |
| 3693 | cur_next += delta; |
| 3694 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 3695 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3696 | break; |
| 3697 | |
| 3698 | case ACT_REMOVE: |
| 3699 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3700 | cur_next += delta; |
| 3701 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 3702 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3703 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3704 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3705 | cur_hdr->len = 0; |
| 3706 | cur_end = NULL; /* null-term has been rewritten */ |
| 3707 | break; |
| 3708 | |
| 3709 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3710 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3711 | if (cur_end) |
| 3712 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3713 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3714 | /* keep the link from this header to next one in case of later |
| 3715 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3716 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3717 | old_idx = cur_idx; |
| 3718 | } |
| 3719 | return 0; |
| 3720 | } |
| 3721 | |
| 3722 | |
| 3723 | /* Apply the filter to the request line. |
| 3724 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3725 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3726 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3727 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3728 | */ |
| 3729 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3730 | { |
| 3731 | char term; |
| 3732 | char *cur_ptr, *cur_end; |
| 3733 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3734 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3735 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3736 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3737 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3738 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3739 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3740 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3741 | (exp->action == ACT_ALLOW || |
| 3742 | exp->action == ACT_DENY || |
| 3743 | exp->action == ACT_TARPIT)) |
| 3744 | return 0; |
| 3745 | else if (exp->action == ACT_REMOVE) |
| 3746 | return 0; |
| 3747 | |
| 3748 | done = 0; |
| 3749 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3750 | 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] | 3751 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3752 | |
| 3753 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3754 | |
| 3755 | /* The annoying part is that pattern matching needs |
| 3756 | * that we modify the contents to null-terminate all |
| 3757 | * strings before testing them. |
| 3758 | */ |
| 3759 | |
| 3760 | term = *cur_end; |
| 3761 | *cur_end = '\0'; |
| 3762 | |
| 3763 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3764 | switch (exp->action) { |
| 3765 | case ACT_SETBE: |
| 3766 | /* It is not possible to jump a second time. |
| 3767 | * FIXME: should we return an HTTP/500 here so that |
| 3768 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3769 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3770 | if (t->be != t->fe) |
| 3771 | break; |
| 3772 | |
| 3773 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3774 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3775 | done = 1; |
| 3776 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3777 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3778 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3779 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3780 | done = 1; |
| 3781 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3782 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3783 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3784 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3785 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3786 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3787 | if (t->listener->counters) |
| 3788 | t->listener->counters->denied_resp++; |
| 3789 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3790 | done = 1; |
| 3791 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3792 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3793 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3794 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3795 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3796 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3797 | if (t->listener->counters) |
| 3798 | t->listener->counters->denied_resp++; |
| 3799 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3800 | done = 1; |
| 3801 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3802 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3803 | case ACT_REPLACE: |
| 3804 | *cur_end = term; /* restore the string terminator */ |
| 3805 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3806 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3807 | /* FIXME: if the user adds a newline in the replacement, the |
| 3808 | * index will not be recalculated for now, and the new line |
| 3809 | * will not be counted as a new header. |
| 3810 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3811 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 3812 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3813 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3814 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3815 | 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] | 3816 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3817 | HTTP_MSG_RQMETH, |
| 3818 | cur_ptr, cur_end + 1, |
| 3819 | NULL, NULL); |
| 3820 | if (unlikely(!cur_end)) |
| 3821 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3822 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3823 | /* we have a full request and we know that we have either a CR |
| 3824 | * or an LF at <ptr>. |
| 3825 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3826 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3827 | 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] | 3828 | /* there is no point trying this regex on headers */ |
| 3829 | return 1; |
| 3830 | } |
| 3831 | } |
| 3832 | *cur_end = term; /* restore the string terminator */ |
| 3833 | return done; |
| 3834 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3835 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3836 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3837 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3838 | /* |
| 3839 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3840 | * 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] | 3841 | * unparsable request. Since it can manage the switch to another backend, it |
| 3842 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3843 | */ |
| 3844 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3845 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3846 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3847 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3848 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3849 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3850 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3851 | /* |
| 3852 | * The interleaving of transformations and verdicts |
| 3853 | * makes it difficult to decide to continue or stop |
| 3854 | * the evaluation. |
| 3855 | */ |
| 3856 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3857 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3858 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3859 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3860 | exp = exp->next; |
| 3861 | continue; |
| 3862 | } |
| 3863 | |
| 3864 | /* Apply the filter to the request line. */ |
| 3865 | ret = apply_filter_to_req_line(t, req, exp); |
| 3866 | if (unlikely(ret < 0)) |
| 3867 | return -1; |
| 3868 | |
| 3869 | if (likely(ret == 0)) { |
| 3870 | /* The filter did not match the request, it can be |
| 3871 | * iterated through all headers. |
| 3872 | */ |
| 3873 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3874 | } |
| 3875 | exp = exp->next; |
| 3876 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3877 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3881 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3882 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 3883 | * Try to retrieve the server associated to the appsession. |
| 3884 | * If the server is found, it's assigned to the session. |
| 3885 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3886 | void manage_client_side_appsession(struct session *t, const char *buf, int len) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 3887 | struct http_txn *txn = &t->txn; |
| 3888 | appsess *asession = NULL; |
| 3889 | char *sessid_temp = NULL; |
| 3890 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3891 | if (len > t->be->appsession_len) { |
| 3892 | len = t->be->appsession_len; |
| 3893 | } |
| 3894 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 3895 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 3896 | /* request-learn option is enabled : store the sessid in the session for future use */ |
| 3897 | if (t->sessid != NULL) { |
| 3898 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
| 3899 | pool_free2(apools.sessid, t->sessid); |
| 3900 | } |
| 3901 | |
| 3902 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 3903 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3904 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3905 | return; |
| 3906 | } |
| 3907 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3908 | memcpy(t->sessid, buf, len); |
| 3909 | t->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 3910 | } |
| 3911 | |
| 3912 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 3913 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3914 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3915 | return; |
| 3916 | } |
| 3917 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3918 | memcpy(sessid_temp, buf, len); |
| 3919 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 3920 | |
| 3921 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 3922 | /* free previously allocated memory */ |
| 3923 | pool_free2(apools.sessid, sessid_temp); |
| 3924 | |
| 3925 | if (asession != NULL) { |
| 3926 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 3927 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 3928 | asession->request_count++; |
| 3929 | |
| 3930 | if (asession->serverid != NULL) { |
| 3931 | struct server *srv = t->be->srv; |
| 3932 | while (srv) { |
| 3933 | if (strcmp(srv->id, asession->serverid) == 0) { |
| 3934 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
| 3935 | /* we found the server and it's usable */ |
| 3936 | txn->flags &= ~TX_CK_MASK; |
| 3937 | txn->flags |= TX_CK_VALID; |
| 3938 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 3939 | t->srv = srv; |
| 3940 | break; |
| 3941 | } else { |
| 3942 | txn->flags &= ~TX_CK_MASK; |
| 3943 | txn->flags |= TX_CK_DOWN; |
| 3944 | } |
| 3945 | } |
| 3946 | srv = srv->next; |
| 3947 | } |
| 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3953 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3954 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3955 | */ |
| 3956 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3957 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3958 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3959 | char *p1, *p2, *p3, *p4; |
| 3960 | char *del_colon, *del_cookie, *colon; |
| 3961 | int app_cookies; |
| 3962 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3963 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3964 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3965 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3966 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3967 | * we start with the start line. |
| 3968 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3969 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3970 | 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] | 3971 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3972 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3973 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3974 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3975 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3976 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3977 | cur_ptr = cur_next; |
| 3978 | cur_end = cur_ptr + cur_hdr->len; |
| 3979 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3980 | |
| 3981 | /* We have one full header between cur_ptr and cur_end, and the |
| 3982 | * next header starts at cur_next. We're only interested in |
| 3983 | * "Cookie:" headers. |
| 3984 | */ |
| 3985 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3986 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3987 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3988 | old_idx = cur_idx; |
| 3989 | continue; |
| 3990 | } |
| 3991 | |
| 3992 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3993 | * attributes whose name begin with a '$', and associate them with |
| 3994 | * the right cookie, if we want to delete this cookie. |
| 3995 | * So there are 3 cases for each cookie read : |
| 3996 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3997 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3998 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3999 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4000 | * "special" cookie. |
| 4001 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4002 | * remove it. If no application cookie persists in the header, we |
| 4003 | * *MUST* delete it |
| 4004 | */ |
| 4005 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4006 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4007 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4008 | /* del_cookie == NULL => nothing to be deleted */ |
| 4009 | del_colon = del_cookie = NULL; |
| 4010 | app_cookies = 0; |
| 4011 | |
| 4012 | while (p1 < cur_end) { |
| 4013 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4014 | while (p1 < cur_end) { |
| 4015 | if (*p1 == ';' || *p1 == ',') |
| 4016 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4017 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4018 | break; |
| 4019 | p1++; |
| 4020 | } |
| 4021 | |
| 4022 | if (p1 == cur_end) |
| 4023 | break; |
| 4024 | |
| 4025 | /* p1 is at the beginning of the cookie name */ |
| 4026 | p2 = p1; |
| 4027 | while (p2 < cur_end && *p2 != '=') |
| 4028 | p2++; |
| 4029 | |
| 4030 | if (p2 == cur_end) |
| 4031 | break; |
| 4032 | |
| 4033 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4034 | if (p3 == cur_end) |
| 4035 | break; |
| 4036 | |
| 4037 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4038 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4039 | p4++; |
| 4040 | |
| 4041 | /* here, we have the cookie name between p1 and p2, |
| 4042 | * and its value between p3 and p4. |
| 4043 | * we can process it : |
| 4044 | * |
| 4045 | * Cookie: NAME=VALUE; |
| 4046 | * | || || | |
| 4047 | * | || || +--> p4 |
| 4048 | * | || |+-------> p3 |
| 4049 | * | || +--------> p2 |
| 4050 | * | |+------------> p1 |
| 4051 | * | +-------------> colon |
| 4052 | * +--------------------> cur_ptr |
| 4053 | */ |
| 4054 | |
| 4055 | if (*p1 == '$') { |
| 4056 | /* skip this one */ |
| 4057 | } |
| 4058 | else { |
| 4059 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4060 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4061 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4062 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4063 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4064 | int log_len = p4 - p1; |
| 4065 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4066 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4067 | Alert("HTTP logging : out of memory.\n"); |
| 4068 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4069 | if (log_len > t->fe->capture_len) |
| 4070 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4071 | memcpy(txn->cli_cookie, p1, log_len); |
| 4072 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4073 | } |
| 4074 | } |
| 4075 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4076 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4077 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4078 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4079 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4080 | char *delim; |
| 4081 | |
| 4082 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4083 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4084 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4085 | * |
| 4086 | * Cookie: NAME=SRV~VALUE; |
| 4087 | * | || || | | |
| 4088 | * | || || | +--> p4 |
| 4089 | * | || || +--------> delim |
| 4090 | * | || |+-----------> p3 |
| 4091 | * | || +------------> p2 |
| 4092 | * | |+----------------> p1 |
| 4093 | * | +-----------------> colon |
| 4094 | * +------------------------> cur_ptr |
| 4095 | */ |
| 4096 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4097 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4098 | for (delim = p3; delim < p4; delim++) |
| 4099 | if (*delim == COOKIE_DELIM) |
| 4100 | break; |
| 4101 | } |
| 4102 | else |
| 4103 | delim = p4; |
| 4104 | |
| 4105 | |
| 4106 | /* Here, we'll look for the first running server which supports the cookie. |
| 4107 | * This allows to share a same cookie between several servers, for example |
| 4108 | * to dedicate backup servers to specific servers only. |
| 4109 | * However, to prevent clients from sticking to cookie-less backup server |
| 4110 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4111 | * empty cookies and mark them as invalid. |
| 4112 | */ |
| 4113 | if (delim == p3) |
| 4114 | srv = NULL; |
| 4115 | |
| 4116 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4117 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4118 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4119 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4120 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4121 | txn->flags &= ~TX_CK_MASK; |
| 4122 | txn->flags |= TX_CK_VALID; |
| 4123 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4124 | t->srv = srv; |
| 4125 | break; |
| 4126 | } else { |
| 4127 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4128 | txn->flags &= ~TX_CK_MASK; |
| 4129 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | srv = srv->next; |
| 4133 | } |
| 4134 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4135 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4136 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4137 | txn->flags &= ~TX_CK_MASK; |
| 4138 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | /* depending on the cookie mode, we may have to either : |
| 4142 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4143 | * the server never sees it ; |
| 4144 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4145 | * application cookie so that it does not get accidentely removed later, |
| 4146 | * if we're in cookie prefix mode |
| 4147 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4148 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4149 | int delta; /* negative */ |
| 4150 | |
| 4151 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4152 | p4 += delta; |
| 4153 | cur_end += delta; |
| 4154 | cur_next += delta; |
| 4155 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4156 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4157 | |
| 4158 | del_cookie = del_colon = NULL; |
| 4159 | app_cookies++; /* protect the header from deletion */ |
| 4160 | } |
| 4161 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4162 | (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] | 4163 | del_cookie = p1; |
| 4164 | del_colon = colon; |
| 4165 | } |
| 4166 | } else { |
| 4167 | /* now we know that we must keep this cookie since it's |
| 4168 | * not ours. But if we wanted to delete our cookie |
| 4169 | * earlier, we cannot remove the complete header, but we |
| 4170 | * can remove the previous block itself. |
| 4171 | */ |
| 4172 | app_cookies++; |
| 4173 | |
| 4174 | if (del_cookie != NULL) { |
| 4175 | int delta; /* negative */ |
| 4176 | |
| 4177 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4178 | p4 += delta; |
| 4179 | cur_end += delta; |
| 4180 | cur_next += delta; |
| 4181 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4182 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4183 | del_cookie = del_colon = NULL; |
| 4184 | } |
| 4185 | } |
| 4186 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4187 | if (t->be->appsession_name != NULL) { |
| 4188 | int cmp_len, value_len; |
| 4189 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4190 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4191 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 4192 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 4193 | value_begin = p1 + t->be->appsession_name_len; |
| 4194 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 4195 | } else { |
| 4196 | cmp_len = p2 - p1; |
| 4197 | value_begin = p3; |
| 4198 | value_len = p4 - p3; |
| 4199 | } |
| 4200 | |
| 4201 | /* let's see if the cookie is our appcookie */ |
| 4202 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 4203 | /* Cool... it's the right one */ |
| 4204 | manage_client_side_appsession(t, value_begin, value_len); |
| 4205 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4206 | #if defined(DEBUG_HASH) |
| 4207 | Alert("manage_client_side_cookies\n"); |
| 4208 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4209 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4210 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4211 | } |
| 4212 | |
| 4213 | /* we'll have to look for another cookie ... */ |
| 4214 | p1 = p4; |
| 4215 | } /* while (p1 < cur_end) */ |
| 4216 | |
| 4217 | /* There's no more cookie on this line. |
| 4218 | * We may have marked the last one(s) for deletion. |
| 4219 | * We must do this now in two ways : |
| 4220 | * - if there is no app cookie, we simply delete the header ; |
| 4221 | * - if there are app cookies, we must delete the end of the |
| 4222 | * string properly, including the colon/semi-colon before |
| 4223 | * the cookie name. |
| 4224 | */ |
| 4225 | if (del_cookie != NULL) { |
| 4226 | int delta; |
| 4227 | if (app_cookies) { |
| 4228 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4229 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4230 | cur_hdr->len += delta; |
| 4231 | } else { |
| 4232 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4233 | |
| 4234 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4235 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4236 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4237 | cur_hdr->len = 0; |
| 4238 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4239 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4240 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | /* keep the link from this header to next one */ |
| 4244 | old_idx = cur_idx; |
| 4245 | } /* end of cookie processing on this header */ |
| 4246 | } |
| 4247 | |
| 4248 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4249 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4250 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4251 | */ |
| 4252 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4253 | { |
| 4254 | char term; |
| 4255 | char *cur_ptr, *cur_end, *cur_next; |
| 4256 | int cur_idx, old_idx, last_hdr; |
| 4257 | struct http_txn *txn = &t->txn; |
| 4258 | struct hdr_idx_elem *cur_hdr; |
| 4259 | int len, delta; |
| 4260 | |
| 4261 | last_hdr = 0; |
| 4262 | |
| 4263 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4264 | old_idx = 0; |
| 4265 | |
| 4266 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4267 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4268 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4269 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4270 | (exp->action == ACT_ALLOW || |
| 4271 | exp->action == ACT_DENY)) |
| 4272 | return 0; |
| 4273 | |
| 4274 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4275 | if (!cur_idx) |
| 4276 | break; |
| 4277 | |
| 4278 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4279 | cur_ptr = cur_next; |
| 4280 | cur_end = cur_ptr + cur_hdr->len; |
| 4281 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4282 | |
| 4283 | /* Now we have one header between cur_ptr and cur_end, |
| 4284 | * and the next header starts at cur_next. |
| 4285 | */ |
| 4286 | |
| 4287 | /* The annoying part is that pattern matching needs |
| 4288 | * that we modify the contents to null-terminate all |
| 4289 | * strings before testing them. |
| 4290 | */ |
| 4291 | |
| 4292 | term = *cur_end; |
| 4293 | *cur_end = '\0'; |
| 4294 | |
| 4295 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4296 | switch (exp->action) { |
| 4297 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4298 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4299 | last_hdr = 1; |
| 4300 | break; |
| 4301 | |
| 4302 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4303 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4304 | last_hdr = 1; |
| 4305 | break; |
| 4306 | |
| 4307 | case ACT_REPLACE: |
| 4308 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4309 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4310 | /* FIXME: if the user adds a newline in the replacement, the |
| 4311 | * index will not be recalculated for now, and the new line |
| 4312 | * will not be counted as a new header. |
| 4313 | */ |
| 4314 | |
| 4315 | cur_end += delta; |
| 4316 | cur_next += delta; |
| 4317 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4318 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4319 | break; |
| 4320 | |
| 4321 | case ACT_REMOVE: |
| 4322 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4323 | cur_next += delta; |
| 4324 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4325 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4326 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4327 | txn->hdr_idx.used--; |
| 4328 | cur_hdr->len = 0; |
| 4329 | cur_end = NULL; /* null-term has been rewritten */ |
| 4330 | break; |
| 4331 | |
| 4332 | } |
| 4333 | } |
| 4334 | if (cur_end) |
| 4335 | *cur_end = term; /* restore the string terminator */ |
| 4336 | |
| 4337 | /* keep the link from this header to next one in case of later |
| 4338 | * removal of next header. |
| 4339 | */ |
| 4340 | old_idx = cur_idx; |
| 4341 | } |
| 4342 | return 0; |
| 4343 | } |
| 4344 | |
| 4345 | |
| 4346 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4347 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4348 | * or -1 if a replacement resulted in an invalid status line. |
| 4349 | */ |
| 4350 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4351 | { |
| 4352 | char term; |
| 4353 | char *cur_ptr, *cur_end; |
| 4354 | int done; |
| 4355 | struct http_txn *txn = &t->txn; |
| 4356 | int len, delta; |
| 4357 | |
| 4358 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4359 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4360 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4361 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4362 | (exp->action == ACT_ALLOW || |
| 4363 | exp->action == ACT_DENY)) |
| 4364 | return 0; |
| 4365 | else if (exp->action == ACT_REMOVE) |
| 4366 | return 0; |
| 4367 | |
| 4368 | done = 0; |
| 4369 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4370 | 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] | 4371 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4372 | |
| 4373 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4374 | |
| 4375 | /* The annoying part is that pattern matching needs |
| 4376 | * that we modify the contents to null-terminate all |
| 4377 | * strings before testing them. |
| 4378 | */ |
| 4379 | |
| 4380 | term = *cur_end; |
| 4381 | *cur_end = '\0'; |
| 4382 | |
| 4383 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4384 | switch (exp->action) { |
| 4385 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4386 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4387 | done = 1; |
| 4388 | break; |
| 4389 | |
| 4390 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4391 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4392 | done = 1; |
| 4393 | break; |
| 4394 | |
| 4395 | case ACT_REPLACE: |
| 4396 | *cur_end = term; /* restore the string terminator */ |
| 4397 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4398 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4399 | /* FIXME: if the user adds a newline in the replacement, the |
| 4400 | * index will not be recalculated for now, and the new line |
| 4401 | * will not be counted as a new header. |
| 4402 | */ |
| 4403 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4404 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4405 | cur_end += delta; |
| 4406 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4407 | 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] | 4408 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4409 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4410 | cur_ptr, cur_end + 1, |
| 4411 | NULL, NULL); |
| 4412 | if (unlikely(!cur_end)) |
| 4413 | return -1; |
| 4414 | |
| 4415 | /* we have a full respnse and we know that we have either a CR |
| 4416 | * or an LF at <ptr>. |
| 4417 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4418 | 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] | 4419 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4420 | /* there is no point trying this regex on headers */ |
| 4421 | return 1; |
| 4422 | } |
| 4423 | } |
| 4424 | *cur_end = term; /* restore the string terminator */ |
| 4425 | return done; |
| 4426 | } |
| 4427 | |
| 4428 | |
| 4429 | |
| 4430 | /* |
| 4431 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4432 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4433 | * unparsable response. |
| 4434 | */ |
| 4435 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4436 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4437 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4438 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4439 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4440 | int ret; |
| 4441 | |
| 4442 | /* |
| 4443 | * The interleaving of transformations and verdicts |
| 4444 | * makes it difficult to decide to continue or stop |
| 4445 | * the evaluation. |
| 4446 | */ |
| 4447 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4448 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4449 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4450 | exp->action == ACT_PASS)) { |
| 4451 | exp = exp->next; |
| 4452 | continue; |
| 4453 | } |
| 4454 | |
| 4455 | /* Apply the filter to the status line. */ |
| 4456 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4457 | if (unlikely(ret < 0)) |
| 4458 | return -1; |
| 4459 | |
| 4460 | if (likely(ret == 0)) { |
| 4461 | /* The filter did not match the response, it can be |
| 4462 | * iterated through all headers. |
| 4463 | */ |
| 4464 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4465 | } |
| 4466 | exp = exp->next; |
| 4467 | } |
| 4468 | return 0; |
| 4469 | } |
| 4470 | |
| 4471 | |
| 4472 | |
| 4473 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4474 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4475 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4476 | */ |
| 4477 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4478 | { |
| 4479 | struct http_txn *txn = &t->txn; |
| 4480 | char *p1, *p2, *p3, *p4; |
| 4481 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4482 | char *cur_ptr, *cur_end, *cur_next; |
| 4483 | int cur_idx, old_idx, delta; |
| 4484 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4485 | /* Iterate through the headers. |
| 4486 | * we start with the start line. |
| 4487 | */ |
| 4488 | old_idx = 0; |
| 4489 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4490 | |
| 4491 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4492 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4493 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4494 | |
| 4495 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4496 | cur_ptr = cur_next; |
| 4497 | cur_end = cur_ptr + cur_hdr->len; |
| 4498 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4499 | |
| 4500 | /* We have one full header between cur_ptr and cur_end, and the |
| 4501 | * next header starts at cur_next. We're only interested in |
| 4502 | * "Cookie:" headers. |
| 4503 | */ |
| 4504 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4505 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4506 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4507 | old_idx = cur_idx; |
| 4508 | continue; |
| 4509 | } |
| 4510 | |
| 4511 | /* 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] | 4512 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4513 | |
| 4514 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4515 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 4516 | * the cookie capture is declared in the fronend. |
| 4517 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4518 | if (t->be->cookie_name == NULL && |
| 4519 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4520 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4521 | return; |
| 4522 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4523 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4524 | |
| 4525 | 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] | 4526 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4527 | break; |
| 4528 | |
| 4529 | /* p1 is at the beginning of the cookie name */ |
| 4530 | p2 = p1; |
| 4531 | |
| 4532 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4533 | p2++; |
| 4534 | |
| 4535 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4536 | break; |
| 4537 | |
| 4538 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4539 | if (p3 == cur_end) |
| 4540 | break; |
| 4541 | |
| 4542 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4543 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4544 | p4++; |
| 4545 | |
| 4546 | /* here, we have the cookie name between p1 and p2, |
| 4547 | * and its value between p3 and p4. |
| 4548 | * we can process it. |
| 4549 | */ |
| 4550 | |
| 4551 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4552 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4553 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4554 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4555 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4556 | int log_len = p4 - p1; |
| 4557 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4558 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4559 | Alert("HTTP logging : out of memory.\n"); |
| 4560 | } |
| 4561 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4562 | if (log_len > t->fe->capture_len) |
| 4563 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4564 | memcpy(txn->srv_cookie, p1, log_len); |
| 4565 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4569 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4570 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4571 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4572 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4573 | |
| 4574 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4575 | * this occurrence because we'll insert another one later. |
| 4576 | * We'll delete it too if the "indirect" option is set and we're in |
| 4577 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4578 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4579 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4580 | /* this header must be deleted */ |
| 4581 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4582 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4583 | txn->hdr_idx.used--; |
| 4584 | cur_hdr->len = 0; |
| 4585 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4586 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4587 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4588 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4589 | } |
| 4590 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4591 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4592 | /* replace bytes p3->p4 with the cookie name associated |
| 4593 | * with this server since we know it. |
| 4594 | */ |
| 4595 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4596 | cur_hdr->len += delta; |
| 4597 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4598 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4599 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4600 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4601 | } |
| 4602 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4603 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4604 | /* insert the cookie name associated with this server |
| 4605 | * before existing cookie, and insert a delimitor between them.. |
| 4606 | */ |
| 4607 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4608 | cur_hdr->len += delta; |
| 4609 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4610 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4611 | |
| 4612 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4613 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4614 | } |
| 4615 | } |
| 4616 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4617 | else if (t->be->appsession_name != NULL) { |
| 4618 | int cmp_len, value_len; |
| 4619 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4620 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4621 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 4622 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 4623 | value_begin = p1 + t->be->appsession_name_len; |
| 4624 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 4625 | } else { |
| 4626 | cmp_len = p2 - p1; |
| 4627 | value_begin = p3; |
| 4628 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4629 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4630 | |
| 4631 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 4632 | /* Cool... it's the right one */ |
| 4633 | if (t->sessid != NULL) { |
| 4634 | /* free previously allocated memory as we don't need it anymore */ |
| 4635 | pool_free2(apools.sessid, t->sessid); |
| 4636 | } |
| 4637 | /* Store the sessid in the session for future use */ |
| 4638 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 4639 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4640 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4641 | return; |
| 4642 | } |
| 4643 | memcpy(t->sessid, value_begin, value_len); |
| 4644 | t->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4645 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4646 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4647 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4648 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4649 | /* keep the link from this header to next one */ |
| 4650 | old_idx = cur_idx; |
| 4651 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4652 | |
| 4653 | if (t->sessid != NULL) { |
| 4654 | appsess *asession = NULL; |
| 4655 | /* only do insert, if lookup fails */ |
| 4656 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid); |
| 4657 | if (asession == NULL) { |
| 4658 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 4659 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4660 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4661 | return; |
| 4662 | } |
| 4663 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 4664 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4665 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4666 | return; |
| 4667 | } |
| 4668 | memcpy(asession->sessid, t->sessid, t->be->appsession_len); |
| 4669 | asession->sessid[t->be->appsession_len] = 0; |
| 4670 | |
| 4671 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4672 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 4673 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4674 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4675 | return; |
| 4676 | } |
| 4677 | asession->serverid[0] = '\0'; |
| 4678 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 4679 | |
| 4680 | asession->request_count = 0; |
| 4681 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 4682 | } |
| 4683 | |
| 4684 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 4685 | asession->request_count++; |
| 4686 | } |
| 4687 | |
| 4688 | #if defined(DEBUG_HASH) |
| 4689 | Alert("manage_server_side_cookies\n"); |
| 4690 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4691 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | |
| 4695 | |
| 4696 | /* |
| 4697 | * Check if response is cacheable or not. Updates t->flags. |
| 4698 | */ |
| 4699 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4700 | { |
| 4701 | struct http_txn *txn = &t->txn; |
| 4702 | char *p1, *p2; |
| 4703 | |
| 4704 | char *cur_ptr, *cur_end, *cur_next; |
| 4705 | int cur_idx; |
| 4706 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4707 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4708 | return; |
| 4709 | |
| 4710 | /* Iterate through the headers. |
| 4711 | * we start with the start line. |
| 4712 | */ |
| 4713 | cur_idx = 0; |
| 4714 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4715 | |
| 4716 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4717 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4718 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4719 | |
| 4720 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4721 | cur_ptr = cur_next; |
| 4722 | cur_end = cur_ptr + cur_hdr->len; |
| 4723 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4724 | |
| 4725 | /* We have one full header between cur_ptr and cur_end, and the |
| 4726 | * next header starts at cur_next. We're only interested in |
| 4727 | * "Cookie:" headers. |
| 4728 | */ |
| 4729 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4730 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4731 | if (val) { |
| 4732 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4733 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4734 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4735 | return; |
| 4736 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4737 | } |
| 4738 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4739 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4740 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4741 | continue; |
| 4742 | |
| 4743 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4744 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4745 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4746 | |
| 4747 | if (p1 >= cur_end) /* no more info */ |
| 4748 | continue; |
| 4749 | |
| 4750 | /* p1 is at the beginning of the value */ |
| 4751 | p2 = p1; |
| 4752 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4753 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4754 | p2++; |
| 4755 | |
| 4756 | /* we have a complete value between p1 and p2 */ |
| 4757 | if (p2 < cur_end && *p2 == '=') { |
| 4758 | /* we have something of the form no-cache="set-cookie" */ |
| 4759 | if ((cur_end - p1 >= 21) && |
| 4760 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4761 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4762 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4763 | continue; |
| 4764 | } |
| 4765 | |
| 4766 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4767 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4768 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4769 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4770 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4771 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4772 | return; |
| 4773 | } |
| 4774 | |
| 4775 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4776 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4777 | continue; |
| 4778 | } |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4783 | /* |
| 4784 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4785 | * If the server is found, it's assigned to the session. |
| 4786 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4787 | 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] | 4788 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4789 | char *end_params, *first_param, *cur_param, *next_param; |
| 4790 | char separator; |
| 4791 | int value_len; |
| 4792 | |
| 4793 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4794 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4795 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4796 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4797 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4798 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4799 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4800 | first_param = NULL; |
| 4801 | switch (mode) { |
| 4802 | case PR_O2_AS_M_PP: |
| 4803 | first_param = memchr(begin, ';', len); |
| 4804 | break; |
| 4805 | case PR_O2_AS_M_QS: |
| 4806 | first_param = memchr(begin, '?', len); |
| 4807 | break; |
| 4808 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4809 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4810 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4811 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4812 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4813 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4814 | switch (mode) { |
| 4815 | case PR_O2_AS_M_PP: |
| 4816 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 4817 | end_params = (char *) begin + len; |
| 4818 | } |
| 4819 | separator = ';'; |
| 4820 | break; |
| 4821 | case PR_O2_AS_M_QS: |
| 4822 | end_params = (char *) begin + len; |
| 4823 | separator = '&'; |
| 4824 | break; |
| 4825 | default: |
| 4826 | /* unknown mode, shouldn't happen */ |
| 4827 | return; |
| 4828 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4829 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4830 | cur_param = next_param = end_params; |
| 4831 | while (cur_param > first_param) { |
| 4832 | cur_param--; |
| 4833 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 4834 | /* let's see if this is the appsession parameter */ |
| 4835 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 4836 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 4837 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 4838 | /* Cool... it's the right one */ |
| 4839 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 4840 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 4841 | if (value_len > 0) { |
| 4842 | manage_client_side_appsession(t, cur_param, value_len); |
| 4843 | } |
| 4844 | break; |
| 4845 | } |
| 4846 | next_param = cur_param; |
| 4847 | } |
| 4848 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4849 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4850 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4851 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4852 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4856 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4857 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4858 | * 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] | 4859 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4860 | * 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] | 4861 | * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 4862 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4863 | * |
| 4864 | * Returns 1 if the session's state changes, otherwise 0. |
| 4865 | */ |
| 4866 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4867 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4868 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4869 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4870 | struct user_auth *user; |
| 4871 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4872 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4873 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4874 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 4875 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4876 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4877 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4878 | return 0; |
| 4879 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4880 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4881 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4882 | /* the URI is in h */ |
| 4883 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4884 | return 0; |
| 4885 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4886 | h += uri_auth->uri_len; |
| 4887 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4888 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4889 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4890 | break; |
| 4891 | } |
| 4892 | h++; |
| 4893 | } |
| 4894 | |
| 4895 | if (uri_auth->refresh) { |
| 4896 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4897 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4898 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4899 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4900 | break; |
| 4901 | } |
| 4902 | h++; |
| 4903 | } |
| 4904 | } |
| 4905 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4906 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4907 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4908 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4909 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4910 | break; |
| 4911 | } |
| 4912 | h++; |
| 4913 | } |
| 4914 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4915 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 4916 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4917 | /* we are in front of a interceptable URI. Let's check |
| 4918 | * if there's an authentication and if it's valid. |
| 4919 | */ |
| 4920 | user = uri_auth->users; |
| 4921 | if (!user) { |
| 4922 | /* no user auth required, it's OK */ |
| 4923 | authenticated = 1; |
| 4924 | } else { |
| 4925 | authenticated = 0; |
| 4926 | |
| 4927 | /* a user list is defined, we have to check. |
| 4928 | * skip 21 chars for "Authorization: Basic ". |
| 4929 | */ |
| 4930 | |
| 4931 | /* FIXME: this should move to an earlier place */ |
| 4932 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4933 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4934 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4935 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4936 | if (len > 14 && |
| 4937 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 4938 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4939 | break; |
| 4940 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4941 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4942 | } |
| 4943 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4944 | if (txn->auth_hdr.len < 21 || |
| 4945 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4946 | user = NULL; |
| 4947 | |
| 4948 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4949 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4950 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4951 | user->user_pwd, user->user_len)) { |
| 4952 | authenticated = 1; |
| 4953 | break; |
| 4954 | } |
| 4955 | user = user->next; |
| 4956 | } |
| 4957 | } |
| 4958 | |
| 4959 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4960 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4961 | |
| 4962 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 4963 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 4964 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4965 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 4966 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4967 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4968 | if (!(t->flags & SN_ERR_MASK)) |
| 4969 | t->flags |= SN_ERR_PRXCOND; |
| 4970 | if (!(t->flags & SN_FINST_MASK)) |
| 4971 | t->flags |= SN_FINST_R; |
| 4972 | return 1; |
| 4973 | } |
| 4974 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4975 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4976 | * data. |
| 4977 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 4978 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4979 | t->data_source = DATA_SRC_STATS; |
| 4980 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 4981 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 4982 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 4983 | t->rep->prod->private = t; |
| 4984 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4985 | return 1; |
| 4986 | } |
| 4987 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4988 | /* |
| 4989 | * Capture a bad request or response and archive it in the proxy's structure. |
| 4990 | */ |
| 4991 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 4992 | struct buffer *buf, struct http_msg *msg, |
| 4993 | struct proxy *other_end) |
| 4994 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 4995 | es->len = buf->r - (buf->data + msg->som); |
| 4996 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4997 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 4998 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4999 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 5000 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5001 | es->when = date; // user-visible date |
| 5002 | es->sid = s->uniq_id; |
| 5003 | es->srv = s->srv; |
| 5004 | es->oe = other_end; |
| 5005 | es->src = s->cli_addr; |
| 5006 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5007 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5008 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5009 | * Print a debug line with a header |
| 5010 | */ |
| 5011 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5012 | { |
| 5013 | int len, max; |
| 5014 | 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] | 5015 | 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] | 5016 | max = end - start; |
| 5017 | UBOUND(max, sizeof(trash) - len - 1); |
| 5018 | len += strlcpy2(trash + len, start, max + 1); |
| 5019 | trash[len++] = '\n'; |
| 5020 | write(1, trash, len); |
| 5021 | } |
| 5022 | |
| 5023 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5024 | /************************************************************************/ |
| 5025 | /* The code below is dedicated to ACL parsing and matching */ |
| 5026 | /************************************************************************/ |
| 5027 | |
| 5028 | |
| 5029 | |
| 5030 | |
| 5031 | /* 1. Check on METHOD |
| 5032 | * We use the pre-parsed method if it is known, and store its number as an |
| 5033 | * integer. If it is unknown, we use the pointer and the length. |
| 5034 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5035 | 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] | 5036 | { |
| 5037 | int len, meth; |
| 5038 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5039 | len = strlen(*text); |
| 5040 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5041 | |
| 5042 | pattern->val.i = meth; |
| 5043 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5044 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5045 | if (!pattern->ptr.str) |
| 5046 | return 0; |
| 5047 | pattern->len = len; |
| 5048 | } |
| 5049 | return 1; |
| 5050 | } |
| 5051 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5052 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5053 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5054 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5055 | { |
| 5056 | int meth; |
| 5057 | struct http_txn *txn = l7; |
| 5058 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5059 | if (!txn) |
| 5060 | return 0; |
| 5061 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5062 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5063 | return 0; |
| 5064 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5065 | meth = txn->meth; |
| 5066 | test->i = meth; |
| 5067 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5068 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5069 | /* ensure the indexes are not affected */ |
| 5070 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5071 | test->len = txn->req.sl.rq.m_l; |
| 5072 | test->ptr = txn->req.sol; |
| 5073 | } |
| 5074 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5075 | return 1; |
| 5076 | } |
| 5077 | |
| 5078 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5079 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5080 | int icase; |
| 5081 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5082 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5083 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5084 | |
| 5085 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5086 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5087 | |
| 5088 | /* Other method, we must compare the strings */ |
| 5089 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5090 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5091 | |
| 5092 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5093 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5094 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5095 | return ACL_PAT_FAIL; |
| 5096 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5097 | } |
| 5098 | |
| 5099 | /* 2. Check on Request/Status Version |
| 5100 | * We simply compare strings here. |
| 5101 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5102 | 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] | 5103 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5104 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5105 | if (!pattern->ptr.str) |
| 5106 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5107 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5108 | return 1; |
| 5109 | } |
| 5110 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5111 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5112 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5113 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5114 | { |
| 5115 | struct http_txn *txn = l7; |
| 5116 | char *ptr; |
| 5117 | int len; |
| 5118 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5119 | if (!txn) |
| 5120 | return 0; |
| 5121 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5122 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5123 | return 0; |
| 5124 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5125 | len = txn->req.sl.rq.v_l; |
| 5126 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5127 | |
| 5128 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5129 | if (len <= 0) |
| 5130 | return 0; |
| 5131 | |
| 5132 | test->ptr = ptr; |
| 5133 | test->len = len; |
| 5134 | |
| 5135 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5136 | return 1; |
| 5137 | } |
| 5138 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5139 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5140 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5141 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5142 | { |
| 5143 | struct http_txn *txn = l7; |
| 5144 | char *ptr; |
| 5145 | int len; |
| 5146 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5147 | if (!txn) |
| 5148 | return 0; |
| 5149 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5150 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5151 | return 0; |
| 5152 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5153 | len = txn->rsp.sl.st.v_l; |
| 5154 | ptr = txn->rsp.sol; |
| 5155 | |
| 5156 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5157 | if (len <= 0) |
| 5158 | return 0; |
| 5159 | |
| 5160 | test->ptr = ptr; |
| 5161 | test->len = len; |
| 5162 | |
| 5163 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5164 | return 1; |
| 5165 | } |
| 5166 | |
| 5167 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5168 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5169 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5170 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5171 | { |
| 5172 | struct http_txn *txn = l7; |
| 5173 | char *ptr; |
| 5174 | int len; |
| 5175 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5176 | if (!txn) |
| 5177 | return 0; |
| 5178 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5179 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5180 | return 0; |
| 5181 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5182 | len = txn->rsp.sl.st.c_l; |
| 5183 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5184 | |
| 5185 | test->i = __strl2ui(ptr, len); |
| 5186 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5187 | return 1; |
| 5188 | } |
| 5189 | |
| 5190 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5191 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5192 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5193 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5194 | { |
| 5195 | struct http_txn *txn = l7; |
| 5196 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5197 | if (!txn) |
| 5198 | return 0; |
| 5199 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5200 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5201 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5202 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5203 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5204 | /* ensure the indexes are not affected */ |
| 5205 | return 0; |
| 5206 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5207 | test->len = txn->req.sl.rq.u_l; |
| 5208 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5209 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5210 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5211 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5212 | return 1; |
| 5213 | } |
| 5214 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5215 | static int |
| 5216 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5217 | struct acl_expr *expr, struct acl_test *test) |
| 5218 | { |
| 5219 | struct http_txn *txn = l7; |
| 5220 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5221 | if (!txn) |
| 5222 | return 0; |
| 5223 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5224 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5225 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5226 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5227 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5228 | /* ensure the indexes are not affected */ |
| 5229 | return 0; |
| 5230 | |
| 5231 | /* Parse HTTP request */ |
| 5232 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5233 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5234 | test->i = AF_INET; |
| 5235 | |
| 5236 | /* |
| 5237 | * If we are parsing url in frontend space, we prepare backend stage |
| 5238 | * to not parse again the same url ! optimization lazyness... |
| 5239 | */ |
| 5240 | if (px->options & PR_O_HTTP_PROXY) |
| 5241 | l4->flags |= SN_ADDR_SET; |
| 5242 | |
| 5243 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5244 | return 1; |
| 5245 | } |
| 5246 | |
| 5247 | static int |
| 5248 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5249 | struct acl_expr *expr, struct acl_test *test) |
| 5250 | { |
| 5251 | struct http_txn *txn = l7; |
| 5252 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5253 | if (!txn) |
| 5254 | return 0; |
| 5255 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5256 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5257 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5258 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5259 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5260 | /* ensure the indexes are not affected */ |
| 5261 | return 0; |
| 5262 | |
| 5263 | /* Same optimization as url_ip */ |
| 5264 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5265 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5266 | |
| 5267 | if (px->options & PR_O_HTTP_PROXY) |
| 5268 | l4->flags |= SN_ADDR_SET; |
| 5269 | |
| 5270 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5271 | return 1; |
| 5272 | } |
| 5273 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5274 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5275 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5276 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5277 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5278 | 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] | 5279 | struct acl_expr *expr, struct acl_test *test) |
| 5280 | { |
| 5281 | struct http_txn *txn = l7; |
| 5282 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5283 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5284 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5285 | if (!txn) |
| 5286 | return 0; |
| 5287 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5288 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5289 | /* search for header from the beginning */ |
| 5290 | ctx->idx = 0; |
| 5291 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5292 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5293 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5294 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5295 | test->len = ctx->vlen; |
| 5296 | test->ptr = (char *)ctx->line + ctx->val; |
| 5297 | return 1; |
| 5298 | } |
| 5299 | |
| 5300 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5301 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5302 | return 0; |
| 5303 | } |
| 5304 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5305 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5306 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5307 | struct acl_expr *expr, struct acl_test *test) |
| 5308 | { |
| 5309 | struct http_txn *txn = l7; |
| 5310 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5311 | if (!txn) |
| 5312 | return 0; |
| 5313 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5314 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5315 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5316 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5317 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5318 | /* ensure the indexes are not affected */ |
| 5319 | return 0; |
| 5320 | |
| 5321 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5322 | } |
| 5323 | |
| 5324 | static int |
| 5325 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5326 | struct acl_expr *expr, struct acl_test *test) |
| 5327 | { |
| 5328 | struct http_txn *txn = l7; |
| 5329 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5330 | if (!txn) |
| 5331 | return 0; |
| 5332 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5333 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5334 | return 0; |
| 5335 | |
| 5336 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5337 | } |
| 5338 | |
| 5339 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5340 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5341 | */ |
| 5342 | static int |
| 5343 | 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] | 5344 | struct acl_expr *expr, struct acl_test *test) |
| 5345 | { |
| 5346 | struct http_txn *txn = l7; |
| 5347 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5348 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5349 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5350 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5351 | if (!txn) |
| 5352 | return 0; |
| 5353 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5354 | ctx.idx = 0; |
| 5355 | cnt = 0; |
| 5356 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5357 | cnt++; |
| 5358 | |
| 5359 | test->i = cnt; |
| 5360 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5361 | return 1; |
| 5362 | } |
| 5363 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5364 | static int |
| 5365 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5366 | struct acl_expr *expr, struct acl_test *test) |
| 5367 | { |
| 5368 | struct http_txn *txn = l7; |
| 5369 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5370 | if (!txn) |
| 5371 | return 0; |
| 5372 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5373 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5374 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5375 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5376 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5377 | /* ensure the indexes are not affected */ |
| 5378 | return 0; |
| 5379 | |
| 5380 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5381 | } |
| 5382 | |
| 5383 | static int |
| 5384 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5385 | struct acl_expr *expr, struct acl_test *test) |
| 5386 | { |
| 5387 | struct http_txn *txn = l7; |
| 5388 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5389 | if (!txn) |
| 5390 | return 0; |
| 5391 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5392 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5393 | return 0; |
| 5394 | |
| 5395 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5396 | } |
| 5397 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5398 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5399 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5400 | * 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] | 5401 | */ |
| 5402 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5403 | 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] | 5404 | struct acl_expr *expr, struct acl_test *test) |
| 5405 | { |
| 5406 | struct http_txn *txn = l7; |
| 5407 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5408 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5409 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5410 | if (!txn) |
| 5411 | return 0; |
| 5412 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5413 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5414 | /* search for header from the beginning */ |
| 5415 | ctx->idx = 0; |
| 5416 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5417 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5418 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5419 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5420 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5421 | return 1; |
| 5422 | } |
| 5423 | |
| 5424 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5425 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5426 | return 0; |
| 5427 | } |
| 5428 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5429 | static int |
| 5430 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5431 | struct acl_expr *expr, struct acl_test *test) |
| 5432 | { |
| 5433 | struct http_txn *txn = l7; |
| 5434 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5435 | if (!txn) |
| 5436 | return 0; |
| 5437 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5438 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5439 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5440 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5441 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5442 | /* ensure the indexes are not affected */ |
| 5443 | return 0; |
| 5444 | |
| 5445 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5446 | } |
| 5447 | |
| 5448 | static int |
| 5449 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5450 | struct acl_expr *expr, struct acl_test *test) |
| 5451 | { |
| 5452 | struct http_txn *txn = l7; |
| 5453 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5454 | if (!txn) |
| 5455 | return 0; |
| 5456 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5457 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5458 | return 0; |
| 5459 | |
| 5460 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5461 | } |
| 5462 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5463 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 5464 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5465 | */ |
| 5466 | static int |
| 5467 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 5468 | struct acl_expr *expr, struct acl_test *test) |
| 5469 | { |
| 5470 | struct http_txn *txn = l7; |
| 5471 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5472 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 5473 | |
| 5474 | if (!txn) |
| 5475 | return 0; |
| 5476 | |
| 5477 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5478 | /* search for header from the beginning */ |
| 5479 | ctx->idx = 0; |
| 5480 | |
| 5481 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5482 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5483 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5484 | /* Same optimization as url_ip */ |
| 5485 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 5486 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 5487 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 5488 | test->i = AF_INET; |
| 5489 | return 1; |
| 5490 | } |
| 5491 | |
| 5492 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5493 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5494 | return 0; |
| 5495 | } |
| 5496 | |
| 5497 | static int |
| 5498 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5499 | struct acl_expr *expr, struct acl_test *test) |
| 5500 | { |
| 5501 | struct http_txn *txn = l7; |
| 5502 | |
| 5503 | if (!txn) |
| 5504 | return 0; |
| 5505 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5506 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5507 | return 0; |
| 5508 | |
| 5509 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5510 | /* ensure the indexes are not affected */ |
| 5511 | return 0; |
| 5512 | |
| 5513 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 5514 | } |
| 5515 | |
| 5516 | static int |
| 5517 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5518 | struct acl_expr *expr, struct acl_test *test) |
| 5519 | { |
| 5520 | struct http_txn *txn = l7; |
| 5521 | |
| 5522 | if (!txn) |
| 5523 | return 0; |
| 5524 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5525 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5526 | return 0; |
| 5527 | |
| 5528 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 5529 | } |
| 5530 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5531 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5532 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5533 | */ |
| 5534 | static int |
| 5535 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5536 | struct acl_expr *expr, struct acl_test *test) |
| 5537 | { |
| 5538 | struct http_txn *txn = l7; |
| 5539 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5540 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5541 | if (!txn) |
| 5542 | return 0; |
| 5543 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5544 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5545 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5546 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5547 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5548 | /* ensure the indexes are not affected */ |
| 5549 | return 0; |
| 5550 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5551 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5552 | ptr = http_get_path(txn); |
| 5553 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5554 | return 0; |
| 5555 | |
| 5556 | /* OK, we got the '/' ! */ |
| 5557 | test->ptr = ptr; |
| 5558 | |
| 5559 | while (ptr < end && *ptr != '?') |
| 5560 | ptr++; |
| 5561 | |
| 5562 | test->len = ptr - test->ptr; |
| 5563 | |
| 5564 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5565 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5566 | return 1; |
| 5567 | } |
| 5568 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5569 | static int |
| 5570 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 5571 | struct acl_expr *expr, struct acl_test *test) |
| 5572 | { |
| 5573 | struct buffer *req = s->req; |
| 5574 | struct http_txn *txn = &s->txn; |
| 5575 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5576 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5577 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 5578 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 5579 | */ |
| 5580 | |
| 5581 | if (!s || !req) |
| 5582 | return 0; |
| 5583 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5584 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5585 | /* Already decoded as OK */ |
| 5586 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 5587 | return 1; |
| 5588 | } |
| 5589 | |
| 5590 | /* Try to decode HTTP request */ |
| 5591 | if (likely(req->lr < req->r)) |
| 5592 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 5593 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5594 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5595 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 5596 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 5597 | return 1; |
| 5598 | } |
| 5599 | /* wait for final state */ |
| 5600 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 5601 | return 0; |
| 5602 | } |
| 5603 | |
| 5604 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 5605 | * perform so that further checks can rely on HTTP tests. |
| 5606 | */ |
| 5607 | msg->sol = req->data + msg->som; |
| 5608 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 5609 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 5610 | s->flags |= SN_REDIRECTABLE; |
| 5611 | |
| 5612 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 5613 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 5614 | return 1; |
| 5615 | } |
| 5616 | |
| 5617 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 5618 | return 1; |
| 5619 | } |
| 5620 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5621 | |
| 5622 | /************************************************************************/ |
| 5623 | /* All supported keywords must be declared here. */ |
| 5624 | /************************************************************************/ |
| 5625 | |
| 5626 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5627 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5628 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 5629 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5630 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5631 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5632 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5633 | { "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] | 5634 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5635 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5636 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5637 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5638 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5639 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5640 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5641 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5642 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5643 | { "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] | 5644 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5645 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5646 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5647 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5648 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5649 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5650 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5651 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5652 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5653 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5654 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5655 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5656 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5657 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5658 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5659 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5660 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5661 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5662 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5663 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5664 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5665 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5666 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5667 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5668 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5669 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5670 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5671 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5672 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5673 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5674 | { "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] | 5675 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5676 | { NULL, NULL, NULL, NULL }, |
| 5677 | |
| 5678 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5679 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5680 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5681 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5682 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5683 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5684 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5685 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5686 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5687 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5688 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5689 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5690 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5691 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5692 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5693 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5694 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5695 | |
| 5696 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5697 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5698 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5699 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5700 | { NULL, NULL, NULL, NULL }, |
| 5701 | #endif |
| 5702 | }}; |
| 5703 | |
| 5704 | |
| 5705 | __attribute__((constructor)) |
| 5706 | static void __http_protocol_init(void) |
| 5707 | { |
| 5708 | acl_register_keywords(&acl_kws); |
| 5709 | } |
| 5710 | |
| 5711 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5712 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5713 | * Local variables: |
| 5714 | * c-indent-level: 8 |
| 5715 | * c-basic-offset: 8 |
| 5716 | * End: |
| 5717 | */ |