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 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 596 | ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 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 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 664 | len = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+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 |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1384 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1385 | * the first state, so that none of the msg pointers has to be initialized |
| 1386 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1387 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1388 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1389 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1390 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1391 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1392 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1393 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | ptr = buf->lr; |
| 1395 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1396 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | if (unlikely(ptr >= end)) |
| 1398 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1399 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1400 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1401 | /* |
| 1402 | * First, states that are specific to the response only. |
| 1403 | * We check them first so that request and headers are |
| 1404 | * closer to each other (accessed more often). |
| 1405 | */ |
| 1406 | http_msg_rpbefore: |
| 1407 | case HTTP_MSG_RPBEFORE: |
| 1408 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1409 | /* we have a start of message, but we have to check |
| 1410 | * first if we need to remove some CRLF. We can only |
| 1411 | * do this when send_max=0. |
| 1412 | */ |
| 1413 | char *beg = buf->w + buf->send_max; |
| 1414 | if (beg >= buf->data + buf->size) |
| 1415 | beg -= buf->size; |
| 1416 | if (unlikely(ptr != beg)) { |
| 1417 | if (buf->send_max) |
| 1418 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1419 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1420 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1421 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1422 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1423 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1424 | hdr_idx_init(idx); |
| 1425 | state = HTTP_MSG_RPVER; |
| 1426 | goto http_msg_rpver; |
| 1427 | } |
| 1428 | |
| 1429 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1430 | goto http_msg_invalid; |
| 1431 | |
| 1432 | if (unlikely(*ptr == '\n')) |
| 1433 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1434 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1435 | /* stop here */ |
| 1436 | |
| 1437 | http_msg_rpbefore_cr: |
| 1438 | case HTTP_MSG_RPBEFORE_CR: |
| 1439 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1440 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1441 | /* stop here */ |
| 1442 | |
| 1443 | http_msg_rpver: |
| 1444 | case HTTP_MSG_RPVER: |
| 1445 | case HTTP_MSG_RPVER_SP: |
| 1446 | case HTTP_MSG_RPCODE: |
| 1447 | case HTTP_MSG_RPCODE_SP: |
| 1448 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1449 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1450 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1451 | if (unlikely(!ptr)) |
| 1452 | return; |
| 1453 | |
| 1454 | /* we have a full response and we know that we have either a CR |
| 1455 | * or an LF at <ptr>. |
| 1456 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1457 | //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] | 1458 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1459 | |
| 1460 | msg->sol = ptr; |
| 1461 | if (likely(*ptr == '\r')) |
| 1462 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1463 | goto http_msg_rpline_end; |
| 1464 | |
| 1465 | http_msg_rpline_end: |
| 1466 | case HTTP_MSG_RPLINE_END: |
| 1467 | /* msg->sol must point to the first of CR or LF. */ |
| 1468 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1469 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1470 | /* stop here */ |
| 1471 | |
| 1472 | /* |
| 1473 | * Second, states that are specific to the request only |
| 1474 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | http_msg_rqbefore: |
| 1476 | case HTTP_MSG_RQBEFORE: |
| 1477 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1478 | /* we have a start of message, but we have to check |
| 1479 | * first if we need to remove some CRLF. We can only |
| 1480 | * do this when send_max=0. |
| 1481 | */ |
| 1482 | char *beg = buf->w + buf->send_max; |
| 1483 | if (beg >= buf->data + buf->size) |
| 1484 | beg -= buf->size; |
| 1485 | if (likely(ptr != beg)) { |
| 1486 | if (buf->send_max) |
| 1487 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1488 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1489 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1491 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1492 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1493 | /* we will need this when keep-alive will be supported |
| 1494 | hdr_idx_init(idx); |
| 1495 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1496 | state = HTTP_MSG_RQMETH; |
| 1497 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1498 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1499 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1501 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1502 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | if (unlikely(*ptr == '\n')) |
| 1504 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1505 | 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] | 1506 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | http_msg_rqbefore_cr: |
| 1509 | case HTTP_MSG_RQBEFORE_CR: |
| 1510 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1511 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1512 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1513 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1514 | http_msg_rqmeth: |
| 1515 | case HTTP_MSG_RQMETH: |
| 1516 | case HTTP_MSG_RQMETH_SP: |
| 1517 | case HTTP_MSG_RQURI: |
| 1518 | case HTTP_MSG_RQURI_SP: |
| 1519 | case HTTP_MSG_RQVER: |
| 1520 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1521 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | if (unlikely(!ptr)) |
| 1523 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | /* we have a full request and we know that we have either a CR |
| 1526 | * or an LF at <ptr>. |
| 1527 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1528 | //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] | 1529 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | msg->sol = ptr; |
| 1532 | if (likely(*ptr == '\r')) |
| 1533 | 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] | 1534 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1535 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | http_msg_rqline_end: |
| 1537 | case HTTP_MSG_RQLINE_END: |
| 1538 | /* check for HTTP/0.9 request : no version information available. |
| 1539 | * msg->sol must point to the first of CR or LF. |
| 1540 | */ |
| 1541 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1542 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1544 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1545 | 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] | 1546 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1547 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1548 | /* |
| 1549 | * Common states below |
| 1550 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1551 | http_msg_hdr_first: |
| 1552 | case HTTP_MSG_HDR_FIRST: |
| 1553 | msg->sol = ptr; |
| 1554 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1555 | goto http_msg_hdr_name; |
| 1556 | } |
| 1557 | |
| 1558 | if (likely(*ptr == '\r')) |
| 1559 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1560 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1561 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | http_msg_hdr_name: |
| 1563 | case HTTP_MSG_HDR_NAME: |
| 1564 | /* assumes msg->sol points to the first char */ |
| 1565 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1566 | 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] | 1567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | if (likely(*ptr == ':')) { |
| 1569 | msg->col = ptr - buf->data; |
| 1570 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1571 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1572 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1573 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1574 | goto http_msg_invalid; |
| 1575 | |
| 1576 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1577 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1578 | |
| 1579 | /* and we still accept this non-token character */ |
| 1580 | 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] | 1581 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | http_msg_hdr_l1_sp: |
| 1583 | case HTTP_MSG_HDR_L1_SP: |
| 1584 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1585 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1586 | 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] | 1587 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | /* header value can be basically anything except CR/LF */ |
| 1589 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1590 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1591 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1592 | goto http_msg_hdr_val; |
| 1593 | } |
| 1594 | |
| 1595 | if (likely(*ptr == '\r')) |
| 1596 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1597 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1598 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1599 | http_msg_hdr_l1_lf: |
| 1600 | case HTTP_MSG_HDR_L1_LF: |
| 1601 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1602 | 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] | 1603 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1604 | http_msg_hdr_l1_lws: |
| 1605 | case HTTP_MSG_HDR_L1_LWS: |
| 1606 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1607 | /* replace HT,CR,LF with spaces */ |
| 1608 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1609 | buf->data[msg->sov] = ' '; |
| 1610 | goto http_msg_hdr_l1_sp; |
| 1611 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1612 | /* we had a header consisting only in spaces ! */ |
| 1613 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1614 | goto http_msg_complete_header; |
| 1615 | |
| 1616 | http_msg_hdr_val: |
| 1617 | case HTTP_MSG_HDR_VAL: |
| 1618 | /* assumes msg->sol points to the first char, msg->col to the |
| 1619 | * colon, and msg->sov points to the first character of the |
| 1620 | * value. |
| 1621 | */ |
| 1622 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1623 | 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] | 1624 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | msg->eol = ptr; |
| 1626 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1627 | * real header end in case it ends with lots of LWS, but is this |
| 1628 | * really needed ? |
| 1629 | */ |
| 1630 | if (likely(*ptr == '\r')) |
| 1631 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1632 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1633 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1634 | http_msg_hdr_l2_lf: |
| 1635 | case HTTP_MSG_HDR_L2_LF: |
| 1636 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1637 | 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] | 1638 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | http_msg_hdr_l2_lws: |
| 1640 | case HTTP_MSG_HDR_L2_LWS: |
| 1641 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1642 | /* LWS: replace HT,CR,LF with spaces */ |
| 1643 | for (; msg->eol < ptr; msg->eol++) |
| 1644 | *msg->eol = ' '; |
| 1645 | goto http_msg_hdr_val; |
| 1646 | } |
| 1647 | http_msg_complete_header: |
| 1648 | /* |
| 1649 | * It was a new header, so the last one is finished. |
| 1650 | * Assumes msg->sol points to the first char, msg->col to the |
| 1651 | * colon, msg->sov points to the first character of the value |
| 1652 | * and msg->eol to the first CR or LF so we know how the line |
| 1653 | * ends. We insert last header into the index. |
| 1654 | */ |
| 1655 | /* |
| 1656 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1657 | write(2, msg->sol, msg->eol-msg->sol); |
| 1658 | fprintf(stderr,"\n"); |
| 1659 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1660 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1662 | idx, idx->tail) < 0)) |
| 1663 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1664 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1665 | msg->sol = ptr; |
| 1666 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1667 | goto http_msg_hdr_name; |
| 1668 | } |
| 1669 | |
| 1670 | if (likely(*ptr == '\r')) |
| 1671 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1672 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1673 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | http_msg_last_lf: |
| 1675 | case HTTP_MSG_LAST_LF: |
| 1676 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1677 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1678 | ptr++; |
| 1679 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1680 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1682 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1683 | return; |
| 1684 | #ifdef DEBUG_FULL |
| 1685 | default: |
| 1686 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1687 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1688 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | } |
| 1690 | http_msg_ood: |
| 1691 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1692 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | buf->lr = ptr; |
| 1694 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1695 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | http_msg_invalid: |
| 1697 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1698 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1699 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | return; |
| 1701 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1703 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1704 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1705 | * nothing is done and 1 is returned. |
| 1706 | */ |
| 1707 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1708 | { |
| 1709 | int delta; |
| 1710 | char *cur_end; |
| 1711 | |
| 1712 | if (msg->sl.rq.v_l != 0) |
| 1713 | return 1; |
| 1714 | |
| 1715 | msg->sol = req->data + msg->som; |
| 1716 | cur_end = msg->sol + msg->sl.rq.l; |
| 1717 | delta = 0; |
| 1718 | |
| 1719 | if (msg->sl.rq.u_l == 0) { |
| 1720 | /* if no URI was set, add "/" */ |
| 1721 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1722 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1723 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1724 | } |
| 1725 | /* add HTTP version */ |
| 1726 | 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] | 1727 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1728 | cur_end += delta; |
| 1729 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1730 | HTTP_MSG_RQMETH, |
| 1731 | msg->sol, cur_end + 1, |
| 1732 | NULL, NULL); |
| 1733 | if (unlikely(!cur_end)) |
| 1734 | return 0; |
| 1735 | |
| 1736 | /* we have a full HTTP/1.0 request now and we know that |
| 1737 | * we have either a CR or an LF at <ptr>. |
| 1738 | */ |
| 1739 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1740 | return 1; |
| 1741 | } |
| 1742 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1743 | /* Parse the Connection: headaer of an HTTP request, and set the transaction |
| 1744 | * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag |
| 1745 | * is also set so that we don't parse a second time. If some dangerous values |
| 1746 | * are encountered, we leave the status to indicate that the request might be |
| 1747 | * interpreted as keep-alive, but we also set the connection flags to indicate |
| 1748 | * that we WANT it to be a close, so that the header will be fixed. This |
| 1749 | * function should only be called when we know we're interested in checking |
| 1750 | * the request (not a CONNECT, and FE or BE mangles the header). |
| 1751 | */ |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1752 | void http_req_parse_connection_header(struct http_txn *txn) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1753 | { |
| 1754 | struct http_msg *msg = &txn->req; |
| 1755 | struct hdr_ctx ctx; |
| 1756 | int conn_cl, conn_ka; |
| 1757 | |
| 1758 | if (txn->flags & TX_CON_HDR_PARS) |
| 1759 | return; |
| 1760 | |
| 1761 | conn_cl = 0; |
| 1762 | conn_ka = 0; |
| 1763 | ctx.idx = 0; |
| 1764 | |
| 1765 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 1766 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 1767 | conn_cl = 1; |
| 1768 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 1769 | conn_ka = 1; |
| 1770 | } |
| 1771 | |
| 1772 | /* Determine if the client wishes keep-alive or close. |
| 1773 | * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections |
| 1774 | * are persistent unless "Connection: close" is explicitly specified. |
| 1775 | * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections. |
| 1776 | * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless |
| 1777 | * they explicitly specify "Connection: Keep-Alive", regardless of any |
| 1778 | * optional "Keep-Alive" header. |
| 1779 | * Note that if we find a request with both "Connection: close" and |
| 1780 | * "Connection: Keep-Alive", we indicate we want a close but don't have |
| 1781 | * it, so that it can be enforced later. |
| 1782 | */ |
| 1783 | |
| 1784 | if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */ |
| 1785 | if (conn_cl) { |
| 1786 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1787 | if (!conn_ka) |
| 1788 | txn->flags |= TX_REQ_CONN_CLO; |
| 1789 | } |
| 1790 | } else { /* HTTP/1.0 */ |
| 1791 | if (!conn_ka) |
| 1792 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO; |
| 1793 | else if (conn_cl) |
| 1794 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1795 | } |
| 1796 | txn->flags |= TX_CON_HDR_PARS; |
| 1797 | } |
| 1798 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1799 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1800 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1801 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1802 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1803 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1804 | * Return >0 on success, 0 when some data is missing, <0 on error. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1805 | * Note: this function is designed to parse wrapped CRLF at the end of the buffer. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1806 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1807 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1808 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1809 | char *ptr = buf->lr; |
| 1810 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1811 | unsigned int chunk = 0; |
| 1812 | |
| 1813 | /* The chunk size is in the following form, though we are only |
| 1814 | * interested in the size and CRLF : |
| 1815 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1816 | */ |
| 1817 | while (1) { |
| 1818 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1819 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1820 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1821 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1822 | if (c < 0) /* not a hex digit anymore */ |
| 1823 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1824 | if (++ptr >= end) |
| 1825 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1826 | if (chunk & 0xF000000) /* overflow will occur */ |
| 1827 | return -1; |
| 1828 | chunk = (chunk << 4) + c; |
| 1829 | } |
| 1830 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1831 | /* empty size not allowed */ |
| 1832 | if (ptr == buf->lr) |
| 1833 | return -1; |
| 1834 | |
| 1835 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1836 | if (++ptr >= end) |
| 1837 | ptr = buf->data; |
| 1838 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1839 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1840 | } |
| 1841 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1842 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1843 | * for the end of chunk size. |
| 1844 | */ |
| 1845 | while (1) { |
| 1846 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1847 | /* we now have a CR or an LF at ptr */ |
| 1848 | if (likely(*ptr == '\r')) { |
| 1849 | if (++ptr >= end) |
| 1850 | ptr = buf->data; |
| 1851 | if (ptr == buf->r) |
| 1852 | return 0; |
| 1853 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1854 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1855 | if (*ptr != '\n') |
| 1856 | return -1; |
| 1857 | if (++ptr >= end) |
| 1858 | ptr = buf->data; |
| 1859 | /* done */ |
| 1860 | break; |
| 1861 | } |
| 1862 | else if (*ptr == ';') { |
| 1863 | /* chunk extension, ends at next CRLF */ |
| 1864 | if (++ptr >= end) |
| 1865 | ptr = buf->data; |
| 1866 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1867 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1868 | |
| 1869 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1870 | if (++ptr >= end) |
| 1871 | ptr = buf->data; |
| 1872 | if (ptr == buf->r) |
| 1873 | return 0; |
| 1874 | } |
| 1875 | /* we have a CRLF now, loop above */ |
| 1876 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1877 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1878 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1879 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1880 | } |
| 1881 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1882 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 1883 | * which may or may not be present. We save that into ->lr and |
| 1884 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1885 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1886 | msg->sov += ptr - buf->lr; |
| 1887 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1888 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1889 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1890 | return 1; |
| 1891 | } |
| 1892 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1893 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 1894 | * message <msg>. The first visited position is buf->lr. If the end of |
| 1895 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1896 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1897 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1898 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 1899 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 1900 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 1901 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1902 | * which implies that all non-trailers data have already been scheduled for |
| 1903 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1904 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1905 | * important to note that this function is designed to be able to parse wrapped |
| 1906 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1907 | */ |
| 1908 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 1909 | { |
| 1910 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 1911 | while (1) { |
| 1912 | char *p1 = NULL, *p2 = NULL; |
| 1913 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1914 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1915 | |
| 1916 | /* scan current line and stop at LF or CRLF */ |
| 1917 | while (1) { |
| 1918 | if (ptr == buf->r) |
| 1919 | return 0; |
| 1920 | |
| 1921 | if (*ptr == '\n') { |
| 1922 | if (!p1) |
| 1923 | p1 = ptr; |
| 1924 | p2 = ptr; |
| 1925 | break; |
| 1926 | } |
| 1927 | |
| 1928 | if (*ptr == '\r') { |
| 1929 | if (p1) |
| 1930 | return -1; |
| 1931 | p1 = ptr; |
| 1932 | } |
| 1933 | |
| 1934 | ptr++; |
| 1935 | if (ptr >= buf->data + buf->size) |
| 1936 | ptr = buf->data; |
| 1937 | } |
| 1938 | |
| 1939 | /* after LF; point to beginning of next line */ |
| 1940 | p2++; |
| 1941 | if (p2 >= buf->data + buf->size) |
| 1942 | p2 = buf->data; |
| 1943 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1944 | bytes = p2 - buf->lr; |
| 1945 | if (bytes < 0) |
| 1946 | bytes += buf->size; |
| 1947 | |
| 1948 | /* schedule this line for forwarding */ |
| 1949 | msg->sov += bytes; |
| 1950 | if (msg->sov >= buf->size) |
| 1951 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1952 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1953 | if (p1 == buf->lr) { |
| 1954 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1955 | * Everything was scheduled for forwarding, there's nothing |
| 1956 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1957 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1958 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1959 | msg->msg_state = HTTP_MSG_DONE; |
| 1960 | return 1; |
| 1961 | } |
| 1962 | /* OK, next line then */ |
| 1963 | buf->lr = p2; |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1968 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 1969 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 1970 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1971 | * not enough data are available, the function does not change anything and |
| 1972 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1973 | * does not change anything. Note: this function is designed to parse wrapped |
| 1974 | * CRLF at the end of the buffer. |
| 1975 | */ |
| 1976 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 1977 | { |
| 1978 | char *ptr; |
| 1979 | int bytes; |
| 1980 | |
| 1981 | /* NB: we'll check data availabilty at the end. It's not a |
| 1982 | * problem because whatever we match first will be checked |
| 1983 | * against the correct length. |
| 1984 | */ |
| 1985 | bytes = 1; |
| 1986 | ptr = buf->lr; |
| 1987 | if (*ptr == '\r') { |
| 1988 | bytes++; |
| 1989 | ptr++; |
| 1990 | if (ptr >= buf->data + buf->size) |
| 1991 | ptr = buf->data; |
| 1992 | } |
| 1993 | |
| 1994 | if (buf->l < bytes) |
| 1995 | return 0; |
| 1996 | |
| 1997 | if (*ptr != '\n') |
| 1998 | return -1; |
| 1999 | |
| 2000 | ptr++; |
| 2001 | if (ptr >= buf->data + buf->size) |
| 2002 | ptr = buf->data; |
| 2003 | buf->lr = ptr; |
| 2004 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2005 | msg->sov = ptr - buf->data; |
| 2006 | msg->som = msg->sov - bytes; |
| 2007 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2008 | return 1; |
| 2009 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2010 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2011 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2012 | { |
| 2013 | char *end = buf->data + buf->size; |
| 2014 | int off = buf->data + buf->size - buf->w; |
| 2015 | |
| 2016 | /* two possible cases : |
| 2017 | * - the buffer is in one contiguous block, we move it in-place |
| 2018 | * - the buffer is in two blocks, we move it via the trash |
| 2019 | */ |
| 2020 | if (buf->l) { |
| 2021 | int block1 = buf->l; |
| 2022 | int block2 = 0; |
| 2023 | if (buf->r <= buf->w) { |
| 2024 | /* non-contiguous block */ |
| 2025 | block1 = buf->data + buf->size - buf->w; |
| 2026 | block2 = buf->r - buf->data; |
| 2027 | } |
| 2028 | if (block2) |
| 2029 | memcpy(trash, buf->data, block2); |
| 2030 | memmove(buf->data, buf->w, block1); |
| 2031 | if (block2) |
| 2032 | memcpy(buf->data + block1, trash, block2); |
| 2033 | } |
| 2034 | |
| 2035 | /* adjust all known pointers */ |
| 2036 | buf->w = buf->data; |
| 2037 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2038 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2039 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2040 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2041 | |
| 2042 | /* adjust relative pointers */ |
| 2043 | msg->som = 0; |
| 2044 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2045 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2046 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2047 | |
| 2048 | msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size; |
| 2049 | msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size; |
| 2050 | |
| 2051 | if (msg->err_pos >= 0) { |
| 2052 | msg->err_pos += off; |
| 2053 | if (msg->err_pos >= buf->size) |
| 2054 | msg->err_pos -= buf->size; |
| 2055 | } |
| 2056 | |
| 2057 | buf->flags &= ~BF_FULL; |
| 2058 | if (buf->l >= buffer_max_len(buf)) |
| 2059 | buf->flags |= BF_FULL; |
| 2060 | } |
| 2061 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2062 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2063 | * processing can continue on next analysers, or zero if it either needs more |
| 2064 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2065 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2066 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2067 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2068 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2069 | 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] | 2070 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2071 | /* |
| 2072 | * We will parse the partial (or complete) lines. |
| 2073 | * We will check the request syntax, and also join multi-line |
| 2074 | * headers. An index of all the lines will be elaborated while |
| 2075 | * parsing. |
| 2076 | * |
| 2077 | * For the parsing, we use a 28 states FSM. |
| 2078 | * |
| 2079 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2080 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2081 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2082 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2083 | * req->lr = first non-visited byte |
| 2084 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2085 | * |
| 2086 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2087 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2088 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2089 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2090 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2091 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2092 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2093 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2094 | struct http_txn *txn = &s->txn; |
| 2095 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2096 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2097 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2098 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2099 | now_ms, __FUNCTION__, |
| 2100 | s, |
| 2101 | req, |
| 2102 | req->rex, req->wex, |
| 2103 | req->flags, |
| 2104 | req->l, |
| 2105 | req->analysers); |
| 2106 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2107 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2108 | s->srv_error = http_return_srv_error; |
| 2109 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2110 | /* There's a protected area at the end of the buffer for rewriting |
| 2111 | * purposes. We don't want to start to parse the request if the |
| 2112 | * protected area is affected, because we may have to move processed |
| 2113 | * data later, which is much more complicated. |
| 2114 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2115 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 2116 | if (unlikely((req->flags & BF_FULL) || |
| 2117 | req->r < req->lr || |
| 2118 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2119 | if (req->send_max) { |
| 2120 | /* some data has still not left the buffer, wake us once that's done */ |
| 2121 | buffer_dont_connect(req); |
| 2122 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2123 | return 0; |
| 2124 | } |
| 2125 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2126 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2127 | } |
| 2128 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2129 | if (likely(req->lr < req->r)) |
| 2130 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2133 | /* 1: we might have to print this header in debug mode */ |
| 2134 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2135 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2136 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2137 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2138 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2139 | sol = req->data + msg->som; |
| 2140 | eol = sol + msg->sl.rq.l; |
| 2141 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2142 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2143 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2144 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2145 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2146 | while (cur_idx) { |
| 2147 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2148 | debug_hdr("clihdr", s, sol, eol); |
| 2149 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2150 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2151 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2152 | } |
| 2153 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2154 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2155 | /* |
| 2156 | * Now we quickly check if we have found a full valid request. |
| 2157 | * If not so, we check the FD and buffer states before leaving. |
| 2158 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2159 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2160 | * requests are checked first. |
| 2161 | * |
| 2162 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2163 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2164 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2165 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2166 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2167 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2168 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 2169 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2170 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2171 | /* 1: Since we are in header mode, if there's no space |
| 2172 | * left for headers, we won't be able to free more |
| 2173 | * later, so the session will never terminate. We |
| 2174 | * must terminate it now. |
| 2175 | */ |
| 2176 | if (unlikely(req->flags & BF_FULL)) { |
| 2177 | /* FIXME: check if URI is set and return Status |
| 2178 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2179 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2180 | goto return_bad_req; |
| 2181 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2182 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2183 | /* 2: have we encountered a read error ? */ |
| 2184 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2185 | if (txn->flags & TX_NOT_FIRST) |
| 2186 | goto failed_keep_alive; |
| 2187 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2188 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2189 | if (msg->err_pos >= 0) |
| 2190 | 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] | 2191 | msg->msg_state = HTTP_MSG_ERROR; |
| 2192 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2193 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2194 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2195 | if (s->listener->counters) |
| 2196 | s->listener->counters->failed_req++; |
| 2197 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2198 | if (!(s->flags & SN_ERR_MASK)) |
| 2199 | s->flags |= SN_ERR_CLICL; |
| 2200 | if (!(s->flags & SN_FINST_MASK)) |
| 2201 | s->flags |= SN_FINST_R; |
| 2202 | return 0; |
| 2203 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2204 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2205 | /* 3: has the read timeout expired ? */ |
| 2206 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2207 | if (txn->flags & TX_NOT_FIRST) |
| 2208 | goto failed_keep_alive; |
| 2209 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2210 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2211 | if (msg->err_pos >= 0) |
| 2212 | 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] | 2213 | txn->status = 408; |
| 2214 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2215 | msg->msg_state = HTTP_MSG_ERROR; |
| 2216 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2217 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2218 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2219 | if (s->listener->counters) |
| 2220 | s->listener->counters->failed_req++; |
| 2221 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2222 | if (!(s->flags & SN_ERR_MASK)) |
| 2223 | s->flags |= SN_ERR_CLITO; |
| 2224 | if (!(s->flags & SN_FINST_MASK)) |
| 2225 | s->flags |= SN_FINST_R; |
| 2226 | return 0; |
| 2227 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2228 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2229 | /* 4: have we encountered a close ? */ |
| 2230 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2231 | if (txn->flags & TX_NOT_FIRST) |
| 2232 | goto failed_keep_alive; |
| 2233 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2234 | if (msg->err_pos >= 0) |
| 2235 | 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] | 2236 | txn->status = 400; |
| 2237 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2238 | msg->msg_state = HTTP_MSG_ERROR; |
| 2239 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2240 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2241 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2242 | if (s->listener->counters) |
| 2243 | s->listener->counters->failed_req++; |
| 2244 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2245 | if (!(s->flags & SN_ERR_MASK)) |
| 2246 | s->flags |= SN_ERR_CLICL; |
| 2247 | if (!(s->flags & SN_FINST_MASK)) |
| 2248 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2249 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2252 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2253 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 5e8949c | 2010-01-03 14:38:03 +0100 | [diff] [blame^] | 2254 | req->flags &= ~BF_DONT_READ; |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2255 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2256 | /* just set the request timeout once at the beginning of the request */ |
| 2257 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 2258 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2259 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2260 | /* we're not ready yet */ |
| 2261 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2262 | |
| 2263 | failed_keep_alive: |
| 2264 | /* Here we process low-level errors for keep-alive requests. In |
| 2265 | * short, if the request is not the first one and it experiences |
| 2266 | * a timeout, read error or shutdown, we just silently close so |
| 2267 | * that the client can try again. |
| 2268 | */ |
| 2269 | txn->status = 0; |
| 2270 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2271 | req->analysers = 0; |
| 2272 | s->logs.logwait = 0; |
| 2273 | stream_int_cond_close(req->prod, NULL); |
| 2274 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2275 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2276 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2277 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2278 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2279 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2280 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2281 | * points to the CRLF of the request line. req->lr points to the first |
| 2282 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2283 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2284 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2285 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2286 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2287 | /* Maybe we found in invalid header name while we were configured not |
| 2288 | * to block on that, so we have to capture it now. |
| 2289 | */ |
| 2290 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2291 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2292 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2293 | /* ensure we keep this pointer to the beginning of the message */ |
| 2294 | msg->sol = req->data + msg->som; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2295 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2296 | /* |
| 2297 | * 1: identify the method |
| 2298 | */ |
| 2299 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 2300 | |
| 2301 | /* we can make use of server redirect on GET and HEAD */ |
| 2302 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2303 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2304 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2305 | /* |
| 2306 | * 2: check if the URI matches the monitor_uri. |
| 2307 | * We have to do this for every request which gets in, because |
| 2308 | * the monitor-uri is defined by the frontend. |
| 2309 | */ |
| 2310 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2311 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2312 | !memcmp(&req->data[msg->sl.rq.u], |
| 2313 | s->fe->monitor_uri, |
| 2314 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2315 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2316 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2317 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2318 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2319 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2320 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2321 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2322 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2323 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2324 | 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] | 2325 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2326 | ret = acl_pass(ret); |
| 2327 | if (cond->pol == ACL_COND_UNLESS) |
| 2328 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2329 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2330 | if (ret) { |
| 2331 | /* we fail this request, let's return 503 service unavail */ |
| 2332 | txn->status = 503; |
| 2333 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2334 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2335 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2336 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2337 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2338 | /* nothing to fail, let's reply normaly */ |
| 2339 | txn->status = 200; |
| 2340 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2341 | goto return_prx_cond; |
| 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2346 | * Note: we cannot log anymore if the request has been |
| 2347 | * classified as invalid. |
| 2348 | */ |
| 2349 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2350 | /* we have a complete HTTP request that we must log */ |
| 2351 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2352 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2353 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2354 | if (urilen >= REQURI_LEN) |
| 2355 | urilen = REQURI_LEN - 1; |
| 2356 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2357 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2358 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2359 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2360 | s->do_log(s); |
| 2361 | } else { |
| 2362 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2363 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2364 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2365 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2366 | /* 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] | 2367 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2368 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2369 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2370 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2371 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 2372 | ((req->data[msg->sl.rq.v + 5] > '1') || |
| 2373 | ((req->data[msg->sl.rq.v + 5] == '1') && |
| 2374 | (req->data[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2375 | txn->flags |= TX_REQ_VER_11; |
| 2376 | |
| 2377 | /* "connection" has not been parsed yet */ |
| 2378 | txn->flags &= ~TX_CON_HDR_PARS; |
| 2379 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2380 | /* transfer length unknown*/ |
| 2381 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2382 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2383 | /* 5: we may need to capture headers */ |
| 2384 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
| 2385 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
| 2386 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2387 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2388 | /* 6: determine the transfer-length. |
| 2389 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2390 | * the presence of a message-body in a REQUEST and its transfer length |
| 2391 | * must be determined that way (in order of precedence) : |
| 2392 | * 1. The presence of a message-body in a request is signaled by the |
| 2393 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2394 | * in the request's header fields. When a request message contains |
| 2395 | * both a message-body of non-zero length and a method that does |
| 2396 | * not define any semantics for that request message-body, then an |
| 2397 | * origin server SHOULD either ignore the message-body or respond |
| 2398 | * with an appropriate error message (e.g., 413). A proxy or |
| 2399 | * gateway, when presented the same request, SHOULD either forward |
| 2400 | * the request inbound with the message- body or ignore the |
| 2401 | * message-body when determining a response. |
| 2402 | * |
| 2403 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2404 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2405 | * transfer-length is defined by the use of this transfer-coding. |
| 2406 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2407 | * transfer-coding is not present, the transfer-length is defined |
| 2408 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2409 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2410 | * 3. If a Content-Length header field is present, its decimal value in |
| 2411 | * OCTETs represents both the entity-length and the transfer-length. |
| 2412 | * If a message is received with both a Transfer-Encoding header |
| 2413 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2414 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2415 | * 4. By the server closing the connection. (Closing the connection |
| 2416 | * cannot be used to indicate the end of a request body, since that |
| 2417 | * would leave no possibility for the server to send back a response.) |
| 2418 | * |
| 2419 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2420 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2421 | * it is terminated by closing the connection. When the "chunked" |
| 2422 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2423 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2424 | */ |
| 2425 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2426 | /* CONNECT sets a tunnel and ignores everything else */ |
| 2427 | if (txn->meth == HTTP_METH_CONNECT) |
| 2428 | goto skip_xfer_len; |
| 2429 | |
| 2430 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2431 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2432 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2433 | while ((txn->flags & TX_REQ_VER_11) && |
| 2434 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2435 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2436 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2437 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2438 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2439 | use_close_only = 1; |
| 2440 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2441 | break; |
| 2442 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2443 | } |
| 2444 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2445 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2446 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2447 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2448 | signed long long cl; |
| 2449 | |
| 2450 | if (!ctx.vlen) |
| 2451 | goto return_bad_req; |
| 2452 | |
| 2453 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2454 | goto return_bad_req; /* parse failure */ |
| 2455 | |
| 2456 | if (cl < 0) |
| 2457 | goto return_bad_req; |
| 2458 | |
| 2459 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2460 | goto return_bad_req; /* already specified, was different */ |
| 2461 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2462 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2463 | msg->hdr_content_len = cl; |
| 2464 | } |
| 2465 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2466 | /* bodyless requests have a known length */ |
| 2467 | if (!use_close_only) |
| 2468 | txn->flags |= TX_REQ_XFER_LEN; |
| 2469 | |
| 2470 | skip_xfer_len: |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2471 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2472 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2473 | req->analyse_exp = TICK_ETERNITY; |
| 2474 | return 1; |
| 2475 | |
| 2476 | return_bad_req: |
| 2477 | /* We centralize bad requests processing here */ |
| 2478 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2479 | /* we detected a parsing error. We want to archive this request |
| 2480 | * in the dedicated proxy area for later troubleshooting. |
| 2481 | */ |
| 2482 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2483 | } |
| 2484 | |
| 2485 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2486 | txn->status = 400; |
| 2487 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2488 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2489 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2490 | if (s->listener->counters) |
| 2491 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2492 | |
| 2493 | return_prx_cond: |
| 2494 | if (!(s->flags & SN_ERR_MASK)) |
| 2495 | s->flags |= SN_ERR_PRXCOND; |
| 2496 | if (!(s->flags & SN_FINST_MASK)) |
| 2497 | s->flags |= SN_FINST_R; |
| 2498 | |
| 2499 | req->analysers = 0; |
| 2500 | req->analyse_exp = TICK_ETERNITY; |
| 2501 | return 0; |
| 2502 | } |
| 2503 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2504 | /* This stream analyser runs all HTTP request processing which is common to |
| 2505 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2506 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2507 | * 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] | 2508 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2509 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2510 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2511 | 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] | 2512 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2513 | struct http_txn *txn = &s->txn; |
| 2514 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2515 | struct acl_cond *cond; |
| 2516 | struct redirect_rule *rule; |
| 2517 | int cur_idx; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2518 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2519 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2520 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2521 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2522 | return 0; |
| 2523 | } |
| 2524 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2525 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2526 | req->analyse_exp = TICK_ETERNITY; |
| 2527 | |
| 2528 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2529 | now_ms, __FUNCTION__, |
| 2530 | s, |
| 2531 | req, |
| 2532 | req->rex, req->wex, |
| 2533 | req->flags, |
| 2534 | req->l, |
| 2535 | req->analysers); |
| 2536 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2537 | /* first check whether we have some ACLs set to block this request */ |
| 2538 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2539 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2540 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2541 | ret = acl_pass(ret); |
| 2542 | if (cond->pol == ACL_COND_UNLESS) |
| 2543 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2544 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2545 | if (ret) { |
| 2546 | txn->status = 403; |
| 2547 | /* let's log the request time */ |
| 2548 | s->logs.tv_request = now; |
| 2549 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2550 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2551 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2552 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2553 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2554 | /* try headers filters */ |
| 2555 | if (px->req_exp != NULL) { |
| 2556 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2557 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2558 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2559 | /* has the request been denied ? */ |
| 2560 | if (txn->flags & TX_CLDENY) { |
| 2561 | /* no need to go further */ |
| 2562 | txn->status = 403; |
| 2563 | /* let's log the request time */ |
| 2564 | s->logs.tv_request = now; |
| 2565 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2566 | goto return_prx_cond; |
| 2567 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2568 | |
| 2569 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2570 | * which may be the same as the connect timeout if unspecified. |
| 2571 | * If unset, then set it to zero because we really want it to |
| 2572 | * eventually expire. We build the tarpit as an analyser. |
| 2573 | */ |
| 2574 | if (txn->flags & TX_CLTARPIT) { |
| 2575 | buffer_erase(s->req); |
| 2576 | /* wipe the request out so that we can drop the connection early |
| 2577 | * if the client closes first. |
| 2578 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2579 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2580 | req->analysers = 0; /* remove switching rules etc... */ |
| 2581 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2582 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2583 | if (!req->analyse_exp) |
| 2584 | req->analyse_exp = tick_add(now_ms, 0); |
| 2585 | return 1; |
| 2586 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2587 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2588 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2589 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2590 | * only change if both the request and the config reference something else. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2591 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2592 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2593 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2594 | ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2595 | int tmp = TX_CON_WANT_TUN; |
| 2596 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2597 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2598 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2599 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2600 | if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) |
| 2601 | tmp = TX_CON_WANT_CLO; |
| 2602 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2603 | if (!(txn->flags & TX_REQ_XFER_LEN)) |
| 2604 | tmp = TX_CON_WANT_CLO; |
| 2605 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2606 | if (!(txn->flags & TX_CON_HDR_PARS)) |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2607 | http_req_parse_connection_header(txn); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2608 | |
| 2609 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2610 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 2611 | } |
| 2612 | |
| 2613 | /* We're really certain of the connection mode (tunnel, close, keep-alive) |
| 2614 | * once we know the backend, because the tunnel mode can be implied by the |
| 2615 | * lack of any close/keepalive options in both the FE and the BE. Since |
| 2616 | * this information can evolve with time, we proceed by trying to make the |
| 2617 | * header status match the desired status. For this, we'll have to adjust |
| 2618 | * the "Connection" header. The test for persistent connections has already |
| 2619 | * been performed, so we only enter here if there is a risk the connection |
| 2620 | * is considered as persistent and we want it to be closed on the server |
| 2621 | * side. It would be nice if we could enter this place only when a |
| 2622 | * Connection header exists. Note that a CONNECT method will not enter |
| 2623 | * here. |
| 2624 | */ |
| 2625 | 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] | 2626 | char *cur_ptr, *cur_end, *cur_next; |
| 2627 | int old_idx, delta, val; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2628 | int must_delete; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2629 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2630 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2631 | must_delete = !(txn->flags & TX_REQ_VER_11); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2632 | 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] | 2633 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2634 | 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] | 2635 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2636 | cur_ptr = cur_next; |
| 2637 | cur_end = cur_ptr + cur_hdr->len; |
| 2638 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2639 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2640 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2641 | if (!val) |
| 2642 | continue; |
| 2643 | |
| 2644 | /* 3 possibilities : |
| 2645 | * - we have already set "Connection: close" or we're in |
| 2646 | * HTTP/1.0, so we remove this line. |
| 2647 | * - we have not yet set "Connection: close", but this line |
| 2648 | * indicates close. We leave it untouched and set the flag. |
| 2649 | * - we have not yet set "Connection: close", and this line |
| 2650 | * indicates non-close. We replace it and set the flag. |
| 2651 | */ |
| 2652 | if (must_delete) { |
| 2653 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 2654 | http_msg_move_end(&txn->req, delta); |
| 2655 | cur_next += delta; |
| 2656 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2657 | txn->hdr_idx.used--; |
| 2658 | cur_hdr->len = 0; |
| 2659 | txn->flags |= TX_REQ_CONN_CLO; |
| 2660 | } else { |
| 2661 | if (cur_end - cur_ptr - val != 5 || |
| 2662 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2663 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2664 | "close", 5); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2665 | cur_next += delta; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2666 | cur_hdr->len += delta; |
| 2667 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2668 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2669 | txn->flags |= TX_REQ_CONN_CLO; |
| 2670 | must_delete = 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2671 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2672 | } /* for loop */ |
| 2673 | } /* if must close keep-alive */ |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2674 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2675 | /* add request headers from the rule sets in the same order */ |
| 2676 | for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) { |
| 2677 | if (unlikely(http_header_add_tail(req, |
| 2678 | &txn->req, |
| 2679 | &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 2680 | px->req_add[cur_idx]) < 0)) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2681 | goto return_bad_req; |
| 2682 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2683 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2684 | /* check if stats URI was requested, and if an auth is needed */ |
| 2685 | if (px->uri_auth != NULL && |
| 2686 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2687 | /* we have to check the URI and auth for this request. |
| 2688 | * FIXME!!! that one is rather dangerous, we want to |
| 2689 | * make it follow standard rules (eg: clear req->analysers). |
| 2690 | */ |
| 2691 | if (stats_check_uri_auth(s, px)) { |
| 2692 | req->analysers = 0; |
| 2693 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2695 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2696 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2697 | /* check whether we have some ACLs set to redirect this request */ |
| 2698 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 2699 | 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] | 2700 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2701 | ret = acl_pass(ret); |
| 2702 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2703 | ret = !ret; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2704 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2705 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2706 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2707 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2708 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2709 | /* build redirect message */ |
| 2710 | switch(rule->code) { |
| 2711 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2712 | msg_fmt = HTTP_303; |
| 2713 | break; |
| 2714 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2715 | msg_fmt = HTTP_301; |
| 2716 | break; |
| 2717 | case 302: |
| 2718 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2719 | msg_fmt = HTTP_302; |
| 2720 | break; |
| 2721 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2722 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2723 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2724 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2725 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2726 | switch(rule->type) { |
| 2727 | case REDIRECT_TYPE_PREFIX: { |
| 2728 | const char *path; |
| 2729 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2730 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2731 | path = http_get_path(txn); |
| 2732 | /* build message using path */ |
| 2733 | if (path) { |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 2734 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2735 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2736 | int qs = 0; |
| 2737 | while (qs < pathlen) { |
| 2738 | if (path[qs] == '?') { |
| 2739 | pathlen = qs; |
| 2740 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2741 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2742 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2743 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2744 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2745 | } else { |
| 2746 | path = "/"; |
| 2747 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2748 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2749 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2750 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2751 | goto return_bad_req; |
| 2752 | |
| 2753 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2754 | * add anything, otherwise it makes it hard for the user to |
| 2755 | * configure a self-redirection. |
| 2756 | */ |
| 2757 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2758 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2759 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2760 | } |
| 2761 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2762 | /* add path */ |
| 2763 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2764 | rdr.len += pathlen; |
| 2765 | break; |
| 2766 | } |
| 2767 | case REDIRECT_TYPE_LOCATION: |
| 2768 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2769 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2770 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2771 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2772 | /* add location */ |
| 2773 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2774 | rdr.len += rule->rdr_len; |
| 2775 | break; |
| 2776 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2777 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2778 | if (rule->cookie_len) { |
| 2779 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2780 | rdr.len += 14; |
| 2781 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2782 | rdr.len += rule->cookie_len; |
| 2783 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2784 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2785 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2786 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2787 | /* add end of headers */ |
| 2788 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2789 | rdr.len += 4; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2790 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2791 | txn->status = rule->code; |
| 2792 | /* let's log the request time */ |
| 2793 | s->logs.tv_request = now; |
| 2794 | stream_int_retnclose(req->prod, &rdr); |
| 2795 | goto return_prx_cond; |
| 2796 | } |
| 2797 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2798 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2799 | /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */ |
| 2800 | if ((txn->flags & TX_REQ_XFER_LEN) && |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2801 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2802 | (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | 349a0f6 | 2009-10-18 21:17:42 +0200 | [diff] [blame] | 2803 | req->flags |= BF_DONT_READ; |
Willy Tarreau | 5e8949c | 2010-01-03 14:38:03 +0100 | [diff] [blame^] | 2804 | else |
| 2805 | req->flags &= ~BF_DONT_READ; |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 2806 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2807 | /* that's OK for us now, let's move on to next analysers */ |
| 2808 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2809 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2810 | return_bad_req: |
| 2811 | /* We centralize bad requests processing here */ |
| 2812 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2813 | /* we detected a parsing error. We want to archive this request |
| 2814 | * in the dedicated proxy area for later troubleshooting. |
| 2815 | */ |
| 2816 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2817 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2818 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2819 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2820 | txn->status = 400; |
| 2821 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2822 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2823 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2824 | if (s->listener->counters) |
| 2825 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2826 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2827 | return_prx_cond: |
| 2828 | if (!(s->flags & SN_ERR_MASK)) |
| 2829 | s->flags |= SN_ERR_PRXCOND; |
| 2830 | if (!(s->flags & SN_FINST_MASK)) |
| 2831 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2832 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2833 | req->analysers = 0; |
| 2834 | req->analyse_exp = TICK_ETERNITY; |
| 2835 | return 0; |
| 2836 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2837 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2838 | /* This function performs all the processing enabled for the current request. |
| 2839 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2840 | * needs more data, encounters an error, or wants to immediately abort the |
| 2841 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 2842 | */ |
| 2843 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 2844 | { |
| 2845 | struct http_txn *txn = &s->txn; |
| 2846 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2847 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2848 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2849 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2850 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2851 | return 0; |
| 2852 | } |
| 2853 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2854 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2855 | now_ms, __FUNCTION__, |
| 2856 | s, |
| 2857 | req, |
| 2858 | req->rex, req->wex, |
| 2859 | req->flags, |
| 2860 | req->l, |
| 2861 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2862 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2863 | /* |
| 2864 | * Right now, we know that we have processed the entire headers |
| 2865 | * and that unwanted requests have been filtered out. We can do |
| 2866 | * whatever we want with the remaining request. Also, now we |
| 2867 | * may have separate values for ->fe, ->be. |
| 2868 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2869 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2870 | /* |
| 2871 | * If HTTP PROXY is set we simply get remote server address |
| 2872 | * parsing incoming request. |
| 2873 | */ |
| 2874 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
| 2875 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
| 2876 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2877 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2878 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2879 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2880 | * Note that doing so might move headers in the request, but |
| 2881 | * the fields will stay coherent and the URI will not move. |
| 2882 | * This should only be performed in the backend. |
| 2883 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2884 | 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] | 2885 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 2886 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2887 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2888 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2889 | * 8: the appsession cookie was looked up very early in 1.2, |
| 2890 | * so let's do the same now. |
| 2891 | */ |
| 2892 | |
| 2893 | /* It needs to look into the URI */ |
| 2894 | if ((s->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 2895 | get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2899 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2900 | * asks for it. |
| 2901 | */ |
| 2902 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 2903 | if (s->cli_addr.ss_family == AF_INET) { |
| 2904 | /* Add an X-Forwarded-For header unless the source IP is |
| 2905 | * in the 'except' network range. |
| 2906 | */ |
| 2907 | if ((!s->fe->except_mask.s_addr || |
| 2908 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 2909 | != s->fe->except_net.s_addr) && |
| 2910 | (!s->be->except_mask.s_addr || |
| 2911 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 2912 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2913 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2914 | unsigned char *pn; |
| 2915 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2916 | |
| 2917 | /* Note: we rely on the backend to get the header name to be used for |
| 2918 | * x-forwarded-for, because the header is really meant for the backends. |
| 2919 | * However, if the backend did not specify any option, we have to rely |
| 2920 | * on the frontend's header name. |
| 2921 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2922 | if (s->be->fwdfor_hdr_len) { |
| 2923 | len = s->be->fwdfor_hdr_len; |
| 2924 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2925 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2926 | len = s->fe->fwdfor_hdr_len; |
| 2927 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 2928 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2929 | 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] | 2930 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2931 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 2932 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2933 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2934 | } |
| 2935 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2936 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 2937 | /* FIXME: for the sake of completeness, we should also support |
| 2938 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2939 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2940 | int len; |
| 2941 | char pn[INET6_ADDRSTRLEN]; |
| 2942 | inet_ntop(AF_INET6, |
| 2943 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2944 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2945 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2946 | /* Note: we rely on the backend to get the header name to be used for |
| 2947 | * x-forwarded-for, because the header is really meant for the backends. |
| 2948 | * However, if the backend did not specify any option, we have to rely |
| 2949 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2950 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2951 | if (s->be->fwdfor_hdr_len) { |
| 2952 | len = s->be->fwdfor_hdr_len; |
| 2953 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 2954 | } else { |
| 2955 | len = s->fe->fwdfor_hdr_len; |
| 2956 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2957 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2958 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2959 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2960 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 2961 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2962 | goto return_bad_req; |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2967 | * 10: add X-Original-To if either the frontend or the backend |
| 2968 | * asks for it. |
| 2969 | */ |
| 2970 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 2971 | |
| 2972 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 2973 | if (s->cli_addr.ss_family == AF_INET) { |
| 2974 | /* Add an X-Original-To header unless the destination IP is |
| 2975 | * in the 'except' network range. |
| 2976 | */ |
| 2977 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 2978 | get_frt_addr(s); |
| 2979 | |
| 2980 | if ((!s->fe->except_mask_to.s_addr || |
| 2981 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 2982 | != s->fe->except_to.s_addr) && |
| 2983 | (!s->be->except_mask_to.s_addr || |
| 2984 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 2985 | != s->be->except_to.s_addr)) { |
| 2986 | int len; |
| 2987 | unsigned char *pn; |
| 2988 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 2989 | |
| 2990 | /* Note: we rely on the backend to get the header name to be used for |
| 2991 | * x-original-to, because the header is really meant for the backends. |
| 2992 | * However, if the backend did not specify any option, we have to rely |
| 2993 | * on the frontend's header name. |
| 2994 | */ |
| 2995 | if (s->be->orgto_hdr_len) { |
| 2996 | len = s->be->orgto_hdr_len; |
| 2997 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 2998 | } else { |
| 2999 | len = s->fe->orgto_hdr_len; |
| 3000 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3001 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3002 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3003 | |
| 3004 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3005 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3006 | goto return_bad_req; |
| 3007 | } |
| 3008 | } |
| 3009 | } |
| 3010 | |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3011 | /* 11: add "Connection: close" if needed and not yet set. */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3012 | 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] | 3013 | if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3014 | "Connection: close", 17) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3015 | goto return_bad_req; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3016 | txn->flags |= TX_REQ_CONN_CLO; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3017 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3018 | |
| 3019 | /* If we have no server assigned yet and we're balancing on url_param |
| 3020 | * with a POST request, we may be interested in checking the body for |
| 3021 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3022 | */ |
| 3023 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3024 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3025 | s->be->url_param_post_limit != 0 && |
| 3026 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 3027 | memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3028 | buffer_dont_connect(req); |
| 3029 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3030 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3031 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3032 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3033 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3034 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3035 | /************************************************************* |
| 3036 | * OK, that's finished for the headers. We have done what we * |
| 3037 | * could. Let's switch to the DATA state. * |
| 3038 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3039 | req->analyse_exp = TICK_ETERNITY; |
| 3040 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3041 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3042 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3043 | /* OK let's go on with the BODY now */ |
| 3044 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3045 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3046 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3047 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3048 | /* we detected a parsing error. We want to archive this request |
| 3049 | * in the dedicated proxy area for later troubleshooting. |
| 3050 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3051 | 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] | 3052 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3053 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3054 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3055 | txn->status = 400; |
| 3056 | req->analysers = 0; |
| 3057 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3058 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3059 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3060 | if (s->listener->counters) |
| 3061 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3062 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3063 | if (!(s->flags & SN_ERR_MASK)) |
| 3064 | s->flags |= SN_ERR_PRXCOND; |
| 3065 | if (!(s->flags & SN_FINST_MASK)) |
| 3066 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3067 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3068 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3069 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3070 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3071 | * returns zero, at the beginning because it prevents any other processing |
| 3072 | * from occurring, and at the end because it terminates the request. |
| 3073 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3074 | 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] | 3075 | { |
| 3076 | struct http_txn *txn = &s->txn; |
| 3077 | |
| 3078 | /* This connection is being tarpitted. The CLIENT side has |
| 3079 | * already set the connect expiration date to the right |
| 3080 | * timeout. We just have to check that the client is still |
| 3081 | * there and that the timeout has not expired. |
| 3082 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3083 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3084 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3085 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3086 | return 0; |
| 3087 | |
| 3088 | /* We will set the queue timer to the time spent, just for |
| 3089 | * logging purposes. We fake a 500 server error, so that the |
| 3090 | * attacker will not suspect his connection has been tarpitted. |
| 3091 | * It will not cause trouble to the logs because we can exclude |
| 3092 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3093 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3094 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3095 | |
| 3096 | txn->status = 500; |
| 3097 | if (req->flags != BF_READ_ERROR) |
| 3098 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3099 | |
| 3100 | req->analysers = 0; |
| 3101 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3102 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3103 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3104 | if (s->listener->counters) |
| 3105 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3106 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3107 | if (!(s->flags & SN_ERR_MASK)) |
| 3108 | s->flags |= SN_ERR_PRXCOND; |
| 3109 | if (!(s->flags & SN_FINST_MASK)) |
| 3110 | s->flags |= SN_FINST_T; |
| 3111 | return 0; |
| 3112 | } |
| 3113 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3114 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3115 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3116 | * must only be called after the standard HTTP request processing has occurred, |
| 3117 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3118 | * read more data, or 1 once it has completed its analysis. |
| 3119 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3120 | 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] | 3121 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3122 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3123 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3124 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3125 | |
| 3126 | /* We have to parse the HTTP request body to find any required data. |
| 3127 | * "balance url_param check_post" should have been the only way to get |
| 3128 | * into this. We were brought here after HTTP header analysis, so all |
| 3129 | * related structures are ready. |
| 3130 | */ |
| 3131 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3132 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3133 | goto missing_data; |
| 3134 | |
| 3135 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3136 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3137 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3138 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3139 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3140 | struct hdr_ctx ctx; |
| 3141 | ctx.idx = 0; |
| 3142 | /* Expect is allowed in 1.1, look for it */ |
| 3143 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3144 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3145 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3146 | } |
| 3147 | } |
| 3148 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3149 | } |
| 3150 | |
| 3151 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3152 | /* we have msg->col and msg->sov which both point to the first |
| 3153 | * byte of message body. msg->som still points to the beginning |
| 3154 | * of the message. We must save the body in req->lr because it |
| 3155 | * survives buffer re-alignments. |
| 3156 | */ |
| 3157 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3158 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3159 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3160 | else |
| 3161 | msg->msg_state = HTTP_MSG_DATA; |
| 3162 | } |
| 3163 | |
| 3164 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3165 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3166 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3167 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3168 | */ |
| 3169 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3170 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3171 | if (!ret) |
| 3172 | goto missing_data; |
| 3173 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3174 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3175 | } |
| 3176 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3177 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3178 | * We have the first non-header byte in msg->col, which is either the |
| 3179 | * beginning of the chunk size or of the data. The first data byte is in |
| 3180 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3181 | * 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] | 3182 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3183 | |
| 3184 | if (msg->hdr_content_len < limit) |
| 3185 | limit = msg->hdr_content_len; |
| 3186 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3187 | if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3188 | goto http_end; |
| 3189 | |
| 3190 | missing_data: |
| 3191 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3192 | if (req->flags & BF_FULL) |
| 3193 | goto return_bad_req; |
| 3194 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3195 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3196 | txn->status = 408; |
| 3197 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3198 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3199 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3200 | |
| 3201 | /* we get here if we need to wait for more data */ |
| 3202 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3203 | /* Not enough data. We'll re-use the http-request |
| 3204 | * timeout here. Ideally, we should set the timeout |
| 3205 | * relative to the accept() date. We just set the |
| 3206 | * request timeout once at the beginning of the |
| 3207 | * request. |
| 3208 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3209 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3210 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3211 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3212 | return 0; |
| 3213 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3214 | |
| 3215 | http_end: |
| 3216 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3217 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3218 | req->analysers &= ~an_bit; |
| 3219 | req->analyse_exp = TICK_ETERNITY; |
| 3220 | return 1; |
| 3221 | |
| 3222 | return_bad_req: /* let's centralize all bad requests */ |
| 3223 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3224 | txn->status = 400; |
| 3225 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3226 | |
| 3227 | return_err_msg: |
| 3228 | req->analysers = 0; |
| 3229 | s->fe->counters.failed_req++; |
| 3230 | if (s->listener->counters) |
| 3231 | s->listener->counters->failed_req++; |
| 3232 | |
| 3233 | if (!(s->flags & SN_ERR_MASK)) |
| 3234 | s->flags |= SN_ERR_PRXCOND; |
| 3235 | if (!(s->flags & SN_FINST_MASK)) |
| 3236 | s->flags |= SN_FINST_R; |
| 3237 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3238 | } |
| 3239 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3240 | /* This function is an analyser which forwards request body (including chunk |
| 3241 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3242 | * zero byte. The only situation where it must not be called is when we're in |
| 3243 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3244 | * remaining data and to resync after end of body. It expects the msg_state to |
| 3245 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 3246 | * read more data, or 1 once we can go on with next request or end the session. |
| 3247 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 3248 | * bytes of pending data + the headers if not already done (between som and sov). |
| 3249 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 3250 | */ |
| 3251 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 3252 | { |
| 3253 | struct http_txn *txn = &s->txn; |
| 3254 | struct http_msg *msg = &s->txn.req; |
| 3255 | |
Willy Tarreau | 21c5e4d | 2010-01-03 00:19:31 +0100 | [diff] [blame] | 3256 | if (req->flags & (BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3257 | req->analysers &= ~an_bit; |
| 3258 | return 1; |
| 3259 | } |
| 3260 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3261 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3262 | return 0; |
| 3263 | |
| 3264 | /* Note that we don't have to send 100-continue back because we don't |
| 3265 | * need the data to complete our job, and it's up to the server to |
| 3266 | * decide whether to return 100, 417 or anything else in return of |
| 3267 | * an "Expect: 100-continue" header. |
| 3268 | */ |
| 3269 | |
| 3270 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3271 | /* we have msg->col and msg->sov which both point to the first |
| 3272 | * byte of message body. msg->som still points to the beginning |
| 3273 | * of the message. We must save the body in req->lr because it |
| 3274 | * survives buffer re-alignments. |
| 3275 | */ |
| 3276 | req->lr = req->data + msg->sov; |
| 3277 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3278 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3279 | else { |
| 3280 | msg->msg_state = HTTP_MSG_DATA; |
| 3281 | } |
| 3282 | } |
| 3283 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3284 | while (1) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 3285 | /* we may have some data pending */ |
| 3286 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 3287 | int bytes = msg->sov - msg->som; |
| 3288 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 3289 | bytes += req->size; |
| 3290 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 3291 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 3292 | msg->som = msg->sov; |
| 3293 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3294 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3295 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
| 3296 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 3297 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3298 | * TRAILERS state. |
| 3299 | */ |
| 3300 | int ret = http_parse_chunk_size(req, msg); |
| 3301 | |
| 3302 | if (!ret) |
| 3303 | goto missing_data; |
| 3304 | else if (ret < 0) |
| 3305 | goto return_bad_req; |
| 3306 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3307 | } |
| 3308 | else if (msg->msg_state == HTTP_MSG_DATA) { |
| 3309 | /* must still forward */ |
| 3310 | if (req->to_forward) |
| 3311 | return 0; |
| 3312 | |
| 3313 | /* we're sending the last bits of request, the server's response |
| 3314 | * is expected in a short time. Most often the first read is enough |
| 3315 | * to bring all the headers, so we're preparing the response buffer |
| 3316 | * to read the response now. Note that we should probably move that |
| 3317 | * to a more appropriate place. |
| 3318 | */ |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3319 | if (txn->rsp.msg_state == HTTP_MSG_RPBEFORE) { |
| 3320 | s->rep->flags &= ~BF_DONT_READ; |
| 3321 | s->rep->flags |= BF_READ_DONTWAIT; |
| 3322 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3323 | |
| 3324 | /* nothing left to forward */ |
| 3325 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3326 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 3327 | else { |
| 3328 | msg->msg_state = HTTP_MSG_DONE; |
| 3329 | } |
| 3330 | } |
| 3331 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 3332 | /* we want the CRLF after the data */ |
| 3333 | int ret; |
| 3334 | |
| 3335 | if (!(req->flags & BF_OUT_EMPTY)) |
| 3336 | return 0; |
| 3337 | /* The output pointer does not move anymore, next unsent data |
| 3338 | * are available at ->w. Let's save that. |
| 3339 | */ |
| 3340 | req->lr = req->w; |
| 3341 | ret = http_skip_chunk_crlf(req, msg); |
| 3342 | |
| 3343 | if (ret == 0) |
| 3344 | goto missing_data; |
| 3345 | else if (ret < 0) |
| 3346 | goto return_bad_req; |
| 3347 | /* we're in MSG_CHUNK_SIZE now */ |
| 3348 | } |
| 3349 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 3350 | int ret = http_forward_trailers(req, msg); |
| 3351 | |
| 3352 | if (ret == 0) |
| 3353 | goto missing_data; |
| 3354 | else if (ret < 0) |
| 3355 | goto return_bad_req; |
| 3356 | /* we're in HTTP_MSG_DONE now */ |
| 3357 | } |
| 3358 | else if (msg->msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3359 | /* No need to read anymore, the request was completely parsed */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3360 | req->flags |= BF_DONT_READ; |
| 3361 | |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3362 | if (txn->rsp.msg_state < HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) { |
| 3363 | /* The server has not finished to respond, so we |
| 3364 | * don't want to move in order not to upset it. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3365 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3366 | return 0; |
| 3367 | } |
| 3368 | |
| 3369 | /* when we support keep-alive or server-close modes, we'll have |
| 3370 | * to reset the transaction here. |
| 3371 | */ |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3372 | |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3373 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3374 | /* initiate a connection close to the server */ |
| 3375 | req->cons->flags |= SI_FL_NOLINGER; |
| 3376 | buffer_shutw_now(req); |
| 3377 | } |
| 3378 | else if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) { |
Willy Tarreau | 9438c71 | 2009-12-29 14:39:48 +0100 | [diff] [blame] | 3379 | /* Option forceclose is set, let's enforce it now |
| 3380 | * that the transfer is complete. We can safely speed |
| 3381 | * up the close because we know the server has received |
| 3382 | * everything we wanted it to receive. |
| 3383 | */ |
| 3384 | req->cons->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 3385 | buffer_abort(req); |
| 3386 | } |
| 3387 | |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3388 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3389 | if (req->flags & BF_OUT_EMPTY) |
| 3390 | msg->msg_state = HTTP_MSG_CLOSED; |
| 3391 | else |
| 3392 | msg->msg_state = HTTP_MSG_CLOSING; |
| 3393 | } |
| 3394 | else { |
| 3395 | /* for other modes, we let further requests pass for now */ |
| 3396 | req->flags &= ~BF_DONT_READ; |
| 3397 | /* FIXME: we're still forced to do that here */ |
| 3398 | s->rep->flags &= ~BF_DONT_READ; |
| 3399 | break; |
| 3400 | } |
| 3401 | } |
| 3402 | else if (msg->msg_state == HTTP_MSG_CLOSING) { |
| 3403 | /* nothing else to forward, just waiting for the buffer to be empty */ |
| 3404 | if (!(req->flags & BF_OUT_EMPTY)) |
| 3405 | return 0; |
| 3406 | msg->msg_state = HTTP_MSG_CLOSED; |
| 3407 | } |
| 3408 | else if (msg->msg_state == HTTP_MSG_CLOSED) { |
| 3409 | req->flags &= ~BF_DONT_READ; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3410 | |
| 3411 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3412 | |
| 3413 | /* FIXME : this part is 1) awful, 2) tricky, 3) duplicated |
| 3414 | * ... but it works. |
| 3415 | * We need a better way to force a connection close without |
| 3416 | * any risk of propagation to the other side. We need a more |
| 3417 | * portable way of releasing a backend's and a server's |
| 3418 | * connections. We need a safer way to reinitialize buffer |
| 3419 | * flags. We also need a more accurate method for computing |
| 3420 | * per-request data. |
| 3421 | */ |
| 3422 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3423 | s->req->cons->shutr(s->req->cons); |
| 3424 | s->req->cons->shutw(s->req->cons); |
| 3425 | |
| 3426 | if (s->flags & SN_BE_ASSIGNED) |
| 3427 | s->be->beconn--; |
| 3428 | |
| 3429 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3430 | session_process_counters(s); |
| 3431 | |
| 3432 | if (s->txn.status) { |
| 3433 | int n; |
| 3434 | |
| 3435 | n = s->txn.status / 100; |
| 3436 | if (n < 1 || n > 5) |
| 3437 | n = 0; |
| 3438 | |
| 3439 | if (s->fe->mode == PR_MODE_HTTP) |
| 3440 | s->fe->counters.p.http.rsp[n]++; |
| 3441 | |
| 3442 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 3443 | (s->be->mode == PR_MODE_HTTP)) |
| 3444 | s->be->counters.p.http.rsp[n]++; |
| 3445 | } |
| 3446 | |
| 3447 | /* don't count other requests' data */ |
| 3448 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3449 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3450 | |
| 3451 | /* let's do a final log if we need it */ |
| 3452 | if (s->logs.logwait && |
| 3453 | !(s->flags & SN_MONITOR) && |
| 3454 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3455 | s->do_log(s); |
| 3456 | } |
| 3457 | |
| 3458 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3459 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3460 | tv_zero(&s->logs.tv_request); |
| 3461 | s->logs.t_queue = -1; |
| 3462 | s->logs.t_connect = -1; |
| 3463 | s->logs.t_data = -1; |
| 3464 | s->logs.t_close = 0; |
| 3465 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3466 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3467 | |
| 3468 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3469 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3470 | |
| 3471 | if (s->pend_pos) |
| 3472 | pendconn_free(s->pend_pos); |
| 3473 | |
| 3474 | if (s->srv) { |
| 3475 | if (s->flags & SN_CURR_SESS) { |
| 3476 | s->flags &= ~SN_CURR_SESS; |
| 3477 | s->srv->cur_sess--; |
| 3478 | } |
| 3479 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3480 | process_srv_queue(s->srv); |
| 3481 | } |
| 3482 | |
| 3483 | if (unlikely(s->srv_conn)) |
| 3484 | sess_change_server(s, NULL); |
| 3485 | s->srv = NULL; |
| 3486 | |
| 3487 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3488 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3489 | s->req->cons->err_type = SI_ET_NONE; |
| 3490 | s->req->cons->err_loc = NULL; |
| 3491 | s->req->cons->exp = TICK_ETERNITY; |
| 3492 | s->req->cons->flags = SI_FL_NONE; |
| 3493 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE); |
| 3494 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE|BF_WRITE_PARTIAL); |
| 3495 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED); |
| 3496 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3497 | s->txn.meth = 0; |
| 3498 | http_reset_txn(s); |
| 3499 | txn->flags |= TX_NOT_FIRST; |
| 3500 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3501 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3502 | |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3503 | /* if the request buffer is not empty, it means we're |
| 3504 | * about to process another request, so send pending |
| 3505 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 5e8949c | 2010-01-03 14:38:03 +0100 | [diff] [blame^] | 3506 | * Also, let's not start reading a small request packet, |
| 3507 | * we may prefer to read a larger one later. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3508 | */ |
Willy Tarreau | 5e8949c | 2010-01-03 14:38:03 +0100 | [diff] [blame^] | 3509 | if (s->req->l > s->req->send_max) { |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3510 | s->rep->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5e8949c | 2010-01-03 14:38:03 +0100 | [diff] [blame^] | 3511 | s->req->flags |= BF_DONT_READ; |
| 3512 | } |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3513 | |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3514 | /* make ->lr point to the first non-forwarded byte */ |
| 3515 | s->req->lr = s->req->w + s->req->send_max; |
| 3516 | if (s->req->lr >= s->req->data + s->req->size) |
| 3517 | s->req->lr -= s->req->size; |
| 3518 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3519 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3520 | s->rep->lr -= s->req->size; |
| 3521 | |
| 3522 | s->req->analysers |= s->fe->fe_req_ana; |
| 3523 | s->rep->analysers = 0; |
| 3524 | } |
| 3525 | |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3526 | /* FIXME: we're still forced to do that here */ |
| 3527 | s->rep->flags &= ~BF_DONT_READ; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3528 | break; |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | /* OK we're done with the data phase */ |
| 3533 | req->analysers &= ~an_bit; |
| 3534 | return 1; |
| 3535 | |
| 3536 | missing_data: |
| 3537 | /* forward the chunk size as well as any pending data */ |
| 3538 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 3539 | buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len); |
| 3540 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 3541 | msg->som = msg->sov; |
| 3542 | } |
| 3543 | |
| 3544 | if (req->flags & BF_FULL) |
| 3545 | goto return_bad_req; |
| 3546 | /* the session handler will take care of timeouts and errors */ |
| 3547 | return 0; |
| 3548 | |
| 3549 | return_bad_req: /* let's centralize all bad requests */ |
| 3550 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3551 | txn->status = 400; |
| 3552 | /* Note: we don't send any error if some data were already sent */ |
| 3553 | stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL); |
| 3554 | |
| 3555 | req->analysers = 0; |
| 3556 | s->fe->counters.failed_req++; |
| 3557 | if (s->listener->counters) |
| 3558 | s->listener->counters->failed_req++; |
| 3559 | |
| 3560 | if (!(s->flags & SN_ERR_MASK)) |
| 3561 | s->flags |= SN_ERR_PRXCOND; |
| 3562 | if (!(s->flags & SN_FINST_MASK)) |
| 3563 | s->flags |= SN_FINST_R; |
| 3564 | return 0; |
| 3565 | } |
| 3566 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3567 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 3568 | * processing can continue on next analysers, or zero if it either needs more |
| 3569 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 3570 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 3571 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 3572 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3573 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3574 | 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] | 3575 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3576 | struct http_txn *txn = &s->txn; |
| 3577 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3578 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3579 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3580 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3581 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3582 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3583 | 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] | 3584 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3585 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3586 | rep, |
| 3587 | rep->rex, rep->wex, |
| 3588 | rep->flags, |
| 3589 | rep->l, |
| 3590 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 3591 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3592 | /* |
| 3593 | * Now parse the partial (or complete) lines. |
| 3594 | * We will check the response syntax, and also join multi-line |
| 3595 | * headers. An index of all the lines will be elaborated while |
| 3596 | * parsing. |
| 3597 | * |
| 3598 | * For the parsing, we use a 28 states FSM. |
| 3599 | * |
| 3600 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3601 | * rep->data + msg->som = beginning of response |
| 3602 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 3603 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3604 | * rep->lr = first non-visited byte |
| 3605 | * rep->r = end of data |
| 3606 | */ |
| 3607 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3608 | /* There's a protected area at the end of the buffer for rewriting |
| 3609 | * purposes. We don't want to start to parse the request if the |
| 3610 | * protected area is affected, because we may have to move processed |
| 3611 | * data later, which is much more complicated. |
| 3612 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3613 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 3614 | if (unlikely((rep->flags & BF_FULL) || |
| 3615 | rep->r < rep->lr || |
| 3616 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 3617 | if (rep->send_max) { |
| 3618 | /* some data has still not left the buffer, wake us once that's done */ |
| 3619 | buffer_dont_close(rep); |
| 3620 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 3621 | return 0; |
| 3622 | } |
| 3623 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 3624 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3625 | } |
| 3626 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3627 | if (likely(rep->lr < rep->r)) |
| 3628 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3629 | } |
| 3630 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3631 | /* 1: we might have to print this header in debug mode */ |
| 3632 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3633 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3634 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3635 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3636 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3637 | sol = rep->data + msg->som; |
| 3638 | eol = sol + msg->sl.rq.l; |
| 3639 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3640 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3641 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 3642 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3643 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3644 | while (cur_idx) { |
| 3645 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 3646 | debug_hdr("srvhdr", s, sol, eol); |
| 3647 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 3648 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 3649 | } |
| 3650 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3651 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3652 | /* |
| 3653 | * Now we quickly check if we have found a full valid response. |
| 3654 | * If not so, we check the FD and buffer states before leaving. |
| 3655 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3656 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3657 | * responses are checked first. |
| 3658 | * |
| 3659 | * Depending on whether the client is still there or not, we |
| 3660 | * may send an error response back or not. Note that normally |
| 3661 | * we should only check for HTTP status there, and check I/O |
| 3662 | * errors somewhere else. |
| 3663 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3664 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3665 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3666 | /* Invalid response */ |
| 3667 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 3668 | /* we detected a parsing error. We want to archive this response |
| 3669 | * in the dedicated proxy area for later troubleshooting. |
| 3670 | */ |
| 3671 | hdr_response_bad: |
| 3672 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 3673 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 3674 | |
| 3675 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3676 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3677 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3678 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 3679 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3680 | |
| 3681 | rep->analysers = 0; |
| 3682 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 3683 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3684 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 3685 | |
| 3686 | if (!(s->flags & SN_ERR_MASK)) |
| 3687 | s->flags |= SN_ERR_PRXCOND; |
| 3688 | if (!(s->flags & SN_FINST_MASK)) |
| 3689 | s->flags |= SN_FINST_H; |
| 3690 | |
| 3691 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3692 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3693 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3694 | /* too large response does not fit in buffer. */ |
| 3695 | else if (rep->flags & BF_FULL) { |
| 3696 | goto hdr_response_bad; |
| 3697 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3698 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3699 | /* read error */ |
| 3700 | else if (rep->flags & BF_READ_ERROR) { |
| 3701 | if (msg->err_pos >= 0) |
| 3702 | 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] | 3703 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3704 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3705 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3706 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3707 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 3708 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3709 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3710 | rep->analysers = 0; |
| 3711 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 3712 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3713 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3714 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3715 | if (!(s->flags & SN_ERR_MASK)) |
| 3716 | s->flags |= SN_ERR_SRVCL; |
| 3717 | if (!(s->flags & SN_FINST_MASK)) |
| 3718 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3719 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3720 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3721 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3722 | /* read timeout : return a 504 to the client. */ |
| 3723 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 3724 | if (msg->err_pos >= 0) |
| 3725 | 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] | 3726 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3727 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3728 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3729 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3730 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 3731 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3732 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3733 | rep->analysers = 0; |
| 3734 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 3735 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3736 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3737 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3738 | if (!(s->flags & SN_ERR_MASK)) |
| 3739 | s->flags |= SN_ERR_SRVTO; |
| 3740 | if (!(s->flags & SN_FINST_MASK)) |
| 3741 | s->flags |= SN_FINST_H; |
| 3742 | return 0; |
| 3743 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3744 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3745 | /* close from server */ |
| 3746 | else if (rep->flags & BF_SHUTR) { |
| 3747 | if (msg->err_pos >= 0) |
| 3748 | 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] | 3749 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3750 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3751 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3752 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3753 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 3754 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3755 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3756 | rep->analysers = 0; |
| 3757 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 3758 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3759 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3760 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3761 | if (!(s->flags & SN_ERR_MASK)) |
| 3762 | s->flags |= SN_ERR_SRVCL; |
| 3763 | if (!(s->flags & SN_FINST_MASK)) |
| 3764 | s->flags |= SN_FINST_H; |
| 3765 | return 0; |
| 3766 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3767 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3768 | /* write error to client (we don't send any message then) */ |
| 3769 | else if (rep->flags & BF_WRITE_ERROR) { |
| 3770 | if (msg->err_pos >= 0) |
| 3771 | 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] | 3772 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3773 | s->be->counters.failed_resp++; |
| 3774 | rep->analysers = 0; |
| 3775 | |
| 3776 | if (!(s->flags & SN_ERR_MASK)) |
| 3777 | s->flags |= SN_ERR_CLICL; |
| 3778 | if (!(s->flags & SN_FINST_MASK)) |
| 3779 | s->flags |= SN_FINST_H; |
| 3780 | |
| 3781 | /* process_session() will take care of the error */ |
| 3782 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3783 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3784 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3785 | buffer_dont_close(rep); |
| 3786 | return 0; |
| 3787 | } |
| 3788 | |
| 3789 | /* More interesting part now : we know that we have a complete |
| 3790 | * response which at least looks like HTTP. We have an indicator |
| 3791 | * of each header's length, so we can parse them quickly. |
| 3792 | */ |
| 3793 | |
| 3794 | if (unlikely(msg->err_pos >= 0)) |
| 3795 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 3796 | |
| 3797 | /* ensure we keep this pointer to the beginning of the message */ |
| 3798 | msg->sol = rep->data + msg->som; |
| 3799 | |
| 3800 | /* |
| 3801 | * 1: get the status code |
| 3802 | */ |
| 3803 | n = rep->data[msg->sl.st.c] - '0'; |
| 3804 | if (n < 1 || n > 5) |
| 3805 | n = 0; |
| 3806 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3807 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3808 | /* check if the response is HTTP/1.1 or above */ |
| 3809 | if ((msg->sl.st.v_l == 8) && |
| 3810 | ((rep->data[msg->som + 5] > '1') || |
| 3811 | ((rep->data[msg->som + 5] == '1') && |
| 3812 | (rep->data[msg->som + 7] >= '1')))) |
| 3813 | txn->flags |= TX_RES_VER_11; |
| 3814 | |
| 3815 | /* "connection" has not been parsed yet */ |
| 3816 | txn->flags &= ~TX_CON_HDR_PARS; |
| 3817 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3818 | /* transfer length unknown*/ |
| 3819 | txn->flags &= ~TX_RES_XFER_LEN; |
| 3820 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3821 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
| 3822 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 3823 | if (txn->status >= 100 && txn->status < 500) |
| 3824 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 3825 | else |
| 3826 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 3827 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3828 | /* |
| 3829 | * 2: check for cacheability. |
| 3830 | */ |
| 3831 | |
| 3832 | switch (txn->status) { |
| 3833 | case 200: |
| 3834 | case 203: |
| 3835 | case 206: |
| 3836 | case 300: |
| 3837 | case 301: |
| 3838 | case 410: |
| 3839 | /* RFC2616 @13.4: |
| 3840 | * "A response received with a status code of |
| 3841 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3842 | * by a cache (...) unless a cache-control |
| 3843 | * directive prohibits caching." |
| 3844 | * |
| 3845 | * RFC2616 @9.5: POST method : |
| 3846 | * "Responses to this method are not cacheable, |
| 3847 | * unless the response includes appropriate |
| 3848 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3849 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3850 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3851 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3852 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3853 | break; |
| 3854 | default: |
| 3855 | break; |
| 3856 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3857 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3858 | /* |
| 3859 | * 3: we may need to capture headers |
| 3860 | */ |
| 3861 | s->logs.logwait &= ~LW_RESP; |
| 3862 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
| 3863 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3864 | txn->rsp.cap, s->fe->rsp_cap); |
| 3865 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3866 | /* 4: determine the transfer-length. |
| 3867 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 3868 | * the presence of a message-body in a RESPONSE and its transfer length |
| 3869 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3870 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3871 | * All responses to the HEAD request method MUST NOT include a |
| 3872 | * message-body, even though the presence of entity-header fields |
| 3873 | * might lead one to believe they do. All 1xx (informational), 204 |
| 3874 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 3875 | * message-body. All other responses do include a message-body, |
| 3876 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3877 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3878 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 3879 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 3880 | * always terminated by the first empty line after the header fields, |
| 3881 | * regardless of the entity-header fields present in the message. |
| 3882 | * |
| 3883 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 3884 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 3885 | * transfer-length is defined by the use of this transfer-coding. |
| 3886 | * If a Transfer-Encoding header field is present and the "chunked" |
| 3887 | * transfer-coding is not present, the transfer-length is defined by |
| 3888 | * the sender closing the connection. |
| 3889 | * |
| 3890 | * 3. If a Content-Length header field is present, its decimal value in |
| 3891 | * OCTETs represents both the entity-length and the transfer-length. |
| 3892 | * If a message is received with both a Transfer-Encoding header |
| 3893 | * field and a Content-Length header field, the latter MUST be ignored. |
| 3894 | * |
| 3895 | * 4. If the message uses the media type "multipart/byteranges", and |
| 3896 | * the transfer-length is not otherwise specified, then this self- |
| 3897 | * delimiting media type defines the transfer-length. This media |
| 3898 | * type MUST NOT be used unless the sender knows that the recipient |
| 3899 | * can parse it; the presence in a request of a Range header with |
| 3900 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 3901 | * client can parse multipart/byteranges responses. |
| 3902 | * |
| 3903 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3904 | */ |
| 3905 | |
| 3906 | /* Skip parsing if no content length is possible. The response flags |
| 3907 | * remain 0 as well as the hdr_content_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3908 | * the real header value, and we note that we know the response's length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3909 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 3910 | */ |
| 3911 | if (txn->meth == HTTP_METH_HEAD || |
| 3912 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3913 | txn->status == 204 || txn->status == 304) { |
| 3914 | txn->flags |= TX_RES_XFER_LEN; |
| 3915 | goto skip_content_length; |
| 3916 | } |
| 3917 | |
| 3918 | if (txn->meth == HTTP_METH_CONNECT) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3919 | goto skip_content_length; |
| 3920 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3921 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3922 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3923 | while ((txn->flags & TX_RES_VER_11) && |
| 3924 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3925 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 3926 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 3927 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 3928 | /* bad transfer-encoding (chunked followed by something else) */ |
| 3929 | use_close_only = 1; |
| 3930 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 3931 | break; |
| 3932 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 3936 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3937 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3938 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 3939 | signed long long cl; |
| 3940 | |
| 3941 | if (!ctx.vlen) |
| 3942 | goto hdr_response_bad; |
| 3943 | |
| 3944 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 3945 | goto hdr_response_bad; /* parse failure */ |
| 3946 | |
| 3947 | if (cl < 0) |
| 3948 | goto hdr_response_bad; |
| 3949 | |
| 3950 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 3951 | goto hdr_response_bad; /* already specified, was different */ |
| 3952 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3953 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3954 | msg->hdr_content_len = cl; |
| 3955 | } |
| 3956 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3957 | /* FIXME: we should also implement the multipart/byterange method. |
| 3958 | * For now on, we resort to close mode in this case (unknown length). |
| 3959 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3960 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3961 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3962 | /* end of job, return OK */ |
| 3963 | rep->analysers &= ~an_bit; |
| 3964 | rep->analyse_exp = TICK_ETERNITY; |
| 3965 | return 1; |
| 3966 | } |
| 3967 | |
| 3968 | /* This function performs all the processing enabled for the current response. |
| 3969 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 3970 | * called again after other functions. It relies on buffers flags, and updates |
| 3971 | * t->rep->analysers. It might make sense to explode it into several other |
| 3972 | * functions. It works like process_request (see indications above). |
| 3973 | */ |
| 3974 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 3975 | { |
| 3976 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3977 | struct http_msg *msg = &txn->rsp; |
| 3978 | struct proxy *cur_proxy; |
| 3979 | int cur_idx; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3980 | int conn_ka = 0, conn_cl = 0; |
| 3981 | int must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3982 | int must_del_close = 0, must_keep = 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3983 | |
| 3984 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3985 | now_ms, __FUNCTION__, |
| 3986 | t, |
| 3987 | rep, |
| 3988 | rep->rex, rep->wex, |
| 3989 | rep->flags, |
| 3990 | rep->l, |
| 3991 | rep->analysers); |
| 3992 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3993 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3994 | return 0; |
| 3995 | |
| 3996 | rep->analysers &= ~an_bit; |
| 3997 | rep->analyse_exp = TICK_ETERNITY; |
| 3998 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3999 | /* Now we have to check if we need to modify the Connection header. |
| 4000 | * This is more difficult on the response than it is on the request, |
| 4001 | * because we can have two different HTTP versions and we don't know |
| 4002 | * how the client will interprete a response. For instance, let's say |
| 4003 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4004 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4005 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4006 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4007 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4008 | * indicating how a request MAY be understood by the client. In case |
| 4009 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4010 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4011 | * will fall back to explicit close. Note that we won't take risks with |
| 4012 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
| 4013 | */ |
| 4014 | |
| 4015 | if ((txn->meth != HTTP_METH_CONNECT) && |
| 4016 | (txn->status >= 200) && |
| 4017 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN && |
| 4018 | !(txn->flags & TX_CON_HDR_PARS)) { |
| 4019 | int may_keep = 0, may_close = 0; /* how it may be understood */ |
| 4020 | struct hdr_ctx ctx; |
| 4021 | |
| 4022 | ctx.idx = 0; |
| 4023 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4024 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 4025 | conn_cl = 1; |
| 4026 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 4027 | conn_ka = 1; |
| 4028 | } |
| 4029 | |
| 4030 | if (conn_cl) { |
| 4031 | /* close header present */ |
| 4032 | may_close = 1; |
| 4033 | if (conn_ka) /* we have both close and keep-alive */ |
| 4034 | may_keep = 1; |
| 4035 | } |
| 4036 | else if (conn_ka) { |
| 4037 | /* keep-alive alone */ |
| 4038 | may_keep = 1; |
| 4039 | } |
| 4040 | else { |
| 4041 | /* no close nor keep-alive header */ |
| 4042 | if (txn->flags & TX_RES_VER_11) |
| 4043 | may_keep = 1; |
| 4044 | else |
| 4045 | may_close = 1; |
| 4046 | |
| 4047 | if (txn->flags & TX_REQ_VER_11) |
| 4048 | may_keep = 1; |
| 4049 | else |
| 4050 | may_close = 1; |
| 4051 | } |
| 4052 | |
| 4053 | /* let's update the transaction status to reflect any close. |
| 4054 | * Note that ambiguous cases with keep & close will also be |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4055 | * handled. We also explicitly state that we will close in |
| 4056 | * case of an ambiguous response having no content-length. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4057 | */ |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4058 | if ((may_close && |
| 4059 | (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) || |
| 4060 | !(txn->flags & TX_RES_XFER_LEN)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4061 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 4062 | |
| 4063 | /* Now we must adjust the response header : |
| 4064 | * - set "close" if may_keep and WANT_CLO |
| 4065 | * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK) |
| 4066 | * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length |
| 4067 | * |
| 4068 | * Until we support the server-close mode, we'll only support the set "close". |
| 4069 | */ |
| 4070 | if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) |
| 4071 | must_close = 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4072 | else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 4073 | may_close && (txn->flags & TX_RES_XFER_LEN)) { |
| 4074 | must_del_close = 1; |
| 4075 | if (!(txn->flags & TX_REQ_VER_11)) |
| 4076 | must_keep = 1; |
| 4077 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4078 | |
| 4079 | txn->flags |= TX_CON_HDR_PARS; |
| 4080 | } |
| 4081 | |
| 4082 | /* We might have to check for "Connection:" if the server |
| 4083 | * returns a connection status that is not compatible with |
| 4084 | * the client's or with the config. |
| 4085 | */ |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4086 | if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4087 | char *cur_ptr, *cur_end, *cur_next; |
| 4088 | int cur_idx, old_idx, delta, val; |
| 4089 | int must_delete; |
| 4090 | struct hdr_idx_elem *cur_hdr; |
| 4091 | |
| 4092 | /* we just have to remove the headers if both sides are 1.0 */ |
| 4093 | must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4094 | |
| 4095 | /* same if we want to re-enable keep-alive on 1.1 */ |
| 4096 | must_delete |= must_del_close; |
| 4097 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4098 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4099 | |
| 4100 | for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) { |
| 4101 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4102 | cur_ptr = cur_next; |
| 4103 | cur_end = cur_ptr + cur_hdr->len; |
| 4104 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4105 | |
| 4106 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 4107 | if (!val) |
| 4108 | continue; |
| 4109 | |
| 4110 | /* 3 possibilities : |
| 4111 | * - we have already set "Connection: close" or we're in |
| 4112 | * HTTP/1.0, so we remove this line. |
| 4113 | * - we have not yet set "Connection: close", but this line |
| 4114 | * indicates close. We leave it untouched and set the flag. |
| 4115 | * - we have not yet set "Connection: close", and this line |
| 4116 | * indicates non-close. We replace it and set the flag. |
| 4117 | */ |
| 4118 | if (must_delete) { |
| 4119 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 4120 | http_msg_move_end(&txn->rsp, delta); |
| 4121 | cur_next += delta; |
| 4122 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4123 | txn->hdr_idx.used--; |
| 4124 | cur_hdr->len = 0; |
| 4125 | must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4126 | must_del_close = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4127 | } else { |
| 4128 | if (cur_end - cur_ptr - val != 5 || |
| 4129 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 4130 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 4131 | "close", 5); |
| 4132 | cur_next += delta; |
| 4133 | cur_hdr->len += delta; |
| 4134 | http_msg_move_end(&txn->rsp, delta); |
| 4135 | } |
| 4136 | must_delete = 1; |
| 4137 | must_close = 0; |
| 4138 | } |
| 4139 | } /* for loop */ |
| 4140 | } /* if must close keep-alive */ |
| 4141 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4142 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4143 | /* |
| 4144 | * 3: we will have to evaluate the filters. |
| 4145 | * As opposed to version 1.2, now they will be evaluated in the |
| 4146 | * filters order and not in the header order. This means that |
| 4147 | * each filter has to be validated among all headers. |
| 4148 | * |
| 4149 | * Filters are tried with ->be first, then with ->fe if it is |
| 4150 | * different from ->be. |
| 4151 | */ |
| 4152 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4153 | cur_proxy = t->be; |
| 4154 | while (1) { |
| 4155 | struct proxy *rule_set = cur_proxy; |
| 4156 | |
| 4157 | /* try headers filters */ |
| 4158 | if (rule_set->rsp_exp != NULL) { |
| 4159 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 4160 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4161 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4162 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4163 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4164 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4165 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4166 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4167 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4168 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4169 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4170 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4171 | if (!(t->flags & SN_ERR_MASK)) |
| 4172 | t->flags |= SN_ERR_PRXCOND; |
| 4173 | if (!(t->flags & SN_FINST_MASK)) |
| 4174 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4175 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4176 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4177 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4178 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4179 | /* has the response been denied ? */ |
| 4180 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4181 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4182 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4183 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4184 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4185 | if (t->listener->counters) |
| 4186 | t->listener->counters->denied_resp++; |
| 4187 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4188 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4189 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4190 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4191 | /* add response headers from the rule sets in the same order */ |
| 4192 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4193 | if (txn->status < 200) |
| 4194 | break; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4195 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4196 | rule_set->rsp_add[cur_idx]) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4197 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4198 | } |
| 4199 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4200 | /* check whether we're already working on the frontend */ |
| 4201 | if (cur_proxy == t->fe) |
| 4202 | break; |
| 4203 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4204 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4205 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4206 | /* |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4207 | * We may be facing a 1xx response (100 continue, 101 switching protocols), |
| 4208 | * in which case this is not the right response, and we're waiting for the |
| 4209 | * next one. Let's allow this response to go to the client and wait for the |
| 4210 | * next one. |
| 4211 | */ |
| 4212 | if (txn->status < 200) { |
| 4213 | hdr_idx_init(&txn->hdr_idx); |
| 4214 | buffer_forward(rep, rep->lr - (rep->data + msg->som)); |
| 4215 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4216 | txn->status = 0; |
| 4217 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4218 | return 1; |
| 4219 | } |
| 4220 | |
| 4221 | /* we don't have any 1xx status code now */ |
| 4222 | |
| 4223 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4224 | * 4: check for server cookie. |
| 4225 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4226 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4227 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4228 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4229 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4230 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4231 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4232 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4233 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4234 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4235 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4236 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4237 | /* |
| 4238 | * 6: add server cookie in the response if needed |
| 4239 | */ |
| 4240 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4241 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4242 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4243 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4244 | /* the server is known, it's not the one the client requested, we have to |
| 4245 | * insert a set-cookie here, except if we want to insert only on POST |
| 4246 | * requests and this one isn't. Note that servers which don't have cookies |
| 4247 | * (eg: some backup servers) will return a full cookie removal request. |
| 4248 | */ |
| 4249 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4250 | t->be->cookie_name, |
| 4251 | 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] | 4252 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4253 | if (t->be->cookie_domain) |
| 4254 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4255 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4256 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4257 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4258 | goto return_bad_resp; |
| 4259 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4260 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4261 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4262 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4263 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4264 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4265 | */ |
| 4266 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4267 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4268 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4269 | |
| 4270 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4271 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4272 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4273 | } |
| 4274 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4275 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4276 | /* |
| 4277 | * 7: check if result will be cacheable with a cookie. |
| 4278 | * We'll block the response if security checks have caught |
| 4279 | * nasty things such as a cacheable cookie. |
| 4280 | */ |
| 4281 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4282 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4283 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4284 | |
| 4285 | /* we're in presence of a cacheable response containing |
| 4286 | * a set-cookie header. We'll block it as requested by |
| 4287 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4288 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4289 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4290 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4291 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4292 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4293 | if (t->listener->counters) |
| 4294 | t->listener->counters->denied_resp++; |
| 4295 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4296 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4297 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4298 | send_log(t->be, LOG_ALERT, |
| 4299 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4300 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4301 | goto return_srv_prx_502; |
| 4302 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4303 | |
| 4304 | /* |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4305 | * 8: add "Connection: close" if needed and not yet set. This is |
| 4306 | * only needed for 1.1 responses since we know there is no other |
| 4307 | * Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4308 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4309 | if (must_close && (txn->flags & TX_RES_VER_11)) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4310 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4311 | "Connection: close", 17) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4312 | goto return_bad_resp; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4313 | must_close = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4314 | } |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4315 | else if (must_keep && !(txn->flags & TX_REQ_VER_11)) { |
| 4316 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 4317 | "Connection: keep-alive", 22) < 0)) |
| 4318 | goto return_bad_resp; |
| 4319 | must_keep = 0; |
| 4320 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4321 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4322 | if (txn->flags & TX_RES_XFER_LEN) |
| 4323 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4324 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4325 | /************************************************************* |
| 4326 | * OK, that's finished for the headers. We have done what we * |
| 4327 | * could. Let's switch to the DATA state. * |
| 4328 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4329 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4330 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4331 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4332 | /* if the user wants to log as soon as possible, without counting |
| 4333 | * bytes from the server, then this is the right moment. We have |
| 4334 | * to temporarily assign bytes_out to log what we currently have. |
| 4335 | */ |
| 4336 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4337 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4338 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4339 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4340 | t->logs.bytes_out = 0; |
| 4341 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4342 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4343 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4344 | * otherwise we would not let the client side wake up. |
| 4345 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4346 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4347 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4348 | } |
| 4349 | return 0; |
| 4350 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4351 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4352 | /* This function is an analyser which forwards response body (including chunk |
| 4353 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4354 | * zero byte. The only situation where it must not be called is when we're in |
| 4355 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4356 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4357 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4358 | * read more data, or 1 once we can go on with next request or end the session. |
| 4359 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4360 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4361 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4362 | */ |
| 4363 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4364 | { |
| 4365 | struct http_txn *txn = &s->txn; |
| 4366 | struct http_msg *msg = &s->txn.rsp; |
| 4367 | |
Willy Tarreau | 21c5e4d | 2010-01-03 00:19:31 +0100 | [diff] [blame] | 4368 | if (res->flags & (BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4369 | res->analysers &= ~an_bit; |
| 4370 | return 1; |
| 4371 | } |
| 4372 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4373 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4374 | return 0; |
| 4375 | |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4376 | /* note: in server-close mode, we don't want to automatically close the |
| 4377 | * output when the input is closed. |
| 4378 | */ |
| 4379 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4380 | buffer_dont_close(res); |
| 4381 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4382 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4383 | /* we have msg->col and msg->sov which both point to the first |
| 4384 | * byte of message body. msg->som still points to the beginning |
| 4385 | * of the message. We must save the body in req->lr because it |
| 4386 | * survives buffer re-alignments. |
| 4387 | */ |
| 4388 | res->lr = res->data + msg->sov; |
| 4389 | if (txn->flags & TX_RES_TE_CHNK) |
| 4390 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4391 | else { |
| 4392 | msg->msg_state = HTTP_MSG_DATA; |
| 4393 | } |
| 4394 | } |
| 4395 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4396 | while (1) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4397 | /* we may have some data pending */ |
| 4398 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4399 | int bytes = msg->sov - msg->som; |
| 4400 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4401 | bytes += res->size; |
| 4402 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 4403 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4404 | msg->som = msg->sov; |
| 4405 | } |
| 4406 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4407 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
| 4408 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4409 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 4410 | */ |
| 4411 | int ret = http_parse_chunk_size(res, msg); |
| 4412 | |
| 4413 | if (!ret) |
| 4414 | goto missing_data; |
| 4415 | else if (ret < 0) |
| 4416 | goto return_bad_res; |
| 4417 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4418 | } |
| 4419 | else if (msg->msg_state == HTTP_MSG_DATA) { |
| 4420 | /* must still forward */ |
| 4421 | if (res->to_forward) |
| 4422 | return 0; |
| 4423 | |
| 4424 | /* nothing left to forward */ |
| 4425 | if (txn->flags & TX_RES_TE_CHNK) |
| 4426 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 4427 | else { |
| 4428 | msg->msg_state = HTTP_MSG_DONE; |
| 4429 | } |
| 4430 | } |
| 4431 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4432 | /* we want the CRLF after the data */ |
| 4433 | int ret; |
| 4434 | |
| 4435 | if (!(res->flags & BF_OUT_EMPTY)) |
| 4436 | return 0; |
| 4437 | /* The output pointer does not move anymore, next unsent data |
| 4438 | * are available at ->w. Let's save that. |
| 4439 | */ |
| 4440 | res->lr = res->w; |
| 4441 | ret = http_skip_chunk_crlf(res, msg); |
| 4442 | |
| 4443 | if (!ret) |
| 4444 | goto missing_data; |
| 4445 | else if (ret < 0) |
| 4446 | goto return_bad_res; |
| 4447 | /* we're in MSG_CHUNK_SIZE now */ |
| 4448 | } |
| 4449 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4450 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4451 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4452 | if (ret == 0) |
| 4453 | goto missing_data; |
| 4454 | else if (ret < 0) |
| 4455 | goto return_bad_res; |
| 4456 | /* we're in HTTP_MSG_DONE now */ |
| 4457 | } |
| 4458 | else if (msg->msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4459 | /* In theory, we don't need to read anymore, but we must |
| 4460 | * still monitor the server connection for a possible close, |
| 4461 | * so we don't set the BF_DONT_READ flag here. |
| 4462 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4463 | |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4464 | if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) { |
| 4465 | /* The client seems to still be sending data, probably |
| 4466 | * because we got an error response during an upload. |
| 4467 | * We have the choice of either breaking the connection |
| 4468 | * or letting it pass through. Let's do the later. |
| 4469 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4470 | return 0; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4471 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4472 | |
| 4473 | /* when we support keep-alive or server-close modes, we'll have |
| 4474 | * to reset the transaction here. |
| 4475 | */ |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4476 | |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 4477 | if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) { |
| 4478 | /* option forceclose is set, let's enforce it now that the transfer is complete. */ |
| 4479 | buffer_abort(res); |
| 4480 | } |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4481 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4482 | /* server close is handled entirely on the req analyser */ |
| 4483 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 4484 | buffer_shutw_now(s->req); |
| 4485 | } |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame] | 4486 | |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4487 | if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4488 | if (res->flags & BF_OUT_EMPTY) |
| 4489 | msg->msg_state = HTTP_MSG_CLOSED; |
| 4490 | else |
| 4491 | msg->msg_state = HTTP_MSG_CLOSING; |
| 4492 | } |
| 4493 | else { |
| 4494 | /* for other modes, we let further responses pass for now */ |
| 4495 | res->flags &= ~BF_DONT_READ; |
| 4496 | /* FIXME: we're still forced to do that here */ |
| 4497 | s->req->flags &= ~BF_DONT_READ; |
| 4498 | break; |
| 4499 | } |
| 4500 | } |
| 4501 | else if (msg->msg_state == HTTP_MSG_CLOSING) { |
| 4502 | /* nothing else to forward, just waiting for the buffer to be empty */ |
| 4503 | if (!(res->flags & BF_OUT_EMPTY)) |
| 4504 | return 0; |
| 4505 | msg->msg_state = HTTP_MSG_CLOSED; |
| 4506 | } |
| 4507 | else if (msg->msg_state == HTTP_MSG_CLOSED) { |
| 4508 | res->flags &= ~BF_DONT_READ; |
| 4509 | /* FIXME: we're still forced to do that here */ |
| 4510 | s->req->flags &= ~BF_DONT_READ; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4511 | break; |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | res->analysers &= ~an_bit; |
| 4516 | return 1; |
| 4517 | |
| 4518 | missing_data: |
| 4519 | /* forward the chunk size as well as any pending data */ |
| 4520 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4521 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 4522 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4523 | msg->som = msg->sov; |
| 4524 | } |
| 4525 | |
| 4526 | if (res->flags & BF_FULL) |
| 4527 | goto return_bad_res; |
| 4528 | /* the session handler will take care of timeouts and errors */ |
| 4529 | return 0; |
| 4530 | |
| 4531 | return_bad_res: /* let's centralize all bad resuests */ |
| 4532 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 4533 | txn->status = 502; |
| 4534 | stream_int_cond_close(res->cons, NULL); |
| 4535 | |
| 4536 | res->analysers = 0; |
| 4537 | s->be->counters.failed_resp++; |
| 4538 | if (s->srv) { |
| 4539 | s->srv->counters.failed_resp++; |
| 4540 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4541 | } |
| 4542 | |
| 4543 | if (!(s->flags & SN_ERR_MASK)) |
| 4544 | s->flags |= SN_ERR_PRXCOND; |
| 4545 | if (!(s->flags & SN_FINST_MASK)) |
| 4546 | s->flags |= SN_FINST_R; |
| 4547 | return 0; |
| 4548 | } |
| 4549 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4550 | /* Iterate the same filter through all request headers. |
| 4551 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4552 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4553 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4554 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4555 | 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] | 4556 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4557 | char term; |
| 4558 | char *cur_ptr, *cur_end, *cur_next; |
| 4559 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4560 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4561 | struct hdr_idx_elem *cur_hdr; |
| 4562 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4564 | last_hdr = 0; |
| 4565 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4566 | 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] | 4567 | old_idx = 0; |
| 4568 | |
| 4569 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4570 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4571 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4572 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4573 | (exp->action == ACT_ALLOW || |
| 4574 | exp->action == ACT_DENY || |
| 4575 | exp->action == ACT_TARPIT)) |
| 4576 | return 0; |
| 4577 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4578 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4579 | if (!cur_idx) |
| 4580 | break; |
| 4581 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4582 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4583 | cur_ptr = cur_next; |
| 4584 | cur_end = cur_ptr + cur_hdr->len; |
| 4585 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4586 | |
| 4587 | /* Now we have one header between cur_ptr and cur_end, |
| 4588 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4589 | */ |
| 4590 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4591 | /* The annoying part is that pattern matching needs |
| 4592 | * that we modify the contents to null-terminate all |
| 4593 | * strings before testing them. |
| 4594 | */ |
| 4595 | |
| 4596 | term = *cur_end; |
| 4597 | *cur_end = '\0'; |
| 4598 | |
| 4599 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4600 | switch (exp->action) { |
| 4601 | case ACT_SETBE: |
| 4602 | /* It is not possible to jump a second time. |
| 4603 | * FIXME: should we return an HTTP/500 here so that |
| 4604 | * the admin knows there's a problem ? |
| 4605 | */ |
| 4606 | if (t->be != t->fe) |
| 4607 | break; |
| 4608 | |
| 4609 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4610 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4611 | last_hdr = 1; |
| 4612 | break; |
| 4613 | |
| 4614 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4615 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4616 | last_hdr = 1; |
| 4617 | break; |
| 4618 | |
| 4619 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4620 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4621 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4622 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4623 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4624 | if (t->listener->counters) |
| 4625 | t->listener->counters->denied_resp++; |
| 4626 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4627 | break; |
| 4628 | |
| 4629 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4630 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4631 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4632 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4633 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4634 | if (t->listener->counters) |
| 4635 | t->listener->counters->denied_resp++; |
| 4636 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4637 | break; |
| 4638 | |
| 4639 | case ACT_REPLACE: |
| 4640 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4641 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4642 | /* FIXME: if the user adds a newline in the replacement, the |
| 4643 | * index will not be recalculated for now, and the new line |
| 4644 | * will not be counted as a new header. |
| 4645 | */ |
| 4646 | |
| 4647 | cur_end += delta; |
| 4648 | cur_next += delta; |
| 4649 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4650 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4651 | break; |
| 4652 | |
| 4653 | case ACT_REMOVE: |
| 4654 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4655 | cur_next += delta; |
| 4656 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4657 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4658 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4659 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4660 | cur_hdr->len = 0; |
| 4661 | cur_end = NULL; /* null-term has been rewritten */ |
| 4662 | break; |
| 4663 | |
| 4664 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4665 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4666 | if (cur_end) |
| 4667 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4668 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4669 | /* keep the link from this header to next one in case of later |
| 4670 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4671 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4672 | old_idx = cur_idx; |
| 4673 | } |
| 4674 | return 0; |
| 4675 | } |
| 4676 | |
| 4677 | |
| 4678 | /* Apply the filter to the request line. |
| 4679 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4680 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4681 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4682 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4683 | */ |
| 4684 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4685 | { |
| 4686 | char term; |
| 4687 | char *cur_ptr, *cur_end; |
| 4688 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4689 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4690 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4691 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4692 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4693 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4694 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4695 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4696 | (exp->action == ACT_ALLOW || |
| 4697 | exp->action == ACT_DENY || |
| 4698 | exp->action == ACT_TARPIT)) |
| 4699 | return 0; |
| 4700 | else if (exp->action == ACT_REMOVE) |
| 4701 | return 0; |
| 4702 | |
| 4703 | done = 0; |
| 4704 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4705 | 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] | 4706 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4707 | |
| 4708 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4709 | |
| 4710 | /* The annoying part is that pattern matching needs |
| 4711 | * that we modify the contents to null-terminate all |
| 4712 | * strings before testing them. |
| 4713 | */ |
| 4714 | |
| 4715 | term = *cur_end; |
| 4716 | *cur_end = '\0'; |
| 4717 | |
| 4718 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4719 | switch (exp->action) { |
| 4720 | case ACT_SETBE: |
| 4721 | /* It is not possible to jump a second time. |
| 4722 | * FIXME: should we return an HTTP/500 here so that |
| 4723 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4724 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4725 | if (t->be != t->fe) |
| 4726 | break; |
| 4727 | |
| 4728 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4729 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4730 | done = 1; |
| 4731 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4733 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4734 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4735 | done = 1; |
| 4736 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4737 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4738 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4739 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4740 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4741 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4742 | if (t->listener->counters) |
| 4743 | t->listener->counters->denied_resp++; |
| 4744 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4745 | done = 1; |
| 4746 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4747 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4748 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4749 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4750 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4751 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4752 | if (t->listener->counters) |
| 4753 | t->listener->counters->denied_resp++; |
| 4754 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4755 | done = 1; |
| 4756 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4757 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4758 | case ACT_REPLACE: |
| 4759 | *cur_end = term; /* restore the string terminator */ |
| 4760 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4761 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4762 | /* FIXME: if the user adds a newline in the replacement, the |
| 4763 | * index will not be recalculated for now, and the new line |
| 4764 | * will not be counted as a new header. |
| 4765 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4766 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4767 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4768 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4769 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4770 | 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] | 4771 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4772 | HTTP_MSG_RQMETH, |
| 4773 | cur_ptr, cur_end + 1, |
| 4774 | NULL, NULL); |
| 4775 | if (unlikely(!cur_end)) |
| 4776 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4777 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4778 | /* we have a full request and we know that we have either a CR |
| 4779 | * or an LF at <ptr>. |
| 4780 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4781 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4782 | 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] | 4783 | /* there is no point trying this regex on headers */ |
| 4784 | return 1; |
| 4785 | } |
| 4786 | } |
| 4787 | *cur_end = term; /* restore the string terminator */ |
| 4788 | return done; |
| 4789 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4790 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4791 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4792 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4793 | /* |
| 4794 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4795 | * 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] | 4796 | * unparsable request. Since it can manage the switch to another backend, it |
| 4797 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4798 | */ |
| 4799 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4800 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4801 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4802 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4803 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4804 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4805 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4806 | /* |
| 4807 | * The interleaving of transformations and verdicts |
| 4808 | * makes it difficult to decide to continue or stop |
| 4809 | * the evaluation. |
| 4810 | */ |
| 4811 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4812 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4813 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4814 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4815 | exp = exp->next; |
| 4816 | continue; |
| 4817 | } |
| 4818 | |
| 4819 | /* Apply the filter to the request line. */ |
| 4820 | ret = apply_filter_to_req_line(t, req, exp); |
| 4821 | if (unlikely(ret < 0)) |
| 4822 | return -1; |
| 4823 | |
| 4824 | if (likely(ret == 0)) { |
| 4825 | /* The filter did not match the request, it can be |
| 4826 | * iterated through all headers. |
| 4827 | */ |
| 4828 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4829 | } |
| 4830 | exp = exp->next; |
| 4831 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4832 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4836 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4837 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4838 | * Try to retrieve the server associated to the appsession. |
| 4839 | * If the server is found, it's assigned to the session. |
| 4840 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4841 | 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] | 4842 | struct http_txn *txn = &t->txn; |
| 4843 | appsess *asession = NULL; |
| 4844 | char *sessid_temp = NULL; |
| 4845 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4846 | if (len > t->be->appsession_len) { |
| 4847 | len = t->be->appsession_len; |
| 4848 | } |
| 4849 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4850 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 4851 | /* request-learn option is enabled : store the sessid in the session for future use */ |
| 4852 | if (t->sessid != NULL) { |
| 4853 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
| 4854 | pool_free2(apools.sessid, t->sessid); |
| 4855 | } |
| 4856 | |
| 4857 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 4858 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4859 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4860 | return; |
| 4861 | } |
| 4862 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4863 | memcpy(t->sessid, buf, len); |
| 4864 | t->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 4868 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4869 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4870 | return; |
| 4871 | } |
| 4872 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 4873 | memcpy(sessid_temp, buf, len); |
| 4874 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4875 | |
| 4876 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 4877 | /* free previously allocated memory */ |
| 4878 | pool_free2(apools.sessid, sessid_temp); |
| 4879 | |
| 4880 | if (asession != NULL) { |
| 4881 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 4882 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 4883 | asession->request_count++; |
| 4884 | |
| 4885 | if (asession->serverid != NULL) { |
| 4886 | struct server *srv = t->be->srv; |
| 4887 | while (srv) { |
| 4888 | if (strcmp(srv->id, asession->serverid) == 0) { |
| 4889 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
| 4890 | /* we found the server and it's usable */ |
| 4891 | txn->flags &= ~TX_CK_MASK; |
| 4892 | txn->flags |= TX_CK_VALID; |
| 4893 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 4894 | t->srv = srv; |
| 4895 | break; |
| 4896 | } else { |
| 4897 | txn->flags &= ~TX_CK_MASK; |
| 4898 | txn->flags |= TX_CK_DOWN; |
| 4899 | } |
| 4900 | } |
| 4901 | srv = srv->next; |
| 4902 | } |
| 4903 | } |
| 4904 | } |
| 4905 | } |
| 4906 | |
| 4907 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4908 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4909 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4910 | */ |
| 4911 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4912 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4913 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4914 | char *p1, *p2, *p3, *p4; |
| 4915 | char *del_colon, *del_cookie, *colon; |
| 4916 | int app_cookies; |
| 4917 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4918 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4919 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4920 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4921 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4922 | * we start with the start line. |
| 4923 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4924 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4925 | 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] | 4926 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4927 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4928 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4929 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4930 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4931 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4932 | cur_ptr = cur_next; |
| 4933 | cur_end = cur_ptr + cur_hdr->len; |
| 4934 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4935 | |
| 4936 | /* We have one full header between cur_ptr and cur_end, and the |
| 4937 | * next header starts at cur_next. We're only interested in |
| 4938 | * "Cookie:" headers. |
| 4939 | */ |
| 4940 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4941 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4942 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4943 | old_idx = cur_idx; |
| 4944 | continue; |
| 4945 | } |
| 4946 | |
| 4947 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4948 | * attributes whose name begin with a '$', and associate them with |
| 4949 | * the right cookie, if we want to delete this cookie. |
| 4950 | * So there are 3 cases for each cookie read : |
| 4951 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4952 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4953 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4954 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4955 | * "special" cookie. |
| 4956 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4957 | * remove it. If no application cookie persists in the header, we |
| 4958 | * *MUST* delete it |
| 4959 | */ |
| 4960 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4961 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4962 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4963 | /* del_cookie == NULL => nothing to be deleted */ |
| 4964 | del_colon = del_cookie = NULL; |
| 4965 | app_cookies = 0; |
| 4966 | |
| 4967 | while (p1 < cur_end) { |
| 4968 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4969 | while (p1 < cur_end) { |
| 4970 | if (*p1 == ';' || *p1 == ',') |
| 4971 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4972 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4973 | break; |
| 4974 | p1++; |
| 4975 | } |
| 4976 | |
| 4977 | if (p1 == cur_end) |
| 4978 | break; |
| 4979 | |
| 4980 | /* p1 is at the beginning of the cookie name */ |
| 4981 | p2 = p1; |
| 4982 | while (p2 < cur_end && *p2 != '=') |
| 4983 | p2++; |
| 4984 | |
| 4985 | if (p2 == cur_end) |
| 4986 | break; |
| 4987 | |
| 4988 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4989 | if (p3 == cur_end) |
| 4990 | break; |
| 4991 | |
| 4992 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4993 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4994 | p4++; |
| 4995 | |
| 4996 | /* here, we have the cookie name between p1 and p2, |
| 4997 | * and its value between p3 and p4. |
| 4998 | * we can process it : |
| 4999 | * |
| 5000 | * Cookie: NAME=VALUE; |
| 5001 | * | || || | |
| 5002 | * | || || +--> p4 |
| 5003 | * | || |+-------> p3 |
| 5004 | * | || +--------> p2 |
| 5005 | * | |+------------> p1 |
| 5006 | * | +-------------> colon |
| 5007 | * +--------------------> cur_ptr |
| 5008 | */ |
| 5009 | |
| 5010 | if (*p1 == '$') { |
| 5011 | /* skip this one */ |
| 5012 | } |
| 5013 | else { |
| 5014 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5015 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5016 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5017 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5018 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5019 | int log_len = p4 - p1; |
| 5020 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5021 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5022 | Alert("HTTP logging : out of memory.\n"); |
| 5023 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5024 | if (log_len > t->fe->capture_len) |
| 5025 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5026 | memcpy(txn->cli_cookie, p1, log_len); |
| 5027 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5028 | } |
| 5029 | } |
| 5030 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5031 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5032 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5033 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5034 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5035 | char *delim; |
| 5036 | |
| 5037 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5038 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5039 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5040 | * |
| 5041 | * Cookie: NAME=SRV~VALUE; |
| 5042 | * | || || | | |
| 5043 | * | || || | +--> p4 |
| 5044 | * | || || +--------> delim |
| 5045 | * | || |+-----------> p3 |
| 5046 | * | || +------------> p2 |
| 5047 | * | |+----------------> p1 |
| 5048 | * | +-----------------> colon |
| 5049 | * +------------------------> cur_ptr |
| 5050 | */ |
| 5051 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5052 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5053 | for (delim = p3; delim < p4; delim++) |
| 5054 | if (*delim == COOKIE_DELIM) |
| 5055 | break; |
| 5056 | } |
| 5057 | else |
| 5058 | delim = p4; |
| 5059 | |
| 5060 | |
| 5061 | /* Here, we'll look for the first running server which supports the cookie. |
| 5062 | * This allows to share a same cookie between several servers, for example |
| 5063 | * to dedicate backup servers to specific servers only. |
| 5064 | * However, to prevent clients from sticking to cookie-less backup server |
| 5065 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5066 | * empty cookies and mark them as invalid. |
| 5067 | */ |
| 5068 | if (delim == p3) |
| 5069 | srv = NULL; |
| 5070 | |
| 5071 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5072 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5073 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5074 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5075 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5076 | txn->flags &= ~TX_CK_MASK; |
| 5077 | txn->flags |= TX_CK_VALID; |
| 5078 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5079 | t->srv = srv; |
| 5080 | break; |
| 5081 | } else { |
| 5082 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5083 | txn->flags &= ~TX_CK_MASK; |
| 5084 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5085 | } |
| 5086 | } |
| 5087 | srv = srv->next; |
| 5088 | } |
| 5089 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5090 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5091 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5092 | txn->flags &= ~TX_CK_MASK; |
| 5093 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5094 | } |
| 5095 | |
| 5096 | /* depending on the cookie mode, we may have to either : |
| 5097 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5098 | * the server never sees it ; |
| 5099 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5100 | * application cookie so that it does not get accidentely removed later, |
| 5101 | * if we're in cookie prefix mode |
| 5102 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5103 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5104 | int delta; /* negative */ |
| 5105 | |
| 5106 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5107 | p4 += delta; |
| 5108 | cur_end += delta; |
| 5109 | cur_next += delta; |
| 5110 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5111 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5112 | |
| 5113 | del_cookie = del_colon = NULL; |
| 5114 | app_cookies++; /* protect the header from deletion */ |
| 5115 | } |
| 5116 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5117 | (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] | 5118 | del_cookie = p1; |
| 5119 | del_colon = colon; |
| 5120 | } |
| 5121 | } else { |
| 5122 | /* now we know that we must keep this cookie since it's |
| 5123 | * not ours. But if we wanted to delete our cookie |
| 5124 | * earlier, we cannot remove the complete header, but we |
| 5125 | * can remove the previous block itself. |
| 5126 | */ |
| 5127 | app_cookies++; |
| 5128 | |
| 5129 | if (del_cookie != NULL) { |
| 5130 | int delta; /* negative */ |
| 5131 | |
| 5132 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5133 | p4 += delta; |
| 5134 | cur_end += delta; |
| 5135 | cur_next += delta; |
| 5136 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5137 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5138 | del_cookie = del_colon = NULL; |
| 5139 | } |
| 5140 | } |
| 5141 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5142 | if (t->be->appsession_name != NULL) { |
| 5143 | int cmp_len, value_len; |
| 5144 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5145 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5146 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5147 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5148 | value_begin = p1 + t->be->appsession_name_len; |
| 5149 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5150 | } else { |
| 5151 | cmp_len = p2 - p1; |
| 5152 | value_begin = p3; |
| 5153 | value_len = p4 - p3; |
| 5154 | } |
| 5155 | |
| 5156 | /* let's see if the cookie is our appcookie */ |
| 5157 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5158 | /* Cool... it's the right one */ |
| 5159 | manage_client_side_appsession(t, value_begin, value_len); |
| 5160 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5161 | #if defined(DEBUG_HASH) |
| 5162 | Alert("manage_client_side_cookies\n"); |
| 5163 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5164 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5165 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5166 | } |
| 5167 | |
| 5168 | /* we'll have to look for another cookie ... */ |
| 5169 | p1 = p4; |
| 5170 | } /* while (p1 < cur_end) */ |
| 5171 | |
| 5172 | /* There's no more cookie on this line. |
| 5173 | * We may have marked the last one(s) for deletion. |
| 5174 | * We must do this now in two ways : |
| 5175 | * - if there is no app cookie, we simply delete the header ; |
| 5176 | * - if there are app cookies, we must delete the end of the |
| 5177 | * string properly, including the colon/semi-colon before |
| 5178 | * the cookie name. |
| 5179 | */ |
| 5180 | if (del_cookie != NULL) { |
| 5181 | int delta; |
| 5182 | if (app_cookies) { |
| 5183 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5184 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5185 | cur_hdr->len += delta; |
| 5186 | } else { |
| 5187 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5188 | |
| 5189 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5190 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5191 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5192 | cur_hdr->len = 0; |
| 5193 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5194 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5195 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5196 | } |
| 5197 | |
| 5198 | /* keep the link from this header to next one */ |
| 5199 | old_idx = cur_idx; |
| 5200 | } /* end of cookie processing on this header */ |
| 5201 | } |
| 5202 | |
| 5203 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5204 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5205 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5206 | */ |
| 5207 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5208 | { |
| 5209 | char term; |
| 5210 | char *cur_ptr, *cur_end, *cur_next; |
| 5211 | int cur_idx, old_idx, last_hdr; |
| 5212 | struct http_txn *txn = &t->txn; |
| 5213 | struct hdr_idx_elem *cur_hdr; |
| 5214 | int len, delta; |
| 5215 | |
| 5216 | last_hdr = 0; |
| 5217 | |
| 5218 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5219 | old_idx = 0; |
| 5220 | |
| 5221 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5222 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5223 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5224 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5225 | (exp->action == ACT_ALLOW || |
| 5226 | exp->action == ACT_DENY)) |
| 5227 | return 0; |
| 5228 | |
| 5229 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5230 | if (!cur_idx) |
| 5231 | break; |
| 5232 | |
| 5233 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5234 | cur_ptr = cur_next; |
| 5235 | cur_end = cur_ptr + cur_hdr->len; |
| 5236 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5237 | |
| 5238 | /* Now we have one header between cur_ptr and cur_end, |
| 5239 | * and the next header starts at cur_next. |
| 5240 | */ |
| 5241 | |
| 5242 | /* The annoying part is that pattern matching needs |
| 5243 | * that we modify the contents to null-terminate all |
| 5244 | * strings before testing them. |
| 5245 | */ |
| 5246 | |
| 5247 | term = *cur_end; |
| 5248 | *cur_end = '\0'; |
| 5249 | |
| 5250 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5251 | switch (exp->action) { |
| 5252 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5253 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5254 | last_hdr = 1; |
| 5255 | break; |
| 5256 | |
| 5257 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5258 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5259 | last_hdr = 1; |
| 5260 | break; |
| 5261 | |
| 5262 | case ACT_REPLACE: |
| 5263 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5264 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5265 | /* FIXME: if the user adds a newline in the replacement, the |
| 5266 | * index will not be recalculated for now, and the new line |
| 5267 | * will not be counted as a new header. |
| 5268 | */ |
| 5269 | |
| 5270 | cur_end += delta; |
| 5271 | cur_next += delta; |
| 5272 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5273 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5274 | break; |
| 5275 | |
| 5276 | case ACT_REMOVE: |
| 5277 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5278 | cur_next += delta; |
| 5279 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5280 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5281 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5282 | txn->hdr_idx.used--; |
| 5283 | cur_hdr->len = 0; |
| 5284 | cur_end = NULL; /* null-term has been rewritten */ |
| 5285 | break; |
| 5286 | |
| 5287 | } |
| 5288 | } |
| 5289 | if (cur_end) |
| 5290 | *cur_end = term; /* restore the string terminator */ |
| 5291 | |
| 5292 | /* keep the link from this header to next one in case of later |
| 5293 | * removal of next header. |
| 5294 | */ |
| 5295 | old_idx = cur_idx; |
| 5296 | } |
| 5297 | return 0; |
| 5298 | } |
| 5299 | |
| 5300 | |
| 5301 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5302 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5303 | * or -1 if a replacement resulted in an invalid status line. |
| 5304 | */ |
| 5305 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5306 | { |
| 5307 | char term; |
| 5308 | char *cur_ptr, *cur_end; |
| 5309 | int done; |
| 5310 | struct http_txn *txn = &t->txn; |
| 5311 | int len, delta; |
| 5312 | |
| 5313 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5314 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5315 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5316 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5317 | (exp->action == ACT_ALLOW || |
| 5318 | exp->action == ACT_DENY)) |
| 5319 | return 0; |
| 5320 | else if (exp->action == ACT_REMOVE) |
| 5321 | return 0; |
| 5322 | |
| 5323 | done = 0; |
| 5324 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5325 | 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] | 5326 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5327 | |
| 5328 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5329 | |
| 5330 | /* The annoying part is that pattern matching needs |
| 5331 | * that we modify the contents to null-terminate all |
| 5332 | * strings before testing them. |
| 5333 | */ |
| 5334 | |
| 5335 | term = *cur_end; |
| 5336 | *cur_end = '\0'; |
| 5337 | |
| 5338 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5339 | switch (exp->action) { |
| 5340 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5341 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5342 | done = 1; |
| 5343 | break; |
| 5344 | |
| 5345 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5346 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5347 | done = 1; |
| 5348 | break; |
| 5349 | |
| 5350 | case ACT_REPLACE: |
| 5351 | *cur_end = term; /* restore the string terminator */ |
| 5352 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5353 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5354 | /* FIXME: if the user adds a newline in the replacement, the |
| 5355 | * index will not be recalculated for now, and the new line |
| 5356 | * will not be counted as a new header. |
| 5357 | */ |
| 5358 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5359 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5360 | cur_end += delta; |
| 5361 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5362 | 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] | 5363 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5364 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5365 | cur_ptr, cur_end + 1, |
| 5366 | NULL, NULL); |
| 5367 | if (unlikely(!cur_end)) |
| 5368 | return -1; |
| 5369 | |
| 5370 | /* we have a full respnse and we know that we have either a CR |
| 5371 | * or an LF at <ptr>. |
| 5372 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5373 | 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] | 5374 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5375 | /* there is no point trying this regex on headers */ |
| 5376 | return 1; |
| 5377 | } |
| 5378 | } |
| 5379 | *cur_end = term; /* restore the string terminator */ |
| 5380 | return done; |
| 5381 | } |
| 5382 | |
| 5383 | |
| 5384 | |
| 5385 | /* |
| 5386 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5387 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5388 | * unparsable response. |
| 5389 | */ |
| 5390 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5391 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5392 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5393 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5394 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5395 | int ret; |
| 5396 | |
| 5397 | /* |
| 5398 | * The interleaving of transformations and verdicts |
| 5399 | * makes it difficult to decide to continue or stop |
| 5400 | * the evaluation. |
| 5401 | */ |
| 5402 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5403 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5404 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5405 | exp->action == ACT_PASS)) { |
| 5406 | exp = exp->next; |
| 5407 | continue; |
| 5408 | } |
| 5409 | |
| 5410 | /* Apply the filter to the status line. */ |
| 5411 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5412 | if (unlikely(ret < 0)) |
| 5413 | return -1; |
| 5414 | |
| 5415 | if (likely(ret == 0)) { |
| 5416 | /* The filter did not match the response, it can be |
| 5417 | * iterated through all headers. |
| 5418 | */ |
| 5419 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5420 | } |
| 5421 | exp = exp->next; |
| 5422 | } |
| 5423 | return 0; |
| 5424 | } |
| 5425 | |
| 5426 | |
| 5427 | |
| 5428 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5429 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5430 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5431 | */ |
| 5432 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5433 | { |
| 5434 | struct http_txn *txn = &t->txn; |
| 5435 | char *p1, *p2, *p3, *p4; |
| 5436 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5437 | char *cur_ptr, *cur_end, *cur_next; |
| 5438 | int cur_idx, old_idx, delta; |
| 5439 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5440 | /* Iterate through the headers. |
| 5441 | * we start with the start line. |
| 5442 | */ |
| 5443 | old_idx = 0; |
| 5444 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5445 | |
| 5446 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5447 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5448 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5449 | |
| 5450 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5451 | cur_ptr = cur_next; |
| 5452 | cur_end = cur_ptr + cur_hdr->len; |
| 5453 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5454 | |
| 5455 | /* We have one full header between cur_ptr and cur_end, and the |
| 5456 | * next header starts at cur_next. We're only interested in |
| 5457 | * "Cookie:" headers. |
| 5458 | */ |
| 5459 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5460 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5461 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5462 | old_idx = cur_idx; |
| 5463 | continue; |
| 5464 | } |
| 5465 | |
| 5466 | /* 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] | 5467 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5468 | |
| 5469 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5470 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 5471 | * the cookie capture is declared in the fronend. |
| 5472 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5473 | if (t->be->cookie_name == NULL && |
| 5474 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5475 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5476 | return; |
| 5477 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5478 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5479 | |
| 5480 | 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] | 5481 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5482 | break; |
| 5483 | |
| 5484 | /* p1 is at the beginning of the cookie name */ |
| 5485 | p2 = p1; |
| 5486 | |
| 5487 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5488 | p2++; |
| 5489 | |
| 5490 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5491 | break; |
| 5492 | |
| 5493 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5494 | if (p3 == cur_end) |
| 5495 | break; |
| 5496 | |
| 5497 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5498 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5499 | p4++; |
| 5500 | |
| 5501 | /* here, we have the cookie name between p1 and p2, |
| 5502 | * and its value between p3 and p4. |
| 5503 | * we can process it. |
| 5504 | */ |
| 5505 | |
| 5506 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5507 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5508 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5509 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5510 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5511 | int log_len = p4 - p1; |
| 5512 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5513 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5514 | Alert("HTTP logging : out of memory.\n"); |
| 5515 | } |
| 5516 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5517 | if (log_len > t->fe->capture_len) |
| 5518 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5519 | memcpy(txn->srv_cookie, p1, log_len); |
| 5520 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5524 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5525 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5526 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5527 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5528 | |
| 5529 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5530 | * this occurrence because we'll insert another one later. |
| 5531 | * We'll delete it too if the "indirect" option is set and we're in |
| 5532 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5533 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5534 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5535 | /* this header must be deleted */ |
| 5536 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5537 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5538 | txn->hdr_idx.used--; |
| 5539 | cur_hdr->len = 0; |
| 5540 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5541 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5542 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5543 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5544 | } |
| 5545 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5546 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5547 | /* replace bytes p3->p4 with the cookie name associated |
| 5548 | * with this server since we know it. |
| 5549 | */ |
| 5550 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5551 | cur_hdr->len += delta; |
| 5552 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5553 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5554 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5555 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5556 | } |
| 5557 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5558 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5559 | /* insert the cookie name associated with this server |
| 5560 | * before existing cookie, and insert a delimitor between them.. |
| 5561 | */ |
| 5562 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5563 | cur_hdr->len += delta; |
| 5564 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5565 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5566 | |
| 5567 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5568 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5569 | } |
| 5570 | } |
| 5571 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5572 | else if (t->be->appsession_name != NULL) { |
| 5573 | int cmp_len, value_len; |
| 5574 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5575 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5576 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5577 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5578 | value_begin = p1 + t->be->appsession_name_len; |
| 5579 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 5580 | } else { |
| 5581 | cmp_len = p2 - p1; |
| 5582 | value_begin = p3; |
| 5583 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5584 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5585 | |
| 5586 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5587 | /* Cool... it's the right one */ |
| 5588 | if (t->sessid != NULL) { |
| 5589 | /* free previously allocated memory as we don't need it anymore */ |
| 5590 | pool_free2(apools.sessid, t->sessid); |
| 5591 | } |
| 5592 | /* Store the sessid in the session for future use */ |
| 5593 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5594 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5595 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5596 | return; |
| 5597 | } |
| 5598 | memcpy(t->sessid, value_begin, value_len); |
| 5599 | t->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5600 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5601 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5602 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5603 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5604 | /* keep the link from this header to next one */ |
| 5605 | old_idx = cur_idx; |
| 5606 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5607 | |
| 5608 | if (t->sessid != NULL) { |
| 5609 | appsess *asession = NULL; |
| 5610 | /* only do insert, if lookup fails */ |
| 5611 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid); |
| 5612 | if (asession == NULL) { |
| 5613 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 5614 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5615 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5616 | return; |
| 5617 | } |
| 5618 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5619 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5620 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5621 | return; |
| 5622 | } |
| 5623 | memcpy(asession->sessid, t->sessid, t->be->appsession_len); |
| 5624 | asession->sessid[t->be->appsession_len] = 0; |
| 5625 | |
| 5626 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5627 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 5628 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5629 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5630 | return; |
| 5631 | } |
| 5632 | asession->serverid[0] = '\0'; |
| 5633 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 5634 | |
| 5635 | asession->request_count = 0; |
| 5636 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 5637 | } |
| 5638 | |
| 5639 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5640 | asession->request_count++; |
| 5641 | } |
| 5642 | |
| 5643 | #if defined(DEBUG_HASH) |
| 5644 | Alert("manage_server_side_cookies\n"); |
| 5645 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5646 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | |
| 5650 | |
| 5651 | /* |
| 5652 | * Check if response is cacheable or not. Updates t->flags. |
| 5653 | */ |
| 5654 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5655 | { |
| 5656 | struct http_txn *txn = &t->txn; |
| 5657 | char *p1, *p2; |
| 5658 | |
| 5659 | char *cur_ptr, *cur_end, *cur_next; |
| 5660 | int cur_idx; |
| 5661 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5662 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5663 | return; |
| 5664 | |
| 5665 | /* Iterate through the headers. |
| 5666 | * we start with the start line. |
| 5667 | */ |
| 5668 | cur_idx = 0; |
| 5669 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5670 | |
| 5671 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5672 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5673 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5674 | |
| 5675 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5676 | cur_ptr = cur_next; |
| 5677 | cur_end = cur_ptr + cur_hdr->len; |
| 5678 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5679 | |
| 5680 | /* We have one full header between cur_ptr and cur_end, and the |
| 5681 | * next header starts at cur_next. We're only interested in |
| 5682 | * "Cookie:" headers. |
| 5683 | */ |
| 5684 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5685 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5686 | if (val) { |
| 5687 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5688 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5689 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5690 | return; |
| 5691 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5692 | } |
| 5693 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5694 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5695 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5696 | continue; |
| 5697 | |
| 5698 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5699 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5700 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5701 | |
| 5702 | if (p1 >= cur_end) /* no more info */ |
| 5703 | continue; |
| 5704 | |
| 5705 | /* p1 is at the beginning of the value */ |
| 5706 | p2 = p1; |
| 5707 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5708 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5709 | p2++; |
| 5710 | |
| 5711 | /* we have a complete value between p1 and p2 */ |
| 5712 | if (p2 < cur_end && *p2 == '=') { |
| 5713 | /* we have something of the form no-cache="set-cookie" */ |
| 5714 | if ((cur_end - p1 >= 21) && |
| 5715 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5716 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5717 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5718 | continue; |
| 5719 | } |
| 5720 | |
| 5721 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5722 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5723 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5724 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5725 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5726 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5727 | return; |
| 5728 | } |
| 5729 | |
| 5730 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5731 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5732 | continue; |
| 5733 | } |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5738 | /* |
| 5739 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5740 | * If the server is found, it's assigned to the session. |
| 5741 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5742 | 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] | 5743 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5744 | char *end_params, *first_param, *cur_param, *next_param; |
| 5745 | char separator; |
| 5746 | int value_len; |
| 5747 | |
| 5748 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5749 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5750 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5751 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5752 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5753 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5754 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5755 | first_param = NULL; |
| 5756 | switch (mode) { |
| 5757 | case PR_O2_AS_M_PP: |
| 5758 | first_param = memchr(begin, ';', len); |
| 5759 | break; |
| 5760 | case PR_O2_AS_M_QS: |
| 5761 | first_param = memchr(begin, '?', len); |
| 5762 | break; |
| 5763 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5764 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5765 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5766 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5767 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5768 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5769 | switch (mode) { |
| 5770 | case PR_O2_AS_M_PP: |
| 5771 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 5772 | end_params = (char *) begin + len; |
| 5773 | } |
| 5774 | separator = ';'; |
| 5775 | break; |
| 5776 | case PR_O2_AS_M_QS: |
| 5777 | end_params = (char *) begin + len; |
| 5778 | separator = '&'; |
| 5779 | break; |
| 5780 | default: |
| 5781 | /* unknown mode, shouldn't happen */ |
| 5782 | return; |
| 5783 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5784 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5785 | cur_param = next_param = end_params; |
| 5786 | while (cur_param > first_param) { |
| 5787 | cur_param--; |
| 5788 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 5789 | /* let's see if this is the appsession parameter */ |
| 5790 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 5791 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 5792 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 5793 | /* Cool... it's the right one */ |
| 5794 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 5795 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 5796 | if (value_len > 0) { |
| 5797 | manage_client_side_appsession(t, cur_param, value_len); |
| 5798 | } |
| 5799 | break; |
| 5800 | } |
| 5801 | next_param = cur_param; |
| 5802 | } |
| 5803 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5804 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5805 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5806 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5807 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5808 | } |
| 5809 | |
| 5810 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5811 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5812 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5813 | * 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] | 5814 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5815 | * 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] | 5816 | * 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] | 5817 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5818 | * |
| 5819 | * Returns 1 if the session's state changes, otherwise 0. |
| 5820 | */ |
| 5821 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5822 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5823 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5824 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5825 | struct user_auth *user; |
| 5826 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5827 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5828 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5829 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5830 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5831 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5832 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5833 | return 0; |
| 5834 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5835 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5836 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5837 | /* the URI is in h */ |
| 5838 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5839 | return 0; |
| 5840 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5841 | h += uri_auth->uri_len; |
| 5842 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5843 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5844 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5845 | break; |
| 5846 | } |
| 5847 | h++; |
| 5848 | } |
| 5849 | |
| 5850 | if (uri_auth->refresh) { |
| 5851 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5852 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5853 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5854 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5855 | break; |
| 5856 | } |
| 5857 | h++; |
| 5858 | } |
| 5859 | } |
| 5860 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5861 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5862 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5863 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5864 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5865 | break; |
| 5866 | } |
| 5867 | h++; |
| 5868 | } |
| 5869 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5870 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5871 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5872 | /* we are in front of a interceptable URI. Let's check |
| 5873 | * if there's an authentication and if it's valid. |
| 5874 | */ |
| 5875 | user = uri_auth->users; |
| 5876 | if (!user) { |
| 5877 | /* no user auth required, it's OK */ |
| 5878 | authenticated = 1; |
| 5879 | } else { |
| 5880 | authenticated = 0; |
| 5881 | |
| 5882 | /* a user list is defined, we have to check. |
| 5883 | * skip 21 chars for "Authorization: Basic ". |
| 5884 | */ |
| 5885 | |
| 5886 | /* FIXME: this should move to an earlier place */ |
| 5887 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5888 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5889 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5890 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5891 | if (len > 14 && |
| 5892 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 5893 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5894 | break; |
| 5895 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5896 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5897 | } |
| 5898 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5899 | if (txn->auth_hdr.len < 21 || |
| 5900 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5901 | user = NULL; |
| 5902 | |
| 5903 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5904 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5905 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5906 | user->user_pwd, user->user_len)) { |
| 5907 | authenticated = 1; |
| 5908 | break; |
| 5909 | } |
| 5910 | user = user->next; |
| 5911 | } |
| 5912 | } |
| 5913 | |
| 5914 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5915 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5916 | |
| 5917 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 5918 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 5919 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5920 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 5921 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5922 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5923 | if (!(t->flags & SN_ERR_MASK)) |
| 5924 | t->flags |= SN_ERR_PRXCOND; |
| 5925 | if (!(t->flags & SN_FINST_MASK)) |
| 5926 | t->flags |= SN_FINST_R; |
| 5927 | return 1; |
| 5928 | } |
| 5929 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5930 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5931 | * data. |
| 5932 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5933 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5934 | t->data_source = DATA_SRC_STATS; |
| 5935 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5936 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 5937 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 5938 | t->rep->prod->private = t; |
| 5939 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5940 | return 1; |
| 5941 | } |
| 5942 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5943 | /* |
| 5944 | * Capture a bad request or response and archive it in the proxy's structure. |
| 5945 | */ |
| 5946 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 5947 | struct buffer *buf, struct http_msg *msg, |
| 5948 | struct proxy *other_end) |
| 5949 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 5950 | es->len = buf->r - (buf->data + msg->som); |
| 5951 | 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] | 5952 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 5953 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5954 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 5955 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 5956 | es->when = date; // user-visible date |
| 5957 | es->sid = s->uniq_id; |
| 5958 | es->srv = s->srv; |
| 5959 | es->oe = other_end; |
| 5960 | es->src = s->cli_addr; |
| 5961 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5962 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5963 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5964 | * Print a debug line with a header |
| 5965 | */ |
| 5966 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5967 | { |
| 5968 | int len, max; |
| 5969 | 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] | 5970 | 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] | 5971 | max = end - start; |
| 5972 | UBOUND(max, sizeof(trash) - len - 1); |
| 5973 | len += strlcpy2(trash + len, start, max + 1); |
| 5974 | trash[len++] = '\n'; |
| 5975 | write(1, trash, len); |
| 5976 | } |
| 5977 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 5978 | /* |
| 5979 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 5980 | * the required fields are properly allocated and that we only need to (re)init |
| 5981 | * them. This should be used before processing any new request. |
| 5982 | */ |
| 5983 | void http_init_txn(struct session *s) |
| 5984 | { |
| 5985 | struct http_txn *txn = &s->txn; |
| 5986 | struct proxy *fe = s->fe; |
| 5987 | |
| 5988 | txn->flags = 0; |
| 5989 | txn->status = -1; |
| 5990 | |
| 5991 | txn->req.sol = txn->req.eol = NULL; |
| 5992 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 5993 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 5994 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 5995 | txn->req.hdr_content_len = 0LL; |
| 5996 | txn->rsp.hdr_content_len = 0LL; |
| 5997 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5998 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5999 | chunk_reset(&txn->auth_hdr); |
| 6000 | |
| 6001 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6002 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6003 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6004 | |
| 6005 | if (txn->req.cap) |
| 6006 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6007 | |
| 6008 | if (txn->rsp.cap) |
| 6009 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6010 | |
| 6011 | if (txn->hdr_idx.v) |
| 6012 | hdr_idx_init(&txn->hdr_idx); |
| 6013 | } |
| 6014 | |
| 6015 | /* to be used at the end of a transaction */ |
| 6016 | void http_end_txn(struct session *s) |
| 6017 | { |
| 6018 | struct http_txn *txn = &s->txn; |
| 6019 | |
| 6020 | /* these ones will have been dynamically allocated */ |
| 6021 | pool_free2(pool2_requri, txn->uri); |
| 6022 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6023 | pool_free2(pool2_capture, txn->srv_cookie); |
| 6024 | txn->uri = NULL; |
| 6025 | txn->srv_cookie = NULL; |
| 6026 | txn->cli_cookie = NULL; |
| 6027 | } |
| 6028 | |
| 6029 | /* to be used at the end of a transaction to prepare a new one */ |
| 6030 | void http_reset_txn(struct session *s) |
| 6031 | { |
| 6032 | http_end_txn(s); |
| 6033 | http_init_txn(s); |
| 6034 | |
| 6035 | s->be = s->fe; |
| 6036 | s->req->analysers = s->listener->analysers; |
| 6037 | s->logs.logwait = s->fe->to_log; |
| 6038 | s->srv = s->prev_srv = s->srv_conn = NULL; |
| 6039 | s->pend_pos = NULL; |
| 6040 | s->conn_retries = s->be->conn_retries; |
| 6041 | |
| 6042 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6043 | |
| 6044 | s->req->rto = s->fe->timeout.client; |
| 6045 | s->req->wto = s->be->timeout.server; |
| 6046 | s->req->cto = s->be->timeout.connect; |
| 6047 | |
| 6048 | s->rep->rto = s->be->timeout.server; |
| 6049 | s->rep->wto = s->fe->timeout.client; |
| 6050 | s->rep->cto = TICK_ETERNITY; |
| 6051 | |
| 6052 | s->req->rex = TICK_ETERNITY; |
| 6053 | s->req->wex = TICK_ETERNITY; |
| 6054 | s->req->analyse_exp = TICK_ETERNITY; |
| 6055 | s->rep->rex = TICK_ETERNITY; |
| 6056 | s->rep->wex = TICK_ETERNITY; |
| 6057 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6058 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6059 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6060 | /************************************************************************/ |
| 6061 | /* The code below is dedicated to ACL parsing and matching */ |
| 6062 | /************************************************************************/ |
| 6063 | |
| 6064 | |
| 6065 | |
| 6066 | |
| 6067 | /* 1. Check on METHOD |
| 6068 | * We use the pre-parsed method if it is known, and store its number as an |
| 6069 | * integer. If it is unknown, we use the pointer and the length. |
| 6070 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6071 | 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] | 6072 | { |
| 6073 | int len, meth; |
| 6074 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6075 | len = strlen(*text); |
| 6076 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6077 | |
| 6078 | pattern->val.i = meth; |
| 6079 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6080 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6081 | if (!pattern->ptr.str) |
| 6082 | return 0; |
| 6083 | pattern->len = len; |
| 6084 | } |
| 6085 | return 1; |
| 6086 | } |
| 6087 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6088 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6089 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6090 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6091 | { |
| 6092 | int meth; |
| 6093 | struct http_txn *txn = l7; |
| 6094 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6095 | if (!txn) |
| 6096 | return 0; |
| 6097 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6098 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6099 | return 0; |
| 6100 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6101 | meth = txn->meth; |
| 6102 | test->i = meth; |
| 6103 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6104 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6105 | /* ensure the indexes are not affected */ |
| 6106 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6107 | test->len = txn->req.sl.rq.m_l; |
| 6108 | test->ptr = txn->req.sol; |
| 6109 | } |
| 6110 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6111 | return 1; |
| 6112 | } |
| 6113 | |
| 6114 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6115 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6116 | int icase; |
| 6117 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6118 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6119 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6120 | |
| 6121 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6122 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6123 | |
| 6124 | /* Other method, we must compare the strings */ |
| 6125 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6126 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6127 | |
| 6128 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6129 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6130 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6131 | return ACL_PAT_FAIL; |
| 6132 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6133 | } |
| 6134 | |
| 6135 | /* 2. Check on Request/Status Version |
| 6136 | * We simply compare strings here. |
| 6137 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6138 | 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] | 6139 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6140 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6141 | if (!pattern->ptr.str) |
| 6142 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6143 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6144 | return 1; |
| 6145 | } |
| 6146 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6147 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6148 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6149 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6150 | { |
| 6151 | struct http_txn *txn = l7; |
| 6152 | char *ptr; |
| 6153 | int len; |
| 6154 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6155 | if (!txn) |
| 6156 | return 0; |
| 6157 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6158 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6159 | return 0; |
| 6160 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6161 | len = txn->req.sl.rq.v_l; |
| 6162 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 6163 | |
| 6164 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6165 | if (len <= 0) |
| 6166 | return 0; |
| 6167 | |
| 6168 | test->ptr = ptr; |
| 6169 | test->len = len; |
| 6170 | |
| 6171 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6172 | return 1; |
| 6173 | } |
| 6174 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6175 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6176 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6177 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6178 | { |
| 6179 | struct http_txn *txn = l7; |
| 6180 | char *ptr; |
| 6181 | int len; |
| 6182 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6183 | if (!txn) |
| 6184 | return 0; |
| 6185 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6186 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6187 | return 0; |
| 6188 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6189 | len = txn->rsp.sl.st.v_l; |
| 6190 | ptr = txn->rsp.sol; |
| 6191 | |
| 6192 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6193 | if (len <= 0) |
| 6194 | return 0; |
| 6195 | |
| 6196 | test->ptr = ptr; |
| 6197 | test->len = len; |
| 6198 | |
| 6199 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6200 | return 1; |
| 6201 | } |
| 6202 | |
| 6203 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6204 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6205 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6206 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6207 | { |
| 6208 | struct http_txn *txn = l7; |
| 6209 | char *ptr; |
| 6210 | int len; |
| 6211 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6212 | if (!txn) |
| 6213 | return 0; |
| 6214 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6215 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6216 | return 0; |
| 6217 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6218 | len = txn->rsp.sl.st.c_l; |
| 6219 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 6220 | |
| 6221 | test->i = __strl2ui(ptr, len); |
| 6222 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6223 | return 1; |
| 6224 | } |
| 6225 | |
| 6226 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6227 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6228 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6229 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6230 | { |
| 6231 | struct http_txn *txn = l7; |
| 6232 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6233 | if (!txn) |
| 6234 | return 0; |
| 6235 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6236 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6237 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6238 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6239 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6240 | /* ensure the indexes are not affected */ |
| 6241 | return 0; |
| 6242 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6243 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6244 | test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6245 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6246 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6247 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6248 | return 1; |
| 6249 | } |
| 6250 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6251 | static int |
| 6252 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6253 | struct acl_expr *expr, struct acl_test *test) |
| 6254 | { |
| 6255 | struct http_txn *txn = l7; |
| 6256 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6257 | if (!txn) |
| 6258 | return 0; |
| 6259 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6260 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6261 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6262 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6263 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6264 | /* ensure the indexes are not affected */ |
| 6265 | return 0; |
| 6266 | |
| 6267 | /* Parse HTTP request */ |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6268 | url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6269 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6270 | test->i = AF_INET; |
| 6271 | |
| 6272 | /* |
| 6273 | * If we are parsing url in frontend space, we prepare backend stage |
| 6274 | * to not parse again the same url ! optimization lazyness... |
| 6275 | */ |
| 6276 | if (px->options & PR_O_HTTP_PROXY) |
| 6277 | l4->flags |= SN_ADDR_SET; |
| 6278 | |
| 6279 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6280 | return 1; |
| 6281 | } |
| 6282 | |
| 6283 | static int |
| 6284 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6285 | struct acl_expr *expr, struct acl_test *test) |
| 6286 | { |
| 6287 | struct http_txn *txn = l7; |
| 6288 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6289 | if (!txn) |
| 6290 | return 0; |
| 6291 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6292 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6293 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6294 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6295 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6296 | /* ensure the indexes are not affected */ |
| 6297 | return 0; |
| 6298 | |
| 6299 | /* Same optimization as url_ip */ |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6300 | url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6301 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6302 | |
| 6303 | if (px->options & PR_O_HTTP_PROXY) |
| 6304 | l4->flags |= SN_ADDR_SET; |
| 6305 | |
| 6306 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6307 | return 1; |
| 6308 | } |
| 6309 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6310 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6311 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6312 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6313 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6314 | 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] | 6315 | struct acl_expr *expr, struct acl_test *test) |
| 6316 | { |
| 6317 | struct http_txn *txn = l7; |
| 6318 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6319 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6320 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6321 | if (!txn) |
| 6322 | return 0; |
| 6323 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6324 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6325 | /* search for header from the beginning */ |
| 6326 | ctx->idx = 0; |
| 6327 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6328 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6329 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6330 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6331 | test->len = ctx->vlen; |
| 6332 | test->ptr = (char *)ctx->line + ctx->val; |
| 6333 | return 1; |
| 6334 | } |
| 6335 | |
| 6336 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6337 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6338 | return 0; |
| 6339 | } |
| 6340 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6341 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6342 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6343 | struct acl_expr *expr, struct acl_test *test) |
| 6344 | { |
| 6345 | struct http_txn *txn = l7; |
| 6346 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6347 | if (!txn) |
| 6348 | return 0; |
| 6349 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6350 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6351 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6352 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6353 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6354 | /* ensure the indexes are not affected */ |
| 6355 | return 0; |
| 6356 | |
| 6357 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6358 | } |
| 6359 | |
| 6360 | static int |
| 6361 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6362 | struct acl_expr *expr, struct acl_test *test) |
| 6363 | { |
| 6364 | struct http_txn *txn = l7; |
| 6365 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6366 | if (!txn) |
| 6367 | return 0; |
| 6368 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6369 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6370 | return 0; |
| 6371 | |
| 6372 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6373 | } |
| 6374 | |
| 6375 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6376 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6377 | */ |
| 6378 | static int |
| 6379 | 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] | 6380 | struct acl_expr *expr, struct acl_test *test) |
| 6381 | { |
| 6382 | struct http_txn *txn = l7; |
| 6383 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6384 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6385 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6386 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6387 | if (!txn) |
| 6388 | return 0; |
| 6389 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6390 | ctx.idx = 0; |
| 6391 | cnt = 0; |
| 6392 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6393 | cnt++; |
| 6394 | |
| 6395 | test->i = cnt; |
| 6396 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6397 | return 1; |
| 6398 | } |
| 6399 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6400 | static int |
| 6401 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6402 | struct acl_expr *expr, struct acl_test *test) |
| 6403 | { |
| 6404 | struct http_txn *txn = l7; |
| 6405 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6406 | if (!txn) |
| 6407 | return 0; |
| 6408 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6409 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6410 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6411 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6412 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6413 | /* ensure the indexes are not affected */ |
| 6414 | return 0; |
| 6415 | |
| 6416 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6417 | } |
| 6418 | |
| 6419 | static int |
| 6420 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6421 | struct acl_expr *expr, struct acl_test *test) |
| 6422 | { |
| 6423 | struct http_txn *txn = l7; |
| 6424 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6425 | if (!txn) |
| 6426 | return 0; |
| 6427 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6428 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6429 | return 0; |
| 6430 | |
| 6431 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6432 | } |
| 6433 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6434 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6435 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6436 | * 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] | 6437 | */ |
| 6438 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6439 | 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] | 6440 | struct acl_expr *expr, struct acl_test *test) |
| 6441 | { |
| 6442 | struct http_txn *txn = l7; |
| 6443 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6444 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6445 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6446 | if (!txn) |
| 6447 | return 0; |
| 6448 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6449 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6450 | /* search for header from the beginning */ |
| 6451 | ctx->idx = 0; |
| 6452 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6453 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6454 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6455 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6456 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6457 | return 1; |
| 6458 | } |
| 6459 | |
| 6460 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6461 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6462 | return 0; |
| 6463 | } |
| 6464 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6465 | static int |
| 6466 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6467 | struct acl_expr *expr, struct acl_test *test) |
| 6468 | { |
| 6469 | struct http_txn *txn = l7; |
| 6470 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6471 | if (!txn) |
| 6472 | return 0; |
| 6473 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6474 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6475 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6476 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6477 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6478 | /* ensure the indexes are not affected */ |
| 6479 | return 0; |
| 6480 | |
| 6481 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6482 | } |
| 6483 | |
| 6484 | static int |
| 6485 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6486 | struct acl_expr *expr, struct acl_test *test) |
| 6487 | { |
| 6488 | struct http_txn *txn = l7; |
| 6489 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6490 | if (!txn) |
| 6491 | return 0; |
| 6492 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6493 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6494 | return 0; |
| 6495 | |
| 6496 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6497 | } |
| 6498 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6499 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 6500 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6501 | */ |
| 6502 | static int |
| 6503 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 6504 | struct acl_expr *expr, struct acl_test *test) |
| 6505 | { |
| 6506 | struct http_txn *txn = l7; |
| 6507 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6508 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 6509 | |
| 6510 | if (!txn) |
| 6511 | return 0; |
| 6512 | |
| 6513 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6514 | /* search for header from the beginning */ |
| 6515 | ctx->idx = 0; |
| 6516 | |
| 6517 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6518 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6519 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6520 | /* Same optimization as url_ip */ |
| 6521 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 6522 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 6523 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 6524 | test->i = AF_INET; |
| 6525 | return 1; |
| 6526 | } |
| 6527 | |
| 6528 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6529 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6530 | return 0; |
| 6531 | } |
| 6532 | |
| 6533 | static int |
| 6534 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6535 | struct acl_expr *expr, struct acl_test *test) |
| 6536 | { |
| 6537 | struct http_txn *txn = l7; |
| 6538 | |
| 6539 | if (!txn) |
| 6540 | return 0; |
| 6541 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6542 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6543 | return 0; |
| 6544 | |
| 6545 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6546 | /* ensure the indexes are not affected */ |
| 6547 | return 0; |
| 6548 | |
| 6549 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 6550 | } |
| 6551 | |
| 6552 | static int |
| 6553 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6554 | struct acl_expr *expr, struct acl_test *test) |
| 6555 | { |
| 6556 | struct http_txn *txn = l7; |
| 6557 | |
| 6558 | if (!txn) |
| 6559 | return 0; |
| 6560 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6561 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6562 | return 0; |
| 6563 | |
| 6564 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 6565 | } |
| 6566 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6567 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 6568 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 6569 | */ |
| 6570 | static int |
| 6571 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6572 | struct acl_expr *expr, struct acl_test *test) |
| 6573 | { |
| 6574 | struct http_txn *txn = l7; |
| 6575 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6576 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6577 | if (!txn) |
| 6578 | return 0; |
| 6579 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6580 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6581 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6582 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6583 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6584 | /* ensure the indexes are not affected */ |
| 6585 | return 0; |
| 6586 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6587 | end = txn->req.sol - txn->req.som + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6588 | ptr = http_get_path(txn); |
| 6589 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6590 | return 0; |
| 6591 | |
| 6592 | /* OK, we got the '/' ! */ |
| 6593 | test->ptr = ptr; |
| 6594 | |
| 6595 | while (ptr < end && *ptr != '?') |
| 6596 | ptr++; |
| 6597 | |
| 6598 | test->len = ptr - test->ptr; |
| 6599 | |
| 6600 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6601 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6602 | return 1; |
| 6603 | } |
| 6604 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6605 | static int |
| 6606 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 6607 | struct acl_expr *expr, struct acl_test *test) |
| 6608 | { |
| 6609 | struct buffer *req = s->req; |
| 6610 | struct http_txn *txn = &s->txn; |
| 6611 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6612 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6613 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 6614 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 6615 | */ |
| 6616 | |
| 6617 | if (!s || !req) |
| 6618 | return 0; |
| 6619 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6620 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6621 | /* Already decoded as OK */ |
| 6622 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 6623 | return 1; |
| 6624 | } |
| 6625 | |
| 6626 | /* Try to decode HTTP request */ |
| 6627 | if (likely(req->lr < req->r)) |
| 6628 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 6629 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6630 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6631 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 6632 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 6633 | return 1; |
| 6634 | } |
| 6635 | /* wait for final state */ |
| 6636 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 6637 | return 0; |
| 6638 | } |
| 6639 | |
| 6640 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 6641 | * perform so that further checks can rely on HTTP tests. |
| 6642 | */ |
| 6643 | msg->sol = req->data + msg->som; |
| 6644 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 6645 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 6646 | s->flags |= SN_REDIRECTABLE; |
| 6647 | |
| 6648 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 6649 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 6650 | return 1; |
| 6651 | } |
| 6652 | |
| 6653 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 6654 | return 1; |
| 6655 | } |
| 6656 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6657 | |
| 6658 | /************************************************************************/ |
| 6659 | /* All supported keywords must be declared here. */ |
| 6660 | /************************************************************************/ |
| 6661 | |
| 6662 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 6663 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6664 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 6665 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6666 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 6667 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6668 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6669 | { "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] | 6670 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6671 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6672 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6673 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6674 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6675 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6676 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6677 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6678 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 6679 | { "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] | 6680 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6681 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 6682 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6683 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6684 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6685 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6686 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6687 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6688 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6689 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 6690 | { "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] | 6691 | { "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] | 6692 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6693 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6694 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 6695 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 6696 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 6697 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 6698 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 6699 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 6700 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 6701 | { "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] | 6702 | { "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] | 6703 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6704 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6705 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6706 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6707 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6708 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6709 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6710 | { "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] | 6711 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6712 | { NULL, NULL, NULL, NULL }, |
| 6713 | |
| 6714 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6715 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 6716 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 6717 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 6718 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 6719 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 6720 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 6721 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 6722 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6723 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 6724 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 6725 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 6726 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 6727 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 6728 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 6729 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 6730 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 6731 | |
| 6732 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 6733 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 6734 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 6735 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 6736 | { NULL, NULL, NULL, NULL }, |
| 6737 | #endif |
| 6738 | }}; |
| 6739 | |
| 6740 | |
| 6741 | __attribute__((constructor)) |
| 6742 | static void __http_protocol_init(void) |
| 6743 | { |
| 6744 | acl_register_keywords(&acl_kws); |
| 6745 | } |
| 6746 | |
| 6747 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6748 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6749 | * Local variables: |
| 6750 | * c-indent-level: 8 |
| 6751 | * c-basic-offset: 8 |
| 6752 | * End: |
| 6753 | */ |