Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 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 | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 82 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 83 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 85 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 96 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 97 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 98 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 103 | const char *HTTP_401_fmt = |
| 104 | "HTTP/1.0 401 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 107 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 114 | [HTTP_ERR_400] = 400, |
| 115 | [HTTP_ERR_403] = 403, |
| 116 | [HTTP_ERR_408] = 408, |
| 117 | [HTTP_ERR_500] = 500, |
| 118 | [HTTP_ERR_502] = 502, |
| 119 | [HTTP_ERR_503] = 503, |
| 120 | [HTTP_ERR_504] = 504, |
| 121 | }; |
| 122 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 123 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | "Cache-Control: no-cache\r\n" |
| 127 | "Connection: close\r\n" |
| 128 | "Content-Type: text/html\r\n" |
| 129 | "\r\n" |
| 130 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 131 | |
| 132 | [HTTP_ERR_403] = |
| 133 | "HTTP/1.0 403 Forbidden\r\n" |
| 134 | "Cache-Control: no-cache\r\n" |
| 135 | "Connection: close\r\n" |
| 136 | "Content-Type: text/html\r\n" |
| 137 | "\r\n" |
| 138 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 139 | |
| 140 | [HTTP_ERR_408] = |
| 141 | "HTTP/1.0 408 Request Time-out\r\n" |
| 142 | "Cache-Control: no-cache\r\n" |
| 143 | "Connection: close\r\n" |
| 144 | "Content-Type: text/html\r\n" |
| 145 | "\r\n" |
| 146 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 147 | |
| 148 | [HTTP_ERR_500] = |
| 149 | "HTTP/1.0 500 Server Error\r\n" |
| 150 | "Cache-Control: no-cache\r\n" |
| 151 | "Connection: close\r\n" |
| 152 | "Content-Type: text/html\r\n" |
| 153 | "\r\n" |
| 154 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 155 | |
| 156 | [HTTP_ERR_502] = |
| 157 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 158 | "Cache-Control: no-cache\r\n" |
| 159 | "Connection: close\r\n" |
| 160 | "Content-Type: text/html\r\n" |
| 161 | "\r\n" |
| 162 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 163 | |
| 164 | [HTTP_ERR_503] = |
| 165 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 166 | "Cache-Control: no-cache\r\n" |
| 167 | "Connection: close\r\n" |
| 168 | "Content-Type: text/html\r\n" |
| 169 | "\r\n" |
| 170 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 171 | |
| 172 | [HTTP_ERR_504] = |
| 173 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 174 | "Cache-Control: no-cache\r\n" |
| 175 | "Connection: close\r\n" |
| 176 | "Content-Type: text/html\r\n" |
| 177 | "\r\n" |
| 178 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 179 | |
| 180 | }; |
| 181 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 182 | /* We must put the messages here since GCC cannot initialize consts depending |
| 183 | * on strlen(). |
| 184 | */ |
| 185 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 186 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 187 | #define FD_SETS_ARE_BITFIELDS |
| 188 | #ifdef FD_SETS_ARE_BITFIELDS |
| 189 | /* |
| 190 | * This map is used with all the FD_* macros to check whether a particular bit |
| 191 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 192 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 193 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 194 | * exclusively to the macros. |
| 195 | */ |
| 196 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 197 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 198 | |
| 199 | #else |
| 200 | #error "Check if your OS uses bitfields for fd_sets" |
| 201 | #endif |
| 202 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 203 | void init_proto_http() |
| 204 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 205 | int i; |
| 206 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 207 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 210 | if (!http_err_msgs[msg]) { |
| 211 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 212 | abort(); |
| 213 | } |
| 214 | |
| 215 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 216 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 217 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 218 | |
| 219 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 220 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 221 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 222 | * printable characters above. |
| 223 | */ |
| 224 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 225 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 226 | for (i = 0; i < 32; i++) { |
| 227 | FD_SET(i, hdr_encode_map); |
| 228 | FD_SET(i, url_encode_map); |
| 229 | } |
| 230 | for (i = 127; i < 256; i++) { |
| 231 | FD_SET(i, hdr_encode_map); |
| 232 | FD_SET(i, url_encode_map); |
| 233 | } |
| 234 | |
| 235 | tmp = "\"#{|}"; |
| 236 | while (*tmp) { |
| 237 | FD_SET(*tmp, hdr_encode_map); |
| 238 | tmp++; |
| 239 | } |
| 240 | |
| 241 | tmp = "\"#"; |
| 242 | while (*tmp) { |
| 243 | FD_SET(*tmp, url_encode_map); |
| 244 | tmp++; |
| 245 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 246 | |
| 247 | /* memory allocations */ |
| 248 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 249 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 250 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 251 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 252 | /* |
| 253 | * We have 26 list of methods (1 per first letter), each of which can have |
| 254 | * up to 3 entries (2 valid, 1 null). |
| 255 | */ |
| 256 | struct http_method_desc { |
| 257 | http_meth_t meth; |
| 258 | int len; |
| 259 | const char text[8]; |
| 260 | }; |
| 261 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 262 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 263 | ['C' - 'A'] = { |
| 264 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 265 | }, |
| 266 | ['D' - 'A'] = { |
| 267 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 268 | }, |
| 269 | ['G' - 'A'] = { |
| 270 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 271 | }, |
| 272 | ['H' - 'A'] = { |
| 273 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 274 | }, |
| 275 | ['P' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 277 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 278 | }, |
| 279 | ['T' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 281 | }, |
| 282 | /* rest is empty like this : |
| 283 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 284 | */ |
| 285 | }; |
| 286 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 287 | /* 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] | 288 | * 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] | 289 | * RFC2616 for those chars. |
| 290 | */ |
| 291 | |
| 292 | const char http_is_spht[256] = { |
| 293 | [' '] = 1, ['\t'] = 1, |
| 294 | }; |
| 295 | |
| 296 | const char http_is_crlf[256] = { |
| 297 | ['\r'] = 1, ['\n'] = 1, |
| 298 | }; |
| 299 | |
| 300 | const char http_is_lws[256] = { |
| 301 | [' '] = 1, ['\t'] = 1, |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_sep[256] = { |
| 306 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 307 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 308 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 309 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 310 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 311 | }; |
| 312 | |
| 313 | const char http_is_ctl[256] = { |
| 314 | [0 ... 31] = 1, |
| 315 | [127] = 1, |
| 316 | }; |
| 317 | |
| 318 | /* |
| 319 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 320 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 321 | * them correctly. Instead, define every non-CTL char's status. |
| 322 | */ |
| 323 | const char http_is_token[256] = { |
| 324 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 325 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 326 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 327 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 328 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 329 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 330 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 331 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 332 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 333 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 334 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 335 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 336 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 337 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 338 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 339 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 340 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 341 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 342 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 343 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 344 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 345 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 346 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 347 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 348 | }; |
| 349 | |
| 350 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 351 | /* |
| 352 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 353 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 354 | */ |
| 355 | const char http_is_ver_token[256] = { |
| 356 | ['.'] = 1, ['/'] = 1, |
| 357 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 358 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 359 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 363 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 364 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 365 | */ |
| 366 | #if defined(DEBUG_FSM) |
| 367 | static void http_silent_debug(int line, struct session *s) |
| 368 | { |
| 369 | int size = 0; |
| 370 | size += snprintf(trash + size, sizeof(trash) - size, |
| 371 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n", |
| 372 | line, |
| 373 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 374 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags); |
| 375 | write(-1, trash, size); |
| 376 | size = 0; |
| 377 | size += snprintf(trash + size, sizeof(trash) - size, |
| 378 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n", |
| 379 | line, |
| 380 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 381 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward); |
| 382 | |
| 383 | write(-1, trash, size); |
| 384 | } |
| 385 | #else |
| 386 | #define http_silent_debug(l,s) do { } while (0) |
| 387 | #endif |
| 388 | |
| 389 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 390 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 391 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 392 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 393 | * of headers is automatically adjusted. The number of bytes added is returned |
| 394 | * on success, otherwise <0 is returned indicating an error. |
| 395 | */ |
| 396 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 397 | struct hdr_idx *hdr_idx, const char *text) |
| 398 | { |
| 399 | int bytes, len; |
| 400 | |
| 401 | len = strlen(text); |
| 402 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 403 | if (!bytes) |
| 404 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 405 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 406 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 411 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 412 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 413 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 414 | * of headers is automatically adjusted. The number of bytes added is returned |
| 415 | * on success, otherwise <0 is returned indicating an error. |
| 416 | */ |
| 417 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 418 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 419 | { |
| 420 | int bytes; |
| 421 | |
| 422 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 423 | if (!bytes) |
| 424 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 425 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 426 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 427 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 430 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 431 | * If so, returns the position of the first non-space character relative to |
| 432 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 433 | * to return a pointer to the place after the first space. Returns 0 if the |
| 434 | * header name does not match. Checks are case-insensitive. |
| 435 | */ |
| 436 | int http_header_match2(const char *hdr, const char *end, |
| 437 | const char *name, int len) |
| 438 | { |
| 439 | const char *val; |
| 440 | |
| 441 | if (hdr + len >= end) |
| 442 | return 0; |
| 443 | if (hdr[len] != ':') |
| 444 | return 0; |
| 445 | if (strncasecmp(hdr, name, len) != 0) |
| 446 | return 0; |
| 447 | val = hdr + len + 1; |
| 448 | while (val < end && HTTP_IS_SPHT(*val)) |
| 449 | val++; |
| 450 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 451 | return len + 2; /* we may replace starting from second space */ |
| 452 | return val - hdr; |
| 453 | } |
| 454 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 455 | /* Find the end of the header value contained between <s> and <e>. |
| 456 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 457 | * a valid header to return a valid result. |
| 458 | */ |
| 459 | const char *find_hdr_value_end(const char *s, const char *e) |
| 460 | { |
| 461 | int quoted, qdpair; |
| 462 | |
| 463 | quoted = qdpair = 0; |
| 464 | for (; s < e; s++) { |
| 465 | if (qdpair) qdpair = 0; |
| 466 | else if (quoted && *s == '\\') qdpair = 1; |
| 467 | else if (quoted && *s == '"') quoted = 0; |
| 468 | else if (*s == '"') quoted = 1; |
| 469 | else if (*s == ',') return s; |
| 470 | } |
| 471 | return s; |
| 472 | } |
| 473 | |
| 474 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 475 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 476 | * structure holds everything necessary to use the header and find next |
| 477 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 478 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 479 | * 1 when it finds a value, and 0 when there is no more. |
| 480 | */ |
| 481 | int http_find_header2(const char *name, int len, |
| 482 | const char *sol, struct hdr_idx *idx, |
| 483 | struct hdr_ctx *ctx) |
| 484 | { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 485 | const char *eol, *sov; |
| 486 | int cur_idx; |
| 487 | |
| 488 | if (ctx->idx) { |
| 489 | /* We have previously returned a value, let's search |
| 490 | * another one on the same line. |
| 491 | */ |
| 492 | cur_idx = ctx->idx; |
| 493 | sol = ctx->line; |
| 494 | sov = sol + ctx->val + ctx->vlen; |
| 495 | eol = sol + idx->v[cur_idx].len; |
| 496 | |
| 497 | if (sov >= eol) |
| 498 | /* no more values in this header */ |
| 499 | goto next_hdr; |
| 500 | |
| 501 | /* values remaining for this header, skip the comma */ |
| 502 | sov++; |
| 503 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 504 | sov++; |
| 505 | |
| 506 | goto return_hdr; |
| 507 | } |
| 508 | |
| 509 | /* first request for this header */ |
| 510 | sol += hdr_idx_first_pos(idx); |
| 511 | cur_idx = hdr_idx_first_idx(idx); |
| 512 | |
| 513 | while (cur_idx) { |
| 514 | eol = sol + idx->v[cur_idx].len; |
| 515 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 516 | if (len == 0) { |
| 517 | /* No argument was passed, we want any header. |
| 518 | * To achieve this, we simply build a fake request. */ |
| 519 | while (sol + len < eol && sol[len] != ':') |
| 520 | len++; |
| 521 | name = sol; |
| 522 | } |
| 523 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 524 | if ((len < eol - sol) && |
| 525 | (sol[len] == ':') && |
| 526 | (strncasecmp(sol, name, len) == 0)) { |
| 527 | |
| 528 | sov = sol + len + 1; |
| 529 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 530 | sov++; |
| 531 | return_hdr: |
| 532 | ctx->line = sol; |
| 533 | ctx->idx = cur_idx; |
| 534 | ctx->val = sov - sol; |
| 535 | |
| 536 | eol = find_hdr_value_end(sov, eol); |
| 537 | ctx->vlen = eol - sov; |
| 538 | return 1; |
| 539 | } |
| 540 | next_hdr: |
| 541 | sol = eol + idx->v[cur_idx].cr + 1; |
| 542 | cur_idx = idx->v[cur_idx].next; |
| 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int http_find_header(const char *name, |
| 548 | const char *sol, struct hdr_idx *idx, |
| 549 | struct hdr_ctx *ctx) |
| 550 | { |
| 551 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 552 | } |
| 553 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 554 | /* This function handles a server error at the stream interface level. The |
| 555 | * stream interface is assumed to be already in a closed state. An optional |
| 556 | * message is copied into the input buffer, and an HTTP status code stored. |
| 557 | * 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] | 558 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 559 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 560 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 561 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 562 | { |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 563 | buffer_erase(si->ob); |
| 564 | buffer_erase(si->ib); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 565 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 566 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 567 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 568 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 569 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | } |
| 571 | if (!(t->flags & SN_ERR_MASK)) |
| 572 | t->flags |= err; |
| 573 | if (!(t->flags & SN_FINST_MASK)) |
| 574 | t->flags |= finst; |
| 575 | } |
| 576 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 577 | /* This function returns the appropriate error location for the given session |
| 578 | * and message. |
| 579 | */ |
| 580 | |
| 581 | struct chunk *error_message(struct session *s, int msgnum) |
| 582 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 583 | if (s->be->errmsg[msgnum].str) |
| 584 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 585 | else if (s->fe->errmsg[msgnum].str) |
| 586 | return &s->fe->errmsg[msgnum]; |
| 587 | else |
| 588 | return &http_err_chunks[msgnum]; |
| 589 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 590 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | /* |
| 592 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 593 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 594 | */ |
| 595 | static http_meth_t find_http_meth(const char *str, const int len) |
| 596 | { |
| 597 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 598 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 599 | |
| 600 | m = ((unsigned)*str - 'A'); |
| 601 | |
| 602 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 603 | for (h = http_methods[m]; h->len > 0; h++) { |
| 604 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 605 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 606 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 607 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 608 | }; |
| 609 | return HTTP_METH_OTHER; |
| 610 | } |
| 611 | return HTTP_METH_NONE; |
| 612 | |
| 613 | } |
| 614 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 615 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 616 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 617 | * It is returned otherwise. |
| 618 | */ |
| 619 | static char * |
| 620 | http_get_path(struct http_txn *txn) |
| 621 | { |
| 622 | char *ptr, *end; |
| 623 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 624 | ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 625 | end = ptr + txn->req.sl.rq.u_l; |
| 626 | |
| 627 | if (ptr >= end) |
| 628 | return NULL; |
| 629 | |
| 630 | /* RFC2616, par. 5.1.2 : |
| 631 | * Request-URI = "*" | absuri | abspath | authority |
| 632 | */ |
| 633 | |
| 634 | if (*ptr == '*') |
| 635 | return NULL; |
| 636 | |
| 637 | if (isalpha((unsigned char)*ptr)) { |
| 638 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 639 | ptr++; |
| 640 | while (ptr < end && |
| 641 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 642 | ptr++; |
| 643 | /* skip '://' */ |
| 644 | if (ptr == end || *ptr++ != ':') |
| 645 | return NULL; |
| 646 | if (ptr == end || *ptr++ != '/') |
| 647 | return NULL; |
| 648 | if (ptr == end || *ptr++ != '/') |
| 649 | return NULL; |
| 650 | } |
| 651 | /* skip [user[:passwd]@]host[:[port]] */ |
| 652 | |
| 653 | while (ptr < end && *ptr != '/') |
| 654 | ptr++; |
| 655 | |
| 656 | if (ptr == end) |
| 657 | return NULL; |
| 658 | |
| 659 | /* OK, we got the '/' ! */ |
| 660 | return ptr; |
| 661 | } |
| 662 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 663 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 664 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 665 | * left unchanged and will follow normal proxy processing. |
| 666 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 667 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 668 | { |
| 669 | struct http_txn *txn; |
| 670 | struct chunk rdr; |
| 671 | char *path; |
| 672 | int len; |
| 673 | |
| 674 | /* 1: create the response header */ |
| 675 | rdr.len = strlen(HTTP_302); |
| 676 | rdr.str = trash; |
| 677 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 678 | |
| 679 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 680 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 681 | return; |
| 682 | |
| 683 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 684 | rdr.len += s->srv->rdr_len; |
| 685 | |
| 686 | /* 3: add the request URI */ |
| 687 | txn = &s->txn; |
| 688 | path = http_get_path(txn); |
| 689 | if (!path) |
| 690 | return; |
| 691 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 692 | 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] | 693 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 694 | return; |
| 695 | |
| 696 | memcpy(rdr.str + rdr.len, path, len); |
| 697 | rdr.len += len; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 698 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 699 | rdr.len += 23; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 700 | |
| 701 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 702 | si->shutr(si); |
| 703 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 704 | si->err_type = SI_ET_NONE; |
| 705 | si->err_loc = NULL; |
| 706 | si->state = SI_ST_CLO; |
| 707 | |
| 708 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 709 | 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] | 710 | |
| 711 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 712 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 713 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 716 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 717 | * that the server side is closed. Note that err_type is actually a |
| 718 | * bitmask, where almost only aborts may be cumulated with other |
| 719 | * values. We consider that aborted operations are more important |
| 720 | * than timeouts or errors due to the fact that nobody else in the |
| 721 | * logs might explain incomplete retries. All others should avoid |
| 722 | * being cumulated. It should normally not be possible to have multiple |
| 723 | * aborts at once, but just in case, the first one in sequence is reported. |
| 724 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 725 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 726 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 727 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 728 | |
| 729 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 730 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 731 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 732 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 733 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 734 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 735 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 736 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 737 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 738 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 739 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 740 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 741 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 742 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 743 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 744 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 745 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 746 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 747 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 748 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 749 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 752 | extern const char sess_term_cond[8]; |
| 753 | extern const char sess_fin_state[8]; |
| 754 | extern const char *monthname[12]; |
| 755 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 756 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 757 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 758 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 759 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 760 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 761 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 762 | void http_sess_clflog(struct session *s) |
| 763 | { |
| 764 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 765 | struct proxy *fe = s->fe; |
| 766 | struct proxy *be = s->be; |
| 767 | struct proxy *prx_log; |
| 768 | struct http_txn *txn = &s->txn; |
| 769 | int tolog, level, err; |
| 770 | char *uri, *h; |
| 771 | char *svid; |
| 772 | struct tm tm; |
| 773 | static char tmpline[MAX_SYSLOG_LEN]; |
| 774 | int hdr; |
| 775 | size_t w; |
| 776 | int t_request; |
| 777 | |
| 778 | prx_log = fe; |
| 779 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 780 | (s->conn_retries != be->conn_retries) || |
| 781 | txn->status >= 500; |
| 782 | |
| 783 | if (s->cli_addr.ss_family == AF_INET) |
| 784 | inet_ntop(AF_INET, |
| 785 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 786 | pn, sizeof(pn)); |
| 787 | else |
| 788 | inet_ntop(AF_INET6, |
| 789 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 790 | pn, sizeof(pn)); |
| 791 | |
| 792 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 793 | |
| 794 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 795 | tolog = fe->to_log; |
| 796 | |
| 797 | h = tmpline; |
| 798 | |
| 799 | w = snprintf(h, sizeof(tmpline), |
| 800 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 801 | pn, |
| 802 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 803 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 804 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 805 | goto trunc; |
| 806 | h += w; |
| 807 | |
| 808 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 809 | goto trunc; |
| 810 | |
| 811 | *(h++) = ' '; |
| 812 | *(h++) = '\"'; |
| 813 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 814 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 815 | '#', url_encode_map, uri); |
| 816 | *(h++) = '\"'; |
| 817 | |
| 818 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 819 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 820 | goto trunc; |
| 821 | h += w; |
| 822 | |
| 823 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 824 | goto trunc; |
| 825 | memcpy(h, " \"-\" \"-\"", 8); |
| 826 | h += 8; |
| 827 | |
| 828 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 829 | " %d %03d", |
| 830 | (s->cli_addr.ss_family == AF_INET) ? |
| 831 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 832 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 833 | (int)s->logs.accept_date.tv_usec/1000); |
| 834 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 835 | goto trunc; |
| 836 | h += w; |
| 837 | |
| 838 | w = strlen(fe->id); |
| 839 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 840 | goto trunc; |
| 841 | *(h++) = ' '; |
| 842 | *(h++) = '\"'; |
| 843 | memcpy(h, fe->id, w); |
| 844 | h += w; |
| 845 | *(h++) = '\"'; |
| 846 | |
| 847 | w = strlen(be->id); |
| 848 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 849 | goto trunc; |
| 850 | *(h++) = ' '; |
| 851 | *(h++) = '\"'; |
| 852 | memcpy(h, be->id, w); |
| 853 | h += w; |
| 854 | *(h++) = '\"'; |
| 855 | |
| 856 | svid = (tolog & LW_SVID) ? |
| 857 | (s->data_source != DATA_SRC_STATS) ? |
| 858 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 859 | |
| 860 | w = strlen(svid); |
| 861 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 862 | goto trunc; |
| 863 | *(h++) = ' '; |
| 864 | *(h++) = '\"'; |
| 865 | memcpy(h, svid, w); |
| 866 | h += w; |
| 867 | *(h++) = '\"'; |
| 868 | |
| 869 | t_request = -1; |
| 870 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 871 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 872 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 873 | " %d %ld %ld %ld %ld", |
| 874 | t_request, |
| 875 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 876 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 877 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 878 | s->logs.t_close); |
| 879 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 880 | goto trunc; |
| 881 | h += w; |
| 882 | |
| 883 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 884 | goto trunc; |
| 885 | *(h++) = ' '; |
| 886 | *(h++) = '\"'; |
| 887 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 888 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 889 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 890 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 891 | *(h++) = '\"'; |
| 892 | |
| 893 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 894 | " %d %d %d %d %d %ld %ld", |
| 895 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 896 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 897 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 898 | |
| 899 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 900 | goto trunc; |
| 901 | h += w; |
| 902 | |
| 903 | if (txn->cli_cookie) { |
| 904 | w = strlen(txn->cli_cookie); |
| 905 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 906 | goto trunc; |
| 907 | *(h++) = ' '; |
| 908 | *(h++) = '\"'; |
| 909 | memcpy(h, txn->cli_cookie, w); |
| 910 | h += w; |
| 911 | *(h++) = '\"'; |
| 912 | } else { |
| 913 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 914 | goto trunc; |
| 915 | memcpy(h, " \"-\"", 4); |
| 916 | h += 4; |
| 917 | } |
| 918 | |
| 919 | if (txn->srv_cookie) { |
| 920 | w = strlen(txn->srv_cookie); |
| 921 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 922 | goto trunc; |
| 923 | *(h++) = ' '; |
| 924 | *(h++) = '\"'; |
| 925 | memcpy(h, txn->srv_cookie, w); |
| 926 | h += w; |
| 927 | *(h++) = '\"'; |
| 928 | } else { |
| 929 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 930 | goto trunc; |
| 931 | memcpy(h, " \"-\"", 4); |
| 932 | h += 4; |
| 933 | } |
| 934 | |
| 935 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 936 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 937 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 938 | goto trunc; |
| 939 | *(h++) = ' '; |
| 940 | *(h++) = '\"'; |
| 941 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 942 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 943 | *(h++) = '\"'; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 948 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 949 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 950 | goto trunc; |
| 951 | *(h++) = ' '; |
| 952 | *(h++) = '\"'; |
| 953 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 954 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 955 | *(h++) = '\"'; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | trunc: |
| 960 | *h = '\0'; |
| 961 | |
| 962 | level = LOG_INFO; |
| 963 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 964 | level = LOG_ERR; |
| 965 | |
| 966 | send_log(prx_log, level, "%s\n", tmpline); |
| 967 | |
| 968 | s->logs.logwait = 0; |
| 969 | } |
| 970 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 971 | /* |
| 972 | * send a log for the session when we have enough info about it. |
| 973 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 974 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 975 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 976 | { |
| 977 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 978 | struct proxy *fe = s->fe; |
| 979 | struct proxy *be = s->be; |
| 980 | struct proxy *prx_log; |
| 981 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 982 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 983 | char *uri, *h; |
| 984 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 985 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 986 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 987 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 988 | int hdr; |
| 989 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 990 | /* if we don't want to log normal traffic, return now */ |
| 991 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 992 | (s->conn_retries != be->conn_retries) || |
| 993 | txn->status >= 500; |
| 994 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 995 | return; |
| 996 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 997 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 998 | return; |
| 999 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1000 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1001 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1002 | return http_sess_clflog(s); |
| 1003 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1004 | if (s->cli_addr.ss_family == AF_INET) |
| 1005 | inet_ntop(AF_INET, |
| 1006 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1007 | pn, sizeof(pn)); |
| 1008 | else |
| 1009 | inet_ntop(AF_INET6, |
| 1010 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1011 | pn, sizeof(pn)); |
| 1012 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1013 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1014 | |
| 1015 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1016 | tolog = fe->to_log; |
| 1017 | |
| 1018 | h = tmpline; |
| 1019 | if (fe->to_log & LW_REQHDR && |
| 1020 | txn->req.cap && |
| 1021 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1022 | *(h++) = ' '; |
| 1023 | *(h++) = '{'; |
| 1024 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1025 | if (hdr) |
| 1026 | *(h++) = '|'; |
| 1027 | if (txn->req.cap[hdr] != NULL) |
| 1028 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1029 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1030 | } |
| 1031 | *(h++) = '}'; |
| 1032 | } |
| 1033 | |
| 1034 | if (fe->to_log & LW_RSPHDR && |
| 1035 | txn->rsp.cap && |
| 1036 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1037 | *(h++) = ' '; |
| 1038 | *(h++) = '{'; |
| 1039 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1040 | if (hdr) |
| 1041 | *(h++) = '|'; |
| 1042 | if (txn->rsp.cap[hdr] != NULL) |
| 1043 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1044 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1045 | } |
| 1046 | *(h++) = '}'; |
| 1047 | } |
| 1048 | |
| 1049 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1050 | *(h++) = ' '; |
| 1051 | *(h++) = '"'; |
| 1052 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1053 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1054 | '#', url_encode_map, uri); |
| 1055 | *(h++) = '"'; |
| 1056 | } |
| 1057 | *h = '\0'; |
| 1058 | |
| 1059 | svid = (tolog & LW_SVID) ? |
| 1060 | (s->data_source != DATA_SRC_STATS) ? |
| 1061 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1062 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1063 | t_request = -1; |
| 1064 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1065 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1066 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1067 | level = LOG_INFO; |
| 1068 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1069 | level = LOG_ERR; |
| 1070 | |
| 1071 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1072 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1073 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1074 | " %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] | 1075 | pn, |
| 1076 | (s->cli_addr.ss_family == AF_INET) ? |
| 1077 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1078 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1079 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1080 | 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] | 1081 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1082 | t_request, |
| 1083 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1084 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1085 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1086 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1087 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1088 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1089 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1090 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1091 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1092 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1093 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1094 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1095 | 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] | 1096 | (s->flags & SN_REDISP)?"+":"", |
| 1097 | (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] | 1098 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1099 | |
| 1100 | s->logs.logwait = 0; |
| 1101 | } |
| 1102 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1103 | |
| 1104 | /* |
| 1105 | * Capture headers from message starting at <som> according to header list |
| 1106 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1107 | */ |
| 1108 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1109 | char **cap, struct cap_hdr *cap_hdr) |
| 1110 | { |
| 1111 | char *eol, *sol, *col, *sov; |
| 1112 | int cur_idx; |
| 1113 | struct cap_hdr *h; |
| 1114 | int len; |
| 1115 | |
| 1116 | sol = som + hdr_idx_first_pos(idx); |
| 1117 | cur_idx = hdr_idx_first_idx(idx); |
| 1118 | |
| 1119 | while (cur_idx) { |
| 1120 | eol = sol + idx->v[cur_idx].len; |
| 1121 | |
| 1122 | col = sol; |
| 1123 | while (col < eol && *col != ':') |
| 1124 | col++; |
| 1125 | |
| 1126 | sov = col + 1; |
| 1127 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1128 | sov++; |
| 1129 | |
| 1130 | for (h = cap_hdr; h; h = h->next) { |
| 1131 | if ((h->namelen == col - sol) && |
| 1132 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1133 | if (cap[h->index] == NULL) |
| 1134 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1135 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1136 | |
| 1137 | if (cap[h->index] == NULL) { |
| 1138 | Alert("HTTP capture : out of memory.\n"); |
| 1139 | continue; |
| 1140 | } |
| 1141 | |
| 1142 | len = eol - sov; |
| 1143 | if (len > h->len) |
| 1144 | len = h->len; |
| 1145 | |
| 1146 | memcpy(cap[h->index], sov, len); |
| 1147 | cap[h->index][len]=0; |
| 1148 | } |
| 1149 | } |
| 1150 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1151 | cur_idx = idx->v[cur_idx].next; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1156 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1157 | */ |
| 1158 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1159 | |
| 1160 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1161 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1162 | */ |
| 1163 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1164 | ptr++; \ |
| 1165 | if (likely(ptr < end)) \ |
| 1166 | goto good; \ |
| 1167 | else { \ |
| 1168 | state = (st); \ |
| 1169 | goto http_msg_ood; \ |
| 1170 | } \ |
| 1171 | } while (0) |
| 1172 | |
| 1173 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1174 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1175 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1176 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1177 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1178 | * will give undefined results. |
| 1179 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1180 | * and that msg->sol points to the beginning of the response. |
| 1181 | * If a complete line is found (which implies that at least one CR or LF is |
| 1182 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1183 | * returned indicating an incomplete line (which does not mean that parts have |
| 1184 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1185 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1186 | * upon next call. |
| 1187 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1188 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1189 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1190 | * 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] | 1191 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1192 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1193 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1194 | unsigned int state, const char *ptr, const char *end, |
| 1195 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1197 | switch (state) { |
| 1198 | http_msg_rpver: |
| 1199 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1200 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1201 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1202 | |
| 1203 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1204 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1205 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1206 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1207 | state = HTTP_MSG_ERROR; |
| 1208 | break; |
| 1209 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1210 | http_msg_rpver_sp: |
| 1211 | case HTTP_MSG_RPVER_SP: |
| 1212 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1213 | msg->sl.st.c = ptr - msg_buf; |
| 1214 | goto http_msg_rpcode; |
| 1215 | } |
| 1216 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1217 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1218 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1219 | state = HTTP_MSG_ERROR; |
| 1220 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1221 | |
| 1222 | http_msg_rpcode: |
| 1223 | case HTTP_MSG_RPCODE: |
| 1224 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1225 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1226 | |
| 1227 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1228 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1229 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1230 | } |
| 1231 | |
| 1232 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1233 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1234 | http_msg_rsp_reason: |
| 1235 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1236 | msg->sl.st.r = ptr - msg_buf; |
| 1237 | msg->sl.st.r_l = 0; |
| 1238 | goto http_msg_rpline_eol; |
| 1239 | |
| 1240 | http_msg_rpcode_sp: |
| 1241 | case HTTP_MSG_RPCODE_SP: |
| 1242 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1243 | msg->sl.st.r = ptr - msg_buf; |
| 1244 | goto http_msg_rpreason; |
| 1245 | } |
| 1246 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1247 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1248 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1249 | goto http_msg_rsp_reason; |
| 1250 | |
| 1251 | http_msg_rpreason: |
| 1252 | case HTTP_MSG_RPREASON: |
| 1253 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1254 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1255 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1256 | http_msg_rpline_eol: |
| 1257 | /* We have seen the end of line. Note that we do not |
| 1258 | * necessarily have the \n yet, but at least we know that we |
| 1259 | * have EITHER \r OR \n, otherwise the response would not be |
| 1260 | * complete. We can then record the response length and return |
| 1261 | * to the caller which will be able to register it. |
| 1262 | */ |
| 1263 | msg->sl.st.l = ptr - msg->sol; |
| 1264 | return ptr; |
| 1265 | |
| 1266 | #ifdef DEBUG_FULL |
| 1267 | default: |
| 1268 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1269 | exit(1); |
| 1270 | #endif |
| 1271 | } |
| 1272 | |
| 1273 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1274 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1275 | if (ret_state) |
| 1276 | *ret_state = state; |
| 1277 | if (ret_ptr) |
| 1278 | *ret_ptr = (char *)ptr; |
| 1279 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | * This function parses a request line between <ptr> and <end>, starting with |
| 1285 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1286 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1287 | * will give undefined results. |
| 1288 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1289 | * and that msg->sol points to the beginning of the request. |
| 1290 | * If a complete line is found (which implies that at least one CR or LF is |
| 1291 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1292 | * returned indicating an incomplete line (which does not mean that parts have |
| 1293 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1294 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1295 | * upon next call. |
| 1296 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1297 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1298 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1299 | * 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] | 1300 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1301 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1302 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1303 | unsigned int state, const char *ptr, const char *end, |
| 1304 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1305 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1306 | switch (state) { |
| 1307 | http_msg_rqmeth: |
| 1308 | case HTTP_MSG_RQMETH: |
| 1309 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1310 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1311 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1312 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1313 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1314 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1315 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1318 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1319 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1320 | http_msg_req09_uri: |
| 1321 | msg->sl.rq.u = ptr - msg_buf; |
| 1322 | http_msg_req09_uri_e: |
| 1323 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1324 | http_msg_req09_ver: |
| 1325 | msg->sl.rq.v = ptr - msg_buf; |
| 1326 | msg->sl.rq.v_l = 0; |
| 1327 | goto http_msg_rqline_eol; |
| 1328 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1329 | state = HTTP_MSG_ERROR; |
| 1330 | break; |
| 1331 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1332 | http_msg_rqmeth_sp: |
| 1333 | case HTTP_MSG_RQMETH_SP: |
| 1334 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1335 | msg->sl.rq.u = ptr - msg_buf; |
| 1336 | goto http_msg_rquri; |
| 1337 | } |
| 1338 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1339 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1340 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1341 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1342 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1343 | http_msg_rquri: |
| 1344 | case HTTP_MSG_RQURI: |
| 1345 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1346 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1347 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1348 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1349 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1350 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1351 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1352 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1353 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1354 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1355 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1356 | http_msg_rquri_sp: |
| 1357 | case HTTP_MSG_RQURI_SP: |
| 1358 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1359 | msg->sl.rq.v = ptr - msg_buf; |
| 1360 | goto http_msg_rqver; |
| 1361 | } |
| 1362 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1363 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1364 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1365 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1366 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1367 | http_msg_rqver: |
| 1368 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1369 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1371 | |
| 1372 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1373 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1374 | http_msg_rqline_eol: |
| 1375 | /* We have seen the end of line. Note that we do not |
| 1376 | * necessarily have the \n yet, but at least we know that we |
| 1377 | * have EITHER \r OR \n, otherwise the request would not be |
| 1378 | * complete. We can then record the request length and return |
| 1379 | * to the caller which will be able to register it. |
| 1380 | */ |
| 1381 | msg->sl.rq.l = ptr - msg->sol; |
| 1382 | return ptr; |
| 1383 | } |
| 1384 | |
| 1385 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1386 | state = HTTP_MSG_ERROR; |
| 1387 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | #ifdef DEBUG_FULL |
| 1390 | default: |
| 1391 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1392 | exit(1); |
| 1393 | #endif |
| 1394 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1395 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1396 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1397 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | if (ret_state) |
| 1399 | *ret_state = state; |
| 1400 | if (ret_ptr) |
| 1401 | *ret_ptr = (char *)ptr; |
| 1402 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1404 | |
| 1405 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1406 | /* |
| 1407 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1408 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1409 | * when data are missing and recalled at the exact same location with no |
| 1410 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1411 | * 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] | 1412 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1413 | * the first state, so that none of the msg pointers has to be initialized |
| 1414 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1415 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1417 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1418 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1419 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1420 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1421 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | ptr = buf->lr; |
| 1423 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | if (unlikely(ptr >= end)) |
| 1426 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1427 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1429 | /* |
| 1430 | * First, states that are specific to the response only. |
| 1431 | * We check them first so that request and headers are |
| 1432 | * closer to each other (accessed more often). |
| 1433 | */ |
| 1434 | http_msg_rpbefore: |
| 1435 | case HTTP_MSG_RPBEFORE: |
| 1436 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1437 | /* we have a start of message, but we have to check |
| 1438 | * first if we need to remove some CRLF. We can only |
| 1439 | * do this when send_max=0. |
| 1440 | */ |
| 1441 | char *beg = buf->w + buf->send_max; |
| 1442 | if (beg >= buf->data + buf->size) |
| 1443 | beg -= buf->size; |
| 1444 | if (unlikely(ptr != beg)) { |
| 1445 | if (buf->send_max) |
| 1446 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1447 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1448 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1449 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1450 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1451 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1452 | hdr_idx_init(idx); |
| 1453 | state = HTTP_MSG_RPVER; |
| 1454 | goto http_msg_rpver; |
| 1455 | } |
| 1456 | |
| 1457 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1458 | goto http_msg_invalid; |
| 1459 | |
| 1460 | if (unlikely(*ptr == '\n')) |
| 1461 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1462 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1463 | /* stop here */ |
| 1464 | |
| 1465 | http_msg_rpbefore_cr: |
| 1466 | case HTTP_MSG_RPBEFORE_CR: |
| 1467 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1468 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1469 | /* stop here */ |
| 1470 | |
| 1471 | http_msg_rpver: |
| 1472 | case HTTP_MSG_RPVER: |
| 1473 | case HTTP_MSG_RPVER_SP: |
| 1474 | case HTTP_MSG_RPCODE: |
| 1475 | case HTTP_MSG_RPCODE_SP: |
| 1476 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1477 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1478 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1479 | if (unlikely(!ptr)) |
| 1480 | return; |
| 1481 | |
| 1482 | /* we have a full response and we know that we have either a CR |
| 1483 | * or an LF at <ptr>. |
| 1484 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1485 | //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] | 1486 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1487 | |
| 1488 | msg->sol = ptr; |
| 1489 | if (likely(*ptr == '\r')) |
| 1490 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1491 | goto http_msg_rpline_end; |
| 1492 | |
| 1493 | http_msg_rpline_end: |
| 1494 | case HTTP_MSG_RPLINE_END: |
| 1495 | /* msg->sol must point to the first of CR or LF. */ |
| 1496 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1497 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1498 | /* stop here */ |
| 1499 | |
| 1500 | /* |
| 1501 | * Second, states that are specific to the request only |
| 1502 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | http_msg_rqbefore: |
| 1504 | case HTTP_MSG_RQBEFORE: |
| 1505 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1506 | /* we have a start of message, but we have to check |
| 1507 | * first if we need to remove some CRLF. We can only |
| 1508 | * do this when send_max=0. |
| 1509 | */ |
| 1510 | char *beg = buf->w + buf->send_max; |
| 1511 | if (beg >= buf->data + buf->size) |
| 1512 | beg -= buf->size; |
| 1513 | if (likely(ptr != beg)) { |
| 1514 | if (buf->send_max) |
| 1515 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1516 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1517 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1519 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1520 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1521 | /* we will need this when keep-alive will be supported |
| 1522 | hdr_idx_init(idx); |
| 1523 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1524 | state = HTTP_MSG_RQMETH; |
| 1525 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1527 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1529 | goto http_msg_invalid; |
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 | if (unlikely(*ptr == '\n')) |
| 1532 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1533 | 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] | 1534 | /* stop here */ |
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_rqbefore_cr: |
| 1537 | case HTTP_MSG_RQBEFORE_CR: |
| 1538 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1539 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1540 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1541 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1542 | http_msg_rqmeth: |
| 1543 | case HTTP_MSG_RQMETH: |
| 1544 | case HTTP_MSG_RQMETH_SP: |
| 1545 | case HTTP_MSG_RQURI: |
| 1546 | case HTTP_MSG_RQURI_SP: |
| 1547 | case HTTP_MSG_RQVER: |
| 1548 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1549 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1550 | if (unlikely(!ptr)) |
| 1551 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1552 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | /* we have a full request and we know that we have either a CR |
| 1554 | * or an LF at <ptr>. |
| 1555 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1556 | //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] | 1557 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1558 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | msg->sol = ptr; |
| 1560 | if (likely(*ptr == '\r')) |
| 1561 | 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] | 1562 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | http_msg_rqline_end: |
| 1565 | case HTTP_MSG_RQLINE_END: |
| 1566 | /* check for HTTP/0.9 request : no version information available. |
| 1567 | * msg->sol must point to the first of CR or LF. |
| 1568 | */ |
| 1569 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1570 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1571 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1572 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1573 | 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] | 1574 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1575 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1576 | /* |
| 1577 | * Common states below |
| 1578 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | http_msg_hdr_first: |
| 1580 | case HTTP_MSG_HDR_FIRST: |
| 1581 | msg->sol = ptr; |
| 1582 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1583 | goto http_msg_hdr_name; |
| 1584 | } |
| 1585 | |
| 1586 | if (likely(*ptr == '\r')) |
| 1587 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1588 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1589 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | http_msg_hdr_name: |
| 1591 | case HTTP_MSG_HDR_NAME: |
| 1592 | /* assumes msg->sol points to the first char */ |
| 1593 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1594 | 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] | 1595 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | if (likely(*ptr == ':')) { |
| 1597 | msg->col = ptr - buf->data; |
| 1598 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1599 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1600 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1601 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1602 | goto http_msg_invalid; |
| 1603 | |
| 1604 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1605 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1606 | |
| 1607 | /* and we still accept this non-token character */ |
| 1608 | 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] | 1609 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | http_msg_hdr_l1_sp: |
| 1611 | case HTTP_MSG_HDR_L1_SP: |
| 1612 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1613 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1614 | 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] | 1615 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1616 | /* header value can be basically anything except CR/LF */ |
| 1617 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1618 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1619 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1620 | goto http_msg_hdr_val; |
| 1621 | } |
| 1622 | |
| 1623 | if (likely(*ptr == '\r')) |
| 1624 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1625 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1626 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1627 | http_msg_hdr_l1_lf: |
| 1628 | case HTTP_MSG_HDR_L1_LF: |
| 1629 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1630 | 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] | 1631 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | http_msg_hdr_l1_lws: |
| 1633 | case HTTP_MSG_HDR_L1_LWS: |
| 1634 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1635 | /* replace HT,CR,LF with spaces */ |
| 1636 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1637 | buf->data[msg->sov] = ' '; |
| 1638 | goto http_msg_hdr_l1_sp; |
| 1639 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1640 | /* we had a header consisting only in spaces ! */ |
| 1641 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | goto http_msg_complete_header; |
| 1643 | |
| 1644 | http_msg_hdr_val: |
| 1645 | case HTTP_MSG_HDR_VAL: |
| 1646 | /* assumes msg->sol points to the first char, msg->col to the |
| 1647 | * colon, and msg->sov points to the first character of the |
| 1648 | * value. |
| 1649 | */ |
| 1650 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1651 | 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] | 1652 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | msg->eol = ptr; |
| 1654 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1655 | * real header end in case it ends with lots of LWS, but is this |
| 1656 | * really needed ? |
| 1657 | */ |
| 1658 | if (likely(*ptr == '\r')) |
| 1659 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1660 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | http_msg_hdr_l2_lf: |
| 1663 | case HTTP_MSG_HDR_L2_LF: |
| 1664 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1665 | 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] | 1666 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1667 | http_msg_hdr_l2_lws: |
| 1668 | case HTTP_MSG_HDR_L2_LWS: |
| 1669 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1670 | /* LWS: replace HT,CR,LF with spaces */ |
| 1671 | for (; msg->eol < ptr; msg->eol++) |
| 1672 | *msg->eol = ' '; |
| 1673 | goto http_msg_hdr_val; |
| 1674 | } |
| 1675 | http_msg_complete_header: |
| 1676 | /* |
| 1677 | * It was a new header, so the last one is finished. |
| 1678 | * Assumes msg->sol points to the first char, msg->col to the |
| 1679 | * colon, msg->sov points to the first character of the value |
| 1680 | * and msg->eol to the first CR or LF so we know how the line |
| 1681 | * ends. We insert last header into the index. |
| 1682 | */ |
| 1683 | /* |
| 1684 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1685 | write(2, msg->sol, msg->eol-msg->sol); |
| 1686 | fprintf(stderr,"\n"); |
| 1687 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1688 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1690 | idx, idx->tail) < 0)) |
| 1691 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | msg->sol = ptr; |
| 1694 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1695 | goto http_msg_hdr_name; |
| 1696 | } |
| 1697 | |
| 1698 | if (likely(*ptr == '\r')) |
| 1699 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1700 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1701 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | http_msg_last_lf: |
| 1703 | case HTTP_MSG_LAST_LF: |
| 1704 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1705 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1706 | ptr++; |
| 1707 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1708 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1710 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1711 | return; |
| 1712 | #ifdef DEBUG_FULL |
| 1713 | default: |
| 1714 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1715 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1716 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1717 | } |
| 1718 | http_msg_ood: |
| 1719 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1720 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | buf->lr = ptr; |
| 1722 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1723 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | http_msg_invalid: |
| 1725 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1726 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1727 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1728 | return; |
| 1729 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1730 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1731 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1732 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1733 | * nothing is done and 1 is returned. |
| 1734 | */ |
| 1735 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1736 | { |
| 1737 | int delta; |
| 1738 | char *cur_end; |
| 1739 | |
| 1740 | if (msg->sl.rq.v_l != 0) |
| 1741 | return 1; |
| 1742 | |
| 1743 | msg->sol = req->data + msg->som; |
| 1744 | cur_end = msg->sol + msg->sl.rq.l; |
| 1745 | delta = 0; |
| 1746 | |
| 1747 | if (msg->sl.rq.u_l == 0) { |
| 1748 | /* if no URI was set, add "/" */ |
| 1749 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1750 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1751 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1752 | } |
| 1753 | /* add HTTP version */ |
| 1754 | 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] | 1755 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1756 | cur_end += delta; |
| 1757 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1758 | HTTP_MSG_RQMETH, |
| 1759 | msg->sol, cur_end + 1, |
| 1760 | NULL, NULL); |
| 1761 | if (unlikely(!cur_end)) |
| 1762 | return 0; |
| 1763 | |
| 1764 | /* we have a full HTTP/1.0 request now and we know that |
| 1765 | * we have either a CR or an LF at <ptr>. |
| 1766 | */ |
| 1767 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1768 | return 1; |
| 1769 | } |
| 1770 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1771 | /* Parse the Connection: headaer of an HTTP request, and set the transaction |
| 1772 | * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag |
| 1773 | * is also set so that we don't parse a second time. If some dangerous values |
| 1774 | * are encountered, we leave the status to indicate that the request might be |
| 1775 | * interpreted as keep-alive, but we also set the connection flags to indicate |
| 1776 | * that we WANT it to be a close, so that the header will be fixed. This |
| 1777 | * function should only be called when we know we're interested in checking |
| 1778 | * the request (not a CONNECT, and FE or BE mangles the header). |
| 1779 | */ |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1780 | void http_req_parse_connection_header(struct http_txn *txn) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1781 | { |
| 1782 | struct http_msg *msg = &txn->req; |
| 1783 | struct hdr_ctx ctx; |
| 1784 | int conn_cl, conn_ka; |
| 1785 | |
| 1786 | if (txn->flags & TX_CON_HDR_PARS) |
| 1787 | return; |
| 1788 | |
| 1789 | conn_cl = 0; |
| 1790 | conn_ka = 0; |
| 1791 | ctx.idx = 0; |
| 1792 | |
| 1793 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 1794 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 1795 | conn_cl = 1; |
| 1796 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 1797 | conn_ka = 1; |
| 1798 | } |
| 1799 | |
| 1800 | /* Determine if the client wishes keep-alive or close. |
| 1801 | * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections |
| 1802 | * are persistent unless "Connection: close" is explicitly specified. |
| 1803 | * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections. |
| 1804 | * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless |
| 1805 | * they explicitly specify "Connection: Keep-Alive", regardless of any |
| 1806 | * optional "Keep-Alive" header. |
| 1807 | * Note that if we find a request with both "Connection: close" and |
| 1808 | * "Connection: Keep-Alive", we indicate we want a close but don't have |
| 1809 | * it, so that it can be enforced later. |
| 1810 | */ |
| 1811 | |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1812 | if (conn_cl && conn_ka) { |
| 1813 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1814 | } |
| 1815 | else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1816 | if (conn_cl) { |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1817 | txn->flags |= TX_REQ_CONN_CLO; |
| 1818 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 1819 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1820 | } |
| 1821 | } else { /* HTTP/1.0 */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1822 | if (!conn_ka) { |
| 1823 | txn->flags |= TX_REQ_CONN_CLO; |
| 1824 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 1825 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1826 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1827 | } |
| 1828 | txn->flags |= TX_CON_HDR_PARS; |
| 1829 | } |
| 1830 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1831 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1832 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1833 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1834 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1835 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1836 | * 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] | 1837 | * 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] | 1838 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1839 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1840 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1841 | char *ptr = buf->lr; |
| 1842 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1843 | unsigned int chunk = 0; |
| 1844 | |
| 1845 | /* The chunk size is in the following form, though we are only |
| 1846 | * interested in the size and CRLF : |
| 1847 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1848 | */ |
| 1849 | while (1) { |
| 1850 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1851 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1852 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1853 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1854 | if (c < 0) /* not a hex digit anymore */ |
| 1855 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1856 | if (++ptr >= end) |
| 1857 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1858 | if (chunk & 0xF000000) /* overflow will occur */ |
| 1859 | return -1; |
| 1860 | chunk = (chunk << 4) + c; |
| 1861 | } |
| 1862 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1863 | /* empty size not allowed */ |
| 1864 | if (ptr == buf->lr) |
| 1865 | return -1; |
| 1866 | |
| 1867 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1868 | if (++ptr >= end) |
| 1869 | ptr = buf->data; |
| 1870 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1871 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1872 | } |
| 1873 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1874 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1875 | * for the end of chunk size. |
| 1876 | */ |
| 1877 | while (1) { |
| 1878 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1879 | /* we now have a CR or an LF at ptr */ |
| 1880 | if (likely(*ptr == '\r')) { |
| 1881 | if (++ptr >= end) |
| 1882 | ptr = buf->data; |
| 1883 | if (ptr == buf->r) |
| 1884 | return 0; |
| 1885 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1886 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1887 | if (*ptr != '\n') |
| 1888 | return -1; |
| 1889 | if (++ptr >= end) |
| 1890 | ptr = buf->data; |
| 1891 | /* done */ |
| 1892 | break; |
| 1893 | } |
| 1894 | else if (*ptr == ';') { |
| 1895 | /* chunk extension, ends at next CRLF */ |
| 1896 | if (++ptr >= end) |
| 1897 | ptr = buf->data; |
| 1898 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1899 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1900 | |
| 1901 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1902 | if (++ptr >= end) |
| 1903 | ptr = buf->data; |
| 1904 | if (ptr == buf->r) |
| 1905 | return 0; |
| 1906 | } |
| 1907 | /* we have a CRLF now, loop above */ |
| 1908 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1909 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1910 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1911 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1912 | } |
| 1913 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1914 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 1915 | * which may or may not be present. We save that into ->lr and |
| 1916 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1917 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1918 | msg->sov += ptr - buf->lr; |
| 1919 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1920 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1921 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1922 | return 1; |
| 1923 | } |
| 1924 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1925 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 1926 | * message <msg>. The first visited position is buf->lr. If the end of |
| 1927 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1928 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1929 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1930 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 1931 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 1932 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 1933 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1934 | * which implies that all non-trailers data have already been scheduled for |
| 1935 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1936 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1937 | * important to note that this function is designed to be able to parse wrapped |
| 1938 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1939 | */ |
| 1940 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 1941 | { |
| 1942 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 1943 | while (1) { |
| 1944 | char *p1 = NULL, *p2 = NULL; |
| 1945 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1946 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1947 | |
| 1948 | /* scan current line and stop at LF or CRLF */ |
| 1949 | while (1) { |
| 1950 | if (ptr == buf->r) |
| 1951 | return 0; |
| 1952 | |
| 1953 | if (*ptr == '\n') { |
| 1954 | if (!p1) |
| 1955 | p1 = ptr; |
| 1956 | p2 = ptr; |
| 1957 | break; |
| 1958 | } |
| 1959 | |
| 1960 | if (*ptr == '\r') { |
| 1961 | if (p1) |
| 1962 | return -1; |
| 1963 | p1 = ptr; |
| 1964 | } |
| 1965 | |
| 1966 | ptr++; |
| 1967 | if (ptr >= buf->data + buf->size) |
| 1968 | ptr = buf->data; |
| 1969 | } |
| 1970 | |
| 1971 | /* after LF; point to beginning of next line */ |
| 1972 | p2++; |
| 1973 | if (p2 >= buf->data + buf->size) |
| 1974 | p2 = buf->data; |
| 1975 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1976 | bytes = p2 - buf->lr; |
| 1977 | if (bytes < 0) |
| 1978 | bytes += buf->size; |
| 1979 | |
| 1980 | /* schedule this line for forwarding */ |
| 1981 | msg->sov += bytes; |
| 1982 | if (msg->sov >= buf->size) |
| 1983 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1984 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1985 | if (p1 == buf->lr) { |
| 1986 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1987 | * Everything was scheduled for forwarding, there's nothing |
| 1988 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1989 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1990 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1991 | msg->msg_state = HTTP_MSG_DONE; |
| 1992 | return 1; |
| 1993 | } |
| 1994 | /* OK, next line then */ |
| 1995 | buf->lr = p2; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2000 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2001 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2002 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2003 | * not enough data are available, the function does not change anything and |
| 2004 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2005 | * does not change anything. Note: this function is designed to parse wrapped |
| 2006 | * CRLF at the end of the buffer. |
| 2007 | */ |
| 2008 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2009 | { |
| 2010 | char *ptr; |
| 2011 | int bytes; |
| 2012 | |
| 2013 | /* NB: we'll check data availabilty at the end. It's not a |
| 2014 | * problem because whatever we match first will be checked |
| 2015 | * against the correct length. |
| 2016 | */ |
| 2017 | bytes = 1; |
| 2018 | ptr = buf->lr; |
| 2019 | if (*ptr == '\r') { |
| 2020 | bytes++; |
| 2021 | ptr++; |
| 2022 | if (ptr >= buf->data + buf->size) |
| 2023 | ptr = buf->data; |
| 2024 | } |
| 2025 | |
| 2026 | if (buf->l < bytes) |
| 2027 | return 0; |
| 2028 | |
| 2029 | if (*ptr != '\n') |
| 2030 | return -1; |
| 2031 | |
| 2032 | ptr++; |
| 2033 | if (ptr >= buf->data + buf->size) |
| 2034 | ptr = buf->data; |
| 2035 | buf->lr = ptr; |
| 2036 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2037 | msg->sov = ptr - buf->data; |
| 2038 | msg->som = msg->sov - bytes; |
| 2039 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2040 | return 1; |
| 2041 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2042 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2043 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2044 | { |
| 2045 | char *end = buf->data + buf->size; |
| 2046 | int off = buf->data + buf->size - buf->w; |
| 2047 | |
| 2048 | /* two possible cases : |
| 2049 | * - the buffer is in one contiguous block, we move it in-place |
| 2050 | * - the buffer is in two blocks, we move it via the trash |
| 2051 | */ |
| 2052 | if (buf->l) { |
| 2053 | int block1 = buf->l; |
| 2054 | int block2 = 0; |
| 2055 | if (buf->r <= buf->w) { |
| 2056 | /* non-contiguous block */ |
| 2057 | block1 = buf->data + buf->size - buf->w; |
| 2058 | block2 = buf->r - buf->data; |
| 2059 | } |
| 2060 | if (block2) |
| 2061 | memcpy(trash, buf->data, block2); |
| 2062 | memmove(buf->data, buf->w, block1); |
| 2063 | if (block2) |
| 2064 | memcpy(buf->data + block1, trash, block2); |
| 2065 | } |
| 2066 | |
| 2067 | /* adjust all known pointers */ |
| 2068 | buf->w = buf->data; |
| 2069 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2070 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2071 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2072 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2073 | |
| 2074 | /* adjust relative pointers */ |
| 2075 | msg->som = 0; |
| 2076 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2077 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2078 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2079 | |
| 2080 | msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size; |
| 2081 | msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size; |
| 2082 | |
| 2083 | if (msg->err_pos >= 0) { |
| 2084 | msg->err_pos += off; |
| 2085 | if (msg->err_pos >= buf->size) |
| 2086 | msg->err_pos -= buf->size; |
| 2087 | } |
| 2088 | |
| 2089 | buf->flags &= ~BF_FULL; |
| 2090 | if (buf->l >= buffer_max_len(buf)) |
| 2091 | buf->flags |= BF_FULL; |
| 2092 | } |
| 2093 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2094 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2095 | * processing can continue on next analysers, or zero if it either needs more |
| 2096 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2097 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2098 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2099 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2100 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2101 | 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] | 2102 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2103 | /* |
| 2104 | * We will parse the partial (or complete) lines. |
| 2105 | * We will check the request syntax, and also join multi-line |
| 2106 | * headers. An index of all the lines will be elaborated while |
| 2107 | * parsing. |
| 2108 | * |
| 2109 | * For the parsing, we use a 28 states FSM. |
| 2110 | * |
| 2111 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2112 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2113 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2114 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2115 | * req->lr = first non-visited byte |
| 2116 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2117 | * |
| 2118 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2119 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2120 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2121 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2122 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2123 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2124 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2125 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2126 | struct http_txn *txn = &s->txn; |
| 2127 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2128 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2129 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2130 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2131 | now_ms, __FUNCTION__, |
| 2132 | s, |
| 2133 | req, |
| 2134 | req->rex, req->wex, |
| 2135 | req->flags, |
| 2136 | req->l, |
| 2137 | req->analysers); |
| 2138 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2139 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2140 | s->srv_error = http_return_srv_error; |
| 2141 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2142 | /* There's a protected area at the end of the buffer for rewriting |
| 2143 | * purposes. We don't want to start to parse the request if the |
| 2144 | * protected area is affected, because we may have to move processed |
| 2145 | * data later, which is much more complicated. |
| 2146 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2147 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2148 | if ((txn->flags & TX_NOT_FIRST) && |
| 2149 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2150 | req->r < req->lr || |
| 2151 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2152 | if (req->send_max) { |
| 2153 | /* some data has still not left the buffer, wake us once that's done */ |
| 2154 | buffer_dont_connect(req); |
| 2155 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2156 | return 0; |
| 2157 | } |
| 2158 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2159 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2160 | } |
| 2161 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2162 | /* Note that we have the same problem with the response ; we |
| 2163 | * may want to send a redirect, error or anything which requires |
| 2164 | * some spare space. So we'll ensure that we have at least |
| 2165 | * maxrewrite bytes available in the response buffer before |
| 2166 | * processing that one. This will only affect pipelined |
| 2167 | * keep-alive requests. |
| 2168 | */ |
| 2169 | if ((txn->flags & TX_NOT_FIRST) && |
| 2170 | unlikely((s->rep->flags & BF_FULL) || |
| 2171 | s->rep->r < s->rep->lr || |
| 2172 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2173 | if (s->rep->send_max) { |
| 2174 | /* don't let a connection request be initiated */ |
| 2175 | buffer_dont_connect(req); |
| 2176 | return 0; |
| 2177 | } |
| 2178 | } |
| 2179 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2180 | if (likely(req->lr < req->r)) |
| 2181 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2184 | /* 1: we might have to print this header in debug mode */ |
| 2185 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2186 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2187 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2188 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2189 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2190 | sol = req->data + msg->som; |
| 2191 | eol = sol + msg->sl.rq.l; |
| 2192 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2193 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2194 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2195 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2196 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2197 | while (cur_idx) { |
| 2198 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2199 | debug_hdr("clihdr", s, sol, eol); |
| 2200 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2201 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2202 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2205 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2206 | /* |
| 2207 | * Now we quickly check if we have found a full valid request. |
| 2208 | * If not so, we check the FD and buffer states before leaving. |
| 2209 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2210 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2211 | * requests are checked first. |
| 2212 | * |
| 2213 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2214 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2215 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2216 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2217 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2218 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2219 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 2220 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2221 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2222 | /* 1: Since we are in header mode, if there's no space |
| 2223 | * left for headers, we won't be able to free more |
| 2224 | * later, so the session will never terminate. We |
| 2225 | * must terminate it now. |
| 2226 | */ |
| 2227 | if (unlikely(req->flags & BF_FULL)) { |
| 2228 | /* FIXME: check if URI is set and return Status |
| 2229 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2230 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2231 | goto return_bad_req; |
| 2232 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2233 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2234 | /* 2: have we encountered a read error ? */ |
| 2235 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2236 | if (txn->flags & TX_NOT_FIRST) |
| 2237 | goto failed_keep_alive; |
| 2238 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2239 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2240 | if (msg->err_pos >= 0) |
| 2241 | 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] | 2242 | msg->msg_state = HTTP_MSG_ERROR; |
| 2243 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2244 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2245 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2246 | if (s->listener->counters) |
| 2247 | s->listener->counters->failed_req++; |
| 2248 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2249 | if (!(s->flags & SN_ERR_MASK)) |
| 2250 | s->flags |= SN_ERR_CLICL; |
| 2251 | if (!(s->flags & SN_FINST_MASK)) |
| 2252 | s->flags |= SN_FINST_R; |
| 2253 | return 0; |
| 2254 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2255 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2256 | /* 3: has the read timeout expired ? */ |
| 2257 | 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] | 2258 | if (txn->flags & TX_NOT_FIRST) |
| 2259 | goto failed_keep_alive; |
| 2260 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2261 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2262 | if (msg->err_pos >= 0) |
| 2263 | 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] | 2264 | txn->status = 408; |
| 2265 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2266 | msg->msg_state = HTTP_MSG_ERROR; |
| 2267 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2268 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2269 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2270 | if (s->listener->counters) |
| 2271 | s->listener->counters->failed_req++; |
| 2272 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2273 | if (!(s->flags & SN_ERR_MASK)) |
| 2274 | s->flags |= SN_ERR_CLITO; |
| 2275 | if (!(s->flags & SN_FINST_MASK)) |
| 2276 | s->flags |= SN_FINST_R; |
| 2277 | return 0; |
| 2278 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2279 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2280 | /* 4: have we encountered a close ? */ |
| 2281 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2282 | if (txn->flags & TX_NOT_FIRST) |
| 2283 | goto failed_keep_alive; |
| 2284 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2285 | if (msg->err_pos >= 0) |
| 2286 | 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] | 2287 | txn->status = 400; |
| 2288 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2289 | msg->msg_state = HTTP_MSG_ERROR; |
| 2290 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2291 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2292 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2293 | if (s->listener->counters) |
| 2294 | s->listener->counters->failed_req++; |
| 2295 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2296 | if (!(s->flags & SN_ERR_MASK)) |
| 2297 | s->flags |= SN_ERR_CLICL; |
| 2298 | if (!(s->flags & SN_FINST_MASK)) |
| 2299 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2300 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2301 | } |
| 2302 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2303 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2304 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2305 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2306 | /* just set the request timeout once at the beginning of the request */ |
| 2307 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 2308 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2309 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2310 | /* we're not ready yet */ |
| 2311 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2312 | |
| 2313 | failed_keep_alive: |
| 2314 | /* Here we process low-level errors for keep-alive requests. In |
| 2315 | * short, if the request is not the first one and it experiences |
| 2316 | * a timeout, read error or shutdown, we just silently close so |
| 2317 | * that the client can try again. |
| 2318 | */ |
| 2319 | txn->status = 0; |
| 2320 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2321 | req->analysers = 0; |
| 2322 | s->logs.logwait = 0; |
| 2323 | stream_int_cond_close(req->prod, NULL); |
| 2324 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2325 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2326 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2327 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2328 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2329 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2330 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2331 | * points to the CRLF of the request line. req->lr points to the first |
| 2332 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2333 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2334 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2335 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2336 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2337 | /* Maybe we found in invalid header name while we were configured not |
| 2338 | * to block on that, so we have to capture it now. |
| 2339 | */ |
| 2340 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2341 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2342 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2343 | /* ensure we keep this pointer to the beginning of the message */ |
| 2344 | msg->sol = req->data + msg->som; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2345 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | /* |
| 2347 | * 1: identify the method |
| 2348 | */ |
| 2349 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 2350 | |
| 2351 | /* we can make use of server redirect on GET and HEAD */ |
| 2352 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2353 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2354 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2355 | /* |
| 2356 | * 2: check if the URI matches the monitor_uri. |
| 2357 | * We have to do this for every request which gets in, because |
| 2358 | * the monitor-uri is defined by the frontend. |
| 2359 | */ |
| 2360 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2361 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2362 | !memcmp(&req->data[msg->sl.rq.u], |
| 2363 | s->fe->monitor_uri, |
| 2364 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2365 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2366 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2367 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2368 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2369 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2370 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2371 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2372 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2373 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2374 | 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] | 2375 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2376 | ret = acl_pass(ret); |
| 2377 | if (cond->pol == ACL_COND_UNLESS) |
| 2378 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2379 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2380 | if (ret) { |
| 2381 | /* we fail this request, let's return 503 service unavail */ |
| 2382 | txn->status = 503; |
| 2383 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2384 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2385 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2386 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2387 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2388 | /* nothing to fail, let's reply normaly */ |
| 2389 | txn->status = 200; |
| 2390 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2391 | goto return_prx_cond; |
| 2392 | } |
| 2393 | |
| 2394 | /* |
| 2395 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2396 | * Note: we cannot log anymore if the request has been |
| 2397 | * classified as invalid. |
| 2398 | */ |
| 2399 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2400 | /* we have a complete HTTP request that we must log */ |
| 2401 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2402 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2403 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2404 | if (urilen >= REQURI_LEN) |
| 2405 | urilen = REQURI_LEN - 1; |
| 2406 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2407 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2408 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2409 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2410 | s->do_log(s); |
| 2411 | } else { |
| 2412 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2413 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2414 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2415 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2416 | /* 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] | 2417 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2418 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2419 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2420 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2421 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 2422 | ((req->data[msg->sl.rq.v + 5] > '1') || |
| 2423 | ((req->data[msg->sl.rq.v + 5] == '1') && |
| 2424 | (req->data[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2425 | txn->flags |= TX_REQ_VER_11; |
| 2426 | |
| 2427 | /* "connection" has not been parsed yet */ |
| 2428 | txn->flags &= ~TX_CON_HDR_PARS; |
| 2429 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2430 | /* transfer length unknown*/ |
| 2431 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2432 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2433 | /* 5: we may need to capture headers */ |
| 2434 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
| 2435 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
| 2436 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2437 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2438 | /* 6: determine the transfer-length. |
| 2439 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2440 | * the presence of a message-body in a REQUEST and its transfer length |
| 2441 | * must be determined that way (in order of precedence) : |
| 2442 | * 1. The presence of a message-body in a request is signaled by the |
| 2443 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2444 | * in the request's header fields. When a request message contains |
| 2445 | * both a message-body of non-zero length and a method that does |
| 2446 | * not define any semantics for that request message-body, then an |
| 2447 | * origin server SHOULD either ignore the message-body or respond |
| 2448 | * with an appropriate error message (e.g., 413). A proxy or |
| 2449 | * gateway, when presented the same request, SHOULD either forward |
| 2450 | * the request inbound with the message- body or ignore the |
| 2451 | * message-body when determining a response. |
| 2452 | * |
| 2453 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2454 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2455 | * transfer-length is defined by the use of this transfer-coding. |
| 2456 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2457 | * transfer-coding is not present, the transfer-length is defined |
| 2458 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2459 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2460 | * 3. If a Content-Length header field is present, its decimal value in |
| 2461 | * OCTETs represents both the entity-length and the transfer-length. |
| 2462 | * If a message is received with both a Transfer-Encoding header |
| 2463 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2464 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2465 | * 4. By the server closing the connection. (Closing the connection |
| 2466 | * cannot be used to indicate the end of a request body, since that |
| 2467 | * would leave no possibility for the server to send back a response.) |
| 2468 | * |
| 2469 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2470 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2471 | * it is terminated by closing the connection. When the "chunked" |
| 2472 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2473 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2474 | */ |
| 2475 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2476 | /* CONNECT sets a tunnel and ignores everything else */ |
| 2477 | if (txn->meth == HTTP_METH_CONNECT) |
| 2478 | goto skip_xfer_len; |
| 2479 | |
| 2480 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2481 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2482 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2483 | while ((txn->flags & TX_REQ_VER_11) && |
| 2484 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2485 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2486 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2487 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2488 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2489 | use_close_only = 1; |
| 2490 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2491 | break; |
| 2492 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2493 | } |
| 2494 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2495 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2496 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2497 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2498 | signed long long cl; |
| 2499 | |
| 2500 | if (!ctx.vlen) |
| 2501 | goto return_bad_req; |
| 2502 | |
| 2503 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2504 | goto return_bad_req; /* parse failure */ |
| 2505 | |
| 2506 | if (cl < 0) |
| 2507 | goto return_bad_req; |
| 2508 | |
| 2509 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2510 | goto return_bad_req; /* already specified, was different */ |
| 2511 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2512 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2513 | msg->hdr_content_len = cl; |
| 2514 | } |
| 2515 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2516 | /* bodyless requests have a known length */ |
| 2517 | if (!use_close_only) |
| 2518 | txn->flags |= TX_REQ_XFER_LEN; |
| 2519 | |
| 2520 | skip_xfer_len: |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2521 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2522 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2523 | req->analyse_exp = TICK_ETERNITY; |
| 2524 | return 1; |
| 2525 | |
| 2526 | return_bad_req: |
| 2527 | /* We centralize bad requests processing here */ |
| 2528 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2529 | /* we detected a parsing error. We want to archive this request |
| 2530 | * in the dedicated proxy area for later troubleshooting. |
| 2531 | */ |
| 2532 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2533 | } |
| 2534 | |
| 2535 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2536 | txn->status = 400; |
| 2537 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2538 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2539 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2540 | if (s->listener->counters) |
| 2541 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2542 | |
| 2543 | return_prx_cond: |
| 2544 | if (!(s->flags & SN_ERR_MASK)) |
| 2545 | s->flags |= SN_ERR_PRXCOND; |
| 2546 | if (!(s->flags & SN_FINST_MASK)) |
| 2547 | s->flags |= SN_FINST_R; |
| 2548 | |
| 2549 | req->analysers = 0; |
| 2550 | req->analyse_exp = TICK_ETERNITY; |
| 2551 | return 0; |
| 2552 | } |
| 2553 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2554 | /* This stream analyser runs all HTTP request processing which is common to |
| 2555 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2556 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2557 | * 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] | 2558 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2559 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2560 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2561 | 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] | 2562 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2563 | struct http_txn *txn = &s->txn; |
| 2564 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2565 | struct acl_cond *cond; |
| 2566 | struct redirect_rule *rule; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2567 | struct wordlist *wl; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2568 | int cur_idx; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2569 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2570 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2571 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2572 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2573 | return 0; |
| 2574 | } |
| 2575 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2576 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2577 | req->analyse_exp = TICK_ETERNITY; |
| 2578 | |
| 2579 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2580 | now_ms, __FUNCTION__, |
| 2581 | s, |
| 2582 | req, |
| 2583 | req->rex, req->wex, |
| 2584 | req->flags, |
| 2585 | req->l, |
| 2586 | req->analysers); |
| 2587 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2588 | /* first check whether we have some ACLs set to block this request */ |
| 2589 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2590 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2591 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2592 | ret = acl_pass(ret); |
| 2593 | if (cond->pol == ACL_COND_UNLESS) |
| 2594 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2595 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2596 | if (ret) { |
| 2597 | txn->status = 403; |
| 2598 | /* let's log the request time */ |
| 2599 | s->logs.tv_request = now; |
| 2600 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2601 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2602 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2603 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2605 | /* try headers filters */ |
| 2606 | if (px->req_exp != NULL) { |
| 2607 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2608 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2609 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2610 | /* has the request been denied ? */ |
| 2611 | if (txn->flags & TX_CLDENY) { |
| 2612 | /* no need to go further */ |
| 2613 | txn->status = 403; |
| 2614 | /* let's log the request time */ |
| 2615 | s->logs.tv_request = now; |
| 2616 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2617 | goto return_prx_cond; |
| 2618 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2619 | |
| 2620 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2621 | * which may be the same as the connect timeout if unspecified. |
| 2622 | * If unset, then set it to zero because we really want it to |
| 2623 | * eventually expire. We build the tarpit as an analyser. |
| 2624 | */ |
| 2625 | if (txn->flags & TX_CLTARPIT) { |
| 2626 | buffer_erase(s->req); |
| 2627 | /* wipe the request out so that we can drop the connection early |
| 2628 | * if the client closes first. |
| 2629 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2630 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2631 | req->analysers = 0; /* remove switching rules etc... */ |
| 2632 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2633 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2634 | if (!req->analyse_exp) |
| 2635 | req->analyse_exp = tick_add(now_ms, 0); |
| 2636 | return 1; |
| 2637 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2638 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2639 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2640 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2641 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2642 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2643 | * in which headers are mangled. However, if another mode is set, it will |
| 2644 | * affect it (eg: server-close/keep-alive + httpclose = close). |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2645 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2646 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2647 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2648 | ((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] | 2649 | int tmp = TX_CON_WANT_TUN; |
| 2650 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2651 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2652 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2653 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2654 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2655 | tmp = TX_CON_WANT_CLO; |
| 2656 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2657 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2658 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2659 | |
Willy Tarreau | 8db1c17 | 2010-01-05 23:12:12 +0100 | [diff] [blame] | 2660 | if (!(txn->flags & TX_CON_HDR_PARS)) |
| 2661 | http_req_parse_connection_header(txn); |
| 2662 | |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2663 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2664 | if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) |
| 2665 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2666 | if (!(txn->flags & TX_REQ_XFER_LEN)) |
| 2667 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2668 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | /* We're really certain of the connection mode (tunnel, close, keep-alive) |
| 2672 | * once we know the backend, because the tunnel mode can be implied by the |
| 2673 | * lack of any close/keepalive options in both the FE and the BE. Since |
| 2674 | * this information can evolve with time, we proceed by trying to make the |
| 2675 | * header status match the desired status. For this, we'll have to adjust |
| 2676 | * the "Connection" header. The test for persistent connections has already |
| 2677 | * been performed, so we only enter here if there is a risk the connection |
| 2678 | * is considered as persistent and we want it to be closed on the server |
| 2679 | * side. It would be nice if we could enter this place only when a |
| 2680 | * Connection header exists. Note that a CONNECT method will not enter |
| 2681 | * here. |
| 2682 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2683 | if (!(txn->flags & TX_REQ_CONN_CLO) && |
| 2684 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 2685 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2686 | char *cur_ptr, *cur_end, *cur_next; |
| 2687 | int old_idx, delta, val; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2688 | int must_delete; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2689 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2690 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2691 | must_delete = !(txn->flags & TX_REQ_VER_11); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2692 | 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] | 2693 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2694 | 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] | 2695 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2696 | cur_ptr = cur_next; |
| 2697 | cur_end = cur_ptr + cur_hdr->len; |
| 2698 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2699 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2700 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2701 | if (!val) |
| 2702 | continue; |
| 2703 | |
| 2704 | /* 3 possibilities : |
| 2705 | * - we have already set "Connection: close" or we're in |
| 2706 | * HTTP/1.0, so we remove this line. |
| 2707 | * - we have not yet set "Connection: close", but this line |
| 2708 | * indicates close. We leave it untouched and set the flag. |
| 2709 | * - we have not yet set "Connection: close", and this line |
| 2710 | * indicates non-close. We replace it and set the flag. |
| 2711 | */ |
| 2712 | if (must_delete) { |
| 2713 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 2714 | http_msg_move_end(&txn->req, delta); |
| 2715 | cur_next += delta; |
| 2716 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2717 | txn->hdr_idx.used--; |
| 2718 | cur_hdr->len = 0; |
| 2719 | txn->flags |= TX_REQ_CONN_CLO; |
| 2720 | } else { |
| 2721 | if (cur_end - cur_ptr - val != 5 || |
| 2722 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2723 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2724 | "close", 5); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2725 | cur_next += delta; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2726 | cur_hdr->len += delta; |
| 2727 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2728 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2729 | txn->flags |= TX_REQ_CONN_CLO; |
| 2730 | must_delete = 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2731 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2732 | } /* for loop */ |
| 2733 | } /* if must close keep-alive */ |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2734 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2735 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2736 | list_for_each_entry(wl, &px->req_add, list) { |
| 2737 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2738 | goto return_bad_req; |
| 2739 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2740 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2741 | /* check if stats URI was requested, and if an auth is needed */ |
| 2742 | if (px->uri_auth != NULL && |
| 2743 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2744 | /* we have to check the URI and auth for this request. |
| 2745 | * FIXME!!! that one is rather dangerous, we want to |
| 2746 | * make it follow standard rules (eg: clear req->analysers). |
| 2747 | */ |
| 2748 | if (stats_check_uri_auth(s, px)) { |
| 2749 | req->analysers = 0; |
| 2750 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2751 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2752 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2753 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2754 | /* check whether we have some ACLs set to redirect this request */ |
| 2755 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2756 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2757 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2758 | if (rule->cond) { |
| 2759 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 2760 | ret = acl_pass(ret); |
| 2761 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2762 | ret = !ret; |
| 2763 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2764 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2765 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2766 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2767 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2768 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2769 | /* build redirect message */ |
| 2770 | switch(rule->code) { |
| 2771 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2772 | msg_fmt = HTTP_303; |
| 2773 | break; |
| 2774 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2775 | msg_fmt = HTTP_301; |
| 2776 | break; |
| 2777 | case 302: |
| 2778 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2779 | msg_fmt = HTTP_302; |
| 2780 | break; |
| 2781 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2782 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2783 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2784 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2785 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2786 | switch(rule->type) { |
| 2787 | case REDIRECT_TYPE_PREFIX: { |
| 2788 | const char *path; |
| 2789 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2790 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2791 | path = http_get_path(txn); |
| 2792 | /* build message using path */ |
| 2793 | if (path) { |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 2794 | 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] | 2795 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2796 | int qs = 0; |
| 2797 | while (qs < pathlen) { |
| 2798 | if (path[qs] == '?') { |
| 2799 | pathlen = qs; |
| 2800 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2801 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2802 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2803 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2804 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2805 | } else { |
| 2806 | path = "/"; |
| 2807 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2808 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2809 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2810 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2811 | goto return_bad_req; |
| 2812 | |
| 2813 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2814 | * add anything, otherwise it makes it hard for the user to |
| 2815 | * configure a self-redirection. |
| 2816 | */ |
| 2817 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2818 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2819 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2820 | } |
| 2821 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2822 | /* add path */ |
| 2823 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2824 | rdr.len += pathlen; |
| 2825 | break; |
| 2826 | } |
| 2827 | case REDIRECT_TYPE_LOCATION: |
| 2828 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2829 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2830 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2831 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2832 | /* add location */ |
| 2833 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2834 | rdr.len += rule->rdr_len; |
| 2835 | break; |
| 2836 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2837 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2838 | if (rule->cookie_len) { |
| 2839 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2840 | rdr.len += 14; |
| 2841 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2842 | rdr.len += rule->cookie_len; |
| 2843 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2844 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2845 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2847 | /* add end of headers and the keep-alive/close status. |
| 2848 | * We may choose to set keep-alive if the Location begins |
| 2849 | * with a slash, because the client will come back to the |
| 2850 | * same server. |
| 2851 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2852 | txn->status = rule->code; |
| 2853 | /* let's log the request time */ |
| 2854 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2855 | |
| 2856 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 2857 | (txn->flags & TX_REQ_XFER_LEN) && |
| 2858 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 2859 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2860 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2861 | /* keep-alive possible */ |
| 2862 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28); |
| 2863 | rdr.len += 28; |
| 2864 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 2865 | /* "eat" the request */ |
| 2866 | buffer_ignore(req, msg->sov - msg->som); |
| 2867 | msg->som = msg->sov; |
| 2868 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 2869 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 2870 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 2871 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2872 | break; |
| 2873 | } else { |
| 2874 | /* keep-alive not possible */ |
| 2875 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 2876 | rdr.len += 23; |
Willy Tarreau | ea65e68 | 2010-01-08 00:26:50 +0100 | [diff] [blame] | 2877 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 2878 | stream_int_cond_close(req->prod, NULL); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2879 | goto return_prx_cond; |
| 2880 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2881 | } |
| 2882 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2883 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2884 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 2885 | * If this happens, then the data will not come immediately, so we must |
| 2886 | * send all what we have without waiting. Note that due to the small gain |
| 2887 | * in waiting for the body of the request, it's easier to simply put the |
| 2888 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 2889 | * itself once used. |
| 2890 | */ |
| 2891 | req->flags |= BF_SEND_DONTWAIT; |
| 2892 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2893 | /* that's OK for us now, let's move on to next analysers */ |
| 2894 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2895 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2896 | return_bad_req: |
| 2897 | /* We centralize bad requests processing here */ |
| 2898 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2899 | /* we detected a parsing error. We want to archive this request |
| 2900 | * in the dedicated proxy area for later troubleshooting. |
| 2901 | */ |
| 2902 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2903 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2904 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2905 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2906 | txn->status = 400; |
| 2907 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2908 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2909 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2910 | if (s->listener->counters) |
| 2911 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2912 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2913 | return_prx_cond: |
| 2914 | if (!(s->flags & SN_ERR_MASK)) |
| 2915 | s->flags |= SN_ERR_PRXCOND; |
| 2916 | if (!(s->flags & SN_FINST_MASK)) |
| 2917 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2918 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2919 | req->analysers = 0; |
| 2920 | req->analyse_exp = TICK_ETERNITY; |
| 2921 | return 0; |
| 2922 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2923 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2924 | /* This function performs all the processing enabled for the current request. |
| 2925 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2926 | * needs more data, encounters an error, or wants to immediately abort the |
| 2927 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 2928 | */ |
| 2929 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 2930 | { |
| 2931 | struct http_txn *txn = &s->txn; |
| 2932 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2933 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2934 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2935 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2936 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2937 | return 0; |
| 2938 | } |
| 2939 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2940 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2941 | now_ms, __FUNCTION__, |
| 2942 | s, |
| 2943 | req, |
| 2944 | req->rex, req->wex, |
| 2945 | req->flags, |
| 2946 | req->l, |
| 2947 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2948 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2949 | /* |
| 2950 | * Right now, we know that we have processed the entire headers |
| 2951 | * and that unwanted requests have been filtered out. We can do |
| 2952 | * whatever we want with the remaining request. Also, now we |
| 2953 | * may have separate values for ->fe, ->be. |
| 2954 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2955 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2956 | /* |
| 2957 | * If HTTP PROXY is set we simply get remote server address |
| 2958 | * parsing incoming request. |
| 2959 | */ |
| 2960 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
| 2961 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
| 2962 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2963 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2964 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2965 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2966 | * Note that doing so might move headers in the request, but |
| 2967 | * the fields will stay coherent and the URI will not move. |
| 2968 | * This should only be performed in the backend. |
| 2969 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2970 | 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] | 2971 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 2972 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2973 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2974 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2975 | * 8: the appsession cookie was looked up very early in 1.2, |
| 2976 | * so let's do the same now. |
| 2977 | */ |
| 2978 | |
| 2979 | /* It needs to look into the URI */ |
| 2980 | if ((s->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 2981 | 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] | 2982 | } |
| 2983 | |
| 2984 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2985 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2986 | * asks for it. |
| 2987 | */ |
| 2988 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 2989 | if (s->cli_addr.ss_family == AF_INET) { |
| 2990 | /* Add an X-Forwarded-For header unless the source IP is |
| 2991 | * in the 'except' network range. |
| 2992 | */ |
| 2993 | if ((!s->fe->except_mask.s_addr || |
| 2994 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 2995 | != s->fe->except_net.s_addr) && |
| 2996 | (!s->be->except_mask.s_addr || |
| 2997 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 2998 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2999 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3000 | unsigned char *pn; |
| 3001 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3002 | |
| 3003 | /* Note: we rely on the backend to get the header name to be used for |
| 3004 | * x-forwarded-for, because the header is really meant for the backends. |
| 3005 | * However, if the backend did not specify any option, we have to rely |
| 3006 | * on the frontend's header name. |
| 3007 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3008 | if (s->be->fwdfor_hdr_len) { |
| 3009 | len = s->be->fwdfor_hdr_len; |
| 3010 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3011 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3012 | len = s->fe->fwdfor_hdr_len; |
| 3013 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3014 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3015 | 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] | 3016 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3017 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3018 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3019 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3020 | } |
| 3021 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3022 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3023 | /* FIXME: for the sake of completeness, we should also support |
| 3024 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3025 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3026 | int len; |
| 3027 | char pn[INET6_ADDRSTRLEN]; |
| 3028 | inet_ntop(AF_INET6, |
| 3029 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3030 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3031 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3032 | /* Note: we rely on the backend to get the header name to be used for |
| 3033 | * x-forwarded-for, because the header is really meant for the backends. |
| 3034 | * However, if the backend did not specify any option, we have to rely |
| 3035 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3036 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3037 | if (s->be->fwdfor_hdr_len) { |
| 3038 | len = s->be->fwdfor_hdr_len; |
| 3039 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3040 | } else { |
| 3041 | len = s->fe->fwdfor_hdr_len; |
| 3042 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3043 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3044 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3045 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3046 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3047 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3048 | goto return_bad_req; |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3053 | * 10: add X-Original-To if either the frontend or the backend |
| 3054 | * asks for it. |
| 3055 | */ |
| 3056 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3057 | |
| 3058 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3059 | if (s->cli_addr.ss_family == AF_INET) { |
| 3060 | /* Add an X-Original-To header unless the destination IP is |
| 3061 | * in the 'except' network range. |
| 3062 | */ |
| 3063 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3064 | get_frt_addr(s); |
| 3065 | |
| 3066 | if ((!s->fe->except_mask_to.s_addr || |
| 3067 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3068 | != s->fe->except_to.s_addr) && |
| 3069 | (!s->be->except_mask_to.s_addr || |
| 3070 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3071 | != s->be->except_to.s_addr)) { |
| 3072 | int len; |
| 3073 | unsigned char *pn; |
| 3074 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3075 | |
| 3076 | /* Note: we rely on the backend to get the header name to be used for |
| 3077 | * x-original-to, because the header is really meant for the backends. |
| 3078 | * However, if the backend did not specify any option, we have to rely |
| 3079 | * on the frontend's header name. |
| 3080 | */ |
| 3081 | if (s->be->orgto_hdr_len) { |
| 3082 | len = s->be->orgto_hdr_len; |
| 3083 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3084 | } else { |
| 3085 | len = s->fe->orgto_hdr_len; |
| 3086 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3087 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3088 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3089 | |
| 3090 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3091 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3092 | goto return_bad_req; |
| 3093 | } |
| 3094 | } |
| 3095 | } |
| 3096 | |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3097 | /* 11: add "Connection: close" if needed and not yet set. */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3098 | if (!(txn->flags & TX_REQ_CONN_CLO) && |
| 3099 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3100 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3101 | if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3102 | "Connection: close", 17) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3103 | goto return_bad_req; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3104 | txn->flags |= TX_REQ_CONN_CLO; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3105 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3106 | |
| 3107 | /* If we have no server assigned yet and we're balancing on url_param |
| 3108 | * with a POST request, we may be interested in checking the body for |
| 3109 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3110 | */ |
| 3111 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3112 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3113 | s->be->url_param_post_limit != 0 && |
| 3114 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 3115 | 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] | 3116 | buffer_dont_connect(req); |
| 3117 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3118 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3119 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3120 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3121 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3122 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3123 | /************************************************************* |
| 3124 | * OK, that's finished for the headers. We have done what we * |
| 3125 | * could. Let's switch to the DATA state. * |
| 3126 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3127 | req->analyse_exp = TICK_ETERNITY; |
| 3128 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3129 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3130 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3131 | /* OK let's go on with the BODY now */ |
| 3132 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3133 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3134 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3135 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3136 | /* we detected a parsing error. We want to archive this request |
| 3137 | * in the dedicated proxy area for later troubleshooting. |
| 3138 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3139 | 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] | 3140 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3141 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3142 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3143 | txn->status = 400; |
| 3144 | req->analysers = 0; |
| 3145 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3146 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3147 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3148 | if (s->listener->counters) |
| 3149 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3150 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3151 | if (!(s->flags & SN_ERR_MASK)) |
| 3152 | s->flags |= SN_ERR_PRXCOND; |
| 3153 | if (!(s->flags & SN_FINST_MASK)) |
| 3154 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3155 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3156 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3157 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3158 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3159 | * returns zero, at the beginning because it prevents any other processing |
| 3160 | * from occurring, and at the end because it terminates the request. |
| 3161 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3162 | 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] | 3163 | { |
| 3164 | struct http_txn *txn = &s->txn; |
| 3165 | |
| 3166 | /* This connection is being tarpitted. The CLIENT side has |
| 3167 | * already set the connect expiration date to the right |
| 3168 | * timeout. We just have to check that the client is still |
| 3169 | * there and that the timeout has not expired. |
| 3170 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3171 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3172 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3173 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3174 | return 0; |
| 3175 | |
| 3176 | /* We will set the queue timer to the time spent, just for |
| 3177 | * logging purposes. We fake a 500 server error, so that the |
| 3178 | * attacker will not suspect his connection has been tarpitted. |
| 3179 | * It will not cause trouble to the logs because we can exclude |
| 3180 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3181 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3182 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3183 | |
| 3184 | txn->status = 500; |
| 3185 | if (req->flags != BF_READ_ERROR) |
| 3186 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3187 | |
| 3188 | req->analysers = 0; |
| 3189 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3190 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3191 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3192 | if (s->listener->counters) |
| 3193 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3194 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3195 | if (!(s->flags & SN_ERR_MASK)) |
| 3196 | s->flags |= SN_ERR_PRXCOND; |
| 3197 | if (!(s->flags & SN_FINST_MASK)) |
| 3198 | s->flags |= SN_FINST_T; |
| 3199 | return 0; |
| 3200 | } |
| 3201 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3202 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3203 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3204 | * must only be called after the standard HTTP request processing has occurred, |
| 3205 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3206 | * read more data, or 1 once it has completed its analysis. |
| 3207 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3208 | 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] | 3209 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3210 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3211 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3212 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3213 | |
| 3214 | /* We have to parse the HTTP request body to find any required data. |
| 3215 | * "balance url_param check_post" should have been the only way to get |
| 3216 | * into this. We were brought here after HTTP header analysis, so all |
| 3217 | * related structures are ready. |
| 3218 | */ |
| 3219 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3220 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3221 | goto missing_data; |
| 3222 | |
| 3223 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3224 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3225 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3226 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3227 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3228 | struct hdr_ctx ctx; |
| 3229 | ctx.idx = 0; |
| 3230 | /* Expect is allowed in 1.1, look for it */ |
| 3231 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3232 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3233 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3234 | } |
| 3235 | } |
| 3236 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3237 | } |
| 3238 | |
| 3239 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3240 | /* we have msg->col and msg->sov which both point to the first |
| 3241 | * byte of message body. msg->som still points to the beginning |
| 3242 | * of the message. We must save the body in req->lr because it |
| 3243 | * survives buffer re-alignments. |
| 3244 | */ |
| 3245 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3246 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3247 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3248 | else |
| 3249 | msg->msg_state = HTTP_MSG_DATA; |
| 3250 | } |
| 3251 | |
| 3252 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3253 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3254 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3255 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3256 | */ |
| 3257 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3258 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3259 | if (!ret) |
| 3260 | goto missing_data; |
| 3261 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3262 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3263 | } |
| 3264 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3265 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3266 | * We have the first non-header byte in msg->col, which is either the |
| 3267 | * beginning of the chunk size or of the data. The first data byte is in |
| 3268 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3269 | * 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] | 3270 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3271 | |
| 3272 | if (msg->hdr_content_len < limit) |
| 3273 | limit = msg->hdr_content_len; |
| 3274 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3275 | 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] | 3276 | goto http_end; |
| 3277 | |
| 3278 | missing_data: |
| 3279 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3280 | if (req->flags & BF_FULL) |
| 3281 | goto return_bad_req; |
| 3282 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3283 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3284 | txn->status = 408; |
| 3285 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3286 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3287 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3288 | |
| 3289 | /* we get here if we need to wait for more data */ |
| 3290 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3291 | /* Not enough data. We'll re-use the http-request |
| 3292 | * timeout here. Ideally, we should set the timeout |
| 3293 | * relative to the accept() date. We just set the |
| 3294 | * request timeout once at the beginning of the |
| 3295 | * request. |
| 3296 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3297 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3298 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3299 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3300 | return 0; |
| 3301 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3302 | |
| 3303 | http_end: |
| 3304 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3305 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3306 | req->analysers &= ~an_bit; |
| 3307 | req->analyse_exp = TICK_ETERNITY; |
| 3308 | return 1; |
| 3309 | |
| 3310 | return_bad_req: /* let's centralize all bad requests */ |
| 3311 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3312 | txn->status = 400; |
| 3313 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3314 | |
| 3315 | return_err_msg: |
| 3316 | req->analysers = 0; |
| 3317 | s->fe->counters.failed_req++; |
| 3318 | if (s->listener->counters) |
| 3319 | s->listener->counters->failed_req++; |
| 3320 | |
| 3321 | if (!(s->flags & SN_ERR_MASK)) |
| 3322 | s->flags |= SN_ERR_PRXCOND; |
| 3323 | if (!(s->flags & SN_FINST_MASK)) |
| 3324 | s->flags |= SN_FINST_R; |
| 3325 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3326 | } |
| 3327 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3328 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3329 | * right now but it works. |
| 3330 | */ |
| 3331 | void http_end_txn_clean_session(struct session *s) |
| 3332 | { |
| 3333 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3334 | * server's connections. We need a safer way to reinitialize buffer |
| 3335 | * flags. We also need a more accurate method for computing per-request |
| 3336 | * data. |
| 3337 | */ |
| 3338 | http_silent_debug(__LINE__, s); |
| 3339 | |
| 3340 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3341 | s->req->cons->shutr(s->req->cons); |
| 3342 | s->req->cons->shutw(s->req->cons); |
| 3343 | |
| 3344 | http_silent_debug(__LINE__, s); |
| 3345 | |
| 3346 | if (s->flags & SN_BE_ASSIGNED) |
| 3347 | s->be->beconn--; |
| 3348 | |
| 3349 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3350 | session_process_counters(s); |
| 3351 | |
| 3352 | if (s->txn.status) { |
| 3353 | int n; |
| 3354 | |
| 3355 | n = s->txn.status / 100; |
| 3356 | if (n < 1 || n > 5) |
| 3357 | n = 0; |
| 3358 | |
| 3359 | if (s->fe->mode == PR_MODE_HTTP) |
| 3360 | s->fe->counters.p.http.rsp[n]++; |
| 3361 | |
| 3362 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 3363 | (s->be->mode == PR_MODE_HTTP)) |
| 3364 | s->be->counters.p.http.rsp[n]++; |
| 3365 | } |
| 3366 | |
| 3367 | /* don't count other requests' data */ |
| 3368 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3369 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3370 | |
| 3371 | /* let's do a final log if we need it */ |
| 3372 | if (s->logs.logwait && |
| 3373 | !(s->flags & SN_MONITOR) && |
| 3374 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3375 | s->do_log(s); |
| 3376 | } |
| 3377 | |
| 3378 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3379 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3380 | tv_zero(&s->logs.tv_request); |
| 3381 | s->logs.t_queue = -1; |
| 3382 | s->logs.t_connect = -1; |
| 3383 | s->logs.t_data = -1; |
| 3384 | s->logs.t_close = 0; |
| 3385 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3386 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3387 | |
| 3388 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3389 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3390 | |
| 3391 | if (s->pend_pos) |
| 3392 | pendconn_free(s->pend_pos); |
| 3393 | |
| 3394 | if (s->srv) { |
| 3395 | if (s->flags & SN_CURR_SESS) { |
| 3396 | s->flags &= ~SN_CURR_SESS; |
| 3397 | s->srv->cur_sess--; |
| 3398 | } |
| 3399 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3400 | process_srv_queue(s->srv); |
| 3401 | } |
| 3402 | |
| 3403 | if (unlikely(s->srv_conn)) |
| 3404 | sess_change_server(s, NULL); |
| 3405 | s->srv = NULL; |
| 3406 | |
| 3407 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3408 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3409 | s->req->cons->err_type = SI_ET_NONE; |
| 3410 | s->req->cons->err_loc = NULL; |
| 3411 | s->req->cons->exp = TICK_ETERNITY; |
| 3412 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3413 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3414 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3415 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED); |
| 3416 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3417 | s->txn.meth = 0; |
| 3418 | http_reset_txn(s); |
| 3419 | s->txn.flags |= TX_NOT_FIRST; |
| 3420 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3421 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3422 | |
| 3423 | /* if the request buffer is not empty, it means we're |
| 3424 | * about to process another request, so send pending |
| 3425 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3426 | * Just don't do this if the buffer is close to be full, |
| 3427 | * because the request will wait for it to flush a little |
| 3428 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3429 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3430 | if (s->req->l > s->req->send_max) { |
| 3431 | if (s->rep->send_max && |
| 3432 | !(s->rep->flags & BF_FULL) && |
| 3433 | s->rep->lr <= s->rep->r && |
| 3434 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3435 | s->rep->flags |= BF_EXPECT_MORE; |
| 3436 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3437 | |
| 3438 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3439 | buffer_auto_read(s->req); |
| 3440 | buffer_auto_close(s->req); |
| 3441 | buffer_auto_read(s->rep); |
| 3442 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3443 | |
| 3444 | /* make ->lr point to the first non-forwarded byte */ |
| 3445 | s->req->lr = s->req->w + s->req->send_max; |
| 3446 | if (s->req->lr >= s->req->data + s->req->size) |
| 3447 | s->req->lr -= s->req->size; |
| 3448 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3449 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3450 | s->rep->lr -= s->req->size; |
| 3451 | |
| 3452 | s->req->analysers |= s->fe->fe_req_ana; |
| 3453 | s->rep->analysers = 0; |
| 3454 | |
| 3455 | http_silent_debug(__LINE__, s); |
| 3456 | } |
| 3457 | |
| 3458 | |
| 3459 | /* This function updates the request state machine according to the response |
| 3460 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3461 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3462 | * it is only used to find when a request/response couple is complete. Both |
| 3463 | * this function and its equivalent should loop until both return zero. It |
| 3464 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3465 | */ |
| 3466 | int http_sync_req_state(struct session *s) |
| 3467 | { |
| 3468 | struct buffer *buf = s->req; |
| 3469 | struct http_txn *txn = &s->txn; |
| 3470 | unsigned int old_flags = buf->flags; |
| 3471 | unsigned int old_state = txn->req.msg_state; |
| 3472 | |
| 3473 | http_silent_debug(__LINE__, s); |
| 3474 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3475 | return 0; |
| 3476 | |
| 3477 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3478 | /* No need to read anymore, the request was completely parsed. |
| 3479 | * We can shut the read side unless we want to abort_on_close. |
| 3480 | */ |
| 3481 | if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)) |
| 3482 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3483 | |
| 3484 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3485 | goto wait_other_side; |
| 3486 | |
| 3487 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3488 | /* The server has not finished to respond, so we |
| 3489 | * don't want to move in order not to upset it. |
| 3490 | */ |
| 3491 | goto wait_other_side; |
| 3492 | } |
| 3493 | |
| 3494 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3495 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3496 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3497 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3498 | goto wait_other_side; |
| 3499 | } |
| 3500 | |
| 3501 | /* When we get here, it means that both the request and the |
| 3502 | * response have finished receiving. Depending on the connection |
| 3503 | * mode, we'll have to wait for the last bytes to leave in either |
| 3504 | * direction, and sometimes for a close to be effective. |
| 3505 | */ |
| 3506 | |
| 3507 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3508 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3509 | /* Server-close mode : queue a connection close to the server */ |
| 3510 | buffer_shutw_now(buf); |
| 3511 | buf->cons->flags |= SI_FL_NOLINGER; |
| 3512 | } |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3513 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3514 | /* Option forceclose is set, let's enforce it now |
| 3515 | * that we're not expecting any new data to come. |
| 3516 | */ |
| 3517 | buffer_shutr_now(buf); |
| 3518 | buffer_shutw_now(buf); |
| 3519 | buf->cons->flags |= SI_FL_NOLINGER; |
| 3520 | } |
| 3521 | /* other modes include httpclose (no action) and keepalive (not implemented) */ |
| 3522 | } |
| 3523 | |
| 3524 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3525 | /* if we've just closed an output, let's switch */ |
| 3526 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3527 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3528 | goto http_msg_closing; |
| 3529 | } |
| 3530 | else { |
| 3531 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3532 | goto http_msg_closed; |
| 3533 | } |
| 3534 | } |
| 3535 | else { |
| 3536 | /* other modes are used as a tunnel right now */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3537 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3538 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3539 | goto wait_other_side; |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3544 | http_msg_closing: |
| 3545 | /* nothing else to forward, just waiting for the output buffer |
| 3546 | * to be empty and for the shutw_now to take effect. |
| 3547 | */ |
| 3548 | if (buf->flags & BF_OUT_EMPTY) { |
| 3549 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3550 | goto http_msg_closed; |
| 3551 | } |
| 3552 | else if (buf->flags & BF_SHUTW) { |
| 3553 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3554 | goto wait_other_side; |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3559 | http_msg_closed: |
| 3560 | goto wait_other_side; |
| 3561 | } |
| 3562 | |
| 3563 | wait_other_side: |
| 3564 | http_silent_debug(__LINE__, s); |
| 3565 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3566 | } |
| 3567 | |
| 3568 | |
| 3569 | /* This function updates the response state machine according to the request |
| 3570 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3571 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3572 | * it is only used to find when a request/response couple is complete. Both |
| 3573 | * this function and its equivalent should loop until both return zero. It |
| 3574 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3575 | */ |
| 3576 | int http_sync_res_state(struct session *s) |
| 3577 | { |
| 3578 | struct buffer *buf = s->rep; |
| 3579 | struct http_txn *txn = &s->txn; |
| 3580 | unsigned int old_flags = buf->flags; |
| 3581 | unsigned int old_state = txn->rsp.msg_state; |
| 3582 | |
| 3583 | http_silent_debug(__LINE__, s); |
| 3584 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3585 | return 0; |
| 3586 | |
| 3587 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3588 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3589 | * still monitor the server connection for a possible close |
| 3590 | * while the request is being uploaded, so we don't disable |
| 3591 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3592 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3593 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3594 | |
| 3595 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3596 | goto wait_other_side; |
| 3597 | |
| 3598 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3599 | /* The client seems to still be sending data, probably |
| 3600 | * because we got an error response during an upload. |
| 3601 | * We have the choice of either breaking the connection |
| 3602 | * or letting it pass through. Let's do the later. |
| 3603 | */ |
| 3604 | goto wait_other_side; |
| 3605 | } |
| 3606 | |
| 3607 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3608 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3609 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3610 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3611 | goto wait_other_side; |
| 3612 | } |
| 3613 | |
| 3614 | /* When we get here, it means that both the request and the |
| 3615 | * response have finished receiving. Depending on the connection |
| 3616 | * mode, we'll have to wait for the last bytes to leave in either |
| 3617 | * direction, and sometimes for a close to be effective. |
| 3618 | */ |
| 3619 | |
| 3620 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3621 | /* Server-close mode : shut read and wait for the request |
| 3622 | * side to close its output buffer. The caller will detect |
| 3623 | * when we're in DONE and the other is in CLOSED and will |
| 3624 | * catch that for the final cleanup. |
| 3625 | */ |
| 3626 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3627 | buffer_shutr_now(buf); |
| 3628 | goto wait_other_side; |
| 3629 | } |
| 3630 | else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) && |
| 3631 | ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) { |
| 3632 | /* Option forceclose is set, let's enforce it now |
| 3633 | * that we're not expecting any new data to come. |
| 3634 | * The caller knows the session is complete once |
| 3635 | * both states are CLOSED. |
| 3636 | */ |
| 3637 | buffer_shutr_now(buf); |
| 3638 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3639 | } |
| 3640 | else { |
| 3641 | /* other modes include httpclose (no action) and keepalive |
| 3642 | * (not implemented). These modes are used as a tunnel right |
| 3643 | * now. |
| 3644 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3645 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3646 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3647 | goto wait_other_side; |
| 3648 | } |
| 3649 | |
| 3650 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3651 | /* if we've just closed an output, let's switch */ |
| 3652 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3653 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3654 | goto http_msg_closing; |
| 3655 | } |
| 3656 | else { |
| 3657 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3658 | goto http_msg_closed; |
| 3659 | } |
| 3660 | } |
| 3661 | goto wait_other_side; |
| 3662 | } |
| 3663 | |
| 3664 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3665 | http_msg_closing: |
| 3666 | /* nothing else to forward, just waiting for the output buffer |
| 3667 | * to be empty and for the shutw_now to take effect. |
| 3668 | */ |
| 3669 | if (buf->flags & BF_OUT_EMPTY) { |
| 3670 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3671 | goto http_msg_closed; |
| 3672 | } |
| 3673 | else if (buf->flags & BF_SHUTW) { |
| 3674 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 3675 | goto wait_other_side; |
| 3676 | } |
| 3677 | } |
| 3678 | |
| 3679 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3680 | http_msg_closed: |
| 3681 | /* drop any pending data */ |
| 3682 | buffer_ignore(buf, buf->l - buf->send_max); |
| 3683 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3684 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3685 | goto wait_other_side; |
| 3686 | } |
| 3687 | |
| 3688 | wait_other_side: |
| 3689 | http_silent_debug(__LINE__, s); |
| 3690 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 3691 | } |
| 3692 | |
| 3693 | |
| 3694 | /* Resync the request and response state machines. Return 1 if either state |
| 3695 | * changes. |
| 3696 | */ |
| 3697 | int http_resync_states(struct session *s) |
| 3698 | { |
| 3699 | struct http_txn *txn = &s->txn; |
| 3700 | int old_req_state = txn->req.msg_state; |
| 3701 | int old_res_state = txn->rsp.msg_state; |
| 3702 | |
| 3703 | http_silent_debug(__LINE__, s); |
| 3704 | http_sync_req_state(s); |
| 3705 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3706 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3707 | if (!http_sync_res_state(s)) |
| 3708 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3709 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3710 | if (!http_sync_req_state(s)) |
| 3711 | break; |
| 3712 | } |
| 3713 | http_silent_debug(__LINE__, s); |
| 3714 | /* OK, both state machines agree on a compatible state. |
| 3715 | * There are a few cases we're interested in : |
| 3716 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 3717 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 3718 | * directions, so let's simply disable both analysers. |
| 3719 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 3720 | * request. |
| 3721 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 3722 | * with server-close mode means we've completed one request and we |
| 3723 | * must re-initialize the server connection. |
| 3724 | */ |
| 3725 | |
| 3726 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 3727 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 3728 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3729 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 3730 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3731 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3732 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3733 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3734 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3735 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3736 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3737 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 3738 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
| 3739 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3740 | s->rep->analysers = 0; |
| 3741 | buffer_auto_close(s->rep); |
| 3742 | buffer_auto_read(s->rep); |
| 3743 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3744 | buffer_abort(s->req); |
| 3745 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3746 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3747 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3748 | } |
| 3749 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3750 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 3751 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 3752 | /* server-close: terminate this server connection and |
| 3753 | * reinitialize a fresh-new transaction. |
| 3754 | */ |
| 3755 | http_end_txn_clean_session(s); |
| 3756 | } |
| 3757 | |
| 3758 | http_silent_debug(__LINE__, s); |
| 3759 | return txn->req.msg_state != old_req_state || |
| 3760 | txn->rsp.msg_state != old_res_state; |
| 3761 | } |
| 3762 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3763 | /* This function is an analyser which forwards request body (including chunk |
| 3764 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3765 | * zero byte. The only situation where it must not be called is when we're in |
| 3766 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3767 | * remaining data and to resync after end of body. It expects the msg_state to |
| 3768 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 3769 | * read more data, or 1 once we can go on with next request or end the session. |
| 3770 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 3771 | * bytes of pending data + the headers if not already done (between som and sov). |
| 3772 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 3773 | */ |
| 3774 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 3775 | { |
| 3776 | struct http_txn *txn = &s->txn; |
| 3777 | struct http_msg *msg = &s->txn.req; |
| 3778 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3779 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3780 | return 0; |
| 3781 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 3782 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 3783 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
| 3784 | /* Output closed while we were sending data. We must abort. */ |
| 3785 | buffer_ignore(req, req->l - req->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3786 | buffer_auto_read(req); |
| 3787 | buffer_auto_close(req); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3788 | req->analysers &= ~an_bit; |
| 3789 | return 1; |
| 3790 | } |
| 3791 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3792 | buffer_dont_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3793 | |
| 3794 | /* Note that we don't have to send 100-continue back because we don't |
| 3795 | * need the data to complete our job, and it's up to the server to |
| 3796 | * decide whether to return 100, 417 or anything else in return of |
| 3797 | * an "Expect: 100-continue" header. |
| 3798 | */ |
| 3799 | |
| 3800 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3801 | /* we have msg->col and msg->sov which both point to the first |
| 3802 | * byte of message body. msg->som still points to the beginning |
| 3803 | * of the message. We must save the body in req->lr because it |
| 3804 | * survives buffer re-alignments. |
| 3805 | */ |
| 3806 | req->lr = req->data + msg->sov; |
| 3807 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3808 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3809 | else { |
| 3810 | msg->msg_state = HTTP_MSG_DATA; |
| 3811 | } |
| 3812 | } |
| 3813 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3814 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3815 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 3816 | /* we may have some data pending */ |
| 3817 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 3818 | int bytes = msg->sov - msg->som; |
| 3819 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 3820 | bytes += req->size; |
| 3821 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 3822 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 3823 | msg->som = msg->sov; |
| 3824 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3825 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3826 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 3827 | /* must still forward */ |
| 3828 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3829 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3830 | |
| 3831 | /* nothing left to forward */ |
| 3832 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3833 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3834 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3835 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3836 | } |
| 3837 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3838 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 3839 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3840 | * TRAILERS state. |
| 3841 | */ |
| 3842 | int ret = http_parse_chunk_size(req, msg); |
| 3843 | |
| 3844 | if (!ret) |
| 3845 | goto missing_data; |
| 3846 | else if (ret < 0) |
| 3847 | goto return_bad_req; |
| 3848 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3849 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3850 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 3851 | /* we want the CRLF after the data */ |
| 3852 | int ret; |
| 3853 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3854 | req->lr = req->w + req->send_max; |
| 3855 | if (req->lr >= req->data + req->size) |
| 3856 | req->lr -= req->size; |
| 3857 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3858 | ret = http_skip_chunk_crlf(req, msg); |
| 3859 | |
| 3860 | if (ret == 0) |
| 3861 | goto missing_data; |
| 3862 | else if (ret < 0) |
| 3863 | goto return_bad_req; |
| 3864 | /* we're in MSG_CHUNK_SIZE now */ |
| 3865 | } |
| 3866 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 3867 | int ret = http_forward_trailers(req, msg); |
| 3868 | |
| 3869 | if (ret == 0) |
| 3870 | goto missing_data; |
| 3871 | else if (ret < 0) |
| 3872 | goto return_bad_req; |
| 3873 | /* we're in HTTP_MSG_DONE now */ |
| 3874 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3875 | else { |
| 3876 | /* other states, DONE...TUNNEL */ |
| 3877 | if (http_resync_states(s)) { |
| 3878 | /* some state changes occurred, maybe the analyser |
| 3879 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3880 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3881 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 3882 | goto return_bad_req; |
| 3883 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3884 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3885 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3886 | } |
| 3887 | } |
| 3888 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3889 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3890 | /* stop waiting for data if the input is closed before the end */ |
| 3891 | if (req->flags & BF_SHUTR) |
| 3892 | goto return_bad_req; |
| 3893 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3894 | /* waiting for the last bits to leave the buffer */ |
| 3895 | if (req->flags & BF_SHUTW) |
| 3896 | goto return_bad_req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3897 | |
| 3898 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3899 | return 0; |
| 3900 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3901 | return_bad_req: /* let's centralize all bad requests */ |
| 3902 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3903 | txn->status = 400; |
| 3904 | /* Note: we don't send any error if some data were already sent */ |
| 3905 | stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL); |
| 3906 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3907 | buffer_auto_read(req); |
| 3908 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3909 | req->analysers = 0; |
| 3910 | s->fe->counters.failed_req++; |
| 3911 | if (s->listener->counters) |
| 3912 | s->listener->counters->failed_req++; |
| 3913 | |
| 3914 | if (!(s->flags & SN_ERR_MASK)) |
| 3915 | s->flags |= SN_ERR_PRXCOND; |
| 3916 | if (!(s->flags & SN_FINST_MASK)) |
| 3917 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3918 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3919 | return 0; |
| 3920 | } |
| 3921 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3922 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 3923 | * processing can continue on next analysers, or zero if it either needs more |
| 3924 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 3925 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 3926 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 3927 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3928 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3929 | 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] | 3930 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3931 | struct http_txn *txn = &s->txn; |
| 3932 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3933 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3934 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3935 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3936 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3937 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3938 | 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] | 3939 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3940 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3941 | rep, |
| 3942 | rep->rex, rep->wex, |
| 3943 | rep->flags, |
| 3944 | rep->l, |
| 3945 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 3946 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3947 | /* |
| 3948 | * Now parse the partial (or complete) lines. |
| 3949 | * We will check the response syntax, and also join multi-line |
| 3950 | * headers. An index of all the lines will be elaborated while |
| 3951 | * parsing. |
| 3952 | * |
| 3953 | * For the parsing, we use a 28 states FSM. |
| 3954 | * |
| 3955 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3956 | * rep->data + msg->som = beginning of response |
| 3957 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 3958 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3959 | * rep->lr = first non-visited byte |
| 3960 | * rep->r = end of data |
| 3961 | */ |
| 3962 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3963 | /* There's a protected area at the end of the buffer for rewriting |
| 3964 | * purposes. We don't want to start to parse the request if the |
| 3965 | * protected area is affected, because we may have to move processed |
| 3966 | * data later, which is much more complicated. |
| 3967 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3968 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 3969 | if (unlikely((rep->flags & BF_FULL) || |
| 3970 | rep->r < rep->lr || |
| 3971 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 3972 | if (rep->send_max) { |
| 3973 | /* some data has still not left the buffer, wake us once that's done */ |
| 3974 | buffer_dont_close(rep); |
| 3975 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 3976 | return 0; |
| 3977 | } |
| 3978 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 3979 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3980 | } |
| 3981 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3982 | if (likely(rep->lr < rep->r)) |
| 3983 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3986 | /* 1: we might have to print this header in debug mode */ |
| 3987 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3988 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3989 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3990 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3991 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3992 | sol = rep->data + msg->som; |
| 3993 | eol = sol + msg->sl.rq.l; |
| 3994 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3995 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3996 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 3997 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3998 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3999 | while (cur_idx) { |
| 4000 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4001 | debug_hdr("srvhdr", s, sol, eol); |
| 4002 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4003 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4004 | } |
| 4005 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4006 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4007 | /* |
| 4008 | * Now we quickly check if we have found a full valid response. |
| 4009 | * If not so, we check the FD and buffer states before leaving. |
| 4010 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4011 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4012 | * responses are checked first. |
| 4013 | * |
| 4014 | * Depending on whether the client is still there or not, we |
| 4015 | * may send an error response back or not. Note that normally |
| 4016 | * we should only check for HTTP status there, and check I/O |
| 4017 | * errors somewhere else. |
| 4018 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4019 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4020 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4021 | /* Invalid response */ |
| 4022 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4023 | /* we detected a parsing error. We want to archive this response |
| 4024 | * in the dedicated proxy area for later troubleshooting. |
| 4025 | */ |
| 4026 | hdr_response_bad: |
| 4027 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4028 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4029 | |
| 4030 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4031 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4032 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4033 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4034 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4035 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4036 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4037 | rep->analysers = 0; |
| 4038 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4039 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4040 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4041 | |
| 4042 | if (!(s->flags & SN_ERR_MASK)) |
| 4043 | s->flags |= SN_ERR_PRXCOND; |
| 4044 | if (!(s->flags & SN_FINST_MASK)) |
| 4045 | s->flags |= SN_FINST_H; |
| 4046 | |
| 4047 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4048 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4049 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4050 | /* too large response does not fit in buffer. */ |
| 4051 | else if (rep->flags & BF_FULL) { |
| 4052 | goto hdr_response_bad; |
| 4053 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4054 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4055 | /* read error */ |
| 4056 | else if (rep->flags & BF_READ_ERROR) { |
| 4057 | if (msg->err_pos >= 0) |
| 4058 | 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] | 4059 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4060 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4061 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4062 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4063 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4064 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4065 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4066 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4067 | rep->analysers = 0; |
| 4068 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4069 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4070 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4071 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4072 | if (!(s->flags & SN_ERR_MASK)) |
| 4073 | s->flags |= SN_ERR_SRVCL; |
| 4074 | if (!(s->flags & SN_FINST_MASK)) |
| 4075 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4076 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4077 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4078 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4079 | /* read timeout : return a 504 to the client. */ |
| 4080 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4081 | if (msg->err_pos >= 0) |
| 4082 | 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] | 4083 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4084 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4085 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4086 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4087 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4088 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4089 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4090 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4091 | rep->analysers = 0; |
| 4092 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4093 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4094 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4095 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4096 | if (!(s->flags & SN_ERR_MASK)) |
| 4097 | s->flags |= SN_ERR_SRVTO; |
| 4098 | if (!(s->flags & SN_FINST_MASK)) |
| 4099 | s->flags |= SN_FINST_H; |
| 4100 | return 0; |
| 4101 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4102 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4103 | /* close from server */ |
| 4104 | else if (rep->flags & BF_SHUTR) { |
| 4105 | if (msg->err_pos >= 0) |
| 4106 | 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] | 4107 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4108 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4109 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4110 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4111 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4112 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4113 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4114 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4115 | rep->analysers = 0; |
| 4116 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4117 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4118 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4119 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4120 | if (!(s->flags & SN_ERR_MASK)) |
| 4121 | s->flags |= SN_ERR_SRVCL; |
| 4122 | if (!(s->flags & SN_FINST_MASK)) |
| 4123 | s->flags |= SN_FINST_H; |
| 4124 | return 0; |
| 4125 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4126 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4127 | /* write error to client (we don't send any message then) */ |
| 4128 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4129 | if (msg->err_pos >= 0) |
| 4130 | 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] | 4131 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4132 | s->be->counters.failed_resp++; |
| 4133 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4134 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4135 | |
| 4136 | if (!(s->flags & SN_ERR_MASK)) |
| 4137 | s->flags |= SN_ERR_CLICL; |
| 4138 | if (!(s->flags & SN_FINST_MASK)) |
| 4139 | s->flags |= SN_FINST_H; |
| 4140 | |
| 4141 | /* process_session() will take care of the error */ |
| 4142 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4143 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4144 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4145 | buffer_dont_close(rep); |
| 4146 | return 0; |
| 4147 | } |
| 4148 | |
| 4149 | /* More interesting part now : we know that we have a complete |
| 4150 | * response which at least looks like HTTP. We have an indicator |
| 4151 | * of each header's length, so we can parse them quickly. |
| 4152 | */ |
| 4153 | |
| 4154 | if (unlikely(msg->err_pos >= 0)) |
| 4155 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4156 | |
| 4157 | /* ensure we keep this pointer to the beginning of the message */ |
| 4158 | msg->sol = rep->data + msg->som; |
| 4159 | |
| 4160 | /* |
| 4161 | * 1: get the status code |
| 4162 | */ |
| 4163 | n = rep->data[msg->sl.st.c] - '0'; |
| 4164 | if (n < 1 || n > 5) |
| 4165 | n = 0; |
| 4166 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4167 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4168 | /* check if the response is HTTP/1.1 or above */ |
| 4169 | if ((msg->sl.st.v_l == 8) && |
| 4170 | ((rep->data[msg->som + 5] > '1') || |
| 4171 | ((rep->data[msg->som + 5] == '1') && |
| 4172 | (rep->data[msg->som + 7] >= '1')))) |
| 4173 | txn->flags |= TX_RES_VER_11; |
| 4174 | |
| 4175 | /* "connection" has not been parsed yet */ |
| 4176 | txn->flags &= ~TX_CON_HDR_PARS; |
| 4177 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4178 | /* transfer length unknown*/ |
| 4179 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4180 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4181 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
| 4182 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4183 | if (txn->status >= 100 && txn->status < 500) |
| 4184 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4185 | else |
| 4186 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4187 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4188 | /* |
| 4189 | * 2: check for cacheability. |
| 4190 | */ |
| 4191 | |
| 4192 | switch (txn->status) { |
| 4193 | case 200: |
| 4194 | case 203: |
| 4195 | case 206: |
| 4196 | case 300: |
| 4197 | case 301: |
| 4198 | case 410: |
| 4199 | /* RFC2616 @13.4: |
| 4200 | * "A response received with a status code of |
| 4201 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4202 | * by a cache (...) unless a cache-control |
| 4203 | * directive prohibits caching." |
| 4204 | * |
| 4205 | * RFC2616 @9.5: POST method : |
| 4206 | * "Responses to this method are not cacheable, |
| 4207 | * unless the response includes appropriate |
| 4208 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4209 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4210 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4211 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4212 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4213 | break; |
| 4214 | default: |
| 4215 | break; |
| 4216 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4217 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4218 | /* |
| 4219 | * 3: we may need to capture headers |
| 4220 | */ |
| 4221 | s->logs.logwait &= ~LW_RESP; |
| 4222 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
| 4223 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 4224 | txn->rsp.cap, s->fe->rsp_cap); |
| 4225 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4226 | /* 4: determine the transfer-length. |
| 4227 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4228 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4229 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4230 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4231 | * All responses to the HEAD request method MUST NOT include a |
| 4232 | * message-body, even though the presence of entity-header fields |
| 4233 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4234 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4235 | * message-body. All other responses do include a message-body, |
| 4236 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4237 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4238 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4239 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4240 | * always terminated by the first empty line after the header fields, |
| 4241 | * regardless of the entity-header fields present in the message. |
| 4242 | * |
| 4243 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4244 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4245 | * transfer-length is defined by the use of this transfer-coding. |
| 4246 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4247 | * transfer-coding is not present, the transfer-length is defined by |
| 4248 | * the sender closing the connection. |
| 4249 | * |
| 4250 | * 3. If a Content-Length header field is present, its decimal value in |
| 4251 | * OCTETs represents both the entity-length and the transfer-length. |
| 4252 | * If a message is received with both a Transfer-Encoding header |
| 4253 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4254 | * |
| 4255 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4256 | * the transfer-length is not otherwise specified, then this self- |
| 4257 | * delimiting media type defines the transfer-length. This media |
| 4258 | * type MUST NOT be used unless the sender knows that the recipient |
| 4259 | * can parse it; the presence in a request of a Range header with |
| 4260 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4261 | * client can parse multipart/byteranges responses. |
| 4262 | * |
| 4263 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4264 | */ |
| 4265 | |
| 4266 | /* Skip parsing if no content length is possible. The response flags |
| 4267 | * 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] | 4268 | * 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] | 4269 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4270 | */ |
| 4271 | if (txn->meth == HTTP_METH_HEAD || |
| 4272 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4273 | txn->status == 204 || txn->status == 304) { |
| 4274 | txn->flags |= TX_RES_XFER_LEN; |
| 4275 | goto skip_content_length; |
| 4276 | } |
| 4277 | |
| 4278 | if (txn->meth == HTTP_METH_CONNECT) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4279 | goto skip_content_length; |
| 4280 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4281 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4282 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4283 | while ((txn->flags & TX_RES_VER_11) && |
| 4284 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4285 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4286 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4287 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4288 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4289 | use_close_only = 1; |
| 4290 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4291 | break; |
| 4292 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4296 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4297 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4298 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4299 | signed long long cl; |
| 4300 | |
| 4301 | if (!ctx.vlen) |
| 4302 | goto hdr_response_bad; |
| 4303 | |
| 4304 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4305 | goto hdr_response_bad; /* parse failure */ |
| 4306 | |
| 4307 | if (cl < 0) |
| 4308 | goto hdr_response_bad; |
| 4309 | |
| 4310 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4311 | goto hdr_response_bad; /* already specified, was different */ |
| 4312 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4313 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4314 | msg->hdr_content_len = cl; |
| 4315 | } |
| 4316 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4317 | /* FIXME: we should also implement the multipart/byterange method. |
| 4318 | * For now on, we resort to close mode in this case (unknown length). |
| 4319 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4320 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4321 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4322 | /* end of job, return OK */ |
| 4323 | rep->analysers &= ~an_bit; |
| 4324 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4325 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4326 | return 1; |
| 4327 | } |
| 4328 | |
| 4329 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4330 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4331 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4332 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4333 | */ |
| 4334 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4335 | { |
| 4336 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4337 | struct http_msg *msg = &txn->rsp; |
| 4338 | struct proxy *cur_proxy; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4339 | struct wordlist *wl; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4340 | int conn_ka = 0, conn_cl = 0; |
| 4341 | int must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4342 | int must_del_close = 0, must_keep = 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4343 | |
| 4344 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4345 | now_ms, __FUNCTION__, |
| 4346 | t, |
| 4347 | rep, |
| 4348 | rep->rex, rep->wex, |
| 4349 | rep->flags, |
| 4350 | rep->l, |
| 4351 | rep->analysers); |
| 4352 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4353 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4354 | return 0; |
| 4355 | |
| 4356 | rep->analysers &= ~an_bit; |
| 4357 | rep->analyse_exp = TICK_ETERNITY; |
| 4358 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4359 | /* Now we have to check if we need to modify the Connection header. |
| 4360 | * This is more difficult on the response than it is on the request, |
| 4361 | * because we can have two different HTTP versions and we don't know |
| 4362 | * how the client will interprete a response. For instance, let's say |
| 4363 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4364 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4365 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4366 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4367 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4368 | * indicating how a request MAY be understood by the client. In case |
| 4369 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4370 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4371 | * will fall back to explicit close. Note that we won't take risks with |
| 4372 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
| 4373 | */ |
| 4374 | |
| 4375 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4376 | (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) && |
| 4377 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4378 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4379 | int may_keep = 0, may_close = 0; /* how it may be understood */ |
| 4380 | struct hdr_ctx ctx; |
| 4381 | |
| 4382 | ctx.idx = 0; |
| 4383 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4384 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 4385 | conn_cl = 1; |
| 4386 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 4387 | conn_ka = 1; |
| 4388 | } |
| 4389 | |
| 4390 | if (conn_cl) { |
| 4391 | /* close header present */ |
| 4392 | may_close = 1; |
| 4393 | if (conn_ka) /* we have both close and keep-alive */ |
| 4394 | may_keep = 1; |
| 4395 | } |
| 4396 | else if (conn_ka) { |
| 4397 | /* keep-alive alone */ |
| 4398 | may_keep = 1; |
| 4399 | } |
| 4400 | else { |
| 4401 | /* no close nor keep-alive header */ |
| 4402 | if (txn->flags & TX_RES_VER_11) |
| 4403 | may_keep = 1; |
| 4404 | else |
| 4405 | may_close = 1; |
| 4406 | |
| 4407 | if (txn->flags & TX_REQ_VER_11) |
| 4408 | may_keep = 1; |
| 4409 | else |
| 4410 | may_close = 1; |
| 4411 | } |
| 4412 | |
| 4413 | /* let's update the transaction status to reflect any close. |
| 4414 | * Note that ambiguous cases with keep & close will also be |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4415 | * handled. We also explicitly state that we will close in |
| 4416 | * case of an ambiguous response having no content-length. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4417 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4418 | if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) && |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4419 | (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) || |
| 4420 | !(txn->flags & TX_RES_XFER_LEN)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4421 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 4422 | |
| 4423 | /* Now we must adjust the response header : |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4424 | * - set "close" if may_keep and (WANT_CLO | httpclose) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4425 | * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK) |
| 4426 | * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4427 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4428 | if (may_keep && |
| 4429 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO || |
| 4430 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4431 | must_close = 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4432 | else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 4433 | may_close && (txn->flags & TX_RES_XFER_LEN)) { |
| 4434 | must_del_close = 1; |
| 4435 | if (!(txn->flags & TX_REQ_VER_11)) |
| 4436 | must_keep = 1; |
| 4437 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4438 | |
| 4439 | txn->flags |= TX_CON_HDR_PARS; |
| 4440 | } |
| 4441 | |
| 4442 | /* We might have to check for "Connection:" if the server |
| 4443 | * returns a connection status that is not compatible with |
| 4444 | * the client's or with the config. |
| 4445 | */ |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4446 | 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] | 4447 | char *cur_ptr, *cur_end, *cur_next; |
| 4448 | int cur_idx, old_idx, delta, val; |
| 4449 | int must_delete; |
| 4450 | struct hdr_idx_elem *cur_hdr; |
| 4451 | |
| 4452 | /* we just have to remove the headers if both sides are 1.0 */ |
| 4453 | 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] | 4454 | |
| 4455 | /* same if we want to re-enable keep-alive on 1.1 */ |
| 4456 | must_delete |= must_del_close; |
| 4457 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4458 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4459 | |
| 4460 | for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) { |
| 4461 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4462 | cur_ptr = cur_next; |
| 4463 | cur_end = cur_ptr + cur_hdr->len; |
| 4464 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4465 | |
| 4466 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 4467 | if (!val) |
| 4468 | continue; |
| 4469 | |
| 4470 | /* 3 possibilities : |
| 4471 | * - we have already set "Connection: close" or we're in |
| 4472 | * HTTP/1.0, so we remove this line. |
| 4473 | * - we have not yet set "Connection: close", but this line |
| 4474 | * indicates close. We leave it untouched and set the flag. |
| 4475 | * - we have not yet set "Connection: close", and this line |
| 4476 | * indicates non-close. We replace it and set the flag. |
| 4477 | */ |
| 4478 | if (must_delete) { |
| 4479 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 4480 | http_msg_move_end(&txn->rsp, delta); |
| 4481 | cur_next += delta; |
| 4482 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4483 | txn->hdr_idx.used--; |
| 4484 | cur_hdr->len = 0; |
| 4485 | must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4486 | must_del_close = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4487 | } else { |
| 4488 | if (cur_end - cur_ptr - val != 5 || |
| 4489 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 4490 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 4491 | "close", 5); |
| 4492 | cur_next += delta; |
| 4493 | cur_hdr->len += delta; |
| 4494 | http_msg_move_end(&txn->rsp, delta); |
| 4495 | } |
| 4496 | must_delete = 1; |
| 4497 | must_close = 0; |
| 4498 | } |
| 4499 | } /* for loop */ |
| 4500 | } /* if must close keep-alive */ |
| 4501 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4502 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4503 | /* |
| 4504 | * 3: we will have to evaluate the filters. |
| 4505 | * As opposed to version 1.2, now they will be evaluated in the |
| 4506 | * filters order and not in the header order. This means that |
| 4507 | * each filter has to be validated among all headers. |
| 4508 | * |
| 4509 | * Filters are tried with ->be first, then with ->fe if it is |
| 4510 | * different from ->be. |
| 4511 | */ |
| 4512 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4513 | cur_proxy = t->be; |
| 4514 | while (1) { |
| 4515 | struct proxy *rule_set = cur_proxy; |
| 4516 | |
| 4517 | /* try headers filters */ |
| 4518 | if (rule_set->rsp_exp != NULL) { |
| 4519 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 4520 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4521 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4522 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4523 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4524 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4525 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4526 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4527 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4528 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4529 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4530 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4531 | if (!(t->flags & SN_ERR_MASK)) |
| 4532 | t->flags |= SN_ERR_PRXCOND; |
| 4533 | if (!(t->flags & SN_FINST_MASK)) |
| 4534 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4535 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4536 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4537 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4538 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4539 | /* has the response been denied ? */ |
| 4540 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4541 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4542 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4543 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4544 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4545 | if (t->listener->counters) |
| 4546 | t->listener->counters->denied_resp++; |
| 4547 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4548 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4549 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4550 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4551 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4552 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4553 | if (txn->status < 200) |
| 4554 | break; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4555 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4556 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4557 | } |
| 4558 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4559 | /* check whether we're already working on the frontend */ |
| 4560 | if (cur_proxy == t->fe) |
| 4561 | break; |
| 4562 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4563 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4564 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4565 | /* |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4566 | * We may be facing a 1xx response (100 continue, 101 switching protocols), |
| 4567 | * in which case this is not the right response, and we're waiting for the |
| 4568 | * next one. Let's allow this response to go to the client and wait for the |
| 4569 | * next one. |
| 4570 | */ |
| 4571 | if (txn->status < 200) { |
| 4572 | hdr_idx_init(&txn->hdr_idx); |
| 4573 | buffer_forward(rep, rep->lr - (rep->data + msg->som)); |
| 4574 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4575 | txn->status = 0; |
| 4576 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4577 | return 1; |
| 4578 | } |
| 4579 | |
| 4580 | /* we don't have any 1xx status code now */ |
| 4581 | |
| 4582 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4583 | * 4: check for server cookie. |
| 4584 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4585 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4586 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4587 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4588 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4589 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4590 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4591 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4592 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4593 | 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] | 4594 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4595 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4596 | /* |
| 4597 | * 6: add server cookie in the response if needed |
| 4598 | */ |
| 4599 | 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] | 4600 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4601 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4602 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4603 | /* the server is known, it's not the one the client requested, we have to |
| 4604 | * insert a set-cookie here, except if we want to insert only on POST |
| 4605 | * requests and this one isn't. Note that servers which don't have cookies |
| 4606 | * (eg: some backup servers) will return a full cookie removal request. |
| 4607 | */ |
| 4608 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4609 | t->be->cookie_name, |
| 4610 | 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] | 4611 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4612 | if (t->be->cookie_domain) |
| 4613 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4614 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4615 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4616 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4617 | goto return_bad_resp; |
| 4618 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4619 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4620 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4621 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4622 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4623 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4624 | */ |
| 4625 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4626 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4627 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4628 | |
| 4629 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4630 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4631 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4632 | } |
| 4633 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4634 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4635 | /* |
| 4636 | * 7: check if result will be cacheable with a cookie. |
| 4637 | * We'll block the response if security checks have caught |
| 4638 | * nasty things such as a cacheable cookie. |
| 4639 | */ |
| 4640 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4641 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4642 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4643 | |
| 4644 | /* we're in presence of a cacheable response containing |
| 4645 | * a set-cookie header. We'll block it as requested by |
| 4646 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4647 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4648 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4649 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4650 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4651 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4652 | if (t->listener->counters) |
| 4653 | t->listener->counters->denied_resp++; |
| 4654 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4655 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4656 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4657 | send_log(t->be, LOG_ALERT, |
| 4658 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4659 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4660 | goto return_srv_prx_502; |
| 4661 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4662 | |
| 4663 | /* |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4664 | * 8: add "Connection: close" if needed and not yet set. This is |
| 4665 | * only needed for 1.1 responses since we know there is no other |
| 4666 | * Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4667 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4668 | if (must_close && (txn->flags & TX_RES_VER_11)) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4669 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4670 | "Connection: close", 17) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4671 | goto return_bad_resp; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4672 | must_close = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4673 | } |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4674 | else if (must_keep && !(txn->flags & TX_REQ_VER_11)) { |
| 4675 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 4676 | "Connection: keep-alive", 22) < 0)) |
| 4677 | goto return_bad_resp; |
| 4678 | must_keep = 0; |
| 4679 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4680 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4681 | if (txn->flags & TX_RES_XFER_LEN) |
| 4682 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4683 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4684 | /************************************************************* |
| 4685 | * OK, that's finished for the headers. We have done what we * |
| 4686 | * could. Let's switch to the DATA state. * |
| 4687 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4688 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4689 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4690 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4691 | /* if the user wants to log as soon as possible, without counting |
| 4692 | * bytes from the server, then this is the right moment. We have |
| 4693 | * to temporarily assign bytes_out to log what we currently have. |
| 4694 | */ |
| 4695 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4696 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4697 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4698 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4699 | t->logs.bytes_out = 0; |
| 4700 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4701 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4702 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4703 | * otherwise we would not let the client side wake up. |
| 4704 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4705 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4706 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4707 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4708 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4709 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4710 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4711 | /* This function is an analyser which forwards response body (including chunk |
| 4712 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4713 | * zero byte. The only situation where it must not be called is when we're in |
| 4714 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4715 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4716 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4717 | * read more data, or 1 once we can go on with next request or end the session. |
| 4718 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4719 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4720 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4721 | */ |
| 4722 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4723 | { |
| 4724 | struct http_txn *txn = &s->txn; |
| 4725 | struct http_msg *msg = &s->txn.rsp; |
| 4726 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4727 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4728 | return 0; |
| 4729 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4730 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4731 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4732 | !s->req->analysers) { |
| 4733 | /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */ |
| 4734 | buffer_ignore(res, res->l - res->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4735 | buffer_auto_read(res); |
| 4736 | buffer_auto_close(res); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4737 | res->analysers &= ~an_bit; |
| 4738 | return 1; |
| 4739 | } |
| 4740 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4741 | buffer_dont_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4742 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4743 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4744 | /* we have msg->col and msg->sov which both point to the first |
| 4745 | * byte of message body. msg->som still points to the beginning |
| 4746 | * of the message. We must save the body in req->lr because it |
| 4747 | * survives buffer re-alignments. |
| 4748 | */ |
| 4749 | res->lr = res->data + msg->sov; |
| 4750 | if (txn->flags & TX_RES_TE_CHNK) |
| 4751 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4752 | else { |
| 4753 | msg->msg_state = HTTP_MSG_DATA; |
| 4754 | } |
| 4755 | } |
| 4756 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4757 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4758 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4759 | /* we may have some data pending */ |
| 4760 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4761 | int bytes = msg->sov - msg->som; |
| 4762 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4763 | bytes += res->size; |
| 4764 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 4765 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4766 | msg->som = msg->sov; |
| 4767 | } |
| 4768 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4769 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4770 | /* must still forward */ |
| 4771 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4772 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4773 | |
| 4774 | /* nothing left to forward */ |
| 4775 | if (txn->flags & TX_RES_TE_CHNK) |
| 4776 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 4777 | else |
| 4778 | msg->msg_state = HTTP_MSG_DONE; |
| 4779 | } |
| 4780 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4781 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4782 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 4783 | */ |
| 4784 | int ret = http_parse_chunk_size(res, msg); |
| 4785 | |
| 4786 | if (!ret) |
| 4787 | goto missing_data; |
| 4788 | else if (ret < 0) |
| 4789 | goto return_bad_res; |
| 4790 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4791 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4792 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4793 | /* we want the CRLF after the data */ |
| 4794 | int ret; |
| 4795 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4796 | res->lr = res->w + res->send_max; |
| 4797 | if (res->lr >= res->data + res->size) |
| 4798 | res->lr -= res->size; |
| 4799 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4800 | ret = http_skip_chunk_crlf(res, msg); |
| 4801 | |
| 4802 | if (!ret) |
| 4803 | goto missing_data; |
| 4804 | else if (ret < 0) |
| 4805 | goto return_bad_res; |
| 4806 | /* we're in MSG_CHUNK_SIZE now */ |
| 4807 | } |
| 4808 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4809 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4810 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4811 | if (ret == 0) |
| 4812 | goto missing_data; |
| 4813 | else if (ret < 0) |
| 4814 | goto return_bad_res; |
| 4815 | /* we're in HTTP_MSG_DONE now */ |
| 4816 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4817 | else { |
| 4818 | /* other states, DONE...TUNNEL */ |
| 4819 | if (http_resync_states(s)) { |
| 4820 | http_silent_debug(__LINE__, s); |
| 4821 | /* some state changes occurred, maybe the analyser |
| 4822 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4823 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4824 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4825 | goto return_bad_res; |
| 4826 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4827 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4828 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4829 | } |
| 4830 | } |
| 4831 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4832 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4833 | /* stop waiting for data if the input is closed before the end */ |
| 4834 | if (res->flags & BF_SHUTR) |
| 4835 | goto return_bad_res; |
| 4836 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4837 | if (!s->req->analysers) |
| 4838 | goto return_bad_res; |
| 4839 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4840 | /* forward the chunk size as well as any pending data */ |
| 4841 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4842 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 4843 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4844 | msg->som = msg->sov; |
| 4845 | } |
| 4846 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4847 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4848 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4849 | return 0; |
| 4850 | |
| 4851 | return_bad_res: /* let's centralize all bad resuests */ |
| 4852 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 4853 | txn->status = 502; |
| 4854 | stream_int_cond_close(res->cons, NULL); |
| 4855 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4856 | buffer_auto_close(res); |
| 4857 | buffer_auto_read(res); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4858 | res->analysers = 0; |
| 4859 | s->be->counters.failed_resp++; |
| 4860 | if (s->srv) { |
| 4861 | s->srv->counters.failed_resp++; |
| 4862 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4863 | } |
| 4864 | |
| 4865 | if (!(s->flags & SN_ERR_MASK)) |
| 4866 | s->flags |= SN_ERR_PRXCOND; |
| 4867 | if (!(s->flags & SN_FINST_MASK)) |
| 4868 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4869 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4870 | return 0; |
| 4871 | } |
| 4872 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4873 | /* Iterate the same filter through all request headers. |
| 4874 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4875 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4876 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4877 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4878 | 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] | 4879 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4880 | char term; |
| 4881 | char *cur_ptr, *cur_end, *cur_next; |
| 4882 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4883 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4884 | struct hdr_idx_elem *cur_hdr; |
| 4885 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4887 | last_hdr = 0; |
| 4888 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4889 | 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] | 4890 | old_idx = 0; |
| 4891 | |
| 4892 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4893 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4894 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4895 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4896 | (exp->action == ACT_ALLOW || |
| 4897 | exp->action == ACT_DENY || |
| 4898 | exp->action == ACT_TARPIT)) |
| 4899 | return 0; |
| 4900 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4901 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4902 | if (!cur_idx) |
| 4903 | break; |
| 4904 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4905 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4906 | cur_ptr = cur_next; |
| 4907 | cur_end = cur_ptr + cur_hdr->len; |
| 4908 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4909 | |
| 4910 | /* Now we have one header between cur_ptr and cur_end, |
| 4911 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4912 | */ |
| 4913 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4914 | /* The annoying part is that pattern matching needs |
| 4915 | * that we modify the contents to null-terminate all |
| 4916 | * strings before testing them. |
| 4917 | */ |
| 4918 | |
| 4919 | term = *cur_end; |
| 4920 | *cur_end = '\0'; |
| 4921 | |
| 4922 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4923 | switch (exp->action) { |
| 4924 | case ACT_SETBE: |
| 4925 | /* It is not possible to jump a second time. |
| 4926 | * FIXME: should we return an HTTP/500 here so that |
| 4927 | * the admin knows there's a problem ? |
| 4928 | */ |
| 4929 | if (t->be != t->fe) |
| 4930 | break; |
| 4931 | |
| 4932 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4933 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4934 | last_hdr = 1; |
| 4935 | break; |
| 4936 | |
| 4937 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4938 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4939 | last_hdr = 1; |
| 4940 | break; |
| 4941 | |
| 4942 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4943 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4944 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4945 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4946 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4947 | if (t->listener->counters) |
| 4948 | t->listener->counters->denied_resp++; |
| 4949 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4950 | break; |
| 4951 | |
| 4952 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4953 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4954 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4955 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4956 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4957 | if (t->listener->counters) |
| 4958 | t->listener->counters->denied_resp++; |
| 4959 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4960 | break; |
| 4961 | |
| 4962 | case ACT_REPLACE: |
| 4963 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4964 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4965 | /* FIXME: if the user adds a newline in the replacement, the |
| 4966 | * index will not be recalculated for now, and the new line |
| 4967 | * will not be counted as a new header. |
| 4968 | */ |
| 4969 | |
| 4970 | cur_end += delta; |
| 4971 | cur_next += delta; |
| 4972 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4973 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4974 | break; |
| 4975 | |
| 4976 | case ACT_REMOVE: |
| 4977 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4978 | cur_next += delta; |
| 4979 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4980 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4981 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4982 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4983 | cur_hdr->len = 0; |
| 4984 | cur_end = NULL; /* null-term has been rewritten */ |
| 4985 | break; |
| 4986 | |
| 4987 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4988 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4989 | if (cur_end) |
| 4990 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4991 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4992 | /* keep the link from this header to next one in case of later |
| 4993 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4994 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4995 | old_idx = cur_idx; |
| 4996 | } |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
| 5000 | |
| 5001 | /* Apply the filter to the request line. |
| 5002 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5003 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5004 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5005 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5006 | */ |
| 5007 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5008 | { |
| 5009 | char term; |
| 5010 | char *cur_ptr, *cur_end; |
| 5011 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5012 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5013 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5014 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5015 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5016 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5017 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5018 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5019 | (exp->action == ACT_ALLOW || |
| 5020 | exp->action == ACT_DENY || |
| 5021 | exp->action == ACT_TARPIT)) |
| 5022 | return 0; |
| 5023 | else if (exp->action == ACT_REMOVE) |
| 5024 | return 0; |
| 5025 | |
| 5026 | done = 0; |
| 5027 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5028 | 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] | 5029 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5030 | |
| 5031 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5032 | |
| 5033 | /* The annoying part is that pattern matching needs |
| 5034 | * that we modify the contents to null-terminate all |
| 5035 | * strings before testing them. |
| 5036 | */ |
| 5037 | |
| 5038 | term = *cur_end; |
| 5039 | *cur_end = '\0'; |
| 5040 | |
| 5041 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5042 | switch (exp->action) { |
| 5043 | case ACT_SETBE: |
| 5044 | /* It is not possible to jump a second time. |
| 5045 | * FIXME: should we return an HTTP/500 here so that |
| 5046 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5047 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5048 | if (t->be != t->fe) |
| 5049 | break; |
| 5050 | |
| 5051 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5052 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5053 | done = 1; |
| 5054 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5055 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5056 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5057 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5058 | done = 1; |
| 5059 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5060 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5061 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5062 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5063 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5064 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5065 | if (t->listener->counters) |
| 5066 | t->listener->counters->denied_resp++; |
| 5067 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5068 | done = 1; |
| 5069 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5070 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5071 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5072 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5073 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5074 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5075 | if (t->listener->counters) |
| 5076 | t->listener->counters->denied_resp++; |
| 5077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5078 | done = 1; |
| 5079 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5081 | case ACT_REPLACE: |
| 5082 | *cur_end = term; /* restore the string terminator */ |
| 5083 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5084 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5085 | /* FIXME: if the user adds a newline in the replacement, the |
| 5086 | * index will not be recalculated for now, and the new line |
| 5087 | * will not be counted as a new header. |
| 5088 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5089 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5090 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5091 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5092 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5093 | 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] | 5094 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5095 | HTTP_MSG_RQMETH, |
| 5096 | cur_ptr, cur_end + 1, |
| 5097 | NULL, NULL); |
| 5098 | if (unlikely(!cur_end)) |
| 5099 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5100 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5101 | /* we have a full request and we know that we have either a CR |
| 5102 | * or an LF at <ptr>. |
| 5103 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5104 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5105 | 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] | 5106 | /* there is no point trying this regex on headers */ |
| 5107 | return 1; |
| 5108 | } |
| 5109 | } |
| 5110 | *cur_end = term; /* restore the string terminator */ |
| 5111 | return done; |
| 5112 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5113 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5114 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5115 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5116 | /* |
| 5117 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 5118 | * 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] | 5119 | * unparsable request. Since it can manage the switch to another backend, it |
| 5120 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5121 | */ |
| 5122 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5123 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5124 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5125 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5126 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5127 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5128 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5129 | /* |
| 5130 | * The interleaving of transformations and verdicts |
| 5131 | * makes it difficult to decide to continue or stop |
| 5132 | * the evaluation. |
| 5133 | */ |
| 5134 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5135 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5136 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5137 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 5138 | exp = exp->next; |
| 5139 | continue; |
| 5140 | } |
| 5141 | |
| 5142 | /* Apply the filter to the request line. */ |
| 5143 | ret = apply_filter_to_req_line(t, req, exp); |
| 5144 | if (unlikely(ret < 0)) |
| 5145 | return -1; |
| 5146 | |
| 5147 | if (likely(ret == 0)) { |
| 5148 | /* The filter did not match the request, it can be |
| 5149 | * iterated through all headers. |
| 5150 | */ |
| 5151 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5152 | } |
| 5153 | exp = exp->next; |
| 5154 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5155 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5156 | } |
| 5157 | |
| 5158 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5159 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5160 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5161 | * Try to retrieve the server associated to the appsession. |
| 5162 | * If the server is found, it's assigned to the session. |
| 5163 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5164 | 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] | 5165 | struct http_txn *txn = &t->txn; |
| 5166 | appsess *asession = NULL; |
| 5167 | char *sessid_temp = NULL; |
| 5168 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5169 | if (len > t->be->appsession_len) { |
| 5170 | len = t->be->appsession_len; |
| 5171 | } |
| 5172 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5173 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5174 | /* request-learn option is enabled : store the sessid in the session for future use */ |
| 5175 | if (t->sessid != NULL) { |
| 5176 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
| 5177 | pool_free2(apools.sessid, t->sessid); |
| 5178 | } |
| 5179 | |
| 5180 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5181 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5182 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5183 | return; |
| 5184 | } |
| 5185 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5186 | memcpy(t->sessid, buf, len); |
| 5187 | t->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5191 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5192 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5193 | return; |
| 5194 | } |
| 5195 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5196 | memcpy(sessid_temp, buf, len); |
| 5197 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5198 | |
| 5199 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5200 | /* free previously allocated memory */ |
| 5201 | pool_free2(apools.sessid, sessid_temp); |
| 5202 | |
| 5203 | if (asession != NULL) { |
| 5204 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5205 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5206 | asession->request_count++; |
| 5207 | |
| 5208 | if (asession->serverid != NULL) { |
| 5209 | struct server *srv = t->be->srv; |
| 5210 | while (srv) { |
| 5211 | if (strcmp(srv->id, asession->serverid) == 0) { |
| 5212 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
| 5213 | /* we found the server and it's usable */ |
| 5214 | txn->flags &= ~TX_CK_MASK; |
| 5215 | txn->flags |= TX_CK_VALID; |
| 5216 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5217 | t->srv = srv; |
| 5218 | break; |
| 5219 | } else { |
| 5220 | txn->flags &= ~TX_CK_MASK; |
| 5221 | txn->flags |= TX_CK_DOWN; |
| 5222 | } |
| 5223 | } |
| 5224 | srv = srv->next; |
| 5225 | } |
| 5226 | } |
| 5227 | } |
| 5228 | } |
| 5229 | |
| 5230 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5231 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5232 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5233 | */ |
| 5234 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5235 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5236 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5237 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5238 | char *del_colon, *del_cookie, *colon; |
| 5239 | int app_cookies; |
| 5240 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5241 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5242 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5243 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5244 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5245 | * we start with the start line. |
| 5246 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5247 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5248 | 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] | 5249 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5250 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5251 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5252 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5253 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5254 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5255 | cur_ptr = cur_next; |
| 5256 | cur_end = cur_ptr + cur_hdr->len; |
| 5257 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5258 | |
| 5259 | /* We have one full header between cur_ptr and cur_end, and the |
| 5260 | * next header starts at cur_next. We're only interested in |
| 5261 | * "Cookie:" headers. |
| 5262 | */ |
| 5263 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5264 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5265 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5266 | old_idx = cur_idx; |
| 5267 | continue; |
| 5268 | } |
| 5269 | |
| 5270 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5271 | * attributes whose name begin with a '$', and associate them with |
| 5272 | * the right cookie, if we want to delete this cookie. |
| 5273 | * So there are 3 cases for each cookie read : |
| 5274 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5275 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5276 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5277 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5278 | * "special" cookie. |
| 5279 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5280 | * remove it. If no application cookie persists in the header, we |
| 5281 | * *MUST* delete it |
| 5282 | */ |
| 5283 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5284 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5285 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5286 | /* del_cookie == NULL => nothing to be deleted */ |
| 5287 | del_colon = del_cookie = NULL; |
| 5288 | app_cookies = 0; |
| 5289 | |
| 5290 | while (p1 < cur_end) { |
| 5291 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5292 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5293 | while (p1 < cur_end) { |
| 5294 | if (*p1 == ';' || *p1 == ',') |
| 5295 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5296 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5297 | break; |
| 5298 | p1++; |
| 5299 | } |
| 5300 | |
| 5301 | if (p1 == cur_end) |
| 5302 | break; |
| 5303 | |
| 5304 | /* p1 is at the beginning of the cookie name */ |
| 5305 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5306 | while (p2 < cur_end && *p2 != '=') { |
| 5307 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5308 | /* oops, the cookie name was truncated, resync */ |
| 5309 | p1 = p2; |
| 5310 | goto resync_name; |
| 5311 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5312 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5313 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5314 | |
| 5315 | if (p2 == cur_end) |
| 5316 | break; |
| 5317 | |
| 5318 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5319 | if (p3 == cur_end) |
| 5320 | break; |
| 5321 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5322 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5323 | p5 = p4 = p3; |
| 5324 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5325 | if (!isspace((unsigned char)*p5)) |
| 5326 | p4 = p5 + 1; |
| 5327 | p5++; |
| 5328 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5329 | |
| 5330 | /* here, we have the cookie name between p1 and p2, |
| 5331 | * and its value between p3 and p4. |
| 5332 | * we can process it : |
| 5333 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5334 | * Cookie: NAME=VALUE ; |
| 5335 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5336 | * | || || +--> p4 |
| 5337 | * | || |+-------> p3 |
| 5338 | * | || +--------> p2 |
| 5339 | * | |+------------> p1 |
| 5340 | * | +-------------> colon |
| 5341 | * +--------------------> cur_ptr |
| 5342 | */ |
| 5343 | |
| 5344 | if (*p1 == '$') { |
| 5345 | /* skip this one */ |
| 5346 | } |
| 5347 | else { |
| 5348 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5349 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5350 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5351 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5352 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5353 | int log_len = p4 - p1; |
| 5354 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5355 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5356 | Alert("HTTP logging : out of memory.\n"); |
| 5357 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5358 | if (log_len > t->fe->capture_len) |
| 5359 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5360 | memcpy(txn->cli_cookie, p1, log_len); |
| 5361 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5362 | } |
| 5363 | } |
| 5364 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5365 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5366 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5367 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5368 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5369 | char *delim; |
| 5370 | |
| 5371 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5372 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5373 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5374 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5375 | * Cookie: NAME=SRV~VALUE ; |
| 5376 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5377 | * | || || | +--> p4 |
| 5378 | * | || || +--------> delim |
| 5379 | * | || |+-----------> p3 |
| 5380 | * | || +------------> p2 |
| 5381 | * | |+----------------> p1 |
| 5382 | * | +-----------------> colon |
| 5383 | * +------------------------> cur_ptr |
| 5384 | */ |
| 5385 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5386 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5387 | for (delim = p3; delim < p4; delim++) |
| 5388 | if (*delim == COOKIE_DELIM) |
| 5389 | break; |
| 5390 | } |
| 5391 | else |
| 5392 | delim = p4; |
| 5393 | |
| 5394 | |
| 5395 | /* Here, we'll look for the first running server which supports the cookie. |
| 5396 | * This allows to share a same cookie between several servers, for example |
| 5397 | * to dedicate backup servers to specific servers only. |
| 5398 | * However, to prevent clients from sticking to cookie-less backup server |
| 5399 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5400 | * empty cookies and mark them as invalid. |
| 5401 | */ |
| 5402 | if (delim == p3) |
| 5403 | srv = NULL; |
| 5404 | |
| 5405 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5406 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5407 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5408 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5409 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5410 | txn->flags &= ~TX_CK_MASK; |
| 5411 | txn->flags |= TX_CK_VALID; |
| 5412 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5413 | t->srv = srv; |
| 5414 | break; |
| 5415 | } else { |
| 5416 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5417 | txn->flags &= ~TX_CK_MASK; |
| 5418 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5419 | } |
| 5420 | } |
| 5421 | srv = srv->next; |
| 5422 | } |
| 5423 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5424 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5425 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5426 | txn->flags &= ~TX_CK_MASK; |
| 5427 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | /* depending on the cookie mode, we may have to either : |
| 5431 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5432 | * the server never sees it ; |
| 5433 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5434 | * application cookie so that it does not get accidentely removed later, |
| 5435 | * if we're in cookie prefix mode |
| 5436 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5437 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5438 | int delta; /* negative */ |
| 5439 | |
| 5440 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5441 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5442 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5443 | cur_end += delta; |
| 5444 | cur_next += delta; |
| 5445 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5446 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5447 | |
| 5448 | del_cookie = del_colon = NULL; |
| 5449 | app_cookies++; /* protect the header from deletion */ |
| 5450 | } |
| 5451 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5452 | (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] | 5453 | del_cookie = p1; |
| 5454 | del_colon = colon; |
| 5455 | } |
| 5456 | } else { |
| 5457 | /* now we know that we must keep this cookie since it's |
| 5458 | * not ours. But if we wanted to delete our cookie |
| 5459 | * earlier, we cannot remove the complete header, but we |
| 5460 | * can remove the previous block itself. |
| 5461 | */ |
| 5462 | app_cookies++; |
| 5463 | |
| 5464 | if (del_cookie != NULL) { |
| 5465 | int delta; /* negative */ |
| 5466 | |
| 5467 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5468 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5469 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5470 | cur_end += delta; |
| 5471 | cur_next += delta; |
| 5472 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5473 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5474 | del_cookie = del_colon = NULL; |
| 5475 | } |
| 5476 | } |
| 5477 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5478 | if (t->be->appsession_name != NULL) { |
| 5479 | int cmp_len, value_len; |
| 5480 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5481 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5482 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5483 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5484 | value_begin = p1 + t->be->appsession_name_len; |
| 5485 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5486 | } else { |
| 5487 | cmp_len = p2 - p1; |
| 5488 | value_begin = p3; |
| 5489 | value_len = p4 - p3; |
| 5490 | } |
| 5491 | |
| 5492 | /* let's see if the cookie is our appcookie */ |
| 5493 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5494 | /* Cool... it's the right one */ |
| 5495 | manage_client_side_appsession(t, value_begin, value_len); |
| 5496 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5497 | #if defined(DEBUG_HASH) |
| 5498 | Alert("manage_client_side_cookies\n"); |
| 5499 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5500 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5501 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5502 | } |
| 5503 | |
| 5504 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5505 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5506 | } /* while (p1 < cur_end) */ |
| 5507 | |
| 5508 | /* There's no more cookie on this line. |
| 5509 | * We may have marked the last one(s) for deletion. |
| 5510 | * We must do this now in two ways : |
| 5511 | * - if there is no app cookie, we simply delete the header ; |
| 5512 | * - if there are app cookies, we must delete the end of the |
| 5513 | * string properly, including the colon/semi-colon before |
| 5514 | * the cookie name. |
| 5515 | */ |
| 5516 | if (del_cookie != NULL) { |
| 5517 | int delta; |
| 5518 | if (app_cookies) { |
| 5519 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5520 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5521 | cur_hdr->len += delta; |
| 5522 | } else { |
| 5523 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5524 | |
| 5525 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5526 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5527 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5528 | cur_hdr->len = 0; |
| 5529 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5530 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5531 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | /* keep the link from this header to next one */ |
| 5535 | old_idx = cur_idx; |
| 5536 | } /* end of cookie processing on this header */ |
| 5537 | } |
| 5538 | |
| 5539 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5540 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5541 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5542 | */ |
| 5543 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5544 | { |
| 5545 | char term; |
| 5546 | char *cur_ptr, *cur_end, *cur_next; |
| 5547 | int cur_idx, old_idx, last_hdr; |
| 5548 | struct http_txn *txn = &t->txn; |
| 5549 | struct hdr_idx_elem *cur_hdr; |
| 5550 | int len, delta; |
| 5551 | |
| 5552 | last_hdr = 0; |
| 5553 | |
| 5554 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5555 | old_idx = 0; |
| 5556 | |
| 5557 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5558 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5559 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5560 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5561 | (exp->action == ACT_ALLOW || |
| 5562 | exp->action == ACT_DENY)) |
| 5563 | return 0; |
| 5564 | |
| 5565 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5566 | if (!cur_idx) |
| 5567 | break; |
| 5568 | |
| 5569 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5570 | cur_ptr = cur_next; |
| 5571 | cur_end = cur_ptr + cur_hdr->len; |
| 5572 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5573 | |
| 5574 | /* Now we have one header between cur_ptr and cur_end, |
| 5575 | * and the next header starts at cur_next. |
| 5576 | */ |
| 5577 | |
| 5578 | /* The annoying part is that pattern matching needs |
| 5579 | * that we modify the contents to null-terminate all |
| 5580 | * strings before testing them. |
| 5581 | */ |
| 5582 | |
| 5583 | term = *cur_end; |
| 5584 | *cur_end = '\0'; |
| 5585 | |
| 5586 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5587 | switch (exp->action) { |
| 5588 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5589 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5590 | last_hdr = 1; |
| 5591 | break; |
| 5592 | |
| 5593 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5594 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5595 | last_hdr = 1; |
| 5596 | break; |
| 5597 | |
| 5598 | case ACT_REPLACE: |
| 5599 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5600 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5601 | /* FIXME: if the user adds a newline in the replacement, the |
| 5602 | * index will not be recalculated for now, and the new line |
| 5603 | * will not be counted as a new header. |
| 5604 | */ |
| 5605 | |
| 5606 | cur_end += delta; |
| 5607 | cur_next += delta; |
| 5608 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5609 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5610 | break; |
| 5611 | |
| 5612 | case ACT_REMOVE: |
| 5613 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5614 | cur_next += delta; |
| 5615 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5616 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5617 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5618 | txn->hdr_idx.used--; |
| 5619 | cur_hdr->len = 0; |
| 5620 | cur_end = NULL; /* null-term has been rewritten */ |
| 5621 | break; |
| 5622 | |
| 5623 | } |
| 5624 | } |
| 5625 | if (cur_end) |
| 5626 | *cur_end = term; /* restore the string terminator */ |
| 5627 | |
| 5628 | /* keep the link from this header to next one in case of later |
| 5629 | * removal of next header. |
| 5630 | */ |
| 5631 | old_idx = cur_idx; |
| 5632 | } |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
| 5636 | |
| 5637 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5638 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5639 | * or -1 if a replacement resulted in an invalid status line. |
| 5640 | */ |
| 5641 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5642 | { |
| 5643 | char term; |
| 5644 | char *cur_ptr, *cur_end; |
| 5645 | int done; |
| 5646 | struct http_txn *txn = &t->txn; |
| 5647 | int len, delta; |
| 5648 | |
| 5649 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5650 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5651 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5652 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5653 | (exp->action == ACT_ALLOW || |
| 5654 | exp->action == ACT_DENY)) |
| 5655 | return 0; |
| 5656 | else if (exp->action == ACT_REMOVE) |
| 5657 | return 0; |
| 5658 | |
| 5659 | done = 0; |
| 5660 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5661 | 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] | 5662 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5663 | |
| 5664 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5665 | |
| 5666 | /* The annoying part is that pattern matching needs |
| 5667 | * that we modify the contents to null-terminate all |
| 5668 | * strings before testing them. |
| 5669 | */ |
| 5670 | |
| 5671 | term = *cur_end; |
| 5672 | *cur_end = '\0'; |
| 5673 | |
| 5674 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5675 | switch (exp->action) { |
| 5676 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5677 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5678 | done = 1; |
| 5679 | break; |
| 5680 | |
| 5681 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5682 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5683 | done = 1; |
| 5684 | break; |
| 5685 | |
| 5686 | case ACT_REPLACE: |
| 5687 | *cur_end = term; /* restore the string terminator */ |
| 5688 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5689 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5690 | /* FIXME: if the user adds a newline in the replacement, the |
| 5691 | * index will not be recalculated for now, and the new line |
| 5692 | * will not be counted as a new header. |
| 5693 | */ |
| 5694 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5695 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5696 | cur_end += delta; |
| 5697 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5698 | 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] | 5699 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5700 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5701 | cur_ptr, cur_end + 1, |
| 5702 | NULL, NULL); |
| 5703 | if (unlikely(!cur_end)) |
| 5704 | return -1; |
| 5705 | |
| 5706 | /* we have a full respnse and we know that we have either a CR |
| 5707 | * or an LF at <ptr>. |
| 5708 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5709 | 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] | 5710 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5711 | /* there is no point trying this regex on headers */ |
| 5712 | return 1; |
| 5713 | } |
| 5714 | } |
| 5715 | *cur_end = term; /* restore the string terminator */ |
| 5716 | return done; |
| 5717 | } |
| 5718 | |
| 5719 | |
| 5720 | |
| 5721 | /* |
| 5722 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5723 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5724 | * unparsable response. |
| 5725 | */ |
| 5726 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5727 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5728 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5729 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5730 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5731 | int ret; |
| 5732 | |
| 5733 | /* |
| 5734 | * The interleaving of transformations and verdicts |
| 5735 | * makes it difficult to decide to continue or stop |
| 5736 | * the evaluation. |
| 5737 | */ |
| 5738 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5739 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5740 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5741 | exp->action == ACT_PASS)) { |
| 5742 | exp = exp->next; |
| 5743 | continue; |
| 5744 | } |
| 5745 | |
| 5746 | /* Apply the filter to the status line. */ |
| 5747 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5748 | if (unlikely(ret < 0)) |
| 5749 | return -1; |
| 5750 | |
| 5751 | if (likely(ret == 0)) { |
| 5752 | /* The filter did not match the response, it can be |
| 5753 | * iterated through all headers. |
| 5754 | */ |
| 5755 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5756 | } |
| 5757 | exp = exp->next; |
| 5758 | } |
| 5759 | return 0; |
| 5760 | } |
| 5761 | |
| 5762 | |
| 5763 | |
| 5764 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5765 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5766 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5767 | */ |
| 5768 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5769 | { |
| 5770 | struct http_txn *txn = &t->txn; |
| 5771 | char *p1, *p2, *p3, *p4; |
| 5772 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5773 | char *cur_ptr, *cur_end, *cur_next; |
| 5774 | int cur_idx, old_idx, delta; |
| 5775 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5776 | /* Iterate through the headers. |
| 5777 | * we start with the start line. |
| 5778 | */ |
| 5779 | old_idx = 0; |
| 5780 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5781 | |
| 5782 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5783 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5784 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5785 | |
| 5786 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5787 | cur_ptr = cur_next; |
| 5788 | cur_end = cur_ptr + cur_hdr->len; |
| 5789 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5790 | |
| 5791 | /* We have one full header between cur_ptr and cur_end, and the |
| 5792 | * next header starts at cur_next. We're only interested in |
| 5793 | * "Cookie:" headers. |
| 5794 | */ |
| 5795 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5796 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5797 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5798 | old_idx = cur_idx; |
| 5799 | continue; |
| 5800 | } |
| 5801 | |
| 5802 | /* 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] | 5803 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5804 | |
| 5805 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5806 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 5807 | * the cookie capture is declared in the fronend. |
| 5808 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5809 | if (t->be->cookie_name == NULL && |
| 5810 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5811 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5812 | return; |
| 5813 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5814 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5815 | |
| 5816 | 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] | 5817 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5818 | break; |
| 5819 | |
| 5820 | /* p1 is at the beginning of the cookie name */ |
| 5821 | p2 = p1; |
| 5822 | |
| 5823 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5824 | p2++; |
| 5825 | |
| 5826 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5827 | break; |
| 5828 | |
| 5829 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5830 | if (p3 == cur_end) |
| 5831 | break; |
| 5832 | |
| 5833 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5834 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5835 | p4++; |
| 5836 | |
| 5837 | /* here, we have the cookie name between p1 and p2, |
| 5838 | * and its value between p3 and p4. |
| 5839 | * we can process it. |
| 5840 | */ |
| 5841 | |
| 5842 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5843 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5844 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5845 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5846 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5847 | int log_len = p4 - p1; |
| 5848 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5849 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5850 | Alert("HTTP logging : out of memory.\n"); |
| 5851 | } |
| 5852 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5853 | if (log_len > t->fe->capture_len) |
| 5854 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5855 | memcpy(txn->srv_cookie, p1, log_len); |
| 5856 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5857 | } |
| 5858 | |
| 5859 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5860 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5861 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5862 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5863 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5864 | |
| 5865 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5866 | * this occurrence because we'll insert another one later. |
| 5867 | * We'll delete it too if the "indirect" option is set and we're in |
| 5868 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5869 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5870 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5871 | /* this header must be deleted */ |
| 5872 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5873 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5874 | txn->hdr_idx.used--; |
| 5875 | cur_hdr->len = 0; |
| 5876 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5877 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5878 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5879 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5880 | } |
| 5881 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5882 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5883 | /* replace bytes p3->p4 with the cookie name associated |
| 5884 | * with this server since we know it. |
| 5885 | */ |
| 5886 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5887 | cur_hdr->len += delta; |
| 5888 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5889 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5890 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5891 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5892 | } |
| 5893 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5894 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5895 | /* insert the cookie name associated with this server |
| 5896 | * before existing cookie, and insert a delimitor between them.. |
| 5897 | */ |
| 5898 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5899 | cur_hdr->len += delta; |
| 5900 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5901 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5902 | |
| 5903 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5904 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5905 | } |
| 5906 | } |
| 5907 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5908 | else if (t->be->appsession_name != NULL) { |
| 5909 | int cmp_len, value_len; |
| 5910 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5911 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5912 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5913 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5914 | value_begin = p1 + t->be->appsession_name_len; |
| 5915 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 5916 | } else { |
| 5917 | cmp_len = p2 - p1; |
| 5918 | value_begin = p3; |
| 5919 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5920 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5921 | |
| 5922 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5923 | /* Cool... it's the right one */ |
| 5924 | if (t->sessid != NULL) { |
| 5925 | /* free previously allocated memory as we don't need it anymore */ |
| 5926 | pool_free2(apools.sessid, t->sessid); |
| 5927 | } |
| 5928 | /* Store the sessid in the session for future use */ |
| 5929 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5930 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5931 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5932 | return; |
| 5933 | } |
| 5934 | memcpy(t->sessid, value_begin, value_len); |
| 5935 | t->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5936 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5937 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5938 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5939 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5940 | /* keep the link from this header to next one */ |
| 5941 | old_idx = cur_idx; |
| 5942 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5943 | |
| 5944 | if (t->sessid != NULL) { |
| 5945 | appsess *asession = NULL; |
| 5946 | /* only do insert, if lookup fails */ |
| 5947 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid); |
| 5948 | if (asession == NULL) { |
| 5949 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 5950 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5951 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5952 | return; |
| 5953 | } |
| 5954 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5955 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5956 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5957 | return; |
| 5958 | } |
| 5959 | memcpy(asession->sessid, t->sessid, t->be->appsession_len); |
| 5960 | asession->sessid[t->be->appsession_len] = 0; |
| 5961 | |
| 5962 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5963 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 5964 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5965 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5966 | return; |
| 5967 | } |
| 5968 | asession->serverid[0] = '\0'; |
| 5969 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 5970 | |
| 5971 | asession->request_count = 0; |
| 5972 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 5973 | } |
| 5974 | |
| 5975 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5976 | asession->request_count++; |
| 5977 | } |
| 5978 | |
| 5979 | #if defined(DEBUG_HASH) |
| 5980 | Alert("manage_server_side_cookies\n"); |
| 5981 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5982 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | |
| 5986 | |
| 5987 | /* |
| 5988 | * Check if response is cacheable or not. Updates t->flags. |
| 5989 | */ |
| 5990 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5991 | { |
| 5992 | struct http_txn *txn = &t->txn; |
| 5993 | char *p1, *p2; |
| 5994 | |
| 5995 | char *cur_ptr, *cur_end, *cur_next; |
| 5996 | int cur_idx; |
| 5997 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5998 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5999 | return; |
| 6000 | |
| 6001 | /* Iterate through the headers. |
| 6002 | * we start with the start line. |
| 6003 | */ |
| 6004 | cur_idx = 0; |
| 6005 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 6006 | |
| 6007 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6008 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6009 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6010 | |
| 6011 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6012 | cur_ptr = cur_next; |
| 6013 | cur_end = cur_ptr + cur_hdr->len; |
| 6014 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6015 | |
| 6016 | /* We have one full header between cur_ptr and cur_end, and the |
| 6017 | * next header starts at cur_next. We're only interested in |
| 6018 | * "Cookie:" headers. |
| 6019 | */ |
| 6020 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6021 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6022 | if (val) { |
| 6023 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6024 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6025 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6026 | return; |
| 6027 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6028 | } |
| 6029 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6030 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6031 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6032 | continue; |
| 6033 | |
| 6034 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6035 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6036 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6037 | |
| 6038 | if (p1 >= cur_end) /* no more info */ |
| 6039 | continue; |
| 6040 | |
| 6041 | /* p1 is at the beginning of the value */ |
| 6042 | p2 = p1; |
| 6043 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6044 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6045 | p2++; |
| 6046 | |
| 6047 | /* we have a complete value between p1 and p2 */ |
| 6048 | if (p2 < cur_end && *p2 == '=') { |
| 6049 | /* we have something of the form no-cache="set-cookie" */ |
| 6050 | if ((cur_end - p1 >= 21) && |
| 6051 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6052 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6053 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6054 | continue; |
| 6055 | } |
| 6056 | |
| 6057 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6058 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6059 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6060 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6061 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6062 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6063 | return; |
| 6064 | } |
| 6065 | |
| 6066 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6067 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6068 | continue; |
| 6069 | } |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6074 | /* |
| 6075 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6076 | * If the server is found, it's assigned to the session. |
| 6077 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6078 | 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] | 6079 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6080 | char *end_params, *first_param, *cur_param, *next_param; |
| 6081 | char separator; |
| 6082 | int value_len; |
| 6083 | |
| 6084 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6085 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6086 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6087 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6088 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6089 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6090 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6091 | first_param = NULL; |
| 6092 | switch (mode) { |
| 6093 | case PR_O2_AS_M_PP: |
| 6094 | first_param = memchr(begin, ';', len); |
| 6095 | break; |
| 6096 | case PR_O2_AS_M_QS: |
| 6097 | first_param = memchr(begin, '?', len); |
| 6098 | break; |
| 6099 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6100 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6101 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6102 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6103 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6104 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6105 | switch (mode) { |
| 6106 | case PR_O2_AS_M_PP: |
| 6107 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6108 | end_params = (char *) begin + len; |
| 6109 | } |
| 6110 | separator = ';'; |
| 6111 | break; |
| 6112 | case PR_O2_AS_M_QS: |
| 6113 | end_params = (char *) begin + len; |
| 6114 | separator = '&'; |
| 6115 | break; |
| 6116 | default: |
| 6117 | /* unknown mode, shouldn't happen */ |
| 6118 | return; |
| 6119 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6120 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6121 | cur_param = next_param = end_params; |
| 6122 | while (cur_param > first_param) { |
| 6123 | cur_param--; |
| 6124 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6125 | /* let's see if this is the appsession parameter */ |
| 6126 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6127 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6128 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6129 | /* Cool... it's the right one */ |
| 6130 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6131 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6132 | if (value_len > 0) { |
| 6133 | manage_client_side_appsession(t, cur_param, value_len); |
| 6134 | } |
| 6135 | break; |
| 6136 | } |
| 6137 | next_param = cur_param; |
| 6138 | } |
| 6139 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6140 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6141 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6142 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6143 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6147 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6148 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 6149 | * 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] | 6150 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6151 | * 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] | 6152 | * 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] | 6153 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6154 | * |
| 6155 | * Returns 1 if the session's state changes, otherwise 0. |
| 6156 | */ |
| 6157 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 6158 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6159 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6160 | struct uri_auth *uri_auth = backend->uri_auth; |
| 6161 | struct user_auth *user; |
| 6162 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6163 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6164 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6165 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6166 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6167 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6168 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6169 | return 0; |
| 6170 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6171 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6172 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6173 | /* the URI is in h */ |
| 6174 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6175 | return 0; |
| 6176 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6177 | h += uri_auth->uri_len; |
| 6178 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 6179 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6180 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6181 | break; |
| 6182 | } |
| 6183 | h++; |
| 6184 | } |
| 6185 | |
| 6186 | if (uri_auth->refresh) { |
| 6187 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6188 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 6189 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6190 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6191 | break; |
| 6192 | } |
| 6193 | h++; |
| 6194 | } |
| 6195 | } |
| 6196 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6197 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6198 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 6199 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6200 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6201 | break; |
| 6202 | } |
| 6203 | h++; |
| 6204 | } |
| 6205 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6206 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6207 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6208 | /* we are in front of a interceptable URI. Let's check |
| 6209 | * if there's an authentication and if it's valid. |
| 6210 | */ |
| 6211 | user = uri_auth->users; |
| 6212 | if (!user) { |
| 6213 | /* no user auth required, it's OK */ |
| 6214 | authenticated = 1; |
| 6215 | } else { |
| 6216 | authenticated = 0; |
| 6217 | |
| 6218 | /* a user list is defined, we have to check. |
| 6219 | * skip 21 chars for "Authorization: Basic ". |
| 6220 | */ |
| 6221 | |
| 6222 | /* FIXME: this should move to an earlier place */ |
| 6223 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6224 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 6225 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6226 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6227 | if (len > 14 && |
| 6228 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 6229 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6230 | break; |
| 6231 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6232 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6233 | } |
| 6234 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6235 | if (txn->auth_hdr.len < 21 || |
| 6236 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6237 | user = NULL; |
| 6238 | |
| 6239 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6240 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 6241 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6242 | user->user_pwd, user->user_len)) { |
| 6243 | authenticated = 1; |
| 6244 | break; |
| 6245 | } |
| 6246 | user = user->next; |
| 6247 | } |
| 6248 | } |
| 6249 | |
| 6250 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6251 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6252 | |
| 6253 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 6254 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 6255 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6256 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 6257 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 6258 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6259 | if (!(t->flags & SN_ERR_MASK)) |
| 6260 | t->flags |= SN_ERR_PRXCOND; |
| 6261 | if (!(t->flags & SN_FINST_MASK)) |
| 6262 | t->flags |= SN_FINST_R; |
| 6263 | return 1; |
| 6264 | } |
| 6265 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6266 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6267 | * data. |
| 6268 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 6269 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6270 | t->data_source = DATA_SRC_STATS; |
| 6271 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 6272 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 6273 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 6274 | t->rep->prod->private = t; |
| 6275 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6276 | return 1; |
| 6277 | } |
| 6278 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6279 | /* |
| 6280 | * Capture a bad request or response and archive it in the proxy's structure. |
| 6281 | */ |
| 6282 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6283 | struct buffer *buf, struct http_msg *msg, |
| 6284 | struct proxy *other_end) |
| 6285 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6286 | es->len = buf->r - (buf->data + msg->som); |
| 6287 | 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] | 6288 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6289 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6290 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6291 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6292 | es->when = date; // user-visible date |
| 6293 | es->sid = s->uniq_id; |
| 6294 | es->srv = s->srv; |
| 6295 | es->oe = other_end; |
| 6296 | es->src = s->cli_addr; |
| 6297 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6298 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6299 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6300 | * Print a debug line with a header |
| 6301 | */ |
| 6302 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6303 | { |
| 6304 | int len, max; |
| 6305 | 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] | 6306 | 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] | 6307 | max = end - start; |
| 6308 | UBOUND(max, sizeof(trash) - len - 1); |
| 6309 | len += strlcpy2(trash + len, start, max + 1); |
| 6310 | trash[len++] = '\n'; |
| 6311 | write(1, trash, len); |
| 6312 | } |
| 6313 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6314 | /* |
| 6315 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6316 | * the required fields are properly allocated and that we only need to (re)init |
| 6317 | * them. This should be used before processing any new request. |
| 6318 | */ |
| 6319 | void http_init_txn(struct session *s) |
| 6320 | { |
| 6321 | struct http_txn *txn = &s->txn; |
| 6322 | struct proxy *fe = s->fe; |
| 6323 | |
| 6324 | txn->flags = 0; |
| 6325 | txn->status = -1; |
| 6326 | |
| 6327 | txn->req.sol = txn->req.eol = NULL; |
| 6328 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6329 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6330 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6331 | txn->req.hdr_content_len = 0LL; |
| 6332 | txn->rsp.hdr_content_len = 0LL; |
| 6333 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6334 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 6335 | chunk_reset(&txn->auth_hdr); |
| 6336 | |
| 6337 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6338 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6339 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6340 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6341 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6342 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6343 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6344 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6345 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6346 | |
| 6347 | if (txn->hdr_idx.v) |
| 6348 | hdr_idx_init(&txn->hdr_idx); |
| 6349 | } |
| 6350 | |
| 6351 | /* to be used at the end of a transaction */ |
| 6352 | void http_end_txn(struct session *s) |
| 6353 | { |
| 6354 | struct http_txn *txn = &s->txn; |
| 6355 | |
| 6356 | /* these ones will have been dynamically allocated */ |
| 6357 | pool_free2(pool2_requri, txn->uri); |
| 6358 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6359 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | 762a236 | 2010-01-09 13:57:26 +0100 | [diff] [blame^] | 6360 | pool_free2(apools.sessid, s->sessid); |
| 6361 | s->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6362 | txn->uri = NULL; |
| 6363 | txn->srv_cookie = NULL; |
| 6364 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6365 | |
| 6366 | if (txn->req.cap) { |
| 6367 | struct cap_hdr *h; |
| 6368 | for (h = s->fe->req_cap; h; h = h->next) |
| 6369 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 6370 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 6371 | } |
| 6372 | |
| 6373 | if (txn->rsp.cap) { |
| 6374 | struct cap_hdr *h; |
| 6375 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 6376 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 6377 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 6378 | } |
| 6379 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6380 | } |
| 6381 | |
| 6382 | /* to be used at the end of a transaction to prepare a new one */ |
| 6383 | void http_reset_txn(struct session *s) |
| 6384 | { |
| 6385 | http_end_txn(s); |
| 6386 | http_init_txn(s); |
| 6387 | |
| 6388 | s->be = s->fe; |
| 6389 | s->req->analysers = s->listener->analysers; |
| 6390 | s->logs.logwait = s->fe->to_log; |
| 6391 | s->srv = s->prev_srv = s->srv_conn = NULL; |
| 6392 | s->pend_pos = NULL; |
| 6393 | s->conn_retries = s->be->conn_retries; |
| 6394 | |
| 6395 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6396 | |
| 6397 | s->req->rto = s->fe->timeout.client; |
| 6398 | s->req->wto = s->be->timeout.server; |
| 6399 | s->req->cto = s->be->timeout.connect; |
| 6400 | |
| 6401 | s->rep->rto = s->be->timeout.server; |
| 6402 | s->rep->wto = s->fe->timeout.client; |
| 6403 | s->rep->cto = TICK_ETERNITY; |
| 6404 | |
| 6405 | s->req->rex = TICK_ETERNITY; |
| 6406 | s->req->wex = TICK_ETERNITY; |
| 6407 | s->req->analyse_exp = TICK_ETERNITY; |
| 6408 | s->rep->rex = TICK_ETERNITY; |
| 6409 | s->rep->wex = TICK_ETERNITY; |
| 6410 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6411 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6412 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6413 | /************************************************************************/ |
| 6414 | /* The code below is dedicated to ACL parsing and matching */ |
| 6415 | /************************************************************************/ |
| 6416 | |
| 6417 | |
| 6418 | |
| 6419 | |
| 6420 | /* 1. Check on METHOD |
| 6421 | * We use the pre-parsed method if it is known, and store its number as an |
| 6422 | * integer. If it is unknown, we use the pointer and the length. |
| 6423 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6424 | 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] | 6425 | { |
| 6426 | int len, meth; |
| 6427 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6428 | len = strlen(*text); |
| 6429 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6430 | |
| 6431 | pattern->val.i = meth; |
| 6432 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6433 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6434 | if (!pattern->ptr.str) |
| 6435 | return 0; |
| 6436 | pattern->len = len; |
| 6437 | } |
| 6438 | return 1; |
| 6439 | } |
| 6440 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6441 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6442 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6443 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6444 | { |
| 6445 | int meth; |
| 6446 | struct http_txn *txn = l7; |
| 6447 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6448 | if (!txn) |
| 6449 | return 0; |
| 6450 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6451 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6452 | return 0; |
| 6453 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6454 | meth = txn->meth; |
| 6455 | test->i = meth; |
| 6456 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6457 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6458 | /* ensure the indexes are not affected */ |
| 6459 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6460 | test->len = txn->req.sl.rq.m_l; |
| 6461 | test->ptr = txn->req.sol; |
| 6462 | } |
| 6463 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6464 | return 1; |
| 6465 | } |
| 6466 | |
| 6467 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6468 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6469 | int icase; |
| 6470 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6471 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6472 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6473 | |
| 6474 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6475 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6476 | |
| 6477 | /* Other method, we must compare the strings */ |
| 6478 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6479 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6480 | |
| 6481 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6482 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6483 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6484 | return ACL_PAT_FAIL; |
| 6485 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6486 | } |
| 6487 | |
| 6488 | /* 2. Check on Request/Status Version |
| 6489 | * We simply compare strings here. |
| 6490 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6491 | 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] | 6492 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6493 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6494 | if (!pattern->ptr.str) |
| 6495 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6496 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6497 | return 1; |
| 6498 | } |
| 6499 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6500 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6501 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6502 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6503 | { |
| 6504 | struct http_txn *txn = l7; |
| 6505 | char *ptr; |
| 6506 | int len; |
| 6507 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6508 | if (!txn) |
| 6509 | return 0; |
| 6510 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6511 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6512 | return 0; |
| 6513 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6514 | len = txn->req.sl.rq.v_l; |
| 6515 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 6516 | |
| 6517 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6518 | if (len <= 0) |
| 6519 | return 0; |
| 6520 | |
| 6521 | test->ptr = ptr; |
| 6522 | test->len = len; |
| 6523 | |
| 6524 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6525 | return 1; |
| 6526 | } |
| 6527 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6528 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6529 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6530 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6531 | { |
| 6532 | struct http_txn *txn = l7; |
| 6533 | char *ptr; |
| 6534 | int len; |
| 6535 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6536 | if (!txn) |
| 6537 | return 0; |
| 6538 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6539 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6540 | return 0; |
| 6541 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6542 | len = txn->rsp.sl.st.v_l; |
| 6543 | ptr = txn->rsp.sol; |
| 6544 | |
| 6545 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6546 | if (len <= 0) |
| 6547 | return 0; |
| 6548 | |
| 6549 | test->ptr = ptr; |
| 6550 | test->len = len; |
| 6551 | |
| 6552 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6553 | return 1; |
| 6554 | } |
| 6555 | |
| 6556 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6557 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6558 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6559 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6560 | { |
| 6561 | struct http_txn *txn = l7; |
| 6562 | char *ptr; |
| 6563 | int len; |
| 6564 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6565 | if (!txn) |
| 6566 | return 0; |
| 6567 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6568 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6569 | return 0; |
| 6570 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6571 | len = txn->rsp.sl.st.c_l; |
| 6572 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 6573 | |
| 6574 | test->i = __strl2ui(ptr, len); |
| 6575 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6576 | return 1; |
| 6577 | } |
| 6578 | |
| 6579 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6580 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6581 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6582 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6583 | { |
| 6584 | struct http_txn *txn = l7; |
| 6585 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6586 | if (!txn) |
| 6587 | return 0; |
| 6588 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6589 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6590 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6591 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6592 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6593 | /* ensure the indexes are not affected */ |
| 6594 | return 0; |
| 6595 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6596 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6597 | 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] | 6598 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6599 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6600 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6601 | return 1; |
| 6602 | } |
| 6603 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6604 | static int |
| 6605 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6606 | struct acl_expr *expr, struct acl_test *test) |
| 6607 | { |
| 6608 | struct http_txn *txn = l7; |
| 6609 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6610 | if (!txn) |
| 6611 | return 0; |
| 6612 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6613 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6614 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6615 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6616 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6617 | /* ensure the indexes are not affected */ |
| 6618 | return 0; |
| 6619 | |
| 6620 | /* Parse HTTP request */ |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6621 | 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] | 6622 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6623 | test->i = AF_INET; |
| 6624 | |
| 6625 | /* |
| 6626 | * If we are parsing url in frontend space, we prepare backend stage |
| 6627 | * to not parse again the same url ! optimization lazyness... |
| 6628 | */ |
| 6629 | if (px->options & PR_O_HTTP_PROXY) |
| 6630 | l4->flags |= SN_ADDR_SET; |
| 6631 | |
| 6632 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6633 | return 1; |
| 6634 | } |
| 6635 | |
| 6636 | static int |
| 6637 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6638 | struct acl_expr *expr, struct acl_test *test) |
| 6639 | { |
| 6640 | struct http_txn *txn = l7; |
| 6641 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6642 | if (!txn) |
| 6643 | return 0; |
| 6644 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6645 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6646 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6647 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6648 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6649 | /* ensure the indexes are not affected */ |
| 6650 | return 0; |
| 6651 | |
| 6652 | /* Same optimization as url_ip */ |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6653 | 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] | 6654 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6655 | |
| 6656 | if (px->options & PR_O_HTTP_PROXY) |
| 6657 | l4->flags |= SN_ADDR_SET; |
| 6658 | |
| 6659 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6660 | return 1; |
| 6661 | } |
| 6662 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6663 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6664 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6665 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6666 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6667 | 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] | 6668 | struct acl_expr *expr, struct acl_test *test) |
| 6669 | { |
| 6670 | struct http_txn *txn = l7; |
| 6671 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6672 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6673 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6674 | if (!txn) |
| 6675 | return 0; |
| 6676 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6677 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6678 | /* search for header from the beginning */ |
| 6679 | ctx->idx = 0; |
| 6680 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6681 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6682 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6683 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6684 | test->len = ctx->vlen; |
| 6685 | test->ptr = (char *)ctx->line + ctx->val; |
| 6686 | return 1; |
| 6687 | } |
| 6688 | |
| 6689 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6690 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6691 | return 0; |
| 6692 | } |
| 6693 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6694 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6695 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6696 | struct acl_expr *expr, struct acl_test *test) |
| 6697 | { |
| 6698 | struct http_txn *txn = l7; |
| 6699 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6700 | if (!txn) |
| 6701 | return 0; |
| 6702 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6703 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6704 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6705 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6706 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6707 | /* ensure the indexes are not affected */ |
| 6708 | return 0; |
| 6709 | |
| 6710 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6711 | } |
| 6712 | |
| 6713 | static int |
| 6714 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6715 | struct acl_expr *expr, struct acl_test *test) |
| 6716 | { |
| 6717 | struct http_txn *txn = l7; |
| 6718 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6719 | if (!txn) |
| 6720 | return 0; |
| 6721 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6722 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6723 | return 0; |
| 6724 | |
| 6725 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6726 | } |
| 6727 | |
| 6728 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6729 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6730 | */ |
| 6731 | static int |
| 6732 | 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] | 6733 | struct acl_expr *expr, struct acl_test *test) |
| 6734 | { |
| 6735 | struct http_txn *txn = l7; |
| 6736 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6737 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6738 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6739 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6740 | if (!txn) |
| 6741 | return 0; |
| 6742 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6743 | ctx.idx = 0; |
| 6744 | cnt = 0; |
| 6745 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6746 | cnt++; |
| 6747 | |
| 6748 | test->i = cnt; |
| 6749 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6750 | return 1; |
| 6751 | } |
| 6752 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6753 | static int |
| 6754 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6755 | struct acl_expr *expr, struct acl_test *test) |
| 6756 | { |
| 6757 | struct http_txn *txn = l7; |
| 6758 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6759 | if (!txn) |
| 6760 | return 0; |
| 6761 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6762 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6763 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6764 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6765 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6766 | /* ensure the indexes are not affected */ |
| 6767 | return 0; |
| 6768 | |
| 6769 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6770 | } |
| 6771 | |
| 6772 | static int |
| 6773 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6774 | struct acl_expr *expr, struct acl_test *test) |
| 6775 | { |
| 6776 | struct http_txn *txn = l7; |
| 6777 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6778 | if (!txn) |
| 6779 | return 0; |
| 6780 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6781 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6782 | return 0; |
| 6783 | |
| 6784 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6785 | } |
| 6786 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6787 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6788 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6789 | * 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] | 6790 | */ |
| 6791 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6792 | 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] | 6793 | struct acl_expr *expr, struct acl_test *test) |
| 6794 | { |
| 6795 | struct http_txn *txn = l7; |
| 6796 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6797 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6798 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6799 | if (!txn) |
| 6800 | return 0; |
| 6801 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6802 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6803 | /* search for header from the beginning */ |
| 6804 | ctx->idx = 0; |
| 6805 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6806 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6807 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6808 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6809 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6810 | return 1; |
| 6811 | } |
| 6812 | |
| 6813 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6814 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6815 | return 0; |
| 6816 | } |
| 6817 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6818 | static int |
| 6819 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6820 | struct acl_expr *expr, struct acl_test *test) |
| 6821 | { |
| 6822 | struct http_txn *txn = l7; |
| 6823 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6824 | if (!txn) |
| 6825 | return 0; |
| 6826 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6827 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6828 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6829 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6830 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6831 | /* ensure the indexes are not affected */ |
| 6832 | return 0; |
| 6833 | |
| 6834 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6835 | } |
| 6836 | |
| 6837 | static int |
| 6838 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6839 | struct acl_expr *expr, struct acl_test *test) |
| 6840 | { |
| 6841 | struct http_txn *txn = l7; |
| 6842 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6843 | if (!txn) |
| 6844 | return 0; |
| 6845 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6846 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6847 | return 0; |
| 6848 | |
| 6849 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6850 | } |
| 6851 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6852 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 6853 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6854 | */ |
| 6855 | static int |
| 6856 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 6857 | struct acl_expr *expr, struct acl_test *test) |
| 6858 | { |
| 6859 | struct http_txn *txn = l7; |
| 6860 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6861 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 6862 | |
| 6863 | if (!txn) |
| 6864 | return 0; |
| 6865 | |
| 6866 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6867 | /* search for header from the beginning */ |
| 6868 | ctx->idx = 0; |
| 6869 | |
| 6870 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6871 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6872 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6873 | /* Same optimization as url_ip */ |
| 6874 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 6875 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 6876 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 6877 | test->i = AF_INET; |
| 6878 | return 1; |
| 6879 | } |
| 6880 | |
| 6881 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6882 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6883 | return 0; |
| 6884 | } |
| 6885 | |
| 6886 | static int |
| 6887 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6888 | struct acl_expr *expr, struct acl_test *test) |
| 6889 | { |
| 6890 | struct http_txn *txn = l7; |
| 6891 | |
| 6892 | if (!txn) |
| 6893 | return 0; |
| 6894 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6895 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6896 | return 0; |
| 6897 | |
| 6898 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6899 | /* ensure the indexes are not affected */ |
| 6900 | return 0; |
| 6901 | |
| 6902 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 6903 | } |
| 6904 | |
| 6905 | static int |
| 6906 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6907 | struct acl_expr *expr, struct acl_test *test) |
| 6908 | { |
| 6909 | struct http_txn *txn = l7; |
| 6910 | |
| 6911 | if (!txn) |
| 6912 | return 0; |
| 6913 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6914 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6915 | return 0; |
| 6916 | |
| 6917 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 6918 | } |
| 6919 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6920 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 6921 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 6922 | */ |
| 6923 | static int |
| 6924 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6925 | struct acl_expr *expr, struct acl_test *test) |
| 6926 | { |
| 6927 | struct http_txn *txn = l7; |
| 6928 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6929 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6930 | if (!txn) |
| 6931 | return 0; |
| 6932 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6933 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6934 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6935 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6936 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6937 | /* ensure the indexes are not affected */ |
| 6938 | return 0; |
| 6939 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6940 | 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] | 6941 | ptr = http_get_path(txn); |
| 6942 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6943 | return 0; |
| 6944 | |
| 6945 | /* OK, we got the '/' ! */ |
| 6946 | test->ptr = ptr; |
| 6947 | |
| 6948 | while (ptr < end && *ptr != '?') |
| 6949 | ptr++; |
| 6950 | |
| 6951 | test->len = ptr - test->ptr; |
| 6952 | |
| 6953 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6954 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6955 | return 1; |
| 6956 | } |
| 6957 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6958 | static int |
| 6959 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 6960 | struct acl_expr *expr, struct acl_test *test) |
| 6961 | { |
| 6962 | struct buffer *req = s->req; |
| 6963 | struct http_txn *txn = &s->txn; |
| 6964 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6965 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6966 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 6967 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 6968 | */ |
| 6969 | |
| 6970 | if (!s || !req) |
| 6971 | return 0; |
| 6972 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6973 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6974 | /* Already decoded as OK */ |
| 6975 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 6976 | return 1; |
| 6977 | } |
| 6978 | |
| 6979 | /* Try to decode HTTP request */ |
| 6980 | if (likely(req->lr < req->r)) |
| 6981 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 6982 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6983 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6984 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 6985 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 6986 | return 1; |
| 6987 | } |
| 6988 | /* wait for final state */ |
| 6989 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 6990 | return 0; |
| 6991 | } |
| 6992 | |
| 6993 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 6994 | * perform so that further checks can rely on HTTP tests. |
| 6995 | */ |
| 6996 | msg->sol = req->data + msg->som; |
| 6997 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 6998 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 6999 | s->flags |= SN_REDIRECTABLE; |
| 7000 | |
| 7001 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7002 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7003 | return 1; |
| 7004 | } |
| 7005 | |
| 7006 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7007 | return 1; |
| 7008 | } |
| 7009 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7010 | |
| 7011 | /************************************************************************/ |
| 7012 | /* All supported keywords must be declared here. */ |
| 7013 | /************************************************************************/ |
| 7014 | |
| 7015 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7016 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7017 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7018 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7019 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 7020 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7021 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7022 | { "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] | 7023 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7024 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7025 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7026 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7027 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7028 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7029 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7030 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7031 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 7032 | { "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] | 7033 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7034 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 7035 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7036 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7037 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7038 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7039 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7040 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7041 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7042 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 7043 | { "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] | 7044 | { "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] | 7045 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7046 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7047 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 7048 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 7049 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 7050 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 7051 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 7052 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 7053 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 7054 | { "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] | 7055 | { "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] | 7056 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7057 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7058 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7059 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7060 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7061 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7062 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7063 | { "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] | 7064 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7065 | { NULL, NULL, NULL, NULL }, |
| 7066 | |
| 7067 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7068 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7069 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7070 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7071 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7072 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7073 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7074 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7075 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7076 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7077 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7078 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7079 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7080 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7081 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7082 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7083 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 7084 | |
| 7085 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 7086 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 7087 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 7088 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 7089 | { NULL, NULL, NULL, NULL }, |
| 7090 | #endif |
| 7091 | }}; |
| 7092 | |
| 7093 | |
| 7094 | __attribute__((constructor)) |
| 7095 | static void __http_protocol_init(void) |
| 7096 | { |
| 7097 | acl_register_keywords(&acl_kws); |
| 7098 | } |
| 7099 | |
| 7100 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7101 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7102 | * Local variables: |
| 7103 | * c-indent-level: 8 |
| 7104 | * c-basic-offset: 8 |
| 7105 | * End: |
| 7106 | */ |