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 | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 566 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 567 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 568 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 569 | } |
| 570 | if (!(t->flags & SN_ERR_MASK)) |
| 571 | t->flags |= err; |
| 572 | if (!(t->flags & SN_FINST_MASK)) |
| 573 | t->flags |= finst; |
| 574 | } |
| 575 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 576 | /* This function returns the appropriate error location for the given session |
| 577 | * and message. |
| 578 | */ |
| 579 | |
| 580 | struct chunk *error_message(struct session *s, int msgnum) |
| 581 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 582 | if (s->be->errmsg[msgnum].str) |
| 583 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 584 | else if (s->fe->errmsg[msgnum].str) |
| 585 | return &s->fe->errmsg[msgnum]; |
| 586 | else |
| 587 | return &http_err_chunks[msgnum]; |
| 588 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 589 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 590 | /* |
| 591 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 592 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 593 | */ |
| 594 | static http_meth_t find_http_meth(const char *str, const int len) |
| 595 | { |
| 596 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 597 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 598 | |
| 599 | m = ((unsigned)*str - 'A'); |
| 600 | |
| 601 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 602 | for (h = http_methods[m]; h->len > 0; h++) { |
| 603 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 604 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 605 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 606 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 607 | }; |
| 608 | return HTTP_METH_OTHER; |
| 609 | } |
| 610 | return HTTP_METH_NONE; |
| 611 | |
| 612 | } |
| 613 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 614 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 615 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 616 | * It is returned otherwise. |
| 617 | */ |
| 618 | static char * |
| 619 | http_get_path(struct http_txn *txn) |
| 620 | { |
| 621 | char *ptr, *end; |
| 622 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 623 | ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 624 | end = ptr + txn->req.sl.rq.u_l; |
| 625 | |
| 626 | if (ptr >= end) |
| 627 | return NULL; |
| 628 | |
| 629 | /* RFC2616, par. 5.1.2 : |
| 630 | * Request-URI = "*" | absuri | abspath | authority |
| 631 | */ |
| 632 | |
| 633 | if (*ptr == '*') |
| 634 | return NULL; |
| 635 | |
| 636 | if (isalpha((unsigned char)*ptr)) { |
| 637 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 638 | ptr++; |
| 639 | while (ptr < end && |
| 640 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 641 | ptr++; |
| 642 | /* skip '://' */ |
| 643 | if (ptr == end || *ptr++ != ':') |
| 644 | return NULL; |
| 645 | if (ptr == end || *ptr++ != '/') |
| 646 | return NULL; |
| 647 | if (ptr == end || *ptr++ != '/') |
| 648 | return NULL; |
| 649 | } |
| 650 | /* skip [user[:passwd]@]host[:[port]] */ |
| 651 | |
| 652 | while (ptr < end && *ptr != '/') |
| 653 | ptr++; |
| 654 | |
| 655 | if (ptr == end) |
| 656 | return NULL; |
| 657 | |
| 658 | /* OK, we got the '/' ! */ |
| 659 | return ptr; |
| 660 | } |
| 661 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 662 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 663 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 664 | * left unchanged and will follow normal proxy processing. |
| 665 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 666 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 667 | { |
| 668 | struct http_txn *txn; |
| 669 | struct chunk rdr; |
| 670 | char *path; |
| 671 | int len; |
| 672 | |
| 673 | /* 1: create the response header */ |
| 674 | rdr.len = strlen(HTTP_302); |
| 675 | rdr.str = trash; |
| 676 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 677 | |
| 678 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 679 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 680 | return; |
| 681 | |
| 682 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 683 | rdr.len += s->srv->rdr_len; |
| 684 | |
| 685 | /* 3: add the request URI */ |
| 686 | txn = &s->txn; |
| 687 | path = http_get_path(txn); |
| 688 | if (!path) |
| 689 | return; |
| 690 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 691 | 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] | 692 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 693 | return; |
| 694 | |
| 695 | memcpy(rdr.str + rdr.len, path, len); |
| 696 | rdr.len += len; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 697 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 698 | rdr.len += 23; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 699 | |
| 700 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 701 | si->shutr(si); |
| 702 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 703 | si->err_type = SI_ET_NONE; |
| 704 | si->err_loc = NULL; |
| 705 | si->state = SI_ST_CLO; |
| 706 | |
| 707 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 708 | 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] | 709 | |
| 710 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 711 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 712 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 713 | } |
| 714 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 715 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 716 | * that the server side is closed. Note that err_type is actually a |
| 717 | * bitmask, where almost only aborts may be cumulated with other |
| 718 | * values. We consider that aborted operations are more important |
| 719 | * than timeouts or errors due to the fact that nobody else in the |
| 720 | * logs might explain incomplete retries. All others should avoid |
| 721 | * being cumulated. It should normally not be possible to have multiple |
| 722 | * aborts at once, but just in case, the first one in sequence is reported. |
| 723 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 724 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 725 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 726 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 727 | |
| 728 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 729 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 730 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 731 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 732 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 733 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 734 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 735 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 736 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 737 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 738 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 739 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 740 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 741 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 742 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 743 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 744 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 745 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 746 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 747 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 748 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 751 | extern const char sess_term_cond[8]; |
| 752 | extern const char sess_fin_state[8]; |
| 753 | extern const char *monthname[12]; |
| 754 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 755 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 756 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 757 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 758 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 759 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 760 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 761 | void http_sess_clflog(struct session *s) |
| 762 | { |
| 763 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 764 | struct proxy *fe = s->fe; |
| 765 | struct proxy *be = s->be; |
| 766 | struct proxy *prx_log; |
| 767 | struct http_txn *txn = &s->txn; |
| 768 | int tolog, level, err; |
| 769 | char *uri, *h; |
| 770 | char *svid; |
| 771 | struct tm tm; |
| 772 | static char tmpline[MAX_SYSLOG_LEN]; |
| 773 | int hdr; |
| 774 | size_t w; |
| 775 | int t_request; |
| 776 | |
| 777 | prx_log = fe; |
| 778 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 779 | (s->conn_retries != be->conn_retries) || |
| 780 | txn->status >= 500; |
| 781 | |
| 782 | if (s->cli_addr.ss_family == AF_INET) |
| 783 | inet_ntop(AF_INET, |
| 784 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 785 | pn, sizeof(pn)); |
| 786 | else |
| 787 | inet_ntop(AF_INET6, |
| 788 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 789 | pn, sizeof(pn)); |
| 790 | |
| 791 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 792 | |
| 793 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 794 | tolog = fe->to_log; |
| 795 | |
| 796 | h = tmpline; |
| 797 | |
| 798 | w = snprintf(h, sizeof(tmpline), |
| 799 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 800 | pn, |
| 801 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 802 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 803 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 804 | goto trunc; |
| 805 | h += w; |
| 806 | |
| 807 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 808 | goto trunc; |
| 809 | |
| 810 | *(h++) = ' '; |
| 811 | *(h++) = '\"'; |
| 812 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 813 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 814 | '#', url_encode_map, uri); |
| 815 | *(h++) = '\"'; |
| 816 | |
| 817 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 818 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 819 | goto trunc; |
| 820 | h += w; |
| 821 | |
| 822 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 823 | goto trunc; |
| 824 | memcpy(h, " \"-\" \"-\"", 8); |
| 825 | h += 8; |
| 826 | |
| 827 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 828 | " %d %03d", |
| 829 | (s->cli_addr.ss_family == AF_INET) ? |
| 830 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 831 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 832 | (int)s->logs.accept_date.tv_usec/1000); |
| 833 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 834 | goto trunc; |
| 835 | h += w; |
| 836 | |
| 837 | w = strlen(fe->id); |
| 838 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 839 | goto trunc; |
| 840 | *(h++) = ' '; |
| 841 | *(h++) = '\"'; |
| 842 | memcpy(h, fe->id, w); |
| 843 | h += w; |
| 844 | *(h++) = '\"'; |
| 845 | |
| 846 | w = strlen(be->id); |
| 847 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 848 | goto trunc; |
| 849 | *(h++) = ' '; |
| 850 | *(h++) = '\"'; |
| 851 | memcpy(h, be->id, w); |
| 852 | h += w; |
| 853 | *(h++) = '\"'; |
| 854 | |
| 855 | svid = (tolog & LW_SVID) ? |
| 856 | (s->data_source != DATA_SRC_STATS) ? |
| 857 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 858 | |
| 859 | w = strlen(svid); |
| 860 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 861 | goto trunc; |
| 862 | *(h++) = ' '; |
| 863 | *(h++) = '\"'; |
| 864 | memcpy(h, svid, w); |
| 865 | h += w; |
| 866 | *(h++) = '\"'; |
| 867 | |
| 868 | t_request = -1; |
| 869 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 870 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 871 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 872 | " %d %ld %ld %ld %ld", |
| 873 | t_request, |
| 874 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 875 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 876 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 877 | s->logs.t_close); |
| 878 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 879 | goto trunc; |
| 880 | h += w; |
| 881 | |
| 882 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 883 | goto trunc; |
| 884 | *(h++) = ' '; |
| 885 | *(h++) = '\"'; |
| 886 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 887 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 888 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 889 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 890 | *(h++) = '\"'; |
| 891 | |
| 892 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 893 | " %d %d %d %d %d %ld %ld", |
| 894 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 895 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 896 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 897 | |
| 898 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 899 | goto trunc; |
| 900 | h += w; |
| 901 | |
| 902 | if (txn->cli_cookie) { |
| 903 | w = strlen(txn->cli_cookie); |
| 904 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 905 | goto trunc; |
| 906 | *(h++) = ' '; |
| 907 | *(h++) = '\"'; |
| 908 | memcpy(h, txn->cli_cookie, w); |
| 909 | h += w; |
| 910 | *(h++) = '\"'; |
| 911 | } else { |
| 912 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 913 | goto trunc; |
| 914 | memcpy(h, " \"-\"", 4); |
| 915 | h += 4; |
| 916 | } |
| 917 | |
| 918 | if (txn->srv_cookie) { |
| 919 | w = strlen(txn->srv_cookie); |
| 920 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 921 | goto trunc; |
| 922 | *(h++) = ' '; |
| 923 | *(h++) = '\"'; |
| 924 | memcpy(h, txn->srv_cookie, w); |
| 925 | h += w; |
| 926 | *(h++) = '\"'; |
| 927 | } else { |
| 928 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 929 | goto trunc; |
| 930 | memcpy(h, " \"-\"", 4); |
| 931 | h += 4; |
| 932 | } |
| 933 | |
| 934 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 935 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 936 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 937 | goto trunc; |
| 938 | *(h++) = ' '; |
| 939 | *(h++) = '\"'; |
| 940 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 941 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 942 | *(h++) = '\"'; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 947 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 948 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 949 | goto trunc; |
| 950 | *(h++) = ' '; |
| 951 | *(h++) = '\"'; |
| 952 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 953 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 954 | *(h++) = '\"'; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | trunc: |
| 959 | *h = '\0'; |
| 960 | |
| 961 | level = LOG_INFO; |
| 962 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 963 | level = LOG_ERR; |
| 964 | |
| 965 | send_log(prx_log, level, "%s\n", tmpline); |
| 966 | |
| 967 | s->logs.logwait = 0; |
| 968 | } |
| 969 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 970 | /* |
| 971 | * send a log for the session when we have enough info about it. |
| 972 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 973 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 974 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 975 | { |
| 976 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 977 | struct proxy *fe = s->fe; |
| 978 | struct proxy *be = s->be; |
| 979 | struct proxy *prx_log; |
| 980 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 981 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 982 | char *uri, *h; |
| 983 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 984 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 985 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 986 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 987 | int hdr; |
| 988 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 989 | /* if we don't want to log normal traffic, return now */ |
| 990 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 991 | (s->conn_retries != be->conn_retries) || |
| 992 | txn->status >= 500; |
| 993 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 994 | return; |
| 995 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 996 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 997 | return; |
| 998 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 999 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1000 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1001 | return http_sess_clflog(s); |
| 1002 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1003 | if (s->cli_addr.ss_family == AF_INET) |
| 1004 | inet_ntop(AF_INET, |
| 1005 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1006 | pn, sizeof(pn)); |
| 1007 | else |
| 1008 | inet_ntop(AF_INET6, |
| 1009 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1010 | pn, sizeof(pn)); |
| 1011 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1012 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1013 | |
| 1014 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1015 | tolog = fe->to_log; |
| 1016 | |
| 1017 | h = tmpline; |
| 1018 | if (fe->to_log & LW_REQHDR && |
| 1019 | txn->req.cap && |
| 1020 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1021 | *(h++) = ' '; |
| 1022 | *(h++) = '{'; |
| 1023 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1024 | if (hdr) |
| 1025 | *(h++) = '|'; |
| 1026 | if (txn->req.cap[hdr] != NULL) |
| 1027 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1028 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1029 | } |
| 1030 | *(h++) = '}'; |
| 1031 | } |
| 1032 | |
| 1033 | if (fe->to_log & LW_RSPHDR && |
| 1034 | txn->rsp.cap && |
| 1035 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1036 | *(h++) = ' '; |
| 1037 | *(h++) = '{'; |
| 1038 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1039 | if (hdr) |
| 1040 | *(h++) = '|'; |
| 1041 | if (txn->rsp.cap[hdr] != NULL) |
| 1042 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1043 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1044 | } |
| 1045 | *(h++) = '}'; |
| 1046 | } |
| 1047 | |
| 1048 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1049 | *(h++) = ' '; |
| 1050 | *(h++) = '"'; |
| 1051 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1052 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1053 | '#', url_encode_map, uri); |
| 1054 | *(h++) = '"'; |
| 1055 | } |
| 1056 | *h = '\0'; |
| 1057 | |
| 1058 | svid = (tolog & LW_SVID) ? |
| 1059 | (s->data_source != DATA_SRC_STATS) ? |
| 1060 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1061 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1062 | t_request = -1; |
| 1063 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1064 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1065 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1066 | level = LOG_INFO; |
| 1067 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1068 | level = LOG_ERR; |
| 1069 | |
| 1070 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1071 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1072 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1073 | " %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] | 1074 | pn, |
| 1075 | (s->cli_addr.ss_family == AF_INET) ? |
| 1076 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1077 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1078 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1079 | 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] | 1080 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1081 | t_request, |
| 1082 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1083 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1084 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1085 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1086 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1087 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1088 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1089 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1090 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1091 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1092 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1093 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1094 | 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] | 1095 | (s->flags & SN_REDISP)?"+":"", |
| 1096 | (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] | 1097 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1098 | |
| 1099 | s->logs.logwait = 0; |
| 1100 | } |
| 1101 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * Capture headers from message starting at <som> according to header list |
| 1105 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1106 | */ |
| 1107 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1108 | char **cap, struct cap_hdr *cap_hdr) |
| 1109 | { |
| 1110 | char *eol, *sol, *col, *sov; |
| 1111 | int cur_idx; |
| 1112 | struct cap_hdr *h; |
| 1113 | int len; |
| 1114 | |
| 1115 | sol = som + hdr_idx_first_pos(idx); |
| 1116 | cur_idx = hdr_idx_first_idx(idx); |
| 1117 | |
| 1118 | while (cur_idx) { |
| 1119 | eol = sol + idx->v[cur_idx].len; |
| 1120 | |
| 1121 | col = sol; |
| 1122 | while (col < eol && *col != ':') |
| 1123 | col++; |
| 1124 | |
| 1125 | sov = col + 1; |
| 1126 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1127 | sov++; |
| 1128 | |
| 1129 | for (h = cap_hdr; h; h = h->next) { |
| 1130 | if ((h->namelen == col - sol) && |
| 1131 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1132 | if (cap[h->index] == NULL) |
| 1133 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1134 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1135 | |
| 1136 | if (cap[h->index] == NULL) { |
| 1137 | Alert("HTTP capture : out of memory.\n"); |
| 1138 | continue; |
| 1139 | } |
| 1140 | |
| 1141 | len = eol - sov; |
| 1142 | if (len > h->len) |
| 1143 | len = h->len; |
| 1144 | |
| 1145 | memcpy(cap[h->index], sov, len); |
| 1146 | cap[h->index][len]=0; |
| 1147 | } |
| 1148 | } |
| 1149 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1150 | cur_idx = idx->v[cur_idx].next; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1155 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1156 | */ |
| 1157 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1158 | |
| 1159 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1160 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1161 | */ |
| 1162 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1163 | ptr++; \ |
| 1164 | if (likely(ptr < end)) \ |
| 1165 | goto good; \ |
| 1166 | else { \ |
| 1167 | state = (st); \ |
| 1168 | goto http_msg_ood; \ |
| 1169 | } \ |
| 1170 | } while (0) |
| 1171 | |
| 1172 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1173 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1174 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1175 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1176 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1177 | * will give undefined results. |
| 1178 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1179 | * and that msg->sol points to the beginning of the response. |
| 1180 | * If a complete line is found (which implies that at least one CR or LF is |
| 1181 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1182 | * returned indicating an incomplete line (which does not mean that parts have |
| 1183 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1184 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1185 | * upon next call. |
| 1186 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1187 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1188 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1189 | * 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] | 1190 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1191 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1192 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1193 | unsigned int state, const char *ptr, const char *end, |
| 1194 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1195 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | switch (state) { |
| 1197 | http_msg_rpver: |
| 1198 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1199 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1200 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1201 | |
| 1202 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1203 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1204 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1205 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1206 | state = HTTP_MSG_ERROR; |
| 1207 | break; |
| 1208 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1209 | http_msg_rpver_sp: |
| 1210 | case HTTP_MSG_RPVER_SP: |
| 1211 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1212 | msg->sl.st.c = ptr - msg_buf; |
| 1213 | goto http_msg_rpcode; |
| 1214 | } |
| 1215 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1216 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1217 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1218 | state = HTTP_MSG_ERROR; |
| 1219 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1220 | |
| 1221 | http_msg_rpcode: |
| 1222 | case HTTP_MSG_RPCODE: |
| 1223 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1224 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1225 | |
| 1226 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1227 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1228 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1229 | } |
| 1230 | |
| 1231 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1232 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1233 | http_msg_rsp_reason: |
| 1234 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1235 | msg->sl.st.r = ptr - msg_buf; |
| 1236 | msg->sl.st.r_l = 0; |
| 1237 | goto http_msg_rpline_eol; |
| 1238 | |
| 1239 | http_msg_rpcode_sp: |
| 1240 | case HTTP_MSG_RPCODE_SP: |
| 1241 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1242 | msg->sl.st.r = ptr - msg_buf; |
| 1243 | goto http_msg_rpreason; |
| 1244 | } |
| 1245 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1246 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1247 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1248 | goto http_msg_rsp_reason; |
| 1249 | |
| 1250 | http_msg_rpreason: |
| 1251 | case HTTP_MSG_RPREASON: |
| 1252 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1253 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1254 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1255 | http_msg_rpline_eol: |
| 1256 | /* We have seen the end of line. Note that we do not |
| 1257 | * necessarily have the \n yet, but at least we know that we |
| 1258 | * have EITHER \r OR \n, otherwise the response would not be |
| 1259 | * complete. We can then record the response length and return |
| 1260 | * to the caller which will be able to register it. |
| 1261 | */ |
| 1262 | msg->sl.st.l = ptr - msg->sol; |
| 1263 | return ptr; |
| 1264 | |
| 1265 | #ifdef DEBUG_FULL |
| 1266 | default: |
| 1267 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1268 | exit(1); |
| 1269 | #endif |
| 1270 | } |
| 1271 | |
| 1272 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1273 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1274 | if (ret_state) |
| 1275 | *ret_state = state; |
| 1276 | if (ret_ptr) |
| 1277 | *ret_ptr = (char *)ptr; |
| 1278 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1283 | * This function parses a request line between <ptr> and <end>, starting with |
| 1284 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1285 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1286 | * will give undefined results. |
| 1287 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1288 | * and that msg->sol points to the beginning of the request. |
| 1289 | * If a complete line is found (which implies that at least one CR or LF is |
| 1290 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1291 | * returned indicating an incomplete line (which does not mean that parts have |
| 1292 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1293 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1294 | * upon next call. |
| 1295 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1296 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1297 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1298 | * 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] | 1299 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1300 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1301 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1302 | unsigned int state, const char *ptr, const char *end, |
| 1303 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1304 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1305 | switch (state) { |
| 1306 | http_msg_rqmeth: |
| 1307 | case HTTP_MSG_RQMETH: |
| 1308 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1309 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1310 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1312 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1313 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1314 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1315 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1316 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1317 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1318 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1319 | http_msg_req09_uri: |
| 1320 | msg->sl.rq.u = ptr - msg_buf; |
| 1321 | http_msg_req09_uri_e: |
| 1322 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1323 | http_msg_req09_ver: |
| 1324 | msg->sl.rq.v = ptr - msg_buf; |
| 1325 | msg->sl.rq.v_l = 0; |
| 1326 | goto http_msg_rqline_eol; |
| 1327 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1328 | state = HTTP_MSG_ERROR; |
| 1329 | break; |
| 1330 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1331 | http_msg_rqmeth_sp: |
| 1332 | case HTTP_MSG_RQMETH_SP: |
| 1333 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1334 | msg->sl.rq.u = ptr - msg_buf; |
| 1335 | goto http_msg_rquri; |
| 1336 | } |
| 1337 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1338 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1339 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1340 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1341 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1342 | http_msg_rquri: |
| 1343 | case HTTP_MSG_RQURI: |
| 1344 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1345 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1348 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1349 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1350 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1351 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1352 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1353 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1354 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1355 | http_msg_rquri_sp: |
| 1356 | case HTTP_MSG_RQURI_SP: |
| 1357 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1358 | msg->sl.rq.v = ptr - msg_buf; |
| 1359 | goto http_msg_rqver; |
| 1360 | } |
| 1361 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1362 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1363 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1364 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1365 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1366 | http_msg_rqver: |
| 1367 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1368 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1369 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1370 | |
| 1371 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1372 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1373 | http_msg_rqline_eol: |
| 1374 | /* We have seen the end of line. Note that we do not |
| 1375 | * necessarily have the \n yet, but at least we know that we |
| 1376 | * have EITHER \r OR \n, otherwise the request would not be |
| 1377 | * complete. We can then record the request length and return |
| 1378 | * to the caller which will be able to register it. |
| 1379 | */ |
| 1380 | msg->sl.rq.l = ptr - msg->sol; |
| 1381 | return ptr; |
| 1382 | } |
| 1383 | |
| 1384 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1385 | state = HTTP_MSG_ERROR; |
| 1386 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1388 | #ifdef DEBUG_FULL |
| 1389 | default: |
| 1390 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1391 | exit(1); |
| 1392 | #endif |
| 1393 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1396 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | if (ret_state) |
| 1398 | *ret_state = state; |
| 1399 | if (ret_ptr) |
| 1400 | *ret_ptr = (char *)ptr; |
| 1401 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1403 | |
| 1404 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1405 | /* |
| 1406 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1407 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1408 | * when data are missing and recalled at the exact same location with no |
| 1409 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1410 | * 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] | 1411 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1412 | * the first state, so that none of the msg pointers has to be initialized |
| 1413 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1414 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1416 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1417 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1420 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | ptr = buf->lr; |
| 1422 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | if (unlikely(ptr >= end)) |
| 1425 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1426 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1428 | /* |
| 1429 | * First, states that are specific to the response only. |
| 1430 | * We check them first so that request and headers are |
| 1431 | * closer to each other (accessed more often). |
| 1432 | */ |
| 1433 | http_msg_rpbefore: |
| 1434 | case HTTP_MSG_RPBEFORE: |
| 1435 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1436 | /* we have a start of message, but we have to check |
| 1437 | * first if we need to remove some CRLF. We can only |
| 1438 | * do this when send_max=0. |
| 1439 | */ |
| 1440 | char *beg = buf->w + buf->send_max; |
| 1441 | if (beg >= buf->data + buf->size) |
| 1442 | beg -= buf->size; |
| 1443 | if (unlikely(ptr != beg)) { |
| 1444 | if (buf->send_max) |
| 1445 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1446 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1447 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1448 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1449 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1450 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1451 | hdr_idx_init(idx); |
| 1452 | state = HTTP_MSG_RPVER; |
| 1453 | goto http_msg_rpver; |
| 1454 | } |
| 1455 | |
| 1456 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1457 | goto http_msg_invalid; |
| 1458 | |
| 1459 | if (unlikely(*ptr == '\n')) |
| 1460 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1461 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1462 | /* stop here */ |
| 1463 | |
| 1464 | http_msg_rpbefore_cr: |
| 1465 | case HTTP_MSG_RPBEFORE_CR: |
| 1466 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1467 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1468 | /* stop here */ |
| 1469 | |
| 1470 | http_msg_rpver: |
| 1471 | case HTTP_MSG_RPVER: |
| 1472 | case HTTP_MSG_RPVER_SP: |
| 1473 | case HTTP_MSG_RPCODE: |
| 1474 | case HTTP_MSG_RPCODE_SP: |
| 1475 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1476 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1477 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1478 | if (unlikely(!ptr)) |
| 1479 | return; |
| 1480 | |
| 1481 | /* we have a full response and we know that we have either a CR |
| 1482 | * or an LF at <ptr>. |
| 1483 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1484 | //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] | 1485 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1486 | |
| 1487 | msg->sol = ptr; |
| 1488 | if (likely(*ptr == '\r')) |
| 1489 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1490 | goto http_msg_rpline_end; |
| 1491 | |
| 1492 | http_msg_rpline_end: |
| 1493 | case HTTP_MSG_RPLINE_END: |
| 1494 | /* msg->sol must point to the first of CR or LF. */ |
| 1495 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1496 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1497 | /* stop here */ |
| 1498 | |
| 1499 | /* |
| 1500 | * Second, states that are specific to the request only |
| 1501 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1502 | http_msg_rqbefore: |
| 1503 | case HTTP_MSG_RQBEFORE: |
| 1504 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1505 | /* we have a start of message, but we have to check |
| 1506 | * first if we need to remove some CRLF. We can only |
| 1507 | * do this when send_max=0. |
| 1508 | */ |
| 1509 | char *beg = buf->w + buf->send_max; |
| 1510 | if (beg >= buf->data + buf->size) |
| 1511 | beg -= buf->size; |
| 1512 | if (likely(ptr != beg)) { |
| 1513 | if (buf->send_max) |
| 1514 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1515 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1516 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1518 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1519 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1520 | /* we will need this when keep-alive will be supported |
| 1521 | hdr_idx_init(idx); |
| 1522 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1523 | state = HTTP_MSG_RQMETH; |
| 1524 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1526 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1528 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1529 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | if (unlikely(*ptr == '\n')) |
| 1531 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1532 | 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] | 1533 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1534 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | http_msg_rqbefore_cr: |
| 1536 | case HTTP_MSG_RQBEFORE_CR: |
| 1537 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1538 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1539 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1540 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | http_msg_rqmeth: |
| 1542 | case HTTP_MSG_RQMETH: |
| 1543 | case HTTP_MSG_RQMETH_SP: |
| 1544 | case HTTP_MSG_RQURI: |
| 1545 | case HTTP_MSG_RQURI_SP: |
| 1546 | case HTTP_MSG_RQVER: |
| 1547 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1548 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | if (unlikely(!ptr)) |
| 1550 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1551 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1552 | /* we have a full request and we know that we have either a CR |
| 1553 | * or an LF at <ptr>. |
| 1554 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1555 | //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] | 1556 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1557 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1558 | msg->sol = ptr; |
| 1559 | if (likely(*ptr == '\r')) |
| 1560 | 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] | 1561 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1562 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | http_msg_rqline_end: |
| 1564 | case HTTP_MSG_RQLINE_END: |
| 1565 | /* check for HTTP/0.9 request : no version information available. |
| 1566 | * msg->sol must point to the first of CR or LF. |
| 1567 | */ |
| 1568 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1569 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1570 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1571 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1572 | 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] | 1573 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1574 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1575 | /* |
| 1576 | * Common states below |
| 1577 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1578 | http_msg_hdr_first: |
| 1579 | case HTTP_MSG_HDR_FIRST: |
| 1580 | msg->sol = ptr; |
| 1581 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1582 | goto http_msg_hdr_name; |
| 1583 | } |
| 1584 | |
| 1585 | if (likely(*ptr == '\r')) |
| 1586 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1587 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | http_msg_hdr_name: |
| 1590 | case HTTP_MSG_HDR_NAME: |
| 1591 | /* assumes msg->sol points to the first char */ |
| 1592 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1593 | 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] | 1594 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1595 | if (likely(*ptr == ':')) { |
| 1596 | msg->col = ptr - buf->data; |
| 1597 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1598 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1599 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1600 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1601 | goto http_msg_invalid; |
| 1602 | |
| 1603 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1604 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1605 | |
| 1606 | /* and we still accept this non-token character */ |
| 1607 | 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] | 1608 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1609 | http_msg_hdr_l1_sp: |
| 1610 | case HTTP_MSG_HDR_L1_SP: |
| 1611 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1612 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1613 | 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] | 1614 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1615 | /* header value can be basically anything except CR/LF */ |
| 1616 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1617 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1619 | goto http_msg_hdr_val; |
| 1620 | } |
| 1621 | |
| 1622 | if (likely(*ptr == '\r')) |
| 1623 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1624 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1625 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | http_msg_hdr_l1_lf: |
| 1627 | case HTTP_MSG_HDR_L1_LF: |
| 1628 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1629 | 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] | 1630 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | http_msg_hdr_l1_lws: |
| 1632 | case HTTP_MSG_HDR_L1_LWS: |
| 1633 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1634 | /* replace HT,CR,LF with spaces */ |
| 1635 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1636 | buf->data[msg->sov] = ' '; |
| 1637 | goto http_msg_hdr_l1_sp; |
| 1638 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1639 | /* we had a header consisting only in spaces ! */ |
| 1640 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1641 | goto http_msg_complete_header; |
| 1642 | |
| 1643 | http_msg_hdr_val: |
| 1644 | case HTTP_MSG_HDR_VAL: |
| 1645 | /* assumes msg->sol points to the first char, msg->col to the |
| 1646 | * colon, and msg->sov points to the first character of the |
| 1647 | * value. |
| 1648 | */ |
| 1649 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1650 | 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] | 1651 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1652 | msg->eol = ptr; |
| 1653 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1654 | * real header end in case it ends with lots of LWS, but is this |
| 1655 | * really needed ? |
| 1656 | */ |
| 1657 | if (likely(*ptr == '\r')) |
| 1658 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1659 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1660 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | http_msg_hdr_l2_lf: |
| 1662 | case HTTP_MSG_HDR_L2_LF: |
| 1663 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1664 | 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] | 1665 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | http_msg_hdr_l2_lws: |
| 1667 | case HTTP_MSG_HDR_L2_LWS: |
| 1668 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1669 | /* LWS: replace HT,CR,LF with spaces */ |
| 1670 | for (; msg->eol < ptr; msg->eol++) |
| 1671 | *msg->eol = ' '; |
| 1672 | goto http_msg_hdr_val; |
| 1673 | } |
| 1674 | http_msg_complete_header: |
| 1675 | /* |
| 1676 | * It was a new header, so the last one is finished. |
| 1677 | * Assumes msg->sol points to the first char, msg->col to the |
| 1678 | * colon, msg->sov points to the first character of the value |
| 1679 | * and msg->eol to the first CR or LF so we know how the line |
| 1680 | * ends. We insert last header into the index. |
| 1681 | */ |
| 1682 | /* |
| 1683 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1684 | write(2, msg->sol, msg->eol-msg->sol); |
| 1685 | fprintf(stderr,"\n"); |
| 1686 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1688 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1689 | idx, idx->tail) < 0)) |
| 1690 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1691 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | msg->sol = ptr; |
| 1693 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1694 | goto http_msg_hdr_name; |
| 1695 | } |
| 1696 | |
| 1697 | if (likely(*ptr == '\r')) |
| 1698 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1699 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1700 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | http_msg_last_lf: |
| 1702 | case HTTP_MSG_LAST_LF: |
| 1703 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1704 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1705 | ptr++; |
| 1706 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1707 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1709 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | return; |
| 1711 | #ifdef DEBUG_FULL |
| 1712 | default: |
| 1713 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1714 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1715 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | } |
| 1717 | http_msg_ood: |
| 1718 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1719 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1720 | buf->lr = ptr; |
| 1721 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1722 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | http_msg_invalid: |
| 1724 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1725 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1726 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | return; |
| 1728 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1729 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1730 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1731 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1732 | * nothing is done and 1 is returned. |
| 1733 | */ |
| 1734 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1735 | { |
| 1736 | int delta; |
| 1737 | char *cur_end; |
| 1738 | |
| 1739 | if (msg->sl.rq.v_l != 0) |
| 1740 | return 1; |
| 1741 | |
| 1742 | msg->sol = req->data + msg->som; |
| 1743 | cur_end = msg->sol + msg->sl.rq.l; |
| 1744 | delta = 0; |
| 1745 | |
| 1746 | if (msg->sl.rq.u_l == 0) { |
| 1747 | /* if no URI was set, add "/" */ |
| 1748 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1749 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1750 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1751 | } |
| 1752 | /* add HTTP version */ |
| 1753 | 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] | 1754 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1755 | cur_end += delta; |
| 1756 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1757 | HTTP_MSG_RQMETH, |
| 1758 | msg->sol, cur_end + 1, |
| 1759 | NULL, NULL); |
| 1760 | if (unlikely(!cur_end)) |
| 1761 | return 0; |
| 1762 | |
| 1763 | /* we have a full HTTP/1.0 request now and we know that |
| 1764 | * we have either a CR or an LF at <ptr>. |
| 1765 | */ |
| 1766 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1767 | return 1; |
| 1768 | } |
| 1769 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1770 | /* Parse the Connection: headaer of an HTTP request, and set the transaction |
| 1771 | * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag |
| 1772 | * is also set so that we don't parse a second time. If some dangerous values |
| 1773 | * are encountered, we leave the status to indicate that the request might be |
| 1774 | * interpreted as keep-alive, but we also set the connection flags to indicate |
| 1775 | * that we WANT it to be a close, so that the header will be fixed. This |
| 1776 | * function should only be called when we know we're interested in checking |
| 1777 | * the request (not a CONNECT, and FE or BE mangles the header). |
| 1778 | */ |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1779 | void http_req_parse_connection_header(struct http_txn *txn) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1780 | { |
| 1781 | struct http_msg *msg = &txn->req; |
| 1782 | struct hdr_ctx ctx; |
| 1783 | int conn_cl, conn_ka; |
| 1784 | |
| 1785 | if (txn->flags & TX_CON_HDR_PARS) |
| 1786 | return; |
| 1787 | |
| 1788 | conn_cl = 0; |
| 1789 | conn_ka = 0; |
| 1790 | ctx.idx = 0; |
| 1791 | |
| 1792 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 1793 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 1794 | conn_cl = 1; |
| 1795 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 1796 | conn_ka = 1; |
| 1797 | } |
| 1798 | |
| 1799 | /* Determine if the client wishes keep-alive or close. |
| 1800 | * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections |
| 1801 | * are persistent unless "Connection: close" is explicitly specified. |
| 1802 | * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections. |
| 1803 | * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless |
| 1804 | * they explicitly specify "Connection: Keep-Alive", regardless of any |
| 1805 | * optional "Keep-Alive" header. |
| 1806 | * Note that if we find a request with both "Connection: close" and |
| 1807 | * "Connection: Keep-Alive", we indicate we want a close but don't have |
| 1808 | * it, so that it can be enforced later. |
| 1809 | */ |
| 1810 | |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1811 | if (conn_cl && conn_ka) { |
| 1812 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1813 | } |
| 1814 | else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1815 | if (conn_cl) { |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1816 | txn->flags |= TX_REQ_CONN_CLO; |
| 1817 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 1818 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1819 | } |
| 1820 | } else { /* HTTP/1.0 */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1821 | if (!conn_ka) { |
| 1822 | txn->flags |= TX_REQ_CONN_CLO; |
| 1823 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 1824 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1825 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1826 | } |
| 1827 | txn->flags |= TX_CON_HDR_PARS; |
| 1828 | } |
| 1829 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1830 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1831 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1832 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1833 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1834 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1835 | * 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] | 1836 | * 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] | 1837 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1838 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1839 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1840 | char *ptr = buf->lr; |
| 1841 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1842 | unsigned int chunk = 0; |
| 1843 | |
| 1844 | /* The chunk size is in the following form, though we are only |
| 1845 | * interested in the size and CRLF : |
| 1846 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1847 | */ |
| 1848 | while (1) { |
| 1849 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1850 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1851 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1852 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1853 | if (c < 0) /* not a hex digit anymore */ |
| 1854 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1855 | if (++ptr >= end) |
| 1856 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1857 | if (chunk & 0xF000000) /* overflow will occur */ |
| 1858 | return -1; |
| 1859 | chunk = (chunk << 4) + c; |
| 1860 | } |
| 1861 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1862 | /* empty size not allowed */ |
| 1863 | if (ptr == buf->lr) |
| 1864 | return -1; |
| 1865 | |
| 1866 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1867 | if (++ptr >= end) |
| 1868 | ptr = buf->data; |
| 1869 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1870 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1873 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1874 | * for the end of chunk size. |
| 1875 | */ |
| 1876 | while (1) { |
| 1877 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1878 | /* we now have a CR or an LF at ptr */ |
| 1879 | if (likely(*ptr == '\r')) { |
| 1880 | if (++ptr >= end) |
| 1881 | ptr = buf->data; |
| 1882 | if (ptr == buf->r) |
| 1883 | return 0; |
| 1884 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1885 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1886 | if (*ptr != '\n') |
| 1887 | return -1; |
| 1888 | if (++ptr >= end) |
| 1889 | ptr = buf->data; |
| 1890 | /* done */ |
| 1891 | break; |
| 1892 | } |
| 1893 | else if (*ptr == ';') { |
| 1894 | /* chunk extension, ends at next CRLF */ |
| 1895 | if (++ptr >= end) |
| 1896 | ptr = buf->data; |
| 1897 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1898 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1899 | |
| 1900 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1901 | if (++ptr >= end) |
| 1902 | ptr = buf->data; |
| 1903 | if (ptr == buf->r) |
| 1904 | return 0; |
| 1905 | } |
| 1906 | /* we have a CRLF now, loop above */ |
| 1907 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1908 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1909 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1910 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1913 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 1914 | * which may or may not be present. We save that into ->lr and |
| 1915 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1916 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1917 | msg->sov += ptr - buf->lr; |
| 1918 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1919 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1920 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1921 | return 1; |
| 1922 | } |
| 1923 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1924 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 1925 | * message <msg>. The first visited position is buf->lr. If the end of |
| 1926 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1927 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1928 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1929 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 1930 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 1931 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 1932 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1933 | * which implies that all non-trailers data have already been scheduled for |
| 1934 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1935 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1936 | * important to note that this function is designed to be able to parse wrapped |
| 1937 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1938 | */ |
| 1939 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 1940 | { |
| 1941 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 1942 | while (1) { |
| 1943 | char *p1 = NULL, *p2 = NULL; |
| 1944 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1945 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1946 | |
| 1947 | /* scan current line and stop at LF or CRLF */ |
| 1948 | while (1) { |
| 1949 | if (ptr == buf->r) |
| 1950 | return 0; |
| 1951 | |
| 1952 | if (*ptr == '\n') { |
| 1953 | if (!p1) |
| 1954 | p1 = ptr; |
| 1955 | p2 = ptr; |
| 1956 | break; |
| 1957 | } |
| 1958 | |
| 1959 | if (*ptr == '\r') { |
| 1960 | if (p1) |
| 1961 | return -1; |
| 1962 | p1 = ptr; |
| 1963 | } |
| 1964 | |
| 1965 | ptr++; |
| 1966 | if (ptr >= buf->data + buf->size) |
| 1967 | ptr = buf->data; |
| 1968 | } |
| 1969 | |
| 1970 | /* after LF; point to beginning of next line */ |
| 1971 | p2++; |
| 1972 | if (p2 >= buf->data + buf->size) |
| 1973 | p2 = buf->data; |
| 1974 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1975 | bytes = p2 - buf->lr; |
| 1976 | if (bytes < 0) |
| 1977 | bytes += buf->size; |
| 1978 | |
| 1979 | /* schedule this line for forwarding */ |
| 1980 | msg->sov += bytes; |
| 1981 | if (msg->sov >= buf->size) |
| 1982 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1983 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1984 | if (p1 == buf->lr) { |
| 1985 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1986 | * Everything was scheduled for forwarding, there's nothing |
| 1987 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1988 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1989 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1990 | msg->msg_state = HTTP_MSG_DONE; |
| 1991 | return 1; |
| 1992 | } |
| 1993 | /* OK, next line then */ |
| 1994 | buf->lr = p2; |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1999 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2000 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2001 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2002 | * not enough data are available, the function does not change anything and |
| 2003 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2004 | * does not change anything. Note: this function is designed to parse wrapped |
| 2005 | * CRLF at the end of the buffer. |
| 2006 | */ |
| 2007 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2008 | { |
| 2009 | char *ptr; |
| 2010 | int bytes; |
| 2011 | |
| 2012 | /* NB: we'll check data availabilty at the end. It's not a |
| 2013 | * problem because whatever we match first will be checked |
| 2014 | * against the correct length. |
| 2015 | */ |
| 2016 | bytes = 1; |
| 2017 | ptr = buf->lr; |
| 2018 | if (*ptr == '\r') { |
| 2019 | bytes++; |
| 2020 | ptr++; |
| 2021 | if (ptr >= buf->data + buf->size) |
| 2022 | ptr = buf->data; |
| 2023 | } |
| 2024 | |
| 2025 | if (buf->l < bytes) |
| 2026 | return 0; |
| 2027 | |
| 2028 | if (*ptr != '\n') |
| 2029 | return -1; |
| 2030 | |
| 2031 | ptr++; |
| 2032 | if (ptr >= buf->data + buf->size) |
| 2033 | ptr = buf->data; |
| 2034 | buf->lr = ptr; |
| 2035 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2036 | msg->sov = ptr - buf->data; |
| 2037 | msg->som = msg->sov - bytes; |
| 2038 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2039 | return 1; |
| 2040 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2041 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2042 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2043 | { |
| 2044 | char *end = buf->data + buf->size; |
| 2045 | int off = buf->data + buf->size - buf->w; |
| 2046 | |
| 2047 | /* two possible cases : |
| 2048 | * - the buffer is in one contiguous block, we move it in-place |
| 2049 | * - the buffer is in two blocks, we move it via the trash |
| 2050 | */ |
| 2051 | if (buf->l) { |
| 2052 | int block1 = buf->l; |
| 2053 | int block2 = 0; |
| 2054 | if (buf->r <= buf->w) { |
| 2055 | /* non-contiguous block */ |
| 2056 | block1 = buf->data + buf->size - buf->w; |
| 2057 | block2 = buf->r - buf->data; |
| 2058 | } |
| 2059 | if (block2) |
| 2060 | memcpy(trash, buf->data, block2); |
| 2061 | memmove(buf->data, buf->w, block1); |
| 2062 | if (block2) |
| 2063 | memcpy(buf->data + block1, trash, block2); |
| 2064 | } |
| 2065 | |
| 2066 | /* adjust all known pointers */ |
| 2067 | buf->w = buf->data; |
| 2068 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2069 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2070 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2071 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2072 | |
| 2073 | /* adjust relative pointers */ |
| 2074 | msg->som = 0; |
| 2075 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2076 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2077 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2078 | |
| 2079 | msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size; |
| 2080 | msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size; |
| 2081 | |
| 2082 | if (msg->err_pos >= 0) { |
| 2083 | msg->err_pos += off; |
| 2084 | if (msg->err_pos >= buf->size) |
| 2085 | msg->err_pos -= buf->size; |
| 2086 | } |
| 2087 | |
| 2088 | buf->flags &= ~BF_FULL; |
| 2089 | if (buf->l >= buffer_max_len(buf)) |
| 2090 | buf->flags |= BF_FULL; |
| 2091 | } |
| 2092 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2093 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2094 | * processing can continue on next analysers, or zero if it either needs more |
| 2095 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2096 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2097 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2098 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2099 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2100 | 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] | 2101 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2102 | /* |
| 2103 | * We will parse the partial (or complete) lines. |
| 2104 | * We will check the request syntax, and also join multi-line |
| 2105 | * headers. An index of all the lines will be elaborated while |
| 2106 | * parsing. |
| 2107 | * |
| 2108 | * For the parsing, we use a 28 states FSM. |
| 2109 | * |
| 2110 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2111 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2112 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2113 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2114 | * req->lr = first non-visited byte |
| 2115 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2116 | * |
| 2117 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2118 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2119 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2120 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2121 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2122 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2123 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2124 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2125 | struct http_txn *txn = &s->txn; |
| 2126 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2127 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2128 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2129 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2130 | now_ms, __FUNCTION__, |
| 2131 | s, |
| 2132 | req, |
| 2133 | req->rex, req->wex, |
| 2134 | req->flags, |
| 2135 | req->l, |
| 2136 | req->analysers); |
| 2137 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2138 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2139 | s->srv_error = http_return_srv_error; |
| 2140 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2141 | /* There's a protected area at the end of the buffer for rewriting |
| 2142 | * purposes. We don't want to start to parse the request if the |
| 2143 | * protected area is affected, because we may have to move processed |
| 2144 | * data later, which is much more complicated. |
| 2145 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2146 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 2147 | if (unlikely((req->flags & BF_FULL) || |
| 2148 | req->r < req->lr || |
| 2149 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2150 | if (req->send_max) { |
| 2151 | /* some data has still not left the buffer, wake us once that's done */ |
| 2152 | buffer_dont_connect(req); |
| 2153 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2154 | return 0; |
| 2155 | } |
| 2156 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2157 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2158 | } |
| 2159 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2160 | if (likely(req->lr < req->r)) |
| 2161 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2164 | /* 1: we might have to print this header in debug mode */ |
| 2165 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2166 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2167 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2168 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2169 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2170 | sol = req->data + msg->som; |
| 2171 | eol = sol + msg->sl.rq.l; |
| 2172 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2173 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2174 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2175 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2176 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2177 | while (cur_idx) { |
| 2178 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2179 | debug_hdr("clihdr", s, sol, eol); |
| 2180 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2181 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2182 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2185 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2186 | /* |
| 2187 | * Now we quickly check if we have found a full valid request. |
| 2188 | * If not so, we check the FD and buffer states before leaving. |
| 2189 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2190 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2191 | * requests are checked first. |
| 2192 | * |
| 2193 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2194 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2195 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
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 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2198 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2199 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 2200 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2201 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2202 | /* 1: Since we are in header mode, if there's no space |
| 2203 | * left for headers, we won't be able to free more |
| 2204 | * later, so the session will never terminate. We |
| 2205 | * must terminate it now. |
| 2206 | */ |
| 2207 | if (unlikely(req->flags & BF_FULL)) { |
| 2208 | /* FIXME: check if URI is set and return Status |
| 2209 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2210 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2211 | goto return_bad_req; |
| 2212 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2213 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2214 | /* 2: have we encountered a read error ? */ |
| 2215 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2216 | if (txn->flags & TX_NOT_FIRST) |
| 2217 | goto failed_keep_alive; |
| 2218 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2219 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2220 | if (msg->err_pos >= 0) |
| 2221 | 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] | 2222 | msg->msg_state = HTTP_MSG_ERROR; |
| 2223 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2224 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2225 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2226 | if (s->listener->counters) |
| 2227 | s->listener->counters->failed_req++; |
| 2228 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2229 | if (!(s->flags & SN_ERR_MASK)) |
| 2230 | s->flags |= SN_ERR_CLICL; |
| 2231 | if (!(s->flags & SN_FINST_MASK)) |
| 2232 | s->flags |= SN_FINST_R; |
| 2233 | return 0; |
| 2234 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2235 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2236 | /* 3: has the read timeout expired ? */ |
| 2237 | 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] | 2238 | if (txn->flags & TX_NOT_FIRST) |
| 2239 | goto failed_keep_alive; |
| 2240 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2241 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2242 | if (msg->err_pos >= 0) |
| 2243 | 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] | 2244 | txn->status = 408; |
| 2245 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2246 | msg->msg_state = HTTP_MSG_ERROR; |
| 2247 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2248 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2249 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2250 | if (s->listener->counters) |
| 2251 | s->listener->counters->failed_req++; |
| 2252 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2253 | if (!(s->flags & SN_ERR_MASK)) |
| 2254 | s->flags |= SN_ERR_CLITO; |
| 2255 | if (!(s->flags & SN_FINST_MASK)) |
| 2256 | s->flags |= SN_FINST_R; |
| 2257 | return 0; |
| 2258 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2259 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2260 | /* 4: have we encountered a close ? */ |
| 2261 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2262 | if (txn->flags & TX_NOT_FIRST) |
| 2263 | goto failed_keep_alive; |
| 2264 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2265 | if (msg->err_pos >= 0) |
| 2266 | 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] | 2267 | txn->status = 400; |
| 2268 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2269 | msg->msg_state = HTTP_MSG_ERROR; |
| 2270 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2271 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2272 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2273 | if (s->listener->counters) |
| 2274 | s->listener->counters->failed_req++; |
| 2275 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2276 | if (!(s->flags & SN_ERR_MASK)) |
| 2277 | s->flags |= SN_ERR_CLICL; |
| 2278 | if (!(s->flags & SN_FINST_MASK)) |
| 2279 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2280 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2281 | } |
| 2282 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2283 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2284 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 5e8949c | 2010-01-03 14:38:03 +0100 | [diff] [blame] | 2285 | req->flags &= ~BF_DONT_READ; |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2286 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2287 | /* just set the request timeout once at the beginning of the request */ |
| 2288 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 2289 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2290 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2291 | /* we're not ready yet */ |
| 2292 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2293 | |
| 2294 | failed_keep_alive: |
| 2295 | /* Here we process low-level errors for keep-alive requests. In |
| 2296 | * short, if the request is not the first one and it experiences |
| 2297 | * a timeout, read error or shutdown, we just silently close so |
| 2298 | * that the client can try again. |
| 2299 | */ |
| 2300 | txn->status = 0; |
| 2301 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2302 | req->analysers = 0; |
| 2303 | s->logs.logwait = 0; |
| 2304 | stream_int_cond_close(req->prod, NULL); |
| 2305 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2306 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2307 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2308 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2309 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2310 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2311 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2312 | * points to the CRLF of the request line. req->lr points to the first |
| 2313 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2314 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2315 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2316 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2317 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2318 | /* Maybe we found in invalid header name while we were configured not |
| 2319 | * to block on that, so we have to capture it now. |
| 2320 | */ |
| 2321 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2322 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2323 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2324 | /* ensure we keep this pointer to the beginning of the message */ |
| 2325 | msg->sol = req->data + msg->som; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2326 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2327 | /* |
| 2328 | * 1: identify the method |
| 2329 | */ |
| 2330 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 2331 | |
| 2332 | /* we can make use of server redirect on GET and HEAD */ |
| 2333 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2334 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2335 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2336 | /* |
| 2337 | * 2: check if the URI matches the monitor_uri. |
| 2338 | * We have to do this for every request which gets in, because |
| 2339 | * the monitor-uri is defined by the frontend. |
| 2340 | */ |
| 2341 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2342 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2343 | !memcmp(&req->data[msg->sl.rq.u], |
| 2344 | s->fe->monitor_uri, |
| 2345 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2346 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2347 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2348 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2349 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2350 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2351 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2352 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2354 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2355 | 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] | 2356 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2357 | ret = acl_pass(ret); |
| 2358 | if (cond->pol == ACL_COND_UNLESS) |
| 2359 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2360 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2361 | if (ret) { |
| 2362 | /* we fail this request, let's return 503 service unavail */ |
| 2363 | txn->status = 503; |
| 2364 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2365 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2366 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2367 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2368 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2369 | /* nothing to fail, let's reply normaly */ |
| 2370 | txn->status = 200; |
| 2371 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2372 | goto return_prx_cond; |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2377 | * Note: we cannot log anymore if the request has been |
| 2378 | * classified as invalid. |
| 2379 | */ |
| 2380 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2381 | /* we have a complete HTTP request that we must log */ |
| 2382 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2383 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2384 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2385 | if (urilen >= REQURI_LEN) |
| 2386 | urilen = REQURI_LEN - 1; |
| 2387 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2388 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2389 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2390 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2391 | s->do_log(s); |
| 2392 | } else { |
| 2393 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2394 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2395 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2396 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2397 | /* 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] | 2398 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2399 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2400 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2401 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2402 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 2403 | ((req->data[msg->sl.rq.v + 5] > '1') || |
| 2404 | ((req->data[msg->sl.rq.v + 5] == '1') && |
| 2405 | (req->data[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2406 | txn->flags |= TX_REQ_VER_11; |
| 2407 | |
| 2408 | /* "connection" has not been parsed yet */ |
| 2409 | txn->flags &= ~TX_CON_HDR_PARS; |
| 2410 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2411 | /* transfer length unknown*/ |
| 2412 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2413 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2414 | /* 5: we may need to capture headers */ |
| 2415 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
| 2416 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
| 2417 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2418 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2419 | /* 6: determine the transfer-length. |
| 2420 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2421 | * the presence of a message-body in a REQUEST and its transfer length |
| 2422 | * must be determined that way (in order of precedence) : |
| 2423 | * 1. The presence of a message-body in a request is signaled by the |
| 2424 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2425 | * in the request's header fields. When a request message contains |
| 2426 | * both a message-body of non-zero length and a method that does |
| 2427 | * not define any semantics for that request message-body, then an |
| 2428 | * origin server SHOULD either ignore the message-body or respond |
| 2429 | * with an appropriate error message (e.g., 413). A proxy or |
| 2430 | * gateway, when presented the same request, SHOULD either forward |
| 2431 | * the request inbound with the message- body or ignore the |
| 2432 | * message-body when determining a response. |
| 2433 | * |
| 2434 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2435 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2436 | * transfer-length is defined by the use of this transfer-coding. |
| 2437 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2438 | * transfer-coding is not present, the transfer-length is defined |
| 2439 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2440 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2441 | * 3. If a Content-Length header field is present, its decimal value in |
| 2442 | * OCTETs represents both the entity-length and the transfer-length. |
| 2443 | * If a message is received with both a Transfer-Encoding header |
| 2444 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2445 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2446 | * 4. By the server closing the connection. (Closing the connection |
| 2447 | * cannot be used to indicate the end of a request body, since that |
| 2448 | * would leave no possibility for the server to send back a response.) |
| 2449 | * |
| 2450 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2451 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2452 | * it is terminated by closing the connection. When the "chunked" |
| 2453 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2454 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2455 | */ |
| 2456 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2457 | /* CONNECT sets a tunnel and ignores everything else */ |
| 2458 | if (txn->meth == HTTP_METH_CONNECT) |
| 2459 | goto skip_xfer_len; |
| 2460 | |
| 2461 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2462 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2463 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2464 | while ((txn->flags & TX_REQ_VER_11) && |
| 2465 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2466 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2467 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2468 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2469 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2470 | use_close_only = 1; |
| 2471 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2472 | break; |
| 2473 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2476 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2477 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2478 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2479 | signed long long cl; |
| 2480 | |
| 2481 | if (!ctx.vlen) |
| 2482 | goto return_bad_req; |
| 2483 | |
| 2484 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2485 | goto return_bad_req; /* parse failure */ |
| 2486 | |
| 2487 | if (cl < 0) |
| 2488 | goto return_bad_req; |
| 2489 | |
| 2490 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2491 | goto return_bad_req; /* already specified, was different */ |
| 2492 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2493 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2494 | msg->hdr_content_len = cl; |
| 2495 | } |
| 2496 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2497 | /* bodyless requests have a known length */ |
| 2498 | if (!use_close_only) |
| 2499 | txn->flags |= TX_REQ_XFER_LEN; |
| 2500 | |
| 2501 | skip_xfer_len: |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2502 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2503 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2504 | req->analyse_exp = TICK_ETERNITY; |
| 2505 | return 1; |
| 2506 | |
| 2507 | return_bad_req: |
| 2508 | /* We centralize bad requests processing here */ |
| 2509 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2510 | /* we detected a parsing error. We want to archive this request |
| 2511 | * in the dedicated proxy area for later troubleshooting. |
| 2512 | */ |
| 2513 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2514 | } |
| 2515 | |
| 2516 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2517 | txn->status = 400; |
| 2518 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2519 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2520 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2521 | if (s->listener->counters) |
| 2522 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2523 | |
| 2524 | return_prx_cond: |
| 2525 | if (!(s->flags & SN_ERR_MASK)) |
| 2526 | s->flags |= SN_ERR_PRXCOND; |
| 2527 | if (!(s->flags & SN_FINST_MASK)) |
| 2528 | s->flags |= SN_FINST_R; |
| 2529 | |
| 2530 | req->analysers = 0; |
| 2531 | req->analyse_exp = TICK_ETERNITY; |
| 2532 | return 0; |
| 2533 | } |
| 2534 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2535 | /* This stream analyser runs all HTTP request processing which is common to |
| 2536 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2537 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2538 | * 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] | 2539 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2540 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2541 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2542 | 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] | 2543 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2544 | struct http_txn *txn = &s->txn; |
| 2545 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2546 | struct acl_cond *cond; |
| 2547 | struct redirect_rule *rule; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2548 | struct wordlist *wl; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2549 | int cur_idx; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2550 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2551 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2552 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2553 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2554 | return 0; |
| 2555 | } |
| 2556 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2557 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2558 | req->analyse_exp = TICK_ETERNITY; |
| 2559 | |
| 2560 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2561 | now_ms, __FUNCTION__, |
| 2562 | s, |
| 2563 | req, |
| 2564 | req->rex, req->wex, |
| 2565 | req->flags, |
| 2566 | req->l, |
| 2567 | req->analysers); |
| 2568 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2569 | /* first check whether we have some ACLs set to block this request */ |
| 2570 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2571 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2572 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2573 | ret = acl_pass(ret); |
| 2574 | if (cond->pol == ACL_COND_UNLESS) |
| 2575 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2576 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2577 | if (ret) { |
| 2578 | txn->status = 403; |
| 2579 | /* let's log the request time */ |
| 2580 | s->logs.tv_request = now; |
| 2581 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2582 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2583 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2584 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2585 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2586 | /* try headers filters */ |
| 2587 | if (px->req_exp != NULL) { |
| 2588 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2589 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2590 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2591 | /* has the request been denied ? */ |
| 2592 | if (txn->flags & TX_CLDENY) { |
| 2593 | /* no need to go further */ |
| 2594 | txn->status = 403; |
| 2595 | /* let's log the request time */ |
| 2596 | s->logs.tv_request = now; |
| 2597 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2598 | goto return_prx_cond; |
| 2599 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2600 | |
| 2601 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2602 | * which may be the same as the connect timeout if unspecified. |
| 2603 | * If unset, then set it to zero because we really want it to |
| 2604 | * eventually expire. We build the tarpit as an analyser. |
| 2605 | */ |
| 2606 | if (txn->flags & TX_CLTARPIT) { |
| 2607 | buffer_erase(s->req); |
| 2608 | /* wipe the request out so that we can drop the connection early |
| 2609 | * if the client closes first. |
| 2610 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2611 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2612 | req->analysers = 0; /* remove switching rules etc... */ |
| 2613 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2614 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2615 | if (!req->analyse_exp) |
| 2616 | req->analyse_exp = tick_add(now_ms, 0); |
| 2617 | return 1; |
| 2618 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2619 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2620 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2621 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2622 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2623 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2624 | * in which headers are mangled. However, if another mode is set, it will |
| 2625 | * affect it (eg: server-close/keep-alive + httpclose = close). |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2626 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2627 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2628 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2629 | ((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] | 2630 | int tmp = TX_CON_WANT_TUN; |
| 2631 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2632 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2633 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2634 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2635 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2636 | tmp = TX_CON_WANT_CLO; |
| 2637 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2638 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2639 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2640 | |
Willy Tarreau | 8db1c17 | 2010-01-05 23:12:12 +0100 | [diff] [blame^] | 2641 | if (!(txn->flags & TX_CON_HDR_PARS)) |
| 2642 | http_req_parse_connection_header(txn); |
| 2643 | |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2644 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2645 | if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) |
| 2646 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2647 | if (!(txn->flags & TX_REQ_XFER_LEN)) |
| 2648 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2649 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | /* We're really certain of the connection mode (tunnel, close, keep-alive) |
| 2653 | * once we know the backend, because the tunnel mode can be implied by the |
| 2654 | * lack of any close/keepalive options in both the FE and the BE. Since |
| 2655 | * this information can evolve with time, we proceed by trying to make the |
| 2656 | * header status match the desired status. For this, we'll have to adjust |
| 2657 | * the "Connection" header. The test for persistent connections has already |
| 2658 | * been performed, so we only enter here if there is a risk the connection |
| 2659 | * is considered as persistent and we want it to be closed on the server |
| 2660 | * side. It would be nice if we could enter this place only when a |
| 2661 | * Connection header exists. Note that a CONNECT method will not enter |
| 2662 | * here. |
| 2663 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2664 | if (!(txn->flags & TX_REQ_CONN_CLO) && |
| 2665 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 2666 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2667 | char *cur_ptr, *cur_end, *cur_next; |
| 2668 | int old_idx, delta, val; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2669 | int must_delete; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2670 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2671 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2672 | must_delete = !(txn->flags & TX_REQ_VER_11); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2673 | 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] | 2674 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2675 | 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] | 2676 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2677 | cur_ptr = cur_next; |
| 2678 | cur_end = cur_ptr + cur_hdr->len; |
| 2679 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2680 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2681 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2682 | if (!val) |
| 2683 | continue; |
| 2684 | |
| 2685 | /* 3 possibilities : |
| 2686 | * - we have already set "Connection: close" or we're in |
| 2687 | * HTTP/1.0, so we remove this line. |
| 2688 | * - we have not yet set "Connection: close", but this line |
| 2689 | * indicates close. We leave it untouched and set the flag. |
| 2690 | * - we have not yet set "Connection: close", and this line |
| 2691 | * indicates non-close. We replace it and set the flag. |
| 2692 | */ |
| 2693 | if (must_delete) { |
| 2694 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 2695 | http_msg_move_end(&txn->req, delta); |
| 2696 | cur_next += delta; |
| 2697 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2698 | txn->hdr_idx.used--; |
| 2699 | cur_hdr->len = 0; |
| 2700 | txn->flags |= TX_REQ_CONN_CLO; |
| 2701 | } else { |
| 2702 | if (cur_end - cur_ptr - val != 5 || |
| 2703 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2704 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2705 | "close", 5); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2706 | cur_next += delta; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2707 | cur_hdr->len += delta; |
| 2708 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2709 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2710 | txn->flags |= TX_REQ_CONN_CLO; |
| 2711 | must_delete = 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2712 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2713 | } /* for loop */ |
| 2714 | } /* if must close keep-alive */ |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2715 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2716 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2717 | list_for_each_entry(wl, &px->req_add, list) { |
| 2718 | 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] | 2719 | goto return_bad_req; |
| 2720 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2721 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2722 | /* check if stats URI was requested, and if an auth is needed */ |
| 2723 | if (px->uri_auth != NULL && |
| 2724 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2725 | /* we have to check the URI and auth for this request. |
| 2726 | * FIXME!!! that one is rather dangerous, we want to |
| 2727 | * make it follow standard rules (eg: clear req->analysers). |
| 2728 | */ |
| 2729 | if (stats_check_uri_auth(s, px)) { |
| 2730 | req->analysers = 0; |
| 2731 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2732 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2733 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2734 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2735 | /* check whether we have some ACLs set to redirect this request */ |
| 2736 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2737 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2738 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2739 | if (rule->cond) { |
| 2740 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 2741 | ret = acl_pass(ret); |
| 2742 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2743 | ret = !ret; |
| 2744 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2745 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2746 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2747 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2748 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2749 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2750 | /* build redirect message */ |
| 2751 | switch(rule->code) { |
| 2752 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2753 | msg_fmt = HTTP_303; |
| 2754 | break; |
| 2755 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2756 | msg_fmt = HTTP_301; |
| 2757 | break; |
| 2758 | case 302: |
| 2759 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2760 | msg_fmt = HTTP_302; |
| 2761 | break; |
| 2762 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2763 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2764 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2765 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2766 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2767 | switch(rule->type) { |
| 2768 | case REDIRECT_TYPE_PREFIX: { |
| 2769 | const char *path; |
| 2770 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2771 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2772 | path = http_get_path(txn); |
| 2773 | /* build message using path */ |
| 2774 | if (path) { |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 2775 | 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] | 2776 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2777 | int qs = 0; |
| 2778 | while (qs < pathlen) { |
| 2779 | if (path[qs] == '?') { |
| 2780 | pathlen = qs; |
| 2781 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2782 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2783 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2784 | } |
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 | } else { |
| 2787 | path = "/"; |
| 2788 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2789 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2790 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2791 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2792 | goto return_bad_req; |
| 2793 | |
| 2794 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2795 | * add anything, otherwise it makes it hard for the user to |
| 2796 | * configure a self-redirection. |
| 2797 | */ |
| 2798 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2799 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2800 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2801 | } |
| 2802 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2803 | /* add path */ |
| 2804 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2805 | rdr.len += pathlen; |
| 2806 | break; |
| 2807 | } |
| 2808 | case REDIRECT_TYPE_LOCATION: |
| 2809 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2810 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2811 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2812 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2813 | /* add location */ |
| 2814 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2815 | rdr.len += rule->rdr_len; |
| 2816 | break; |
| 2817 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2818 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2819 | if (rule->cookie_len) { |
| 2820 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2821 | rdr.len += 14; |
| 2822 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2823 | rdr.len += rule->cookie_len; |
| 2824 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2825 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2826 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2827 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2828 | /* add end of headers and the keep-alive/close status. |
| 2829 | * We may choose to set keep-alive if the Location begins |
| 2830 | * with a slash, because the client will come back to the |
| 2831 | * same server. |
| 2832 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2833 | txn->status = rule->code; |
| 2834 | /* let's log the request time */ |
| 2835 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2836 | |
| 2837 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 2838 | (txn->flags & TX_REQ_XFER_LEN) && |
| 2839 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 2840 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2841 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2842 | /* keep-alive possible */ |
| 2843 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28); |
| 2844 | rdr.len += 28; |
| 2845 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 2846 | /* "eat" the request */ |
| 2847 | buffer_ignore(req, msg->sov - msg->som); |
| 2848 | msg->som = msg->sov; |
| 2849 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 2850 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 2851 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 2852 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2853 | break; |
| 2854 | } else { |
| 2855 | /* keep-alive not possible */ |
| 2856 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 2857 | rdr.len += 23; |
| 2858 | stream_int_cond_close(req->prod, &rdr); |
| 2859 | goto return_prx_cond; |
| 2860 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2861 | } |
| 2862 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2863 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2864 | /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */ |
| 2865 | if ((txn->flags & TX_REQ_XFER_LEN) && |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2866 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2867 | (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | 349a0f6 | 2009-10-18 21:17:42 +0200 | [diff] [blame] | 2868 | req->flags |= BF_DONT_READ; |
Willy Tarreau | 5e8949c | 2010-01-03 14:38:03 +0100 | [diff] [blame] | 2869 | else |
| 2870 | req->flags &= ~BF_DONT_READ; |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 2871 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2872 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 2873 | * If this happens, then the data will not come immediately, so we must |
| 2874 | * send all what we have without waiting. Note that due to the small gain |
| 2875 | * in waiting for the body of the request, it's easier to simply put the |
| 2876 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 2877 | * itself once used. |
| 2878 | */ |
| 2879 | req->flags |= BF_SEND_DONTWAIT; |
| 2880 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2881 | /* that's OK for us now, let's move on to next analysers */ |
| 2882 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2883 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2884 | return_bad_req: |
| 2885 | /* We centralize bad requests processing here */ |
| 2886 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2887 | /* we detected a parsing error. We want to archive this request |
| 2888 | * in the dedicated proxy area for later troubleshooting. |
| 2889 | */ |
| 2890 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2891 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2892 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2893 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2894 | txn->status = 400; |
| 2895 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2896 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2897 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2898 | if (s->listener->counters) |
| 2899 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2900 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2901 | return_prx_cond: |
| 2902 | if (!(s->flags & SN_ERR_MASK)) |
| 2903 | s->flags |= SN_ERR_PRXCOND; |
| 2904 | if (!(s->flags & SN_FINST_MASK)) |
| 2905 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2906 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2907 | req->analysers = 0; |
| 2908 | req->analyse_exp = TICK_ETERNITY; |
| 2909 | return 0; |
| 2910 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2911 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2912 | /* This function performs all the processing enabled for the current request. |
| 2913 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2914 | * needs more data, encounters an error, or wants to immediately abort the |
| 2915 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 2916 | */ |
| 2917 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 2918 | { |
| 2919 | struct http_txn *txn = &s->txn; |
| 2920 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2921 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2922 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2923 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2924 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2925 | return 0; |
| 2926 | } |
| 2927 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2928 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2929 | now_ms, __FUNCTION__, |
| 2930 | s, |
| 2931 | req, |
| 2932 | req->rex, req->wex, |
| 2933 | req->flags, |
| 2934 | req->l, |
| 2935 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2936 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2937 | /* |
| 2938 | * Right now, we know that we have processed the entire headers |
| 2939 | * and that unwanted requests have been filtered out. We can do |
| 2940 | * whatever we want with the remaining request. Also, now we |
| 2941 | * may have separate values for ->fe, ->be. |
| 2942 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2943 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2944 | /* |
| 2945 | * If HTTP PROXY is set we simply get remote server address |
| 2946 | * parsing incoming request. |
| 2947 | */ |
| 2948 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
| 2949 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
| 2950 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2951 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2952 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2953 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2954 | * Note that doing so might move headers in the request, but |
| 2955 | * the fields will stay coherent and the URI will not move. |
| 2956 | * This should only be performed in the backend. |
| 2957 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2958 | 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] | 2959 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 2960 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2961 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2962 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2963 | * 8: the appsession cookie was looked up very early in 1.2, |
| 2964 | * so let's do the same now. |
| 2965 | */ |
| 2966 | |
| 2967 | /* It needs to look into the URI */ |
| 2968 | if ((s->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 2969 | 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] | 2970 | } |
| 2971 | |
| 2972 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2973 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2974 | * asks for it. |
| 2975 | */ |
| 2976 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 2977 | if (s->cli_addr.ss_family == AF_INET) { |
| 2978 | /* Add an X-Forwarded-For header unless the source IP is |
| 2979 | * in the 'except' network range. |
| 2980 | */ |
| 2981 | if ((!s->fe->except_mask.s_addr || |
| 2982 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 2983 | != s->fe->except_net.s_addr) && |
| 2984 | (!s->be->except_mask.s_addr || |
| 2985 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 2986 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2987 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2988 | unsigned char *pn; |
| 2989 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2990 | |
| 2991 | /* Note: we rely on the backend to get the header name to be used for |
| 2992 | * x-forwarded-for, because the header is really meant for the backends. |
| 2993 | * However, if the backend did not specify any option, we have to rely |
| 2994 | * on the frontend's header name. |
| 2995 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2996 | if (s->be->fwdfor_hdr_len) { |
| 2997 | len = s->be->fwdfor_hdr_len; |
| 2998 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2999 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3000 | len = s->fe->fwdfor_hdr_len; |
| 3001 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3002 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3003 | 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] | 3004 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3005 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3006 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3007 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3008 | } |
| 3009 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3010 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3011 | /* FIXME: for the sake of completeness, we should also support |
| 3012 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3013 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3014 | int len; |
| 3015 | char pn[INET6_ADDRSTRLEN]; |
| 3016 | inet_ntop(AF_INET6, |
| 3017 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3018 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3019 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3020 | /* Note: we rely on the backend to get the header name to be used for |
| 3021 | * x-forwarded-for, because the header is really meant for the backends. |
| 3022 | * However, if the backend did not specify any option, we have to rely |
| 3023 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3024 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3025 | if (s->be->fwdfor_hdr_len) { |
| 3026 | len = s->be->fwdfor_hdr_len; |
| 3027 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3028 | } else { |
| 3029 | len = s->fe->fwdfor_hdr_len; |
| 3030 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
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 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3033 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3034 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3035 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3036 | goto return_bad_req; |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3041 | * 10: add X-Original-To if either the frontend or the backend |
| 3042 | * asks for it. |
| 3043 | */ |
| 3044 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3045 | |
| 3046 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3047 | if (s->cli_addr.ss_family == AF_INET) { |
| 3048 | /* Add an X-Original-To header unless the destination IP is |
| 3049 | * in the 'except' network range. |
| 3050 | */ |
| 3051 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3052 | get_frt_addr(s); |
| 3053 | |
| 3054 | if ((!s->fe->except_mask_to.s_addr || |
| 3055 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3056 | != s->fe->except_to.s_addr) && |
| 3057 | (!s->be->except_mask_to.s_addr || |
| 3058 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3059 | != s->be->except_to.s_addr)) { |
| 3060 | int len; |
| 3061 | unsigned char *pn; |
| 3062 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3063 | |
| 3064 | /* Note: we rely on the backend to get the header name to be used for |
| 3065 | * x-original-to, because the header is really meant for the backends. |
| 3066 | * However, if the backend did not specify any option, we have to rely |
| 3067 | * on the frontend's header name. |
| 3068 | */ |
| 3069 | if (s->be->orgto_hdr_len) { |
| 3070 | len = s->be->orgto_hdr_len; |
| 3071 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3072 | } else { |
| 3073 | len = s->fe->orgto_hdr_len; |
| 3074 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3075 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3076 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3077 | |
| 3078 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3079 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3080 | goto return_bad_req; |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3085 | /* 11: add "Connection: close" if needed and not yet set. */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3086 | if (!(txn->flags & TX_REQ_CONN_CLO) && |
| 3087 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3088 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3089 | if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3090 | "Connection: close", 17) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3091 | goto return_bad_req; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3092 | txn->flags |= TX_REQ_CONN_CLO; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3093 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3094 | |
| 3095 | /* If we have no server assigned yet and we're balancing on url_param |
| 3096 | * with a POST request, we may be interested in checking the body for |
| 3097 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3098 | */ |
| 3099 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3100 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3101 | s->be->url_param_post_limit != 0 && |
| 3102 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 3103 | 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] | 3104 | buffer_dont_connect(req); |
| 3105 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3106 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3107 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3108 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3109 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3110 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3111 | /************************************************************* |
| 3112 | * OK, that's finished for the headers. We have done what we * |
| 3113 | * could. Let's switch to the DATA state. * |
| 3114 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3115 | req->analyse_exp = TICK_ETERNITY; |
| 3116 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3117 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3118 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3119 | /* OK let's go on with the BODY now */ |
| 3120 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3121 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3122 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3123 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3124 | /* we detected a parsing error. We want to archive this request |
| 3125 | * in the dedicated proxy area for later troubleshooting. |
| 3126 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3127 | 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] | 3128 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3129 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3130 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3131 | txn->status = 400; |
| 3132 | req->analysers = 0; |
| 3133 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3134 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3135 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3136 | if (s->listener->counters) |
| 3137 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3138 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3139 | if (!(s->flags & SN_ERR_MASK)) |
| 3140 | s->flags |= SN_ERR_PRXCOND; |
| 3141 | if (!(s->flags & SN_FINST_MASK)) |
| 3142 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3143 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3144 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3145 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3146 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3147 | * returns zero, at the beginning because it prevents any other processing |
| 3148 | * from occurring, and at the end because it terminates the request. |
| 3149 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3150 | 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] | 3151 | { |
| 3152 | struct http_txn *txn = &s->txn; |
| 3153 | |
| 3154 | /* This connection is being tarpitted. The CLIENT side has |
| 3155 | * already set the connect expiration date to the right |
| 3156 | * timeout. We just have to check that the client is still |
| 3157 | * there and that the timeout has not expired. |
| 3158 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3159 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3160 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3161 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3162 | return 0; |
| 3163 | |
| 3164 | /* We will set the queue timer to the time spent, just for |
| 3165 | * logging purposes. We fake a 500 server error, so that the |
| 3166 | * attacker will not suspect his connection has been tarpitted. |
| 3167 | * It will not cause trouble to the logs because we can exclude |
| 3168 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3169 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3170 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3171 | |
| 3172 | txn->status = 500; |
| 3173 | if (req->flags != BF_READ_ERROR) |
| 3174 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3175 | |
| 3176 | req->analysers = 0; |
| 3177 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3178 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3179 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3180 | if (s->listener->counters) |
| 3181 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3182 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3183 | if (!(s->flags & SN_ERR_MASK)) |
| 3184 | s->flags |= SN_ERR_PRXCOND; |
| 3185 | if (!(s->flags & SN_FINST_MASK)) |
| 3186 | s->flags |= SN_FINST_T; |
| 3187 | return 0; |
| 3188 | } |
| 3189 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3190 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3191 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3192 | * must only be called after the standard HTTP request processing has occurred, |
| 3193 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3194 | * read more data, or 1 once it has completed its analysis. |
| 3195 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3196 | 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] | 3197 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3198 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3199 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3200 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3201 | |
| 3202 | /* We have to parse the HTTP request body to find any required data. |
| 3203 | * "balance url_param check_post" should have been the only way to get |
| 3204 | * into this. We were brought here after HTTP header analysis, so all |
| 3205 | * related structures are ready. |
| 3206 | */ |
| 3207 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3208 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3209 | goto missing_data; |
| 3210 | |
| 3211 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3212 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3213 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3214 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3215 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3216 | struct hdr_ctx ctx; |
| 3217 | ctx.idx = 0; |
| 3218 | /* Expect is allowed in 1.1, look for it */ |
| 3219 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3220 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3221 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3222 | } |
| 3223 | } |
| 3224 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3225 | } |
| 3226 | |
| 3227 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3228 | /* we have msg->col and msg->sov which both point to the first |
| 3229 | * byte of message body. msg->som still points to the beginning |
| 3230 | * of the message. We must save the body in req->lr because it |
| 3231 | * survives buffer re-alignments. |
| 3232 | */ |
| 3233 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3234 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3235 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3236 | else |
| 3237 | msg->msg_state = HTTP_MSG_DATA; |
| 3238 | } |
| 3239 | |
| 3240 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3241 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3242 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3243 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3244 | */ |
| 3245 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3246 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3247 | if (!ret) |
| 3248 | goto missing_data; |
| 3249 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3250 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3251 | } |
| 3252 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3253 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3254 | * We have the first non-header byte in msg->col, which is either the |
| 3255 | * beginning of the chunk size or of the data. The first data byte is in |
| 3256 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3257 | * 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] | 3258 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3259 | |
| 3260 | if (msg->hdr_content_len < limit) |
| 3261 | limit = msg->hdr_content_len; |
| 3262 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3263 | 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] | 3264 | goto http_end; |
| 3265 | |
| 3266 | missing_data: |
| 3267 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3268 | if (req->flags & BF_FULL) |
| 3269 | goto return_bad_req; |
| 3270 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3271 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3272 | txn->status = 408; |
| 3273 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3274 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3275 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3276 | |
| 3277 | /* we get here if we need to wait for more data */ |
| 3278 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3279 | /* Not enough data. We'll re-use the http-request |
| 3280 | * timeout here. Ideally, we should set the timeout |
| 3281 | * relative to the accept() date. We just set the |
| 3282 | * request timeout once at the beginning of the |
| 3283 | * request. |
| 3284 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3285 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3286 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3287 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3288 | return 0; |
| 3289 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3290 | |
| 3291 | http_end: |
| 3292 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3293 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3294 | req->analysers &= ~an_bit; |
| 3295 | req->analyse_exp = TICK_ETERNITY; |
| 3296 | return 1; |
| 3297 | |
| 3298 | return_bad_req: /* let's centralize all bad requests */ |
| 3299 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3300 | txn->status = 400; |
| 3301 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3302 | |
| 3303 | return_err_msg: |
| 3304 | req->analysers = 0; |
| 3305 | s->fe->counters.failed_req++; |
| 3306 | if (s->listener->counters) |
| 3307 | s->listener->counters->failed_req++; |
| 3308 | |
| 3309 | if (!(s->flags & SN_ERR_MASK)) |
| 3310 | s->flags |= SN_ERR_PRXCOND; |
| 3311 | if (!(s->flags & SN_FINST_MASK)) |
| 3312 | s->flags |= SN_FINST_R; |
| 3313 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3314 | } |
| 3315 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3316 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3317 | * right now but it works. |
| 3318 | */ |
| 3319 | void http_end_txn_clean_session(struct session *s) |
| 3320 | { |
| 3321 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3322 | * server's connections. We need a safer way to reinitialize buffer |
| 3323 | * flags. We also need a more accurate method for computing per-request |
| 3324 | * data. |
| 3325 | */ |
| 3326 | http_silent_debug(__LINE__, s); |
| 3327 | |
| 3328 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3329 | s->req->cons->shutr(s->req->cons); |
| 3330 | s->req->cons->shutw(s->req->cons); |
| 3331 | |
| 3332 | http_silent_debug(__LINE__, s); |
| 3333 | |
| 3334 | if (s->flags & SN_BE_ASSIGNED) |
| 3335 | s->be->beconn--; |
| 3336 | |
| 3337 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3338 | session_process_counters(s); |
| 3339 | |
| 3340 | if (s->txn.status) { |
| 3341 | int n; |
| 3342 | |
| 3343 | n = s->txn.status / 100; |
| 3344 | if (n < 1 || n > 5) |
| 3345 | n = 0; |
| 3346 | |
| 3347 | if (s->fe->mode == PR_MODE_HTTP) |
| 3348 | s->fe->counters.p.http.rsp[n]++; |
| 3349 | |
| 3350 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 3351 | (s->be->mode == PR_MODE_HTTP)) |
| 3352 | s->be->counters.p.http.rsp[n]++; |
| 3353 | } |
| 3354 | |
| 3355 | /* don't count other requests' data */ |
| 3356 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3357 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3358 | |
| 3359 | /* let's do a final log if we need it */ |
| 3360 | if (s->logs.logwait && |
| 3361 | !(s->flags & SN_MONITOR) && |
| 3362 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3363 | s->do_log(s); |
| 3364 | } |
| 3365 | |
| 3366 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3367 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3368 | tv_zero(&s->logs.tv_request); |
| 3369 | s->logs.t_queue = -1; |
| 3370 | s->logs.t_connect = -1; |
| 3371 | s->logs.t_data = -1; |
| 3372 | s->logs.t_close = 0; |
| 3373 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3374 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3375 | |
| 3376 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3377 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3378 | |
| 3379 | if (s->pend_pos) |
| 3380 | pendconn_free(s->pend_pos); |
| 3381 | |
| 3382 | if (s->srv) { |
| 3383 | if (s->flags & SN_CURR_SESS) { |
| 3384 | s->flags &= ~SN_CURR_SESS; |
| 3385 | s->srv->cur_sess--; |
| 3386 | } |
| 3387 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3388 | process_srv_queue(s->srv); |
| 3389 | } |
| 3390 | |
| 3391 | if (unlikely(s->srv_conn)) |
| 3392 | sess_change_server(s, NULL); |
| 3393 | s->srv = NULL; |
| 3394 | |
| 3395 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3396 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3397 | s->req->cons->err_type = SI_ET_NONE; |
| 3398 | s->req->cons->err_loc = NULL; |
| 3399 | s->req->cons->exp = TICK_ETERNITY; |
| 3400 | s->req->cons->flags = SI_FL_NONE; |
| 3401 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE); |
| 3402 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE|BF_WRITE_PARTIAL); |
| 3403 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED); |
| 3404 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3405 | s->txn.meth = 0; |
| 3406 | http_reset_txn(s); |
| 3407 | s->txn.flags |= TX_NOT_FIRST; |
| 3408 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3409 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3410 | |
| 3411 | /* if the request buffer is not empty, it means we're |
| 3412 | * about to process another request, so send pending |
| 3413 | * data with MSG_MORE to merge TCP packets when possible. |
| 3414 | * Also, let's not start reading a small request packet, |
| 3415 | * we may prefer to read a larger one later. |
| 3416 | */ |
| 3417 | s->req->flags &= ~BF_DONT_READ; |
| 3418 | if (s->req->l > s->req->send_max) { |
| 3419 | s->rep->flags |= BF_EXPECT_MORE; |
| 3420 | s->req->flags |= BF_DONT_READ; |
| 3421 | } |
| 3422 | |
| 3423 | /* make ->lr point to the first non-forwarded byte */ |
| 3424 | s->req->lr = s->req->w + s->req->send_max; |
| 3425 | if (s->req->lr >= s->req->data + s->req->size) |
| 3426 | s->req->lr -= s->req->size; |
| 3427 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3428 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3429 | s->rep->lr -= s->req->size; |
| 3430 | |
| 3431 | s->req->analysers |= s->fe->fe_req_ana; |
| 3432 | s->rep->analysers = 0; |
| 3433 | |
| 3434 | http_silent_debug(__LINE__, s); |
| 3435 | } |
| 3436 | |
| 3437 | |
| 3438 | /* This function updates the request state machine according to the response |
| 3439 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3440 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3441 | * it is only used to find when a request/response couple is complete. Both |
| 3442 | * this function and its equivalent should loop until both return zero. It |
| 3443 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3444 | */ |
| 3445 | int http_sync_req_state(struct session *s) |
| 3446 | { |
| 3447 | struct buffer *buf = s->req; |
| 3448 | struct http_txn *txn = &s->txn; |
| 3449 | unsigned int old_flags = buf->flags; |
| 3450 | unsigned int old_state = txn->req.msg_state; |
| 3451 | |
| 3452 | http_silent_debug(__LINE__, s); |
| 3453 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3454 | return 0; |
| 3455 | |
| 3456 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
| 3457 | /* No need to read anymore, the request was completely parsed */ |
| 3458 | buf->flags |= BF_DONT_READ; |
| 3459 | |
| 3460 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3461 | goto wait_other_side; |
| 3462 | |
| 3463 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3464 | /* The server has not finished to respond, so we |
| 3465 | * don't want to move in order not to upset it. |
| 3466 | */ |
| 3467 | goto wait_other_side; |
| 3468 | } |
| 3469 | |
| 3470 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3471 | /* if any side switches to tunnel mode, the other one does too */ |
| 3472 | buf->flags &= ~BF_DONT_READ; |
| 3473 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3474 | goto wait_other_side; |
| 3475 | } |
| 3476 | |
| 3477 | /* When we get here, it means that both the request and the |
| 3478 | * response have finished receiving. Depending on the connection |
| 3479 | * mode, we'll have to wait for the last bytes to leave in either |
| 3480 | * direction, and sometimes for a close to be effective. |
| 3481 | */ |
| 3482 | |
| 3483 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3484 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3485 | /* Server-close mode : queue a connection close to the server */ |
| 3486 | buffer_shutw_now(buf); |
| 3487 | buf->cons->flags |= SI_FL_NOLINGER; |
| 3488 | } |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3489 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3490 | /* Option forceclose is set, let's enforce it now |
| 3491 | * that we're not expecting any new data to come. |
| 3492 | */ |
| 3493 | buffer_shutr_now(buf); |
| 3494 | buffer_shutw_now(buf); |
| 3495 | buf->cons->flags |= SI_FL_NOLINGER; |
| 3496 | } |
| 3497 | /* other modes include httpclose (no action) and keepalive (not implemented) */ |
| 3498 | } |
| 3499 | |
| 3500 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3501 | /* if we've just closed an output, let's switch */ |
| 3502 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3503 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3504 | goto http_msg_closing; |
| 3505 | } |
| 3506 | else { |
| 3507 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3508 | goto http_msg_closed; |
| 3509 | } |
| 3510 | } |
| 3511 | else { |
| 3512 | /* other modes are used as a tunnel right now */ |
| 3513 | buf->flags &= ~BF_DONT_READ; |
| 3514 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3515 | goto wait_other_side; |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3520 | http_msg_closing: |
| 3521 | /* nothing else to forward, just waiting for the output buffer |
| 3522 | * to be empty and for the shutw_now to take effect. |
| 3523 | */ |
| 3524 | if (buf->flags & BF_OUT_EMPTY) { |
| 3525 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3526 | goto http_msg_closed; |
| 3527 | } |
| 3528 | else if (buf->flags & BF_SHUTW) { |
| 3529 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3530 | goto wait_other_side; |
| 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3535 | http_msg_closed: |
| 3536 | goto wait_other_side; |
| 3537 | } |
| 3538 | |
| 3539 | wait_other_side: |
| 3540 | http_silent_debug(__LINE__, s); |
| 3541 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3542 | } |
| 3543 | |
| 3544 | |
| 3545 | /* This function updates the response state machine according to the request |
| 3546 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3547 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3548 | * it is only used to find when a request/response couple is complete. Both |
| 3549 | * this function and its equivalent should loop until both return zero. It |
| 3550 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3551 | */ |
| 3552 | int http_sync_res_state(struct session *s) |
| 3553 | { |
| 3554 | struct buffer *buf = s->rep; |
| 3555 | struct http_txn *txn = &s->txn; |
| 3556 | unsigned int old_flags = buf->flags; |
| 3557 | unsigned int old_state = txn->rsp.msg_state; |
| 3558 | |
| 3559 | http_silent_debug(__LINE__, s); |
| 3560 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3561 | return 0; |
| 3562 | |
| 3563 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3564 | /* In theory, we don't need to read anymore, but we must |
| 3565 | * still monitor the server connection for a possible close, |
| 3566 | * so we don't set the BF_DONT_READ flag here. |
| 3567 | */ |
| 3568 | /* buf->flags |= BF_DONT_READ; */ |
| 3569 | |
| 3570 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3571 | goto wait_other_side; |
| 3572 | |
| 3573 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3574 | /* The client seems to still be sending data, probably |
| 3575 | * because we got an error response during an upload. |
| 3576 | * We have the choice of either breaking the connection |
| 3577 | * or letting it pass through. Let's do the later. |
| 3578 | */ |
| 3579 | goto wait_other_side; |
| 3580 | } |
| 3581 | |
| 3582 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3583 | /* if any side switches to tunnel mode, the other one does too */ |
| 3584 | buf->flags &= ~BF_DONT_READ; |
| 3585 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3586 | goto wait_other_side; |
| 3587 | } |
| 3588 | |
| 3589 | /* When we get here, it means that both the request and the |
| 3590 | * response have finished receiving. Depending on the connection |
| 3591 | * mode, we'll have to wait for the last bytes to leave in either |
| 3592 | * direction, and sometimes for a close to be effective. |
| 3593 | */ |
| 3594 | |
| 3595 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3596 | /* Server-close mode : shut read and wait for the request |
| 3597 | * side to close its output buffer. The caller will detect |
| 3598 | * when we're in DONE and the other is in CLOSED and will |
| 3599 | * catch that for the final cleanup. |
| 3600 | */ |
| 3601 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3602 | buffer_shutr_now(buf); |
| 3603 | goto wait_other_side; |
| 3604 | } |
| 3605 | else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) && |
| 3606 | ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) { |
| 3607 | /* Option forceclose is set, let's enforce it now |
| 3608 | * that we're not expecting any new data to come. |
| 3609 | * The caller knows the session is complete once |
| 3610 | * both states are CLOSED. |
| 3611 | */ |
| 3612 | buffer_shutr_now(buf); |
| 3613 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3614 | } |
| 3615 | else { |
| 3616 | /* other modes include httpclose (no action) and keepalive |
| 3617 | * (not implemented). These modes are used as a tunnel right |
| 3618 | * now. |
| 3619 | */ |
| 3620 | buf->flags &= ~BF_DONT_READ; |
| 3621 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3622 | goto wait_other_side; |
| 3623 | } |
| 3624 | |
| 3625 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3626 | /* if we've just closed an output, let's switch */ |
| 3627 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3628 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3629 | goto http_msg_closing; |
| 3630 | } |
| 3631 | else { |
| 3632 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3633 | goto http_msg_closed; |
| 3634 | } |
| 3635 | } |
| 3636 | goto wait_other_side; |
| 3637 | } |
| 3638 | |
| 3639 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3640 | http_msg_closing: |
| 3641 | /* nothing else to forward, just waiting for the output buffer |
| 3642 | * to be empty and for the shutw_now to take effect. |
| 3643 | */ |
| 3644 | if (buf->flags & BF_OUT_EMPTY) { |
| 3645 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3646 | goto http_msg_closed; |
| 3647 | } |
| 3648 | else if (buf->flags & BF_SHUTW) { |
| 3649 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 3650 | goto wait_other_side; |
| 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3655 | http_msg_closed: |
| 3656 | /* drop any pending data */ |
| 3657 | buffer_ignore(buf, buf->l - buf->send_max); |
| 3658 | buffer_auto_close(buf); |
| 3659 | goto wait_other_side; |
| 3660 | } |
| 3661 | |
| 3662 | wait_other_side: |
| 3663 | http_silent_debug(__LINE__, s); |
| 3664 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 3665 | } |
| 3666 | |
| 3667 | |
| 3668 | /* Resync the request and response state machines. Return 1 if either state |
| 3669 | * changes. |
| 3670 | */ |
| 3671 | int http_resync_states(struct session *s) |
| 3672 | { |
| 3673 | struct http_txn *txn = &s->txn; |
| 3674 | int old_req_state = txn->req.msg_state; |
| 3675 | int old_res_state = txn->rsp.msg_state; |
| 3676 | |
| 3677 | http_silent_debug(__LINE__, s); |
| 3678 | http_sync_req_state(s); |
| 3679 | while (1) { |
| 3680 | http_silent_debug(__LINE__, s); |
| 3681 | if (!http_sync_res_state(s)) |
| 3682 | break; |
| 3683 | http_silent_debug(__LINE__, s); |
| 3684 | if (!http_sync_req_state(s)) |
| 3685 | break; |
| 3686 | } |
| 3687 | http_silent_debug(__LINE__, s); |
| 3688 | /* OK, both state machines agree on a compatible state. |
| 3689 | * There are a few cases we're interested in : |
| 3690 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 3691 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 3692 | * directions, so let's simply disable both analysers. |
| 3693 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 3694 | * request. |
| 3695 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 3696 | * with server-close mode means we've completed one request and we |
| 3697 | * must re-initialize the server connection. |
| 3698 | */ |
| 3699 | |
| 3700 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 3701 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 3702 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3703 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 3704 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3705 | s->req->flags &= ~BF_DONT_READ; |
| 3706 | buffer_auto_close(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3707 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3708 | buffer_auto_close(s->rep); |
| 3709 | s->rep->flags &= ~BF_DONT_READ; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3710 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3711 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 3712 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
| 3713 | (s->rep->flags & BF_SHUTW)) { |
| 3714 | s->rep->flags &= ~BF_DONT_READ; |
| 3715 | s->req->flags &= ~BF_DONT_READ; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3716 | buffer_abort(s->req); |
| 3717 | buffer_auto_close(s->req); |
| 3718 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
| 3719 | s->req->analysers = 0; |
| 3720 | s->rep->analysers = 0; |
| 3721 | } |
| 3722 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3723 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 3724 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 3725 | /* server-close: terminate this server connection and |
| 3726 | * reinitialize a fresh-new transaction. |
| 3727 | */ |
| 3728 | http_end_txn_clean_session(s); |
| 3729 | } |
| 3730 | |
| 3731 | http_silent_debug(__LINE__, s); |
| 3732 | return txn->req.msg_state != old_req_state || |
| 3733 | txn->rsp.msg_state != old_res_state; |
| 3734 | } |
| 3735 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3736 | /* This function is an analyser which forwards request body (including chunk |
| 3737 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3738 | * zero byte. The only situation where it must not be called is when we're in |
| 3739 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3740 | * remaining data and to resync after end of body. It expects the msg_state to |
| 3741 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 3742 | * read more data, or 1 once we can go on with next request or end the session. |
| 3743 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 3744 | * bytes of pending data + the headers if not already done (between som and sov). |
| 3745 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 3746 | */ |
| 3747 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 3748 | { |
| 3749 | struct http_txn *txn = &s->txn; |
| 3750 | struct http_msg *msg = &s->txn.req; |
| 3751 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3752 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3753 | return 0; |
| 3754 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 3755 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 3756 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
| 3757 | /* Output closed while we were sending data. We must abort. */ |
| 3758 | buffer_ignore(req, req->l - req->send_max); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3759 | req->analysers &= ~an_bit; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3760 | req->flags &= ~BF_DONT_READ; |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3761 | return 1; |
| 3762 | } |
| 3763 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3764 | buffer_dont_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3765 | |
| 3766 | /* Note that we don't have to send 100-continue back because we don't |
| 3767 | * need the data to complete our job, and it's up to the server to |
| 3768 | * decide whether to return 100, 417 or anything else in return of |
| 3769 | * an "Expect: 100-continue" header. |
| 3770 | */ |
| 3771 | |
| 3772 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3773 | /* we have msg->col and msg->sov which both point to the first |
| 3774 | * byte of message body. msg->som still points to the beginning |
| 3775 | * of the message. We must save the body in req->lr because it |
| 3776 | * survives buffer re-alignments. |
| 3777 | */ |
| 3778 | req->lr = req->data + msg->sov; |
| 3779 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3780 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3781 | else { |
| 3782 | msg->msg_state = HTTP_MSG_DATA; |
| 3783 | } |
| 3784 | } |
| 3785 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3786 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3787 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 3788 | /* we may have some data pending */ |
| 3789 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 3790 | int bytes = msg->sov - msg->som; |
| 3791 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 3792 | bytes += req->size; |
| 3793 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 3794 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 3795 | msg->som = msg->sov; |
| 3796 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3797 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3798 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 3799 | /* must still forward */ |
| 3800 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3801 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3802 | |
| 3803 | /* nothing left to forward */ |
| 3804 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3805 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3806 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3807 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3808 | } |
| 3809 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3810 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 3811 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3812 | * TRAILERS state. |
| 3813 | */ |
| 3814 | int ret = http_parse_chunk_size(req, msg); |
| 3815 | |
| 3816 | if (!ret) |
| 3817 | goto missing_data; |
| 3818 | else if (ret < 0) |
| 3819 | goto return_bad_req; |
| 3820 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3821 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3822 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 3823 | /* we want the CRLF after the data */ |
| 3824 | int ret; |
| 3825 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3826 | req->lr = req->w + req->send_max; |
| 3827 | if (req->lr >= req->data + req->size) |
| 3828 | req->lr -= req->size; |
| 3829 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3830 | ret = http_skip_chunk_crlf(req, msg); |
| 3831 | |
| 3832 | if (ret == 0) |
| 3833 | goto missing_data; |
| 3834 | else if (ret < 0) |
| 3835 | goto return_bad_req; |
| 3836 | /* we're in MSG_CHUNK_SIZE now */ |
| 3837 | } |
| 3838 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 3839 | int ret = http_forward_trailers(req, msg); |
| 3840 | |
| 3841 | if (ret == 0) |
| 3842 | goto missing_data; |
| 3843 | else if (ret < 0) |
| 3844 | goto return_bad_req; |
| 3845 | /* we're in HTTP_MSG_DONE now */ |
| 3846 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3847 | else { |
| 3848 | /* other states, DONE...TUNNEL */ |
| 3849 | if (http_resync_states(s)) { |
| 3850 | /* some state changes occurred, maybe the analyser |
| 3851 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3852 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3853 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 3854 | goto return_bad_req; |
| 3855 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3856 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3857 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3858 | } |
| 3859 | } |
| 3860 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3861 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3862 | /* stop waiting for data if the input is closed before the end */ |
| 3863 | if (req->flags & BF_SHUTR) |
| 3864 | goto return_bad_req; |
| 3865 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3866 | /* waiting for the last bits to leave the buffer */ |
| 3867 | if (req->flags & BF_SHUTW) |
| 3868 | goto return_bad_req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3869 | |
| 3870 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3871 | return 0; |
| 3872 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3873 | return_bad_req: /* let's centralize all bad requests */ |
| 3874 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3875 | txn->status = 400; |
| 3876 | /* Note: we don't send any error if some data were already sent */ |
| 3877 | stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL); |
| 3878 | |
| 3879 | req->analysers = 0; |
| 3880 | s->fe->counters.failed_req++; |
| 3881 | if (s->listener->counters) |
| 3882 | s->listener->counters->failed_req++; |
| 3883 | |
| 3884 | if (!(s->flags & SN_ERR_MASK)) |
| 3885 | s->flags |= SN_ERR_PRXCOND; |
| 3886 | if (!(s->flags & SN_FINST_MASK)) |
| 3887 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3888 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3889 | return 0; |
| 3890 | } |
| 3891 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3892 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 3893 | * processing can continue on next analysers, or zero if it either needs more |
| 3894 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 3895 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 3896 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 3897 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3898 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3899 | 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] | 3900 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3901 | struct http_txn *txn = &s->txn; |
| 3902 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3903 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3904 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3905 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3906 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3907 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3908 | 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] | 3909 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3910 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3911 | rep, |
| 3912 | rep->rex, rep->wex, |
| 3913 | rep->flags, |
| 3914 | rep->l, |
| 3915 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 3916 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3917 | /* |
| 3918 | * Now parse the partial (or complete) lines. |
| 3919 | * We will check the response syntax, and also join multi-line |
| 3920 | * headers. An index of all the lines will be elaborated while |
| 3921 | * parsing. |
| 3922 | * |
| 3923 | * For the parsing, we use a 28 states FSM. |
| 3924 | * |
| 3925 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3926 | * rep->data + msg->som = beginning of response |
| 3927 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 3928 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3929 | * rep->lr = first non-visited byte |
| 3930 | * rep->r = end of data |
| 3931 | */ |
| 3932 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3933 | /* There's a protected area at the end of the buffer for rewriting |
| 3934 | * purposes. We don't want to start to parse the request if the |
| 3935 | * protected area is affected, because we may have to move processed |
| 3936 | * data later, which is much more complicated. |
| 3937 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3938 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 3939 | if (unlikely((rep->flags & BF_FULL) || |
| 3940 | rep->r < rep->lr || |
| 3941 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 3942 | if (rep->send_max) { |
| 3943 | /* some data has still not left the buffer, wake us once that's done */ |
| 3944 | buffer_dont_close(rep); |
| 3945 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 3946 | return 0; |
| 3947 | } |
| 3948 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 3949 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3950 | } |
| 3951 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3952 | if (likely(rep->lr < rep->r)) |
| 3953 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3954 | } |
| 3955 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3956 | /* 1: we might have to print this header in debug mode */ |
| 3957 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3958 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3959 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3960 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3961 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3962 | sol = rep->data + msg->som; |
| 3963 | eol = sol + msg->sl.rq.l; |
| 3964 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3965 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3966 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 3967 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3968 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3969 | while (cur_idx) { |
| 3970 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 3971 | debug_hdr("srvhdr", s, sol, eol); |
| 3972 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 3973 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 3974 | } |
| 3975 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3976 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3977 | /* |
| 3978 | * Now we quickly check if we have found a full valid response. |
| 3979 | * If not so, we check the FD and buffer states before leaving. |
| 3980 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3981 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3982 | * responses are checked first. |
| 3983 | * |
| 3984 | * Depending on whether the client is still there or not, we |
| 3985 | * may send an error response back or not. Note that normally |
| 3986 | * we should only check for HTTP status there, and check I/O |
| 3987 | * errors somewhere else. |
| 3988 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3989 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3990 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3991 | /* Invalid response */ |
| 3992 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 3993 | /* we detected a parsing error. We want to archive this response |
| 3994 | * in the dedicated proxy area for later troubleshooting. |
| 3995 | */ |
| 3996 | hdr_response_bad: |
| 3997 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 3998 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 3999 | |
| 4000 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4001 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4002 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4003 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4004 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4005 | |
| 4006 | rep->analysers = 0; |
| 4007 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4008 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4009 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4010 | |
| 4011 | if (!(s->flags & SN_ERR_MASK)) |
| 4012 | s->flags |= SN_ERR_PRXCOND; |
| 4013 | if (!(s->flags & SN_FINST_MASK)) |
| 4014 | s->flags |= SN_FINST_H; |
| 4015 | |
| 4016 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4017 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4018 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4019 | /* too large response does not fit in buffer. */ |
| 4020 | else if (rep->flags & BF_FULL) { |
| 4021 | goto hdr_response_bad; |
| 4022 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4023 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4024 | /* read error */ |
| 4025 | else if (rep->flags & BF_READ_ERROR) { |
| 4026 | if (msg->err_pos >= 0) |
| 4027 | 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] | 4028 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4029 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4030 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4031 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4032 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4033 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4034 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4035 | rep->analysers = 0; |
| 4036 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4037 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4038 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4039 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4040 | if (!(s->flags & SN_ERR_MASK)) |
| 4041 | s->flags |= SN_ERR_SRVCL; |
| 4042 | if (!(s->flags & SN_FINST_MASK)) |
| 4043 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4044 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4045 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4046 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4047 | /* read timeout : return a 504 to the client. */ |
| 4048 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4049 | if (msg->err_pos >= 0) |
| 4050 | 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] | 4051 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4052 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4053 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4054 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4055 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4056 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4057 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4058 | rep->analysers = 0; |
| 4059 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4060 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4061 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4062 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4063 | if (!(s->flags & SN_ERR_MASK)) |
| 4064 | s->flags |= SN_ERR_SRVTO; |
| 4065 | if (!(s->flags & SN_FINST_MASK)) |
| 4066 | s->flags |= SN_FINST_H; |
| 4067 | return 0; |
| 4068 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4069 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4070 | /* close from server */ |
| 4071 | else if (rep->flags & BF_SHUTR) { |
| 4072 | if (msg->err_pos >= 0) |
| 4073 | 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] | 4074 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4075 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4076 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4077 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4078 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4079 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4080 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4081 | rep->analysers = 0; |
| 4082 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4083 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4084 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4085 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4086 | if (!(s->flags & SN_ERR_MASK)) |
| 4087 | s->flags |= SN_ERR_SRVCL; |
| 4088 | if (!(s->flags & SN_FINST_MASK)) |
| 4089 | s->flags |= SN_FINST_H; |
| 4090 | return 0; |
| 4091 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4092 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4093 | /* write error to client (we don't send any message then) */ |
| 4094 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4095 | if (msg->err_pos >= 0) |
| 4096 | 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] | 4097 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4098 | s->be->counters.failed_resp++; |
| 4099 | rep->analysers = 0; |
| 4100 | |
| 4101 | if (!(s->flags & SN_ERR_MASK)) |
| 4102 | s->flags |= SN_ERR_CLICL; |
| 4103 | if (!(s->flags & SN_FINST_MASK)) |
| 4104 | s->flags |= SN_FINST_H; |
| 4105 | |
| 4106 | /* process_session() will take care of the error */ |
| 4107 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4108 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4109 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4110 | buffer_dont_close(rep); |
| 4111 | return 0; |
| 4112 | } |
| 4113 | |
| 4114 | /* More interesting part now : we know that we have a complete |
| 4115 | * response which at least looks like HTTP. We have an indicator |
| 4116 | * of each header's length, so we can parse them quickly. |
| 4117 | */ |
| 4118 | |
| 4119 | if (unlikely(msg->err_pos >= 0)) |
| 4120 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4121 | |
| 4122 | /* ensure we keep this pointer to the beginning of the message */ |
| 4123 | msg->sol = rep->data + msg->som; |
| 4124 | |
| 4125 | /* |
| 4126 | * 1: get the status code |
| 4127 | */ |
| 4128 | n = rep->data[msg->sl.st.c] - '0'; |
| 4129 | if (n < 1 || n > 5) |
| 4130 | n = 0; |
| 4131 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4132 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4133 | /* check if the response is HTTP/1.1 or above */ |
| 4134 | if ((msg->sl.st.v_l == 8) && |
| 4135 | ((rep->data[msg->som + 5] > '1') || |
| 4136 | ((rep->data[msg->som + 5] == '1') && |
| 4137 | (rep->data[msg->som + 7] >= '1')))) |
| 4138 | txn->flags |= TX_RES_VER_11; |
| 4139 | |
| 4140 | /* "connection" has not been parsed yet */ |
| 4141 | txn->flags &= ~TX_CON_HDR_PARS; |
| 4142 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4143 | /* transfer length unknown*/ |
| 4144 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4145 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4146 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
| 4147 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4148 | if (txn->status >= 100 && txn->status < 500) |
| 4149 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4150 | else |
| 4151 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4152 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4153 | /* |
| 4154 | * 2: check for cacheability. |
| 4155 | */ |
| 4156 | |
| 4157 | switch (txn->status) { |
| 4158 | case 200: |
| 4159 | case 203: |
| 4160 | case 206: |
| 4161 | case 300: |
| 4162 | case 301: |
| 4163 | case 410: |
| 4164 | /* RFC2616 @13.4: |
| 4165 | * "A response received with a status code of |
| 4166 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4167 | * by a cache (...) unless a cache-control |
| 4168 | * directive prohibits caching." |
| 4169 | * |
| 4170 | * RFC2616 @9.5: POST method : |
| 4171 | * "Responses to this method are not cacheable, |
| 4172 | * unless the response includes appropriate |
| 4173 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4174 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4175 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4176 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4177 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4178 | break; |
| 4179 | default: |
| 4180 | break; |
| 4181 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4182 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4183 | /* |
| 4184 | * 3: we may need to capture headers |
| 4185 | */ |
| 4186 | s->logs.logwait &= ~LW_RESP; |
| 4187 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
| 4188 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 4189 | txn->rsp.cap, s->fe->rsp_cap); |
| 4190 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4191 | /* 4: determine the transfer-length. |
| 4192 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4193 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4194 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4195 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4196 | * All responses to the HEAD request method MUST NOT include a |
| 4197 | * message-body, even though the presence of entity-header fields |
| 4198 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4199 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4200 | * message-body. All other responses do include a message-body, |
| 4201 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4202 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4203 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4204 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4205 | * always terminated by the first empty line after the header fields, |
| 4206 | * regardless of the entity-header fields present in the message. |
| 4207 | * |
| 4208 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4209 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4210 | * transfer-length is defined by the use of this transfer-coding. |
| 4211 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4212 | * transfer-coding is not present, the transfer-length is defined by |
| 4213 | * the sender closing the connection. |
| 4214 | * |
| 4215 | * 3. If a Content-Length header field is present, its decimal value in |
| 4216 | * OCTETs represents both the entity-length and the transfer-length. |
| 4217 | * If a message is received with both a Transfer-Encoding header |
| 4218 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4219 | * |
| 4220 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4221 | * the transfer-length is not otherwise specified, then this self- |
| 4222 | * delimiting media type defines the transfer-length. This media |
| 4223 | * type MUST NOT be used unless the sender knows that the recipient |
| 4224 | * can parse it; the presence in a request of a Range header with |
| 4225 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4226 | * client can parse multipart/byteranges responses. |
| 4227 | * |
| 4228 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4229 | */ |
| 4230 | |
| 4231 | /* Skip parsing if no content length is possible. The response flags |
| 4232 | * 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] | 4233 | * 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] | 4234 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4235 | */ |
| 4236 | if (txn->meth == HTTP_METH_HEAD || |
| 4237 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4238 | txn->status == 204 || txn->status == 304) { |
| 4239 | txn->flags |= TX_RES_XFER_LEN; |
| 4240 | goto skip_content_length; |
| 4241 | } |
| 4242 | |
| 4243 | if (txn->meth == HTTP_METH_CONNECT) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4244 | goto skip_content_length; |
| 4245 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4246 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4247 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4248 | while ((txn->flags & TX_RES_VER_11) && |
| 4249 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4250 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4251 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4252 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4253 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4254 | use_close_only = 1; |
| 4255 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4256 | break; |
| 4257 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4261 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4262 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4263 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4264 | signed long long cl; |
| 4265 | |
| 4266 | if (!ctx.vlen) |
| 4267 | goto hdr_response_bad; |
| 4268 | |
| 4269 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4270 | goto hdr_response_bad; /* parse failure */ |
| 4271 | |
| 4272 | if (cl < 0) |
| 4273 | goto hdr_response_bad; |
| 4274 | |
| 4275 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4276 | goto hdr_response_bad; /* already specified, was different */ |
| 4277 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4278 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4279 | msg->hdr_content_len = cl; |
| 4280 | } |
| 4281 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4282 | /* FIXME: we should also implement the multipart/byterange method. |
| 4283 | * For now on, we resort to close mode in this case (unknown length). |
| 4284 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4285 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4286 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4287 | /* end of job, return OK */ |
| 4288 | rep->analysers &= ~an_bit; |
| 4289 | rep->analyse_exp = TICK_ETERNITY; |
| 4290 | return 1; |
| 4291 | } |
| 4292 | |
| 4293 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4294 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4295 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4296 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4297 | */ |
| 4298 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4299 | { |
| 4300 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4301 | struct http_msg *msg = &txn->rsp; |
| 4302 | struct proxy *cur_proxy; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4303 | struct wordlist *wl; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4304 | int conn_ka = 0, conn_cl = 0; |
| 4305 | int must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4306 | int must_del_close = 0, must_keep = 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4307 | |
| 4308 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4309 | now_ms, __FUNCTION__, |
| 4310 | t, |
| 4311 | rep, |
| 4312 | rep->rex, rep->wex, |
| 4313 | rep->flags, |
| 4314 | rep->l, |
| 4315 | rep->analysers); |
| 4316 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4317 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4318 | return 0; |
| 4319 | |
| 4320 | rep->analysers &= ~an_bit; |
| 4321 | rep->analyse_exp = TICK_ETERNITY; |
| 4322 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4323 | /* Now we have to check if we need to modify the Connection header. |
| 4324 | * This is more difficult on the response than it is on the request, |
| 4325 | * because we can have two different HTTP versions and we don't know |
| 4326 | * how the client will interprete a response. For instance, let's say |
| 4327 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4328 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4329 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4330 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4331 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4332 | * indicating how a request MAY be understood by the client. In case |
| 4333 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4334 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4335 | * will fall back to explicit close. Note that we won't take risks with |
| 4336 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
| 4337 | */ |
| 4338 | |
| 4339 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4340 | (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) && |
| 4341 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4342 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4343 | int may_keep = 0, may_close = 0; /* how it may be understood */ |
| 4344 | struct hdr_ctx ctx; |
| 4345 | |
| 4346 | ctx.idx = 0; |
| 4347 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4348 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 4349 | conn_cl = 1; |
| 4350 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 4351 | conn_ka = 1; |
| 4352 | } |
| 4353 | |
| 4354 | if (conn_cl) { |
| 4355 | /* close header present */ |
| 4356 | may_close = 1; |
| 4357 | if (conn_ka) /* we have both close and keep-alive */ |
| 4358 | may_keep = 1; |
| 4359 | } |
| 4360 | else if (conn_ka) { |
| 4361 | /* keep-alive alone */ |
| 4362 | may_keep = 1; |
| 4363 | } |
| 4364 | else { |
| 4365 | /* no close nor keep-alive header */ |
| 4366 | if (txn->flags & TX_RES_VER_11) |
| 4367 | may_keep = 1; |
| 4368 | else |
| 4369 | may_close = 1; |
| 4370 | |
| 4371 | if (txn->flags & TX_REQ_VER_11) |
| 4372 | may_keep = 1; |
| 4373 | else |
| 4374 | may_close = 1; |
| 4375 | } |
| 4376 | |
| 4377 | /* let's update the transaction status to reflect any close. |
| 4378 | * Note that ambiguous cases with keep & close will also be |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4379 | * handled. We also explicitly state that we will close in |
| 4380 | * case of an ambiguous response having no content-length. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4381 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4382 | 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] | 4383 | (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) || |
| 4384 | !(txn->flags & TX_RES_XFER_LEN)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4385 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 4386 | |
| 4387 | /* Now we must adjust the response header : |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4388 | * - set "close" if may_keep and (WANT_CLO | httpclose) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4389 | * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK) |
| 4390 | * - 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] | 4391 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4392 | if (may_keep && |
| 4393 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO || |
| 4394 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4395 | must_close = 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4396 | else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 4397 | may_close && (txn->flags & TX_RES_XFER_LEN)) { |
| 4398 | must_del_close = 1; |
| 4399 | if (!(txn->flags & TX_REQ_VER_11)) |
| 4400 | must_keep = 1; |
| 4401 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4402 | |
| 4403 | txn->flags |= TX_CON_HDR_PARS; |
| 4404 | } |
| 4405 | |
| 4406 | /* We might have to check for "Connection:" if the server |
| 4407 | * returns a connection status that is not compatible with |
| 4408 | * the client's or with the config. |
| 4409 | */ |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4410 | 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] | 4411 | char *cur_ptr, *cur_end, *cur_next; |
| 4412 | int cur_idx, old_idx, delta, val; |
| 4413 | int must_delete; |
| 4414 | struct hdr_idx_elem *cur_hdr; |
| 4415 | |
| 4416 | /* we just have to remove the headers if both sides are 1.0 */ |
| 4417 | 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] | 4418 | |
| 4419 | /* same if we want to re-enable keep-alive on 1.1 */ |
| 4420 | must_delete |= must_del_close; |
| 4421 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4422 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4423 | |
| 4424 | for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) { |
| 4425 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4426 | cur_ptr = cur_next; |
| 4427 | cur_end = cur_ptr + cur_hdr->len; |
| 4428 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4429 | |
| 4430 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 4431 | if (!val) |
| 4432 | continue; |
| 4433 | |
| 4434 | /* 3 possibilities : |
| 4435 | * - we have already set "Connection: close" or we're in |
| 4436 | * HTTP/1.0, so we remove this line. |
| 4437 | * - we have not yet set "Connection: close", but this line |
| 4438 | * indicates close. We leave it untouched and set the flag. |
| 4439 | * - we have not yet set "Connection: close", and this line |
| 4440 | * indicates non-close. We replace it and set the flag. |
| 4441 | */ |
| 4442 | if (must_delete) { |
| 4443 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 4444 | http_msg_move_end(&txn->rsp, delta); |
| 4445 | cur_next += delta; |
| 4446 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4447 | txn->hdr_idx.used--; |
| 4448 | cur_hdr->len = 0; |
| 4449 | must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4450 | must_del_close = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4451 | } else { |
| 4452 | if (cur_end - cur_ptr - val != 5 || |
| 4453 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 4454 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 4455 | "close", 5); |
| 4456 | cur_next += delta; |
| 4457 | cur_hdr->len += delta; |
| 4458 | http_msg_move_end(&txn->rsp, delta); |
| 4459 | } |
| 4460 | must_delete = 1; |
| 4461 | must_close = 0; |
| 4462 | } |
| 4463 | } /* for loop */ |
| 4464 | } /* if must close keep-alive */ |
| 4465 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4466 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4467 | /* |
| 4468 | * 3: we will have to evaluate the filters. |
| 4469 | * As opposed to version 1.2, now they will be evaluated in the |
| 4470 | * filters order and not in the header order. This means that |
| 4471 | * each filter has to be validated among all headers. |
| 4472 | * |
| 4473 | * Filters are tried with ->be first, then with ->fe if it is |
| 4474 | * different from ->be. |
| 4475 | */ |
| 4476 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4477 | cur_proxy = t->be; |
| 4478 | while (1) { |
| 4479 | struct proxy *rule_set = cur_proxy; |
| 4480 | |
| 4481 | /* try headers filters */ |
| 4482 | if (rule_set->rsp_exp != NULL) { |
| 4483 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 4484 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4485 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4486 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4487 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4488 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4489 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4490 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4491 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4492 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4493 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4494 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4495 | if (!(t->flags & SN_ERR_MASK)) |
| 4496 | t->flags |= SN_ERR_PRXCOND; |
| 4497 | if (!(t->flags & SN_FINST_MASK)) |
| 4498 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4499 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4500 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4501 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4502 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4503 | /* has the response been denied ? */ |
| 4504 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4505 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4506 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4507 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4508 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4509 | if (t->listener->counters) |
| 4510 | t->listener->counters->denied_resp++; |
| 4511 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4512 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4513 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4514 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4515 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4516 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4517 | if (txn->status < 200) |
| 4518 | break; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4519 | 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] | 4520 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4521 | } |
| 4522 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4523 | /* check whether we're already working on the frontend */ |
| 4524 | if (cur_proxy == t->fe) |
| 4525 | break; |
| 4526 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4527 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4528 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4529 | /* |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4530 | * We may be facing a 1xx response (100 continue, 101 switching protocols), |
| 4531 | * in which case this is not the right response, and we're waiting for the |
| 4532 | * next one. Let's allow this response to go to the client and wait for the |
| 4533 | * next one. |
| 4534 | */ |
| 4535 | if (txn->status < 200) { |
| 4536 | hdr_idx_init(&txn->hdr_idx); |
| 4537 | buffer_forward(rep, rep->lr - (rep->data + msg->som)); |
| 4538 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4539 | txn->status = 0; |
| 4540 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4541 | return 1; |
| 4542 | } |
| 4543 | |
| 4544 | /* we don't have any 1xx status code now */ |
| 4545 | |
| 4546 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4547 | * 4: check for server cookie. |
| 4548 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4549 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4550 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4551 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4552 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4553 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4554 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4555 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4556 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4557 | 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] | 4558 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4559 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4560 | /* |
| 4561 | * 6: add server cookie in the response if needed |
| 4562 | */ |
| 4563 | 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] | 4564 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4565 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4566 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4567 | /* the server is known, it's not the one the client requested, we have to |
| 4568 | * insert a set-cookie here, except if we want to insert only on POST |
| 4569 | * requests and this one isn't. Note that servers which don't have cookies |
| 4570 | * (eg: some backup servers) will return a full cookie removal request. |
| 4571 | */ |
| 4572 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4573 | t->be->cookie_name, |
| 4574 | 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] | 4575 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4576 | if (t->be->cookie_domain) |
| 4577 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4578 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4579 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4580 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4581 | goto return_bad_resp; |
| 4582 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4583 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4584 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4585 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4586 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4587 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4588 | */ |
| 4589 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4590 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4591 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4592 | |
| 4593 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4594 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4595 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4596 | } |
| 4597 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4598 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4599 | /* |
| 4600 | * 7: check if result will be cacheable with a cookie. |
| 4601 | * We'll block the response if security checks have caught |
| 4602 | * nasty things such as a cacheable cookie. |
| 4603 | */ |
| 4604 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4605 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4606 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4607 | |
| 4608 | /* we're in presence of a cacheable response containing |
| 4609 | * a set-cookie header. We'll block it as requested by |
| 4610 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4611 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4612 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4613 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4614 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4615 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4616 | if (t->listener->counters) |
| 4617 | t->listener->counters->denied_resp++; |
| 4618 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4619 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4620 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4621 | send_log(t->be, LOG_ALERT, |
| 4622 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4623 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4624 | goto return_srv_prx_502; |
| 4625 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4626 | |
| 4627 | /* |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4628 | * 8: add "Connection: close" if needed and not yet set. This is |
| 4629 | * only needed for 1.1 responses since we know there is no other |
| 4630 | * Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4631 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4632 | if (must_close && (txn->flags & TX_RES_VER_11)) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4633 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4634 | "Connection: close", 17) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4635 | goto return_bad_resp; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4636 | must_close = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4637 | } |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4638 | else if (must_keep && !(txn->flags & TX_REQ_VER_11)) { |
| 4639 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 4640 | "Connection: keep-alive", 22) < 0)) |
| 4641 | goto return_bad_resp; |
| 4642 | must_keep = 0; |
| 4643 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4644 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4645 | if (txn->flags & TX_RES_XFER_LEN) |
| 4646 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4647 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4648 | /************************************************************* |
| 4649 | * OK, that's finished for the headers. We have done what we * |
| 4650 | * could. Let's switch to the DATA state. * |
| 4651 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4652 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4653 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4654 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4655 | /* if the user wants to log as soon as possible, without counting |
| 4656 | * bytes from the server, then this is the right moment. We have |
| 4657 | * to temporarily assign bytes_out to log what we currently have. |
| 4658 | */ |
| 4659 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4660 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4661 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4662 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4663 | t->logs.bytes_out = 0; |
| 4664 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4665 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4666 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4667 | * otherwise we would not let the client side wake up. |
| 4668 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4669 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4670 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4671 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4672 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4673 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4674 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4675 | /* This function is an analyser which forwards response body (including chunk |
| 4676 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4677 | * zero byte. The only situation where it must not be called is when we're in |
| 4678 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4679 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4680 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4681 | * read more data, or 1 once we can go on with next request or end the session. |
| 4682 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4683 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4684 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4685 | */ |
| 4686 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4687 | { |
| 4688 | struct http_txn *txn = &s->txn; |
| 4689 | struct http_msg *msg = &s->txn.rsp; |
| 4690 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4691 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4692 | return 0; |
| 4693 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4694 | 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] | 4695 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4696 | !s->req->analysers) { |
| 4697 | /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */ |
| 4698 | buffer_ignore(res, res->l - res->send_max); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4699 | res->analysers &= ~an_bit; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4700 | res->flags &= ~BF_DONT_READ; |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4701 | return 1; |
| 4702 | } |
| 4703 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4704 | buffer_dont_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4705 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4706 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4707 | /* we have msg->col and msg->sov which both point to the first |
| 4708 | * byte of message body. msg->som still points to the beginning |
| 4709 | * of the message. We must save the body in req->lr because it |
| 4710 | * survives buffer re-alignments. |
| 4711 | */ |
| 4712 | res->lr = res->data + msg->sov; |
| 4713 | if (txn->flags & TX_RES_TE_CHNK) |
| 4714 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4715 | else { |
| 4716 | msg->msg_state = HTTP_MSG_DATA; |
| 4717 | } |
| 4718 | } |
| 4719 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4720 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4721 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4722 | /* we may have some data pending */ |
| 4723 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4724 | int bytes = msg->sov - msg->som; |
| 4725 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4726 | bytes += res->size; |
| 4727 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 4728 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4729 | msg->som = msg->sov; |
| 4730 | } |
| 4731 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4732 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4733 | /* must still forward */ |
| 4734 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4735 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4736 | |
| 4737 | /* nothing left to forward */ |
| 4738 | if (txn->flags & TX_RES_TE_CHNK) |
| 4739 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 4740 | else |
| 4741 | msg->msg_state = HTTP_MSG_DONE; |
| 4742 | } |
| 4743 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4744 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4745 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 4746 | */ |
| 4747 | int ret = http_parse_chunk_size(res, msg); |
| 4748 | |
| 4749 | if (!ret) |
| 4750 | goto missing_data; |
| 4751 | else if (ret < 0) |
| 4752 | goto return_bad_res; |
| 4753 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4754 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4755 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4756 | /* we want the CRLF after the data */ |
| 4757 | int ret; |
| 4758 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4759 | res->lr = res->w + res->send_max; |
| 4760 | if (res->lr >= res->data + res->size) |
| 4761 | res->lr -= res->size; |
| 4762 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4763 | ret = http_skip_chunk_crlf(res, msg); |
| 4764 | |
| 4765 | if (!ret) |
| 4766 | goto missing_data; |
| 4767 | else if (ret < 0) |
| 4768 | goto return_bad_res; |
| 4769 | /* we're in MSG_CHUNK_SIZE now */ |
| 4770 | } |
| 4771 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4772 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4773 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4774 | if (ret == 0) |
| 4775 | goto missing_data; |
| 4776 | else if (ret < 0) |
| 4777 | goto return_bad_res; |
| 4778 | /* we're in HTTP_MSG_DONE now */ |
| 4779 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4780 | else { |
| 4781 | /* other states, DONE...TUNNEL */ |
| 4782 | if (http_resync_states(s)) { |
| 4783 | http_silent_debug(__LINE__, s); |
| 4784 | /* some state changes occurred, maybe the analyser |
| 4785 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4786 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4787 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4788 | goto return_bad_res; |
| 4789 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4790 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4791 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4792 | } |
| 4793 | } |
| 4794 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4795 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4796 | /* stop waiting for data if the input is closed before the end */ |
| 4797 | if (res->flags & BF_SHUTR) |
| 4798 | goto return_bad_res; |
| 4799 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4800 | if (!s->req->analysers) |
| 4801 | goto return_bad_res; |
| 4802 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4803 | /* forward the chunk size as well as any pending data */ |
| 4804 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4805 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 4806 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4807 | msg->som = msg->sov; |
| 4808 | } |
| 4809 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4810 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4811 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4812 | return 0; |
| 4813 | |
| 4814 | return_bad_res: /* let's centralize all bad resuests */ |
| 4815 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 4816 | txn->status = 502; |
| 4817 | stream_int_cond_close(res->cons, NULL); |
| 4818 | |
| 4819 | res->analysers = 0; |
| 4820 | s->be->counters.failed_resp++; |
| 4821 | if (s->srv) { |
| 4822 | s->srv->counters.failed_resp++; |
| 4823 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4824 | } |
| 4825 | |
| 4826 | if (!(s->flags & SN_ERR_MASK)) |
| 4827 | s->flags |= SN_ERR_PRXCOND; |
| 4828 | if (!(s->flags & SN_FINST_MASK)) |
| 4829 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4830 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4831 | return 0; |
| 4832 | } |
| 4833 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4834 | /* Iterate the same filter through all request headers. |
| 4835 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4836 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4837 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4838 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4839 | 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] | 4840 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4841 | char term; |
| 4842 | char *cur_ptr, *cur_end, *cur_next; |
| 4843 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4844 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4845 | struct hdr_idx_elem *cur_hdr; |
| 4846 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4847 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4848 | last_hdr = 0; |
| 4849 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4850 | 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] | 4851 | old_idx = 0; |
| 4852 | |
| 4853 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4854 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4855 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4856 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4857 | (exp->action == ACT_ALLOW || |
| 4858 | exp->action == ACT_DENY || |
| 4859 | exp->action == ACT_TARPIT)) |
| 4860 | return 0; |
| 4861 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4862 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4863 | if (!cur_idx) |
| 4864 | break; |
| 4865 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4866 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4867 | cur_ptr = cur_next; |
| 4868 | cur_end = cur_ptr + cur_hdr->len; |
| 4869 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4870 | |
| 4871 | /* Now we have one header between cur_ptr and cur_end, |
| 4872 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4873 | */ |
| 4874 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4875 | /* The annoying part is that pattern matching needs |
| 4876 | * that we modify the contents to null-terminate all |
| 4877 | * strings before testing them. |
| 4878 | */ |
| 4879 | |
| 4880 | term = *cur_end; |
| 4881 | *cur_end = '\0'; |
| 4882 | |
| 4883 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4884 | switch (exp->action) { |
| 4885 | case ACT_SETBE: |
| 4886 | /* It is not possible to jump a second time. |
| 4887 | * FIXME: should we return an HTTP/500 here so that |
| 4888 | * the admin knows there's a problem ? |
| 4889 | */ |
| 4890 | if (t->be != t->fe) |
| 4891 | break; |
| 4892 | |
| 4893 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4894 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4895 | last_hdr = 1; |
| 4896 | break; |
| 4897 | |
| 4898 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4899 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4900 | last_hdr = 1; |
| 4901 | break; |
| 4902 | |
| 4903 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4904 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4905 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4906 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4907 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4908 | if (t->listener->counters) |
| 4909 | t->listener->counters->denied_resp++; |
| 4910 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4911 | break; |
| 4912 | |
| 4913 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4914 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4915 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4916 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4917 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4918 | if (t->listener->counters) |
| 4919 | t->listener->counters->denied_resp++; |
| 4920 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4921 | break; |
| 4922 | |
| 4923 | case ACT_REPLACE: |
| 4924 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4925 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4926 | /* FIXME: if the user adds a newline in the replacement, the |
| 4927 | * index will not be recalculated for now, and the new line |
| 4928 | * will not be counted as a new header. |
| 4929 | */ |
| 4930 | |
| 4931 | cur_end += delta; |
| 4932 | cur_next += delta; |
| 4933 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4934 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4935 | break; |
| 4936 | |
| 4937 | case ACT_REMOVE: |
| 4938 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4939 | cur_next += delta; |
| 4940 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4941 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4942 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4943 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4944 | cur_hdr->len = 0; |
| 4945 | cur_end = NULL; /* null-term has been rewritten */ |
| 4946 | break; |
| 4947 | |
| 4948 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4949 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4950 | if (cur_end) |
| 4951 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4952 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4953 | /* keep the link from this header to next one in case of later |
| 4954 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4955 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4956 | old_idx = cur_idx; |
| 4957 | } |
| 4958 | return 0; |
| 4959 | } |
| 4960 | |
| 4961 | |
| 4962 | /* Apply the filter to the request line. |
| 4963 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4964 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4965 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4966 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4967 | */ |
| 4968 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4969 | { |
| 4970 | char term; |
| 4971 | char *cur_ptr, *cur_end; |
| 4972 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4973 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4974 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4975 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4976 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4977 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4978 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4979 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4980 | (exp->action == ACT_ALLOW || |
| 4981 | exp->action == ACT_DENY || |
| 4982 | exp->action == ACT_TARPIT)) |
| 4983 | return 0; |
| 4984 | else if (exp->action == ACT_REMOVE) |
| 4985 | return 0; |
| 4986 | |
| 4987 | done = 0; |
| 4988 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4989 | 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] | 4990 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4991 | |
| 4992 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4993 | |
| 4994 | /* The annoying part is that pattern matching needs |
| 4995 | * that we modify the contents to null-terminate all |
| 4996 | * strings before testing them. |
| 4997 | */ |
| 4998 | |
| 4999 | term = *cur_end; |
| 5000 | *cur_end = '\0'; |
| 5001 | |
| 5002 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5003 | switch (exp->action) { |
| 5004 | case ACT_SETBE: |
| 5005 | /* It is not possible to jump a second time. |
| 5006 | * FIXME: should we return an HTTP/500 here so that |
| 5007 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5008 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5009 | if (t->be != t->fe) |
| 5010 | break; |
| 5011 | |
| 5012 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5013 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5014 | done = 1; |
| 5015 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5016 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5017 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5018 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5019 | done = 1; |
| 5020 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5021 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5022 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5023 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5024 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5025 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5026 | if (t->listener->counters) |
| 5027 | t->listener->counters->denied_resp++; |
| 5028 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5029 | done = 1; |
| 5030 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5031 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5032 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5033 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5034 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5035 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5036 | if (t->listener->counters) |
| 5037 | t->listener->counters->denied_resp++; |
| 5038 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5039 | done = 1; |
| 5040 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5041 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5042 | case ACT_REPLACE: |
| 5043 | *cur_end = term; /* restore the string terminator */ |
| 5044 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5045 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5046 | /* FIXME: if the user adds a newline in the replacement, the |
| 5047 | * index will not be recalculated for now, and the new line |
| 5048 | * will not be counted as a new header. |
| 5049 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5050 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5051 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5052 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5053 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5054 | 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] | 5055 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5056 | HTTP_MSG_RQMETH, |
| 5057 | cur_ptr, cur_end + 1, |
| 5058 | NULL, NULL); |
| 5059 | if (unlikely(!cur_end)) |
| 5060 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5061 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5062 | /* we have a full request and we know that we have either a CR |
| 5063 | * or an LF at <ptr>. |
| 5064 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5065 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5066 | 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] | 5067 | /* there is no point trying this regex on headers */ |
| 5068 | return 1; |
| 5069 | } |
| 5070 | } |
| 5071 | *cur_end = term; /* restore the string terminator */ |
| 5072 | return done; |
| 5073 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5074 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5075 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5076 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5077 | /* |
| 5078 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 5079 | * 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] | 5080 | * unparsable request. Since it can manage the switch to another backend, it |
| 5081 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5082 | */ |
| 5083 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5084 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5085 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5086 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5087 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5088 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5089 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5090 | /* |
| 5091 | * The interleaving of transformations and verdicts |
| 5092 | * makes it difficult to decide to continue or stop |
| 5093 | * the evaluation. |
| 5094 | */ |
| 5095 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5096 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5097 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5098 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 5099 | exp = exp->next; |
| 5100 | continue; |
| 5101 | } |
| 5102 | |
| 5103 | /* Apply the filter to the request line. */ |
| 5104 | ret = apply_filter_to_req_line(t, req, exp); |
| 5105 | if (unlikely(ret < 0)) |
| 5106 | return -1; |
| 5107 | |
| 5108 | if (likely(ret == 0)) { |
| 5109 | /* The filter did not match the request, it can be |
| 5110 | * iterated through all headers. |
| 5111 | */ |
| 5112 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5113 | } |
| 5114 | exp = exp->next; |
| 5115 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5116 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5120 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5121 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5122 | * Try to retrieve the server associated to the appsession. |
| 5123 | * If the server is found, it's assigned to the session. |
| 5124 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5125 | 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] | 5126 | struct http_txn *txn = &t->txn; |
| 5127 | appsess *asession = NULL; |
| 5128 | char *sessid_temp = NULL; |
| 5129 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5130 | if (len > t->be->appsession_len) { |
| 5131 | len = t->be->appsession_len; |
| 5132 | } |
| 5133 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5134 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5135 | /* request-learn option is enabled : store the sessid in the session for future use */ |
| 5136 | if (t->sessid != NULL) { |
| 5137 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
| 5138 | pool_free2(apools.sessid, t->sessid); |
| 5139 | } |
| 5140 | |
| 5141 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5142 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5143 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5144 | return; |
| 5145 | } |
| 5146 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5147 | memcpy(t->sessid, buf, len); |
| 5148 | t->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5152 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5153 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5154 | return; |
| 5155 | } |
| 5156 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5157 | memcpy(sessid_temp, buf, len); |
| 5158 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5159 | |
| 5160 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5161 | /* free previously allocated memory */ |
| 5162 | pool_free2(apools.sessid, sessid_temp); |
| 5163 | |
| 5164 | if (asession != NULL) { |
| 5165 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5166 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5167 | asession->request_count++; |
| 5168 | |
| 5169 | if (asession->serverid != NULL) { |
| 5170 | struct server *srv = t->be->srv; |
| 5171 | while (srv) { |
| 5172 | if (strcmp(srv->id, asession->serverid) == 0) { |
| 5173 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
| 5174 | /* we found the server and it's usable */ |
| 5175 | txn->flags &= ~TX_CK_MASK; |
| 5176 | txn->flags |= TX_CK_VALID; |
| 5177 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5178 | t->srv = srv; |
| 5179 | break; |
| 5180 | } else { |
| 5181 | txn->flags &= ~TX_CK_MASK; |
| 5182 | txn->flags |= TX_CK_DOWN; |
| 5183 | } |
| 5184 | } |
| 5185 | srv = srv->next; |
| 5186 | } |
| 5187 | } |
| 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5192 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5193 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5194 | */ |
| 5195 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5196 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5197 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5198 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5199 | char *del_colon, *del_cookie, *colon; |
| 5200 | int app_cookies; |
| 5201 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5202 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5203 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5204 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5205 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5206 | * we start with the start line. |
| 5207 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5208 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5209 | 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] | 5210 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5211 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5212 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5213 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5214 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5215 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5216 | cur_ptr = cur_next; |
| 5217 | cur_end = cur_ptr + cur_hdr->len; |
| 5218 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5219 | |
| 5220 | /* We have one full header between cur_ptr and cur_end, and the |
| 5221 | * next header starts at cur_next. We're only interested in |
| 5222 | * "Cookie:" headers. |
| 5223 | */ |
| 5224 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5225 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5226 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5227 | old_idx = cur_idx; |
| 5228 | continue; |
| 5229 | } |
| 5230 | |
| 5231 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5232 | * attributes whose name begin with a '$', and associate them with |
| 5233 | * the right cookie, if we want to delete this cookie. |
| 5234 | * So there are 3 cases for each cookie read : |
| 5235 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5236 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5237 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5238 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5239 | * "special" cookie. |
| 5240 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5241 | * remove it. If no application cookie persists in the header, we |
| 5242 | * *MUST* delete it |
| 5243 | */ |
| 5244 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5245 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5246 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5247 | /* del_cookie == NULL => nothing to be deleted */ |
| 5248 | del_colon = del_cookie = NULL; |
| 5249 | app_cookies = 0; |
| 5250 | |
| 5251 | while (p1 < cur_end) { |
| 5252 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5253 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5254 | while (p1 < cur_end) { |
| 5255 | if (*p1 == ';' || *p1 == ',') |
| 5256 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5257 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5258 | break; |
| 5259 | p1++; |
| 5260 | } |
| 5261 | |
| 5262 | if (p1 == cur_end) |
| 5263 | break; |
| 5264 | |
| 5265 | /* p1 is at the beginning of the cookie name */ |
| 5266 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5267 | while (p2 < cur_end && *p2 != '=') { |
| 5268 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5269 | /* oops, the cookie name was truncated, resync */ |
| 5270 | p1 = p2; |
| 5271 | goto resync_name; |
| 5272 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5273 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5274 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5275 | |
| 5276 | if (p2 == cur_end) |
| 5277 | break; |
| 5278 | |
| 5279 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5280 | if (p3 == cur_end) |
| 5281 | break; |
| 5282 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5283 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5284 | p5 = p4 = p3; |
| 5285 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5286 | if (!isspace((unsigned char)*p5)) |
| 5287 | p4 = p5 + 1; |
| 5288 | p5++; |
| 5289 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5290 | |
| 5291 | /* here, we have the cookie name between p1 and p2, |
| 5292 | * and its value between p3 and p4. |
| 5293 | * we can process it : |
| 5294 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5295 | * Cookie: NAME=VALUE ; |
| 5296 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5297 | * | || || +--> p4 |
| 5298 | * | || |+-------> p3 |
| 5299 | * | || +--------> p2 |
| 5300 | * | |+------------> p1 |
| 5301 | * | +-------------> colon |
| 5302 | * +--------------------> cur_ptr |
| 5303 | */ |
| 5304 | |
| 5305 | if (*p1 == '$') { |
| 5306 | /* skip this one */ |
| 5307 | } |
| 5308 | else { |
| 5309 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5310 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5311 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5312 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5313 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5314 | int log_len = p4 - p1; |
| 5315 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5316 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5317 | Alert("HTTP logging : out of memory.\n"); |
| 5318 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5319 | if (log_len > t->fe->capture_len) |
| 5320 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5321 | memcpy(txn->cli_cookie, p1, log_len); |
| 5322 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5323 | } |
| 5324 | } |
| 5325 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5326 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5327 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5328 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5329 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5330 | char *delim; |
| 5331 | |
| 5332 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5333 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5334 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5335 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5336 | * Cookie: NAME=SRV~VALUE ; |
| 5337 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5338 | * | || || | +--> p4 |
| 5339 | * | || || +--------> delim |
| 5340 | * | || |+-----------> p3 |
| 5341 | * | || +------------> p2 |
| 5342 | * | |+----------------> p1 |
| 5343 | * | +-----------------> colon |
| 5344 | * +------------------------> cur_ptr |
| 5345 | */ |
| 5346 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5347 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5348 | for (delim = p3; delim < p4; delim++) |
| 5349 | if (*delim == COOKIE_DELIM) |
| 5350 | break; |
| 5351 | } |
| 5352 | else |
| 5353 | delim = p4; |
| 5354 | |
| 5355 | |
| 5356 | /* Here, we'll look for the first running server which supports the cookie. |
| 5357 | * This allows to share a same cookie between several servers, for example |
| 5358 | * to dedicate backup servers to specific servers only. |
| 5359 | * However, to prevent clients from sticking to cookie-less backup server |
| 5360 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5361 | * empty cookies and mark them as invalid. |
| 5362 | */ |
| 5363 | if (delim == p3) |
| 5364 | srv = NULL; |
| 5365 | |
| 5366 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5367 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5368 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5369 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5370 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5371 | txn->flags &= ~TX_CK_MASK; |
| 5372 | txn->flags |= TX_CK_VALID; |
| 5373 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5374 | t->srv = srv; |
| 5375 | break; |
| 5376 | } else { |
| 5377 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5378 | txn->flags &= ~TX_CK_MASK; |
| 5379 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | srv = srv->next; |
| 5383 | } |
| 5384 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5385 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5386 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5387 | txn->flags &= ~TX_CK_MASK; |
| 5388 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | /* depending on the cookie mode, we may have to either : |
| 5392 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5393 | * the server never sees it ; |
| 5394 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5395 | * application cookie so that it does not get accidentely removed later, |
| 5396 | * if we're in cookie prefix mode |
| 5397 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5398 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5399 | int delta; /* negative */ |
| 5400 | |
| 5401 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5402 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5403 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5404 | cur_end += delta; |
| 5405 | cur_next += delta; |
| 5406 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5407 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5408 | |
| 5409 | del_cookie = del_colon = NULL; |
| 5410 | app_cookies++; /* protect the header from deletion */ |
| 5411 | } |
| 5412 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5413 | (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] | 5414 | del_cookie = p1; |
| 5415 | del_colon = colon; |
| 5416 | } |
| 5417 | } else { |
| 5418 | /* now we know that we must keep this cookie since it's |
| 5419 | * not ours. But if we wanted to delete our cookie |
| 5420 | * earlier, we cannot remove the complete header, but we |
| 5421 | * can remove the previous block itself. |
| 5422 | */ |
| 5423 | app_cookies++; |
| 5424 | |
| 5425 | if (del_cookie != NULL) { |
| 5426 | int delta; /* negative */ |
| 5427 | |
| 5428 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5429 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5430 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5431 | cur_end += delta; |
| 5432 | cur_next += delta; |
| 5433 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5434 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5435 | del_cookie = del_colon = NULL; |
| 5436 | } |
| 5437 | } |
| 5438 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5439 | if (t->be->appsession_name != NULL) { |
| 5440 | int cmp_len, value_len; |
| 5441 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5442 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5443 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5444 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5445 | value_begin = p1 + t->be->appsession_name_len; |
| 5446 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5447 | } else { |
| 5448 | cmp_len = p2 - p1; |
| 5449 | value_begin = p3; |
| 5450 | value_len = p4 - p3; |
| 5451 | } |
| 5452 | |
| 5453 | /* let's see if the cookie is our appcookie */ |
| 5454 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5455 | /* Cool... it's the right one */ |
| 5456 | manage_client_side_appsession(t, value_begin, value_len); |
| 5457 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5458 | #if defined(DEBUG_HASH) |
| 5459 | Alert("manage_client_side_cookies\n"); |
| 5460 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5461 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5462 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5463 | } |
| 5464 | |
| 5465 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5466 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5467 | } /* while (p1 < cur_end) */ |
| 5468 | |
| 5469 | /* There's no more cookie on this line. |
| 5470 | * We may have marked the last one(s) for deletion. |
| 5471 | * We must do this now in two ways : |
| 5472 | * - if there is no app cookie, we simply delete the header ; |
| 5473 | * - if there are app cookies, we must delete the end of the |
| 5474 | * string properly, including the colon/semi-colon before |
| 5475 | * the cookie name. |
| 5476 | */ |
| 5477 | if (del_cookie != NULL) { |
| 5478 | int delta; |
| 5479 | if (app_cookies) { |
| 5480 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5481 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5482 | cur_hdr->len += delta; |
| 5483 | } else { |
| 5484 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5485 | |
| 5486 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5487 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5488 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5489 | cur_hdr->len = 0; |
| 5490 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5491 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5492 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /* keep the link from this header to next one */ |
| 5496 | old_idx = cur_idx; |
| 5497 | } /* end of cookie processing on this header */ |
| 5498 | } |
| 5499 | |
| 5500 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5501 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5502 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5503 | */ |
| 5504 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5505 | { |
| 5506 | char term; |
| 5507 | char *cur_ptr, *cur_end, *cur_next; |
| 5508 | int cur_idx, old_idx, last_hdr; |
| 5509 | struct http_txn *txn = &t->txn; |
| 5510 | struct hdr_idx_elem *cur_hdr; |
| 5511 | int len, delta; |
| 5512 | |
| 5513 | last_hdr = 0; |
| 5514 | |
| 5515 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5516 | old_idx = 0; |
| 5517 | |
| 5518 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5519 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5520 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5521 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5522 | (exp->action == ACT_ALLOW || |
| 5523 | exp->action == ACT_DENY)) |
| 5524 | return 0; |
| 5525 | |
| 5526 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5527 | if (!cur_idx) |
| 5528 | break; |
| 5529 | |
| 5530 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5531 | cur_ptr = cur_next; |
| 5532 | cur_end = cur_ptr + cur_hdr->len; |
| 5533 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5534 | |
| 5535 | /* Now we have one header between cur_ptr and cur_end, |
| 5536 | * and the next header starts at cur_next. |
| 5537 | */ |
| 5538 | |
| 5539 | /* The annoying part is that pattern matching needs |
| 5540 | * that we modify the contents to null-terminate all |
| 5541 | * strings before testing them. |
| 5542 | */ |
| 5543 | |
| 5544 | term = *cur_end; |
| 5545 | *cur_end = '\0'; |
| 5546 | |
| 5547 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5548 | switch (exp->action) { |
| 5549 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5550 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5551 | last_hdr = 1; |
| 5552 | break; |
| 5553 | |
| 5554 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5555 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5556 | last_hdr = 1; |
| 5557 | break; |
| 5558 | |
| 5559 | case ACT_REPLACE: |
| 5560 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5561 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5562 | /* FIXME: if the user adds a newline in the replacement, the |
| 5563 | * index will not be recalculated for now, and the new line |
| 5564 | * will not be counted as a new header. |
| 5565 | */ |
| 5566 | |
| 5567 | cur_end += delta; |
| 5568 | cur_next += delta; |
| 5569 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5570 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5571 | break; |
| 5572 | |
| 5573 | case ACT_REMOVE: |
| 5574 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5575 | cur_next += delta; |
| 5576 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5577 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5578 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5579 | txn->hdr_idx.used--; |
| 5580 | cur_hdr->len = 0; |
| 5581 | cur_end = NULL; /* null-term has been rewritten */ |
| 5582 | break; |
| 5583 | |
| 5584 | } |
| 5585 | } |
| 5586 | if (cur_end) |
| 5587 | *cur_end = term; /* restore the string terminator */ |
| 5588 | |
| 5589 | /* keep the link from this header to next one in case of later |
| 5590 | * removal of next header. |
| 5591 | */ |
| 5592 | old_idx = cur_idx; |
| 5593 | } |
| 5594 | return 0; |
| 5595 | } |
| 5596 | |
| 5597 | |
| 5598 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5599 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5600 | * or -1 if a replacement resulted in an invalid status line. |
| 5601 | */ |
| 5602 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5603 | { |
| 5604 | char term; |
| 5605 | char *cur_ptr, *cur_end; |
| 5606 | int done; |
| 5607 | struct http_txn *txn = &t->txn; |
| 5608 | int len, delta; |
| 5609 | |
| 5610 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5611 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5612 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5613 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5614 | (exp->action == ACT_ALLOW || |
| 5615 | exp->action == ACT_DENY)) |
| 5616 | return 0; |
| 5617 | else if (exp->action == ACT_REMOVE) |
| 5618 | return 0; |
| 5619 | |
| 5620 | done = 0; |
| 5621 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5622 | 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] | 5623 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5624 | |
| 5625 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5626 | |
| 5627 | /* The annoying part is that pattern matching needs |
| 5628 | * that we modify the contents to null-terminate all |
| 5629 | * strings before testing them. |
| 5630 | */ |
| 5631 | |
| 5632 | term = *cur_end; |
| 5633 | *cur_end = '\0'; |
| 5634 | |
| 5635 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5636 | switch (exp->action) { |
| 5637 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5638 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5639 | done = 1; |
| 5640 | break; |
| 5641 | |
| 5642 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5643 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5644 | done = 1; |
| 5645 | break; |
| 5646 | |
| 5647 | case ACT_REPLACE: |
| 5648 | *cur_end = term; /* restore the string terminator */ |
| 5649 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5650 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5651 | /* FIXME: if the user adds a newline in the replacement, the |
| 5652 | * index will not be recalculated for now, and the new line |
| 5653 | * will not be counted as a new header. |
| 5654 | */ |
| 5655 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5656 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5657 | cur_end += delta; |
| 5658 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 5659 | 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] | 5660 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5661 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5662 | cur_ptr, cur_end + 1, |
| 5663 | NULL, NULL); |
| 5664 | if (unlikely(!cur_end)) |
| 5665 | return -1; |
| 5666 | |
| 5667 | /* we have a full respnse and we know that we have either a CR |
| 5668 | * or an LF at <ptr>. |
| 5669 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5670 | 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] | 5671 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5672 | /* there is no point trying this regex on headers */ |
| 5673 | return 1; |
| 5674 | } |
| 5675 | } |
| 5676 | *cur_end = term; /* restore the string terminator */ |
| 5677 | return done; |
| 5678 | } |
| 5679 | |
| 5680 | |
| 5681 | |
| 5682 | /* |
| 5683 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5684 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5685 | * unparsable response. |
| 5686 | */ |
| 5687 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5688 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5689 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5690 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5691 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5692 | int ret; |
| 5693 | |
| 5694 | /* |
| 5695 | * The interleaving of transformations and verdicts |
| 5696 | * makes it difficult to decide to continue or stop |
| 5697 | * the evaluation. |
| 5698 | */ |
| 5699 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5700 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5701 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5702 | exp->action == ACT_PASS)) { |
| 5703 | exp = exp->next; |
| 5704 | continue; |
| 5705 | } |
| 5706 | |
| 5707 | /* Apply the filter to the status line. */ |
| 5708 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5709 | if (unlikely(ret < 0)) |
| 5710 | return -1; |
| 5711 | |
| 5712 | if (likely(ret == 0)) { |
| 5713 | /* The filter did not match the response, it can be |
| 5714 | * iterated through all headers. |
| 5715 | */ |
| 5716 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5717 | } |
| 5718 | exp = exp->next; |
| 5719 | } |
| 5720 | return 0; |
| 5721 | } |
| 5722 | |
| 5723 | |
| 5724 | |
| 5725 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5726 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5727 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5728 | */ |
| 5729 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5730 | { |
| 5731 | struct http_txn *txn = &t->txn; |
| 5732 | char *p1, *p2, *p3, *p4; |
| 5733 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5734 | char *cur_ptr, *cur_end, *cur_next; |
| 5735 | int cur_idx, old_idx, delta; |
| 5736 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5737 | /* Iterate through the headers. |
| 5738 | * we start with the start line. |
| 5739 | */ |
| 5740 | old_idx = 0; |
| 5741 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5742 | |
| 5743 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5744 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5745 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5746 | |
| 5747 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5748 | cur_ptr = cur_next; |
| 5749 | cur_end = cur_ptr + cur_hdr->len; |
| 5750 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5751 | |
| 5752 | /* We have one full header between cur_ptr and cur_end, and the |
| 5753 | * next header starts at cur_next. We're only interested in |
| 5754 | * "Cookie:" headers. |
| 5755 | */ |
| 5756 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5757 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5758 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5759 | old_idx = cur_idx; |
| 5760 | continue; |
| 5761 | } |
| 5762 | |
| 5763 | /* 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] | 5764 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5765 | |
| 5766 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5767 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 5768 | * the cookie capture is declared in the fronend. |
| 5769 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5770 | if (t->be->cookie_name == NULL && |
| 5771 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5772 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5773 | return; |
| 5774 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5775 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5776 | |
| 5777 | 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] | 5778 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5779 | break; |
| 5780 | |
| 5781 | /* p1 is at the beginning of the cookie name */ |
| 5782 | p2 = p1; |
| 5783 | |
| 5784 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5785 | p2++; |
| 5786 | |
| 5787 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5788 | break; |
| 5789 | |
| 5790 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5791 | if (p3 == cur_end) |
| 5792 | break; |
| 5793 | |
| 5794 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5795 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5796 | p4++; |
| 5797 | |
| 5798 | /* here, we have the cookie name between p1 and p2, |
| 5799 | * and its value between p3 and p4. |
| 5800 | * we can process it. |
| 5801 | */ |
| 5802 | |
| 5803 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5804 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5805 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5806 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5807 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5808 | int log_len = p4 - p1; |
| 5809 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5810 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5811 | Alert("HTTP logging : out of memory.\n"); |
| 5812 | } |
| 5813 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5814 | if (log_len > t->fe->capture_len) |
| 5815 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5816 | memcpy(txn->srv_cookie, p1, log_len); |
| 5817 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5818 | } |
| 5819 | |
| 5820 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5821 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5822 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5823 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5824 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5825 | |
| 5826 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5827 | * this occurrence because we'll insert another one later. |
| 5828 | * We'll delete it too if the "indirect" option is set and we're in |
| 5829 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5830 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5831 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5832 | /* this header must be deleted */ |
| 5833 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5834 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5835 | txn->hdr_idx.used--; |
| 5836 | cur_hdr->len = 0; |
| 5837 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5838 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5839 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5840 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5841 | } |
| 5842 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5843 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5844 | /* replace bytes p3->p4 with the cookie name associated |
| 5845 | * with this server since we know it. |
| 5846 | */ |
| 5847 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5848 | cur_hdr->len += delta; |
| 5849 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5850 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5851 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5852 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5853 | } |
| 5854 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5855 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5856 | /* insert the cookie name associated with this server |
| 5857 | * before existing cookie, and insert a delimitor between them.. |
| 5858 | */ |
| 5859 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5860 | cur_hdr->len += delta; |
| 5861 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5862 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5863 | |
| 5864 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5865 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5866 | } |
| 5867 | } |
| 5868 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5869 | else if (t->be->appsession_name != NULL) { |
| 5870 | int cmp_len, value_len; |
| 5871 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5872 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5873 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5874 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5875 | value_begin = p1 + t->be->appsession_name_len; |
| 5876 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 5877 | } else { |
| 5878 | cmp_len = p2 - p1; |
| 5879 | value_begin = p3; |
| 5880 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5881 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5882 | |
| 5883 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5884 | /* Cool... it's the right one */ |
| 5885 | if (t->sessid != NULL) { |
| 5886 | /* free previously allocated memory as we don't need it anymore */ |
| 5887 | pool_free2(apools.sessid, t->sessid); |
| 5888 | } |
| 5889 | /* Store the sessid in the session for future use */ |
| 5890 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5891 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5892 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5893 | return; |
| 5894 | } |
| 5895 | memcpy(t->sessid, value_begin, value_len); |
| 5896 | t->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5897 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5898 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5899 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5900 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5901 | /* keep the link from this header to next one */ |
| 5902 | old_idx = cur_idx; |
| 5903 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5904 | |
| 5905 | if (t->sessid != NULL) { |
| 5906 | appsess *asession = NULL; |
| 5907 | /* only do insert, if lookup fails */ |
| 5908 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid); |
| 5909 | if (asession == NULL) { |
| 5910 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 5911 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5912 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5913 | return; |
| 5914 | } |
| 5915 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5916 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5917 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5918 | return; |
| 5919 | } |
| 5920 | memcpy(asession->sessid, t->sessid, t->be->appsession_len); |
| 5921 | asession->sessid[t->be->appsession_len] = 0; |
| 5922 | |
| 5923 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5924 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 5925 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5926 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5927 | return; |
| 5928 | } |
| 5929 | asession->serverid[0] = '\0'; |
| 5930 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 5931 | |
| 5932 | asession->request_count = 0; |
| 5933 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 5934 | } |
| 5935 | |
| 5936 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5937 | asession->request_count++; |
| 5938 | } |
| 5939 | |
| 5940 | #if defined(DEBUG_HASH) |
| 5941 | Alert("manage_server_side_cookies\n"); |
| 5942 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5943 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5944 | } |
| 5945 | |
| 5946 | |
| 5947 | |
| 5948 | /* |
| 5949 | * Check if response is cacheable or not. Updates t->flags. |
| 5950 | */ |
| 5951 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5952 | { |
| 5953 | struct http_txn *txn = &t->txn; |
| 5954 | char *p1, *p2; |
| 5955 | |
| 5956 | char *cur_ptr, *cur_end, *cur_next; |
| 5957 | int cur_idx; |
| 5958 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5959 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5960 | return; |
| 5961 | |
| 5962 | /* Iterate through the headers. |
| 5963 | * we start with the start line. |
| 5964 | */ |
| 5965 | cur_idx = 0; |
| 5966 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5967 | |
| 5968 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5969 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5970 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5971 | |
| 5972 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5973 | cur_ptr = cur_next; |
| 5974 | cur_end = cur_ptr + cur_hdr->len; |
| 5975 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5976 | |
| 5977 | /* We have one full header between cur_ptr and cur_end, and the |
| 5978 | * next header starts at cur_next. We're only interested in |
| 5979 | * "Cookie:" headers. |
| 5980 | */ |
| 5981 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5982 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5983 | if (val) { |
| 5984 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5985 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5986 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5987 | return; |
| 5988 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5989 | } |
| 5990 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5991 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5992 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5993 | continue; |
| 5994 | |
| 5995 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5996 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5997 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5998 | |
| 5999 | if (p1 >= cur_end) /* no more info */ |
| 6000 | continue; |
| 6001 | |
| 6002 | /* p1 is at the beginning of the value */ |
| 6003 | p2 = p1; |
| 6004 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6005 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6006 | p2++; |
| 6007 | |
| 6008 | /* we have a complete value between p1 and p2 */ |
| 6009 | if (p2 < cur_end && *p2 == '=') { |
| 6010 | /* we have something of the form no-cache="set-cookie" */ |
| 6011 | if ((cur_end - p1 >= 21) && |
| 6012 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6013 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6014 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6015 | continue; |
| 6016 | } |
| 6017 | |
| 6018 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6019 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6020 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6021 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6022 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6023 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6024 | return; |
| 6025 | } |
| 6026 | |
| 6027 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6028 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6029 | continue; |
| 6030 | } |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6035 | /* |
| 6036 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6037 | * If the server is found, it's assigned to the session. |
| 6038 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6039 | 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] | 6040 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6041 | char *end_params, *first_param, *cur_param, *next_param; |
| 6042 | char separator; |
| 6043 | int value_len; |
| 6044 | |
| 6045 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6046 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6047 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6048 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6049 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6050 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6051 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6052 | first_param = NULL; |
| 6053 | switch (mode) { |
| 6054 | case PR_O2_AS_M_PP: |
| 6055 | first_param = memchr(begin, ';', len); |
| 6056 | break; |
| 6057 | case PR_O2_AS_M_QS: |
| 6058 | first_param = memchr(begin, '?', len); |
| 6059 | break; |
| 6060 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6061 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6062 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6063 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6064 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6065 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6066 | switch (mode) { |
| 6067 | case PR_O2_AS_M_PP: |
| 6068 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6069 | end_params = (char *) begin + len; |
| 6070 | } |
| 6071 | separator = ';'; |
| 6072 | break; |
| 6073 | case PR_O2_AS_M_QS: |
| 6074 | end_params = (char *) begin + len; |
| 6075 | separator = '&'; |
| 6076 | break; |
| 6077 | default: |
| 6078 | /* unknown mode, shouldn't happen */ |
| 6079 | return; |
| 6080 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6081 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6082 | cur_param = next_param = end_params; |
| 6083 | while (cur_param > first_param) { |
| 6084 | cur_param--; |
| 6085 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6086 | /* let's see if this is the appsession parameter */ |
| 6087 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6088 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6089 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6090 | /* Cool... it's the right one */ |
| 6091 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6092 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6093 | if (value_len > 0) { |
| 6094 | manage_client_side_appsession(t, cur_param, value_len); |
| 6095 | } |
| 6096 | break; |
| 6097 | } |
| 6098 | next_param = cur_param; |
| 6099 | } |
| 6100 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6101 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6102 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6103 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6104 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6108 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6109 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 6110 | * 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] | 6111 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6112 | * 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] | 6113 | * 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] | 6114 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6115 | * |
| 6116 | * Returns 1 if the session's state changes, otherwise 0. |
| 6117 | */ |
| 6118 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 6119 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6120 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6121 | struct uri_auth *uri_auth = backend->uri_auth; |
| 6122 | struct user_auth *user; |
| 6123 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6124 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6125 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6126 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6127 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6128 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6129 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6130 | return 0; |
| 6131 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6132 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6133 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6134 | /* the URI is in h */ |
| 6135 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6136 | return 0; |
| 6137 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6138 | h += uri_auth->uri_len; |
| 6139 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 6140 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6141 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6142 | break; |
| 6143 | } |
| 6144 | h++; |
| 6145 | } |
| 6146 | |
| 6147 | if (uri_auth->refresh) { |
| 6148 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6149 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 6150 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6151 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6152 | break; |
| 6153 | } |
| 6154 | h++; |
| 6155 | } |
| 6156 | } |
| 6157 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6158 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6159 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 6160 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6161 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6162 | break; |
| 6163 | } |
| 6164 | h++; |
| 6165 | } |
| 6166 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6167 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6168 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6169 | /* we are in front of a interceptable URI. Let's check |
| 6170 | * if there's an authentication and if it's valid. |
| 6171 | */ |
| 6172 | user = uri_auth->users; |
| 6173 | if (!user) { |
| 6174 | /* no user auth required, it's OK */ |
| 6175 | authenticated = 1; |
| 6176 | } else { |
| 6177 | authenticated = 0; |
| 6178 | |
| 6179 | /* a user list is defined, we have to check. |
| 6180 | * skip 21 chars for "Authorization: Basic ". |
| 6181 | */ |
| 6182 | |
| 6183 | /* FIXME: this should move to an earlier place */ |
| 6184 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6185 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 6186 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6187 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6188 | if (len > 14 && |
| 6189 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 6190 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6191 | break; |
| 6192 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6193 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6194 | } |
| 6195 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6196 | if (txn->auth_hdr.len < 21 || |
| 6197 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6198 | user = NULL; |
| 6199 | |
| 6200 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6201 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 6202 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6203 | user->user_pwd, user->user_len)) { |
| 6204 | authenticated = 1; |
| 6205 | break; |
| 6206 | } |
| 6207 | user = user->next; |
| 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6212 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6213 | |
| 6214 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 6215 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 6216 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6217 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 6218 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 6219 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6220 | if (!(t->flags & SN_ERR_MASK)) |
| 6221 | t->flags |= SN_ERR_PRXCOND; |
| 6222 | if (!(t->flags & SN_FINST_MASK)) |
| 6223 | t->flags |= SN_FINST_R; |
| 6224 | return 1; |
| 6225 | } |
| 6226 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6227 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6228 | * data. |
| 6229 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 6230 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6231 | t->data_source = DATA_SRC_STATS; |
| 6232 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 6233 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 6234 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 6235 | t->rep->prod->private = t; |
| 6236 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6237 | return 1; |
| 6238 | } |
| 6239 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6240 | /* |
| 6241 | * Capture a bad request or response and archive it in the proxy's structure. |
| 6242 | */ |
| 6243 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6244 | struct buffer *buf, struct http_msg *msg, |
| 6245 | struct proxy *other_end) |
| 6246 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6247 | es->len = buf->r - (buf->data + msg->som); |
| 6248 | 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] | 6249 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6250 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6251 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6252 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6253 | es->when = date; // user-visible date |
| 6254 | es->sid = s->uniq_id; |
| 6255 | es->srv = s->srv; |
| 6256 | es->oe = other_end; |
| 6257 | es->src = s->cli_addr; |
| 6258 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6259 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6260 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6261 | * Print a debug line with a header |
| 6262 | */ |
| 6263 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6264 | { |
| 6265 | int len, max; |
| 6266 | 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] | 6267 | 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] | 6268 | max = end - start; |
| 6269 | UBOUND(max, sizeof(trash) - len - 1); |
| 6270 | len += strlcpy2(trash + len, start, max + 1); |
| 6271 | trash[len++] = '\n'; |
| 6272 | write(1, trash, len); |
| 6273 | } |
| 6274 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6275 | /* |
| 6276 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6277 | * the required fields are properly allocated and that we only need to (re)init |
| 6278 | * them. This should be used before processing any new request. |
| 6279 | */ |
| 6280 | void http_init_txn(struct session *s) |
| 6281 | { |
| 6282 | struct http_txn *txn = &s->txn; |
| 6283 | struct proxy *fe = s->fe; |
| 6284 | |
| 6285 | txn->flags = 0; |
| 6286 | txn->status = -1; |
| 6287 | |
| 6288 | txn->req.sol = txn->req.eol = NULL; |
| 6289 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6290 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6291 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6292 | txn->req.hdr_content_len = 0LL; |
| 6293 | txn->rsp.hdr_content_len = 0LL; |
| 6294 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6295 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 6296 | chunk_reset(&txn->auth_hdr); |
| 6297 | |
| 6298 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6299 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6300 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6301 | |
| 6302 | if (txn->req.cap) |
| 6303 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6304 | |
| 6305 | if (txn->rsp.cap) |
| 6306 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6307 | |
| 6308 | if (txn->hdr_idx.v) |
| 6309 | hdr_idx_init(&txn->hdr_idx); |
| 6310 | } |
| 6311 | |
| 6312 | /* to be used at the end of a transaction */ |
| 6313 | void http_end_txn(struct session *s) |
| 6314 | { |
| 6315 | struct http_txn *txn = &s->txn; |
| 6316 | |
| 6317 | /* these ones will have been dynamically allocated */ |
| 6318 | pool_free2(pool2_requri, txn->uri); |
| 6319 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6320 | pool_free2(pool2_capture, txn->srv_cookie); |
| 6321 | txn->uri = NULL; |
| 6322 | txn->srv_cookie = NULL; |
| 6323 | txn->cli_cookie = NULL; |
| 6324 | } |
| 6325 | |
| 6326 | /* to be used at the end of a transaction to prepare a new one */ |
| 6327 | void http_reset_txn(struct session *s) |
| 6328 | { |
| 6329 | http_end_txn(s); |
| 6330 | http_init_txn(s); |
| 6331 | |
| 6332 | s->be = s->fe; |
| 6333 | s->req->analysers = s->listener->analysers; |
| 6334 | s->logs.logwait = s->fe->to_log; |
| 6335 | s->srv = s->prev_srv = s->srv_conn = NULL; |
| 6336 | s->pend_pos = NULL; |
| 6337 | s->conn_retries = s->be->conn_retries; |
| 6338 | |
| 6339 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6340 | |
| 6341 | s->req->rto = s->fe->timeout.client; |
| 6342 | s->req->wto = s->be->timeout.server; |
| 6343 | s->req->cto = s->be->timeout.connect; |
| 6344 | |
| 6345 | s->rep->rto = s->be->timeout.server; |
| 6346 | s->rep->wto = s->fe->timeout.client; |
| 6347 | s->rep->cto = TICK_ETERNITY; |
| 6348 | |
| 6349 | s->req->rex = TICK_ETERNITY; |
| 6350 | s->req->wex = TICK_ETERNITY; |
| 6351 | s->req->analyse_exp = TICK_ETERNITY; |
| 6352 | s->rep->rex = TICK_ETERNITY; |
| 6353 | s->rep->wex = TICK_ETERNITY; |
| 6354 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6355 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6356 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6357 | /************************************************************************/ |
| 6358 | /* The code below is dedicated to ACL parsing and matching */ |
| 6359 | /************************************************************************/ |
| 6360 | |
| 6361 | |
| 6362 | |
| 6363 | |
| 6364 | /* 1. Check on METHOD |
| 6365 | * We use the pre-parsed method if it is known, and store its number as an |
| 6366 | * integer. If it is unknown, we use the pointer and the length. |
| 6367 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6368 | 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] | 6369 | { |
| 6370 | int len, meth; |
| 6371 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6372 | len = strlen(*text); |
| 6373 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6374 | |
| 6375 | pattern->val.i = meth; |
| 6376 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6377 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6378 | if (!pattern->ptr.str) |
| 6379 | return 0; |
| 6380 | pattern->len = len; |
| 6381 | } |
| 6382 | return 1; |
| 6383 | } |
| 6384 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6385 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6386 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6387 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6388 | { |
| 6389 | int meth; |
| 6390 | struct http_txn *txn = l7; |
| 6391 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6392 | if (!txn) |
| 6393 | return 0; |
| 6394 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6395 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6396 | return 0; |
| 6397 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6398 | meth = txn->meth; |
| 6399 | test->i = meth; |
| 6400 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6401 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6402 | /* ensure the indexes are not affected */ |
| 6403 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6404 | test->len = txn->req.sl.rq.m_l; |
| 6405 | test->ptr = txn->req.sol; |
| 6406 | } |
| 6407 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6408 | return 1; |
| 6409 | } |
| 6410 | |
| 6411 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6412 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6413 | int icase; |
| 6414 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6415 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6416 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6417 | |
| 6418 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6419 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6420 | |
| 6421 | /* Other method, we must compare the strings */ |
| 6422 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6423 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6424 | |
| 6425 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6426 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6427 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6428 | return ACL_PAT_FAIL; |
| 6429 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | /* 2. Check on Request/Status Version |
| 6433 | * We simply compare strings here. |
| 6434 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6435 | 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] | 6436 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6437 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6438 | if (!pattern->ptr.str) |
| 6439 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6440 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6441 | return 1; |
| 6442 | } |
| 6443 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6444 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6445 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6446 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6447 | { |
| 6448 | struct http_txn *txn = l7; |
| 6449 | char *ptr; |
| 6450 | int len; |
| 6451 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6452 | if (!txn) |
| 6453 | return 0; |
| 6454 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6455 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6456 | return 0; |
| 6457 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6458 | len = txn->req.sl.rq.v_l; |
| 6459 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 6460 | |
| 6461 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6462 | if (len <= 0) |
| 6463 | return 0; |
| 6464 | |
| 6465 | test->ptr = ptr; |
| 6466 | test->len = len; |
| 6467 | |
| 6468 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6469 | return 1; |
| 6470 | } |
| 6471 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6472 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6473 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6474 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6475 | { |
| 6476 | struct http_txn *txn = l7; |
| 6477 | char *ptr; |
| 6478 | int len; |
| 6479 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6480 | if (!txn) |
| 6481 | return 0; |
| 6482 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6483 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6484 | return 0; |
| 6485 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6486 | len = txn->rsp.sl.st.v_l; |
| 6487 | ptr = txn->rsp.sol; |
| 6488 | |
| 6489 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6490 | if (len <= 0) |
| 6491 | return 0; |
| 6492 | |
| 6493 | test->ptr = ptr; |
| 6494 | test->len = len; |
| 6495 | |
| 6496 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6497 | return 1; |
| 6498 | } |
| 6499 | |
| 6500 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6501 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6502 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6503 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6504 | { |
| 6505 | struct http_txn *txn = l7; |
| 6506 | char *ptr; |
| 6507 | int len; |
| 6508 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6509 | if (!txn) |
| 6510 | return 0; |
| 6511 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6512 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6513 | return 0; |
| 6514 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6515 | len = txn->rsp.sl.st.c_l; |
| 6516 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 6517 | |
| 6518 | test->i = __strl2ui(ptr, len); |
| 6519 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6520 | return 1; |
| 6521 | } |
| 6522 | |
| 6523 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6524 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6525 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6526 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6527 | { |
| 6528 | struct http_txn *txn = l7; |
| 6529 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6530 | if (!txn) |
| 6531 | return 0; |
| 6532 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6533 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6534 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6535 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6536 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6537 | /* ensure the indexes are not affected */ |
| 6538 | return 0; |
| 6539 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6540 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6541 | 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] | 6542 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6543 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6544 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6545 | return 1; |
| 6546 | } |
| 6547 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6548 | static int |
| 6549 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6550 | struct acl_expr *expr, struct acl_test *test) |
| 6551 | { |
| 6552 | struct http_txn *txn = l7; |
| 6553 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6554 | if (!txn) |
| 6555 | return 0; |
| 6556 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6557 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6558 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6559 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6560 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6561 | /* ensure the indexes are not affected */ |
| 6562 | return 0; |
| 6563 | |
| 6564 | /* Parse HTTP request */ |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6565 | 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] | 6566 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6567 | test->i = AF_INET; |
| 6568 | |
| 6569 | /* |
| 6570 | * If we are parsing url in frontend space, we prepare backend stage |
| 6571 | * to not parse again the same url ! optimization lazyness... |
| 6572 | */ |
| 6573 | if (px->options & PR_O_HTTP_PROXY) |
| 6574 | l4->flags |= SN_ADDR_SET; |
| 6575 | |
| 6576 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6577 | return 1; |
| 6578 | } |
| 6579 | |
| 6580 | static int |
| 6581 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6582 | struct acl_expr *expr, struct acl_test *test) |
| 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) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6590 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6591 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6592 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6593 | /* ensure the indexes are not affected */ |
| 6594 | return 0; |
| 6595 | |
| 6596 | /* Same optimization as url_ip */ |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6597 | 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] | 6598 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6599 | |
| 6600 | if (px->options & PR_O_HTTP_PROXY) |
| 6601 | l4->flags |= SN_ADDR_SET; |
| 6602 | |
| 6603 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6604 | return 1; |
| 6605 | } |
| 6606 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6607 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6608 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6609 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6610 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6611 | 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] | 6612 | struct acl_expr *expr, struct acl_test *test) |
| 6613 | { |
| 6614 | struct http_txn *txn = l7; |
| 6615 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6616 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6617 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6618 | if (!txn) |
| 6619 | return 0; |
| 6620 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6621 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6622 | /* search for header from the beginning */ |
| 6623 | ctx->idx = 0; |
| 6624 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6625 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6626 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6627 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6628 | test->len = ctx->vlen; |
| 6629 | test->ptr = (char *)ctx->line + ctx->val; |
| 6630 | return 1; |
| 6631 | } |
| 6632 | |
| 6633 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6634 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6635 | return 0; |
| 6636 | } |
| 6637 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6638 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6639 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6640 | struct acl_expr *expr, struct acl_test *test) |
| 6641 | { |
| 6642 | struct http_txn *txn = l7; |
| 6643 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6644 | if (!txn) |
| 6645 | return 0; |
| 6646 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6647 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6648 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6649 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6650 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6651 | /* ensure the indexes are not affected */ |
| 6652 | return 0; |
| 6653 | |
| 6654 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6655 | } |
| 6656 | |
| 6657 | static int |
| 6658 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6659 | struct acl_expr *expr, struct acl_test *test) |
| 6660 | { |
| 6661 | struct http_txn *txn = l7; |
| 6662 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6663 | if (!txn) |
| 6664 | return 0; |
| 6665 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6666 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6667 | return 0; |
| 6668 | |
| 6669 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6670 | } |
| 6671 | |
| 6672 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6673 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6674 | */ |
| 6675 | static int |
| 6676 | 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] | 6677 | struct acl_expr *expr, struct acl_test *test) |
| 6678 | { |
| 6679 | struct http_txn *txn = l7; |
| 6680 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6681 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6682 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6683 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6684 | if (!txn) |
| 6685 | return 0; |
| 6686 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6687 | ctx.idx = 0; |
| 6688 | cnt = 0; |
| 6689 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6690 | cnt++; |
| 6691 | |
| 6692 | test->i = cnt; |
| 6693 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6694 | return 1; |
| 6695 | } |
| 6696 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6697 | static int |
| 6698 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6699 | struct acl_expr *expr, struct acl_test *test) |
| 6700 | { |
| 6701 | struct http_txn *txn = l7; |
| 6702 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6703 | if (!txn) |
| 6704 | return 0; |
| 6705 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6706 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6707 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6708 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6709 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6710 | /* ensure the indexes are not affected */ |
| 6711 | return 0; |
| 6712 | |
| 6713 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6714 | } |
| 6715 | |
| 6716 | static int |
| 6717 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6718 | struct acl_expr *expr, struct acl_test *test) |
| 6719 | { |
| 6720 | struct http_txn *txn = l7; |
| 6721 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6722 | if (!txn) |
| 6723 | return 0; |
| 6724 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6725 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6726 | return 0; |
| 6727 | |
| 6728 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6729 | } |
| 6730 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6731 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6732 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6733 | * 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] | 6734 | */ |
| 6735 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6736 | 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] | 6737 | struct acl_expr *expr, struct acl_test *test) |
| 6738 | { |
| 6739 | struct http_txn *txn = l7; |
| 6740 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6741 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6742 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6743 | if (!txn) |
| 6744 | return 0; |
| 6745 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6746 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6747 | /* search for header from the beginning */ |
| 6748 | ctx->idx = 0; |
| 6749 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6750 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6751 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6752 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6753 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6754 | return 1; |
| 6755 | } |
| 6756 | |
| 6757 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6758 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6759 | return 0; |
| 6760 | } |
| 6761 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6762 | static int |
| 6763 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6764 | struct acl_expr *expr, struct acl_test *test) |
| 6765 | { |
| 6766 | struct http_txn *txn = l7; |
| 6767 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6768 | if (!txn) |
| 6769 | return 0; |
| 6770 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6771 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6772 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6773 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6774 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6775 | /* ensure the indexes are not affected */ |
| 6776 | return 0; |
| 6777 | |
| 6778 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6779 | } |
| 6780 | |
| 6781 | static int |
| 6782 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6783 | struct acl_expr *expr, struct acl_test *test) |
| 6784 | { |
| 6785 | struct http_txn *txn = l7; |
| 6786 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6787 | if (!txn) |
| 6788 | return 0; |
| 6789 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6790 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6791 | return 0; |
| 6792 | |
| 6793 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6794 | } |
| 6795 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6796 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 6797 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6798 | */ |
| 6799 | static int |
| 6800 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 6801 | struct acl_expr *expr, struct acl_test *test) |
| 6802 | { |
| 6803 | struct http_txn *txn = l7; |
| 6804 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6805 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 6806 | |
| 6807 | if (!txn) |
| 6808 | return 0; |
| 6809 | |
| 6810 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6811 | /* search for header from the beginning */ |
| 6812 | ctx->idx = 0; |
| 6813 | |
| 6814 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6815 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6816 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6817 | /* Same optimization as url_ip */ |
| 6818 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 6819 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 6820 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 6821 | test->i = AF_INET; |
| 6822 | return 1; |
| 6823 | } |
| 6824 | |
| 6825 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6826 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6827 | return 0; |
| 6828 | } |
| 6829 | |
| 6830 | static int |
| 6831 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6832 | struct acl_expr *expr, struct acl_test *test) |
| 6833 | { |
| 6834 | struct http_txn *txn = l7; |
| 6835 | |
| 6836 | if (!txn) |
| 6837 | return 0; |
| 6838 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6839 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6840 | return 0; |
| 6841 | |
| 6842 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6843 | /* ensure the indexes are not affected */ |
| 6844 | return 0; |
| 6845 | |
| 6846 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 6847 | } |
| 6848 | |
| 6849 | static int |
| 6850 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6851 | struct acl_expr *expr, struct acl_test *test) |
| 6852 | { |
| 6853 | struct http_txn *txn = l7; |
| 6854 | |
| 6855 | if (!txn) |
| 6856 | return 0; |
| 6857 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6858 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6859 | return 0; |
| 6860 | |
| 6861 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 6862 | } |
| 6863 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6864 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 6865 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 6866 | */ |
| 6867 | static int |
| 6868 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6869 | struct acl_expr *expr, struct acl_test *test) |
| 6870 | { |
| 6871 | struct http_txn *txn = l7; |
| 6872 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6873 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6874 | if (!txn) |
| 6875 | return 0; |
| 6876 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6877 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6878 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6879 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6880 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6881 | /* ensure the indexes are not affected */ |
| 6882 | return 0; |
| 6883 | |
Willy Tarreau | a95a1f4 | 2010-01-03 13:04:35 +0100 | [diff] [blame] | 6884 | 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] | 6885 | ptr = http_get_path(txn); |
| 6886 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6887 | return 0; |
| 6888 | |
| 6889 | /* OK, we got the '/' ! */ |
| 6890 | test->ptr = ptr; |
| 6891 | |
| 6892 | while (ptr < end && *ptr != '?') |
| 6893 | ptr++; |
| 6894 | |
| 6895 | test->len = ptr - test->ptr; |
| 6896 | |
| 6897 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6898 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6899 | return 1; |
| 6900 | } |
| 6901 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6902 | static int |
| 6903 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 6904 | struct acl_expr *expr, struct acl_test *test) |
| 6905 | { |
| 6906 | struct buffer *req = s->req; |
| 6907 | struct http_txn *txn = &s->txn; |
| 6908 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6909 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6910 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 6911 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 6912 | */ |
| 6913 | |
| 6914 | if (!s || !req) |
| 6915 | return 0; |
| 6916 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6917 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6918 | /* Already decoded as OK */ |
| 6919 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 6920 | return 1; |
| 6921 | } |
| 6922 | |
| 6923 | /* Try to decode HTTP request */ |
| 6924 | if (likely(req->lr < req->r)) |
| 6925 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 6926 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6927 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6928 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 6929 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 6930 | return 1; |
| 6931 | } |
| 6932 | /* wait for final state */ |
| 6933 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 6934 | return 0; |
| 6935 | } |
| 6936 | |
| 6937 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 6938 | * perform so that further checks can rely on HTTP tests. |
| 6939 | */ |
| 6940 | msg->sol = req->data + msg->som; |
| 6941 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 6942 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 6943 | s->flags |= SN_REDIRECTABLE; |
| 6944 | |
| 6945 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 6946 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 6947 | return 1; |
| 6948 | } |
| 6949 | |
| 6950 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 6951 | return 1; |
| 6952 | } |
| 6953 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6954 | |
| 6955 | /************************************************************************/ |
| 6956 | /* All supported keywords must be declared here. */ |
| 6957 | /************************************************************************/ |
| 6958 | |
| 6959 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 6960 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6961 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 6962 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6963 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 6964 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6965 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6966 | { "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] | 6967 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6968 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6969 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6970 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6971 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6972 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6973 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6974 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6975 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 6976 | { "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] | 6977 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6978 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 6979 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 6980 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 6981 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 6982 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 6983 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 6984 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 6985 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 6986 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 6987 | { "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] | 6988 | { "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] | 6989 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 6990 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 6991 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 6992 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 6993 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 6994 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 6995 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 6996 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 6997 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 6998 | { "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] | 6999 | { "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] | 7000 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7001 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7002 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7003 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7004 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7005 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7006 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7007 | { "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] | 7008 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7009 | { NULL, NULL, NULL, NULL }, |
| 7010 | |
| 7011 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7012 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7013 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7014 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7015 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7016 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7017 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7018 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7019 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7020 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7021 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7022 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7023 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7024 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7025 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7026 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7027 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 7028 | |
| 7029 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 7030 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 7031 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 7032 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 7033 | { NULL, NULL, NULL, NULL }, |
| 7034 | #endif |
| 7035 | }}; |
| 7036 | |
| 7037 | |
| 7038 | __attribute__((constructor)) |
| 7039 | static void __http_protocol_init(void) |
| 7040 | { |
| 7041 | acl_register_keywords(&acl_kws); |
| 7042 | } |
| 7043 | |
| 7044 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7045 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7046 | * Local variables: |
| 7047 | * c-indent-level: 8 |
| 7048 | * c-basic-offset: 8 |
| 7049 | * End: |
| 7050 | */ |