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> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 27 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
| 29 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 30 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/memory.h> |
| 32 | #include <common/mini-clist.h> |
| 33 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 34 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/time.h> |
| 36 | #include <common/uri_auth.h> |
| 37 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
| 39 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 42 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 43 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <proto/backend.h> |
| 45 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 46 | #include <proto/checks.h> |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 47 | #include <proto/client.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 48 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/fd.h> |
| 50 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 51 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 52 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 54 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 56 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 58 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 59 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/task.h> |
| 61 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 62 | const char HTTP_100[] = |
| 63 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 64 | |
| 65 | const struct chunk http_100_chunk = { |
| 66 | .str = (char *)&HTTP_100, |
| 67 | .len = sizeof(HTTP_100)-1 |
| 68 | }; |
| 69 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 70 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 71 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 72 | "HTTP/1.0 200 OK\r\n" |
| 73 | "Cache-Control: no-cache\r\n" |
| 74 | "Connection: close\r\n" |
| 75 | "Content-Type: text/html\r\n" |
| 76 | "\r\n" |
| 77 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 78 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 79 | const struct chunk http_200_chunk = { |
| 80 | .str = (char *)&HTTP_200, |
| 81 | .len = sizeof(HTTP_200)-1 |
| 82 | }; |
| 83 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 84 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 85 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 89 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 90 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 94 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 98 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 101 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 105 | const char *HTTP_401_fmt = |
| 106 | "HTTP/1.0 401 Unauthorized\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 109 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 111 | "\r\n" |
| 112 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 113 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 114 | const char *HTTP_407_fmt = |
| 115 | "HTTP/1.0 407 Unauthorized\r\n" |
| 116 | "Cache-Control: no-cache\r\n" |
| 117 | "Connection: close\r\n" |
| 118 | "Content-Type: text/html\r\n" |
| 119 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 120 | "\r\n" |
| 121 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 122 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 123 | |
| 124 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 125 | [HTTP_ERR_400] = 400, |
| 126 | [HTTP_ERR_403] = 403, |
| 127 | [HTTP_ERR_408] = 408, |
| 128 | [HTTP_ERR_500] = 500, |
| 129 | [HTTP_ERR_502] = 502, |
| 130 | [HTTP_ERR_503] = 503, |
| 131 | [HTTP_ERR_504] = 504, |
| 132 | }; |
| 133 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 134 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 136 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 137 | "Cache-Control: no-cache\r\n" |
| 138 | "Connection: close\r\n" |
| 139 | "Content-Type: text/html\r\n" |
| 140 | "\r\n" |
| 141 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 142 | |
| 143 | [HTTP_ERR_403] = |
| 144 | "HTTP/1.0 403 Forbidden\r\n" |
| 145 | "Cache-Control: no-cache\r\n" |
| 146 | "Connection: close\r\n" |
| 147 | "Content-Type: text/html\r\n" |
| 148 | "\r\n" |
| 149 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 150 | |
| 151 | [HTTP_ERR_408] = |
| 152 | "HTTP/1.0 408 Request Time-out\r\n" |
| 153 | "Cache-Control: no-cache\r\n" |
| 154 | "Connection: close\r\n" |
| 155 | "Content-Type: text/html\r\n" |
| 156 | "\r\n" |
| 157 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 158 | |
| 159 | [HTTP_ERR_500] = |
| 160 | "HTTP/1.0 500 Server Error\r\n" |
| 161 | "Cache-Control: no-cache\r\n" |
| 162 | "Connection: close\r\n" |
| 163 | "Content-Type: text/html\r\n" |
| 164 | "\r\n" |
| 165 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 166 | |
| 167 | [HTTP_ERR_502] = |
| 168 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 169 | "Cache-Control: no-cache\r\n" |
| 170 | "Connection: close\r\n" |
| 171 | "Content-Type: text/html\r\n" |
| 172 | "\r\n" |
| 173 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 174 | |
| 175 | [HTTP_ERR_503] = |
| 176 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 177 | "Cache-Control: no-cache\r\n" |
| 178 | "Connection: close\r\n" |
| 179 | "Content-Type: text/html\r\n" |
| 180 | "\r\n" |
| 181 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 182 | |
| 183 | [HTTP_ERR_504] = |
| 184 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 185 | "Cache-Control: no-cache\r\n" |
| 186 | "Connection: close\r\n" |
| 187 | "Content-Type: text/html\r\n" |
| 188 | "\r\n" |
| 189 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 190 | |
| 191 | }; |
| 192 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 193 | /* We must put the messages here since GCC cannot initialize consts depending |
| 194 | * on strlen(). |
| 195 | */ |
| 196 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 197 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 198 | #define FD_SETS_ARE_BITFIELDS |
| 199 | #ifdef FD_SETS_ARE_BITFIELDS |
| 200 | /* |
| 201 | * This map is used with all the FD_* macros to check whether a particular bit |
| 202 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 203 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 204 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 205 | * exclusively to the macros. |
| 206 | */ |
| 207 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 208 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 209 | |
| 210 | #else |
| 211 | #error "Check if your OS uses bitfields for fd_sets" |
| 212 | #endif |
| 213 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | void init_proto_http() |
| 215 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 216 | int i; |
| 217 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 218 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 219 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 220 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 221 | if (!http_err_msgs[msg]) { |
| 222 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 223 | abort(); |
| 224 | } |
| 225 | |
| 226 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 227 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 228 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 229 | |
| 230 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 231 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 232 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 233 | * printable characters above. |
| 234 | */ |
| 235 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 236 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 237 | for (i = 0; i < 32; i++) { |
| 238 | FD_SET(i, hdr_encode_map); |
| 239 | FD_SET(i, url_encode_map); |
| 240 | } |
| 241 | for (i = 127; i < 256; i++) { |
| 242 | FD_SET(i, hdr_encode_map); |
| 243 | FD_SET(i, url_encode_map); |
| 244 | } |
| 245 | |
| 246 | tmp = "\"#{|}"; |
| 247 | while (*tmp) { |
| 248 | FD_SET(*tmp, hdr_encode_map); |
| 249 | tmp++; |
| 250 | } |
| 251 | |
| 252 | tmp = "\"#"; |
| 253 | while (*tmp) { |
| 254 | FD_SET(*tmp, url_encode_map); |
| 255 | tmp++; |
| 256 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 257 | |
| 258 | /* memory allocations */ |
| 259 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 260 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 261 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 263 | /* |
| 264 | * We have 26 list of methods (1 per first letter), each of which can have |
| 265 | * up to 3 entries (2 valid, 1 null). |
| 266 | */ |
| 267 | struct http_method_desc { |
| 268 | http_meth_t meth; |
| 269 | int len; |
| 270 | const char text[8]; |
| 271 | }; |
| 272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 273 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 274 | ['C' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 276 | }, |
| 277 | ['D' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 279 | }, |
| 280 | ['G' - 'A'] = { |
| 281 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 282 | }, |
| 283 | ['H' - 'A'] = { |
| 284 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 285 | }, |
| 286 | ['P' - 'A'] = { |
| 287 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 288 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 289 | }, |
| 290 | ['T' - 'A'] = { |
| 291 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 292 | }, |
| 293 | /* rest is empty like this : |
| 294 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 295 | */ |
| 296 | }; |
| 297 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 298 | /* 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] | 299 | * 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] | 300 | * RFC2616 for those chars. |
| 301 | */ |
| 302 | |
| 303 | const char http_is_spht[256] = { |
| 304 | [' '] = 1, ['\t'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_crlf[256] = { |
| 308 | ['\r'] = 1, ['\n'] = 1, |
| 309 | }; |
| 310 | |
| 311 | const char http_is_lws[256] = { |
| 312 | [' '] = 1, ['\t'] = 1, |
| 313 | ['\r'] = 1, ['\n'] = 1, |
| 314 | }; |
| 315 | |
| 316 | const char http_is_sep[256] = { |
| 317 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 318 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 319 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 320 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 321 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 322 | }; |
| 323 | |
| 324 | const char http_is_ctl[256] = { |
| 325 | [0 ... 31] = 1, |
| 326 | [127] = 1, |
| 327 | }; |
| 328 | |
| 329 | /* |
| 330 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 331 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 332 | * them correctly. Instead, define every non-CTL char's status. |
| 333 | */ |
| 334 | const char http_is_token[256] = { |
| 335 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 336 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 337 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 338 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 339 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 340 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 341 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 342 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 343 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 344 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 345 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 346 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 347 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 348 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 349 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 350 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 351 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 352 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 353 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 354 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 355 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 356 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 357 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 358 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 359 | }; |
| 360 | |
| 361 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 362 | /* |
| 363 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 364 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 365 | */ |
| 366 | const char http_is_ver_token[256] = { |
| 367 | ['.'] = 1, ['/'] = 1, |
| 368 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 369 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 370 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 371 | }; |
| 372 | |
| 373 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 374 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 375 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 376 | */ |
| 377 | #if defined(DEBUG_FSM) |
| 378 | static void http_silent_debug(int line, struct session *s) |
| 379 | { |
| 380 | int size = 0; |
| 381 | size += snprintf(trash + size, sizeof(trash) - size, |
| 382 | "[%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", |
| 383 | line, |
| 384 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 385 | 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); |
| 386 | write(-1, trash, size); |
| 387 | size = 0; |
| 388 | size += snprintf(trash + size, sizeof(trash) - size, |
| 389 | " %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", |
| 390 | line, |
| 391 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 392 | 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); |
| 393 | |
| 394 | write(-1, trash, size); |
| 395 | } |
| 396 | #else |
| 397 | #define http_silent_debug(l,s) do { } while (0) |
| 398 | #endif |
| 399 | |
| 400 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 401 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 402 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 403 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 404 | * of headers is automatically adjusted. The number of bytes added is returned |
| 405 | * on success, otherwise <0 is returned indicating an error. |
| 406 | */ |
| 407 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 408 | struct hdr_idx *hdr_idx, const char *text) |
| 409 | { |
| 410 | int bytes, len; |
| 411 | |
| 412 | len = strlen(text); |
| 413 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 414 | if (!bytes) |
| 415 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 416 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 417 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 422 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 423 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 424 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 425 | * of headers is automatically adjusted. The number of bytes added is returned |
| 426 | * on success, otherwise <0 is returned indicating an error. |
| 427 | */ |
| 428 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 429 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 430 | { |
| 431 | int bytes; |
| 432 | |
| 433 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 434 | if (!bytes) |
| 435 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 436 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 437 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 438 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 439 | |
| 440 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 441 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 442 | * If so, returns the position of the first non-space character relative to |
| 443 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 444 | * to return a pointer to the place after the first space. Returns 0 if the |
| 445 | * header name does not match. Checks are case-insensitive. |
| 446 | */ |
| 447 | int http_header_match2(const char *hdr, const char *end, |
| 448 | const char *name, int len) |
| 449 | { |
| 450 | const char *val; |
| 451 | |
| 452 | if (hdr + len >= end) |
| 453 | return 0; |
| 454 | if (hdr[len] != ':') |
| 455 | return 0; |
| 456 | if (strncasecmp(hdr, name, len) != 0) |
| 457 | return 0; |
| 458 | val = hdr + len + 1; |
| 459 | while (val < end && HTTP_IS_SPHT(*val)) |
| 460 | val++; |
| 461 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 462 | return len + 2; /* we may replace starting from second space */ |
| 463 | return val - hdr; |
| 464 | } |
| 465 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 466 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 467 | * par 2.2 for more information. Note that it requires a valid header to return |
| 468 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 469 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 470 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 471 | { |
| 472 | int quoted, qdpair; |
| 473 | |
| 474 | quoted = qdpair = 0; |
| 475 | for (; s < e; s++) { |
| 476 | if (qdpair) qdpair = 0; |
| 477 | else if (quoted && *s == '\\') qdpair = 1; |
| 478 | else if (quoted && *s == '"') quoted = 0; |
| 479 | else if (*s == '"') quoted = 1; |
| 480 | else if (*s == ',') return s; |
| 481 | } |
| 482 | return s; |
| 483 | } |
| 484 | |
| 485 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 486 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 487 | * structure holds everything necessary to use the header and find next |
| 488 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 489 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 490 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 491 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 492 | * is NULL when searching for a new values of a header, the current header is |
| 493 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 494 | */ |
| 495 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 496 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | struct hdr_ctx *ctx) |
| 498 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 499 | char *eol, *sov; |
| 500 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 501 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | cur_idx = ctx->idx; |
| 503 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 504 | /* We have previously returned a value, let's search |
| 505 | * another one on the same line. |
| 506 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 508 | ctx->del = ctx->val + ctx->vlen; |
| 509 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | eol = sol + idx->v[cur_idx].len; |
| 511 | |
| 512 | if (sov >= eol) |
| 513 | /* no more values in this header */ |
| 514 | goto next_hdr; |
| 515 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 516 | /* values remaining for this header, skip the comma but save it |
| 517 | * for later use (eg: for header deletion). |
| 518 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 519 | sov++; |
| 520 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 521 | sov++; |
| 522 | |
| 523 | goto return_hdr; |
| 524 | } |
| 525 | |
| 526 | /* first request for this header */ |
| 527 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 528 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 529 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 530 | while (cur_idx) { |
| 531 | eol = sol + idx->v[cur_idx].len; |
| 532 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 533 | if (len == 0) { |
| 534 | /* No argument was passed, we want any header. |
| 535 | * To achieve this, we simply build a fake request. */ |
| 536 | while (sol + len < eol && sol[len] != ':') |
| 537 | len++; |
| 538 | name = sol; |
| 539 | } |
| 540 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 541 | if ((len < eol - sol) && |
| 542 | (sol[len] == ':') && |
| 543 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 544 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 545 | sov = sol + len + 1; |
| 546 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 547 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 548 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 549 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 550 | ctx->prev = old_idx; |
| 551 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 552 | ctx->idx = cur_idx; |
| 553 | ctx->val = sov - sol; |
| 554 | |
| 555 | eol = find_hdr_value_end(sov, eol); |
| 556 | ctx->vlen = eol - sov; |
| 557 | return 1; |
| 558 | } |
| 559 | next_hdr: |
| 560 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 562 | cur_idx = idx->v[cur_idx].next; |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 568 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 569 | struct hdr_ctx *ctx) |
| 570 | { |
| 571 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 572 | } |
| 573 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 574 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 575 | * the http_find_header functions. The value is removed, as well as surrounding |
| 576 | * commas if any. If the removed value was alone, the whole header is removed. |
| 577 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 578 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 579 | * no more header, so any processing may stop. The ctx is always left in a form |
| 580 | * that can be handled by http_find_header2() to find next occurrence. |
| 581 | */ |
| 582 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 583 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 584 | { |
| 585 | int cur_idx = ctx->idx; |
| 586 | char *sol = ctx->line; |
| 587 | struct hdr_idx_elem *hdr; |
| 588 | int delta, skip_comma; |
| 589 | |
| 590 | if (!cur_idx) |
| 591 | return 0; |
| 592 | |
| 593 | hdr = &idx->v[cur_idx]; |
| 594 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 595 | /* This was the only value of the header, we must now remove it entirely. */ |
| 596 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 597 | http_msg_move_end(msg, delta); |
| 598 | idx->used--; |
| 599 | hdr->len = 0; /* unused entry */ |
| 600 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
| 601 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 602 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 603 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 604 | ctx->vlen = 0; |
| 605 | return ctx->idx; |
| 606 | } |
| 607 | |
| 608 | /* This was not the only value of this header. We have to remove between |
| 609 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 610 | * of the list, we remove the last separator. |
| 611 | */ |
| 612 | |
| 613 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 614 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 615 | sol + ctx->val + ctx->vlen + skip_comma, |
| 616 | NULL, 0); |
| 617 | hdr->len += delta; |
| 618 | http_msg_move_end(msg, delta); |
| 619 | ctx->val = ctx->del; |
| 620 | ctx->vlen = 0; |
| 621 | return ctx->idx; |
| 622 | } |
| 623 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 624 | /* This function handles a server error at the stream interface level. The |
| 625 | * stream interface is assumed to be already in a closed state. An optional |
| 626 | * message is copied into the input buffer, and an HTTP status code stored. |
| 627 | * 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] | 628 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 629 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 630 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 631 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 632 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 633 | buffer_auto_read(si->ob); |
| 634 | buffer_abort(si->ob); |
| 635 | buffer_auto_close(si->ob); |
| 636 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 637 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 638 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 639 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 640 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 641 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | } |
| 643 | if (!(t->flags & SN_ERR_MASK)) |
| 644 | t->flags |= err; |
| 645 | if (!(t->flags & SN_FINST_MASK)) |
| 646 | t->flags |= finst; |
| 647 | } |
| 648 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 649 | /* This function returns the appropriate error location for the given session |
| 650 | * and message. |
| 651 | */ |
| 652 | |
| 653 | struct chunk *error_message(struct session *s, int msgnum) |
| 654 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 655 | if (s->be->errmsg[msgnum].str) |
| 656 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 657 | else if (s->fe->errmsg[msgnum].str) |
| 658 | return &s->fe->errmsg[msgnum]; |
| 659 | else |
| 660 | return &http_err_chunks[msgnum]; |
| 661 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 663 | /* |
| 664 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 665 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 666 | */ |
| 667 | static http_meth_t find_http_meth(const char *str, const int len) |
| 668 | { |
| 669 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 670 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 671 | |
| 672 | m = ((unsigned)*str - 'A'); |
| 673 | |
| 674 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 675 | for (h = http_methods[m]; h->len > 0; h++) { |
| 676 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 677 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 678 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 679 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 680 | }; |
| 681 | return HTTP_METH_OTHER; |
| 682 | } |
| 683 | return HTTP_METH_NONE; |
| 684 | |
| 685 | } |
| 686 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 687 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 688 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 689 | * It is returned otherwise. |
| 690 | */ |
| 691 | static char * |
| 692 | http_get_path(struct http_txn *txn) |
| 693 | { |
| 694 | char *ptr, *end; |
| 695 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 696 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 697 | end = ptr + txn->req.sl.rq.u_l; |
| 698 | |
| 699 | if (ptr >= end) |
| 700 | return NULL; |
| 701 | |
| 702 | /* RFC2616, par. 5.1.2 : |
| 703 | * Request-URI = "*" | absuri | abspath | authority |
| 704 | */ |
| 705 | |
| 706 | if (*ptr == '*') |
| 707 | return NULL; |
| 708 | |
| 709 | if (isalpha((unsigned char)*ptr)) { |
| 710 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 711 | ptr++; |
| 712 | while (ptr < end && |
| 713 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 714 | ptr++; |
| 715 | /* skip '://' */ |
| 716 | if (ptr == end || *ptr++ != ':') |
| 717 | return NULL; |
| 718 | if (ptr == end || *ptr++ != '/') |
| 719 | return NULL; |
| 720 | if (ptr == end || *ptr++ != '/') |
| 721 | return NULL; |
| 722 | } |
| 723 | /* skip [user[:passwd]@]host[:[port]] */ |
| 724 | |
| 725 | while (ptr < end && *ptr != '/') |
| 726 | ptr++; |
| 727 | |
| 728 | if (ptr == end) |
| 729 | return NULL; |
| 730 | |
| 731 | /* OK, we got the '/' ! */ |
| 732 | return ptr; |
| 733 | } |
| 734 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 735 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 736 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 737 | * left unchanged and will follow normal proxy processing. |
| 738 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 739 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 740 | { |
| 741 | struct http_txn *txn; |
| 742 | struct chunk rdr; |
| 743 | char *path; |
| 744 | int len; |
| 745 | |
| 746 | /* 1: create the response header */ |
| 747 | rdr.len = strlen(HTTP_302); |
| 748 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 749 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 750 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 751 | |
| 752 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 753 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 754 | return; |
| 755 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 756 | /* special prefix "/" means don't change URL */ |
| 757 | if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') { |
| 758 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 759 | rdr.len += s->srv->rdr_len; |
| 760 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 761 | |
| 762 | /* 3: add the request URI */ |
| 763 | txn = &s->txn; |
| 764 | path = http_get_path(txn); |
| 765 | if (!path) |
| 766 | return; |
| 767 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 768 | len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 769 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 770 | return; |
| 771 | |
| 772 | memcpy(rdr.str + rdr.len, path, len); |
| 773 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 774 | |
| 775 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 776 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 777 | rdr.len += 29; |
| 778 | } else { |
| 779 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 780 | rdr.len += 23; |
| 781 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 782 | |
| 783 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 784 | si->shutr(si); |
| 785 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 786 | si->err_type = SI_ET_NONE; |
| 787 | si->err_loc = NULL; |
| 788 | si->state = SI_ST_CLO; |
| 789 | |
| 790 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 791 | 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] | 792 | |
| 793 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 794 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 795 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 798 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 799 | * that the server side is closed. Note that err_type is actually a |
| 800 | * bitmask, where almost only aborts may be cumulated with other |
| 801 | * values. We consider that aborted operations are more important |
| 802 | * than timeouts or errors due to the fact that nobody else in the |
| 803 | * logs might explain incomplete retries. All others should avoid |
| 804 | * being cumulated. It should normally not be possible to have multiple |
| 805 | * aborts at once, but just in case, the first one in sequence is reported. |
| 806 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 807 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 808 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 809 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 810 | |
| 811 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 812 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 813 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 814 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 815 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 816 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 817 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 818 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 819 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 820 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 821 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 822 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 823 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 824 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 825 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 826 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 827 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 828 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 829 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 830 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 831 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 834 | extern const char sess_term_cond[8]; |
| 835 | extern const char sess_fin_state[8]; |
| 836 | extern const char *monthname[12]; |
| 837 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 838 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 839 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 840 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 841 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 842 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 843 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 844 | void http_sess_clflog(struct session *s) |
| 845 | { |
| 846 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 847 | struct proxy *fe = s->fe; |
| 848 | struct proxy *be = s->be; |
| 849 | struct proxy *prx_log; |
| 850 | struct http_txn *txn = &s->txn; |
| 851 | int tolog, level, err; |
| 852 | char *uri, *h; |
| 853 | char *svid; |
| 854 | struct tm tm; |
| 855 | static char tmpline[MAX_SYSLOG_LEN]; |
| 856 | int hdr; |
| 857 | size_t w; |
| 858 | int t_request; |
| 859 | |
| 860 | prx_log = fe; |
| 861 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 862 | (s->conn_retries != be->conn_retries) || |
| 863 | txn->status >= 500; |
| 864 | |
| 865 | if (s->cli_addr.ss_family == AF_INET) |
| 866 | inet_ntop(AF_INET, |
| 867 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 868 | pn, sizeof(pn)); |
| 869 | else |
| 870 | inet_ntop(AF_INET6, |
| 871 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 872 | pn, sizeof(pn)); |
| 873 | |
| 874 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 875 | |
| 876 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 877 | tolog = fe->to_log; |
| 878 | |
| 879 | h = tmpline; |
| 880 | |
| 881 | w = snprintf(h, sizeof(tmpline), |
| 882 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 883 | pn, |
| 884 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 885 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 886 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 887 | goto trunc; |
| 888 | h += w; |
| 889 | |
| 890 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 891 | goto trunc; |
| 892 | |
| 893 | *(h++) = ' '; |
| 894 | *(h++) = '\"'; |
| 895 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 896 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 897 | '#', url_encode_map, uri); |
| 898 | *(h++) = '\"'; |
| 899 | |
| 900 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 901 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 902 | goto trunc; |
| 903 | h += w; |
| 904 | |
| 905 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 906 | goto trunc; |
| 907 | memcpy(h, " \"-\" \"-\"", 8); |
| 908 | h += 8; |
| 909 | |
| 910 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 911 | " %d %03d", |
| 912 | (s->cli_addr.ss_family == AF_INET) ? |
| 913 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 914 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 915 | (int)s->logs.accept_date.tv_usec/1000); |
| 916 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 917 | goto trunc; |
| 918 | h += w; |
| 919 | |
| 920 | w = strlen(fe->id); |
| 921 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 922 | goto trunc; |
| 923 | *(h++) = ' '; |
| 924 | *(h++) = '\"'; |
| 925 | memcpy(h, fe->id, w); |
| 926 | h += w; |
| 927 | *(h++) = '\"'; |
| 928 | |
| 929 | w = strlen(be->id); |
| 930 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 931 | goto trunc; |
| 932 | *(h++) = ' '; |
| 933 | *(h++) = '\"'; |
| 934 | memcpy(h, be->id, w); |
| 935 | h += w; |
| 936 | *(h++) = '\"'; |
| 937 | |
| 938 | svid = (tolog & LW_SVID) ? |
| 939 | (s->data_source != DATA_SRC_STATS) ? |
| 940 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 941 | |
| 942 | w = strlen(svid); |
| 943 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 944 | goto trunc; |
| 945 | *(h++) = ' '; |
| 946 | *(h++) = '\"'; |
| 947 | memcpy(h, svid, w); |
| 948 | h += w; |
| 949 | *(h++) = '\"'; |
| 950 | |
| 951 | t_request = -1; |
| 952 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 953 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 954 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 955 | " %d %ld %ld %ld %ld", |
| 956 | t_request, |
| 957 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 958 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 959 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 960 | s->logs.t_close); |
| 961 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 962 | goto trunc; |
| 963 | h += w; |
| 964 | |
| 965 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 966 | goto trunc; |
| 967 | *(h++) = ' '; |
| 968 | *(h++) = '\"'; |
| 969 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 970 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 971 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 972 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 973 | *(h++) = '\"'; |
| 974 | |
| 975 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 976 | " %d %d %d %d %d %ld %ld", |
| 977 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 978 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 979 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 980 | |
| 981 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 982 | goto trunc; |
| 983 | h += w; |
| 984 | |
| 985 | if (txn->cli_cookie) { |
| 986 | w = strlen(txn->cli_cookie); |
| 987 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 988 | goto trunc; |
| 989 | *(h++) = ' '; |
| 990 | *(h++) = '\"'; |
| 991 | memcpy(h, txn->cli_cookie, w); |
| 992 | h += w; |
| 993 | *(h++) = '\"'; |
| 994 | } else { |
| 995 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 996 | goto trunc; |
| 997 | memcpy(h, " \"-\"", 4); |
| 998 | h += 4; |
| 999 | } |
| 1000 | |
| 1001 | if (txn->srv_cookie) { |
| 1002 | w = strlen(txn->srv_cookie); |
| 1003 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1004 | goto trunc; |
| 1005 | *(h++) = ' '; |
| 1006 | *(h++) = '\"'; |
| 1007 | memcpy(h, txn->srv_cookie, w); |
| 1008 | h += w; |
| 1009 | *(h++) = '\"'; |
| 1010 | } else { |
| 1011 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1012 | goto trunc; |
| 1013 | memcpy(h, " \"-\"", 4); |
| 1014 | h += 4; |
| 1015 | } |
| 1016 | |
| 1017 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1018 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1019 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1020 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1021 | if (txn->req.cap[hdr] != NULL) { |
| 1022 | *(h++) = ' '; |
| 1023 | *(h++) = '\"'; |
| 1024 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1025 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1026 | *(h++) = '\"'; |
| 1027 | } else { |
| 1028 | memcpy(h, " \"-\"", 4); |
| 1029 | h += 4; |
| 1030 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1035 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1036 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1037 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1038 | if (txn->rsp.cap[hdr] != NULL) { |
| 1039 | *(h++) = ' '; |
| 1040 | *(h++) = '\"'; |
| 1041 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1042 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1043 | *(h++) = '\"'; |
| 1044 | } else { |
| 1045 | memcpy(h, " \"-\"", 4); |
| 1046 | h += 4; |
| 1047 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | trunc: |
| 1052 | *h = '\0'; |
| 1053 | |
| 1054 | level = LOG_INFO; |
| 1055 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1056 | level = LOG_ERR; |
| 1057 | |
| 1058 | send_log(prx_log, level, "%s\n", tmpline); |
| 1059 | |
| 1060 | s->logs.logwait = 0; |
| 1061 | } |
| 1062 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1063 | /* |
| 1064 | * send a log for the session when we have enough info about it. |
| 1065 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1066 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1067 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1068 | { |
| 1069 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1070 | struct proxy *fe = s->fe; |
| 1071 | struct proxy *be = s->be; |
| 1072 | struct proxy *prx_log; |
| 1073 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1074 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1075 | char *uri, *h; |
| 1076 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1077 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1078 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1079 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1080 | int hdr; |
| 1081 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1082 | /* if we don't want to log normal traffic, return now */ |
| 1083 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 1084 | (s->conn_retries != be->conn_retries) || |
| 1085 | txn->status >= 500; |
| 1086 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1087 | return; |
| 1088 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1089 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1090 | return; |
| 1091 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1092 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1093 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1094 | return http_sess_clflog(s); |
| 1095 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1096 | if (s->cli_addr.ss_family == AF_INET) |
| 1097 | inet_ntop(AF_INET, |
| 1098 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1099 | pn, sizeof(pn)); |
| 1100 | else |
| 1101 | inet_ntop(AF_INET6, |
| 1102 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1103 | pn, sizeof(pn)); |
| 1104 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1105 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1106 | |
| 1107 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1108 | tolog = fe->to_log; |
| 1109 | |
| 1110 | h = tmpline; |
| 1111 | if (fe->to_log & LW_REQHDR && |
| 1112 | txn->req.cap && |
| 1113 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1114 | *(h++) = ' '; |
| 1115 | *(h++) = '{'; |
| 1116 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1117 | if (hdr) |
| 1118 | *(h++) = '|'; |
| 1119 | if (txn->req.cap[hdr] != NULL) |
| 1120 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1121 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1122 | } |
| 1123 | *(h++) = '}'; |
| 1124 | } |
| 1125 | |
| 1126 | if (fe->to_log & LW_RSPHDR && |
| 1127 | txn->rsp.cap && |
| 1128 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1129 | *(h++) = ' '; |
| 1130 | *(h++) = '{'; |
| 1131 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1132 | if (hdr) |
| 1133 | *(h++) = '|'; |
| 1134 | if (txn->rsp.cap[hdr] != NULL) |
| 1135 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1136 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1137 | } |
| 1138 | *(h++) = '}'; |
| 1139 | } |
| 1140 | |
| 1141 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1142 | *(h++) = ' '; |
| 1143 | *(h++) = '"'; |
| 1144 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1145 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1146 | '#', url_encode_map, uri); |
| 1147 | *(h++) = '"'; |
| 1148 | } |
| 1149 | *h = '\0'; |
| 1150 | |
| 1151 | svid = (tolog & LW_SVID) ? |
| 1152 | (s->data_source != DATA_SRC_STATS) ? |
| 1153 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1154 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1155 | t_request = -1; |
| 1156 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1157 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1158 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1159 | level = LOG_INFO; |
| 1160 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1161 | level = LOG_ERR; |
| 1162 | |
| 1163 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1164 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1165 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1166 | " %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] | 1167 | pn, |
| 1168 | (s->cli_addr.ss_family == AF_INET) ? |
| 1169 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1170 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1171 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1172 | 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] | 1173 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1174 | t_request, |
| 1175 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1176 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1177 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1178 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1179 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1180 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1181 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1182 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1183 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1184 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1185 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1186 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1187 | 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] | 1188 | (s->flags & SN_REDISP)?"+":"", |
| 1189 | (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] | 1190 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1191 | |
| 1192 | s->logs.logwait = 0; |
| 1193 | } |
| 1194 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1195 | |
| 1196 | /* |
| 1197 | * Capture headers from message starting at <som> according to header list |
| 1198 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1199 | */ |
| 1200 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1201 | char **cap, struct cap_hdr *cap_hdr) |
| 1202 | { |
| 1203 | char *eol, *sol, *col, *sov; |
| 1204 | int cur_idx; |
| 1205 | struct cap_hdr *h; |
| 1206 | int len; |
| 1207 | |
| 1208 | sol = som + hdr_idx_first_pos(idx); |
| 1209 | cur_idx = hdr_idx_first_idx(idx); |
| 1210 | |
| 1211 | while (cur_idx) { |
| 1212 | eol = sol + idx->v[cur_idx].len; |
| 1213 | |
| 1214 | col = sol; |
| 1215 | while (col < eol && *col != ':') |
| 1216 | col++; |
| 1217 | |
| 1218 | sov = col + 1; |
| 1219 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1220 | sov++; |
| 1221 | |
| 1222 | for (h = cap_hdr; h; h = h->next) { |
| 1223 | if ((h->namelen == col - sol) && |
| 1224 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1225 | if (cap[h->index] == NULL) |
| 1226 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1227 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1228 | |
| 1229 | if (cap[h->index] == NULL) { |
| 1230 | Alert("HTTP capture : out of memory.\n"); |
| 1231 | continue; |
| 1232 | } |
| 1233 | |
| 1234 | len = eol - sov; |
| 1235 | if (len > h->len) |
| 1236 | len = h->len; |
| 1237 | |
| 1238 | memcpy(cap[h->index], sov, len); |
| 1239 | cap[h->index][len]=0; |
| 1240 | } |
| 1241 | } |
| 1242 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1243 | cur_idx = idx->v[cur_idx].next; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1248 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1249 | */ |
| 1250 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1251 | |
| 1252 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1253 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1254 | */ |
| 1255 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1256 | ptr++; \ |
| 1257 | if (likely(ptr < end)) \ |
| 1258 | goto good; \ |
| 1259 | else { \ |
| 1260 | state = (st); \ |
| 1261 | goto http_msg_ood; \ |
| 1262 | } \ |
| 1263 | } while (0) |
| 1264 | |
| 1265 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1266 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1267 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1269 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1270 | * will give undefined results. |
| 1271 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1272 | * and that msg->sol points to the beginning of the response. |
| 1273 | * If a complete line is found (which implies that at least one CR or LF is |
| 1274 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1275 | * returned indicating an incomplete line (which does not mean that parts have |
| 1276 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1277 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1278 | * upon next call. |
| 1279 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1280 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1281 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1282 | * 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] | 1283 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1285 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1286 | unsigned int state, const char *ptr, const char *end, |
| 1287 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1288 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1289 | switch (state) { |
| 1290 | http_msg_rpver: |
| 1291 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1292 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1294 | |
| 1295 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1296 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1297 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1298 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1299 | state = HTTP_MSG_ERROR; |
| 1300 | break; |
| 1301 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1302 | http_msg_rpver_sp: |
| 1303 | case HTTP_MSG_RPVER_SP: |
| 1304 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1305 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1306 | goto http_msg_rpcode; |
| 1307 | } |
| 1308 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1309 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1310 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1311 | state = HTTP_MSG_ERROR; |
| 1312 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1313 | |
| 1314 | http_msg_rpcode: |
| 1315 | case HTTP_MSG_RPCODE: |
| 1316 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1317 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1318 | |
| 1319 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1320 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1321 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1322 | } |
| 1323 | |
| 1324 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1325 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1326 | http_msg_rsp_reason: |
| 1327 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1328 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1329 | msg->sl.st.r_l = 0; |
| 1330 | goto http_msg_rpline_eol; |
| 1331 | |
| 1332 | http_msg_rpcode_sp: |
| 1333 | case HTTP_MSG_RPCODE_SP: |
| 1334 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1335 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1336 | goto http_msg_rpreason; |
| 1337 | } |
| 1338 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1339 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1340 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1341 | goto http_msg_rsp_reason; |
| 1342 | |
| 1343 | http_msg_rpreason: |
| 1344 | case HTTP_MSG_RPREASON: |
| 1345 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1346 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1347 | msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | http_msg_rpline_eol: |
| 1349 | /* We have seen the end of line. Note that we do not |
| 1350 | * necessarily have the \n yet, but at least we know that we |
| 1351 | * have EITHER \r OR \n, otherwise the response would not be |
| 1352 | * complete. We can then record the response length and return |
| 1353 | * to the caller which will be able to register it. |
| 1354 | */ |
| 1355 | msg->sl.st.l = ptr - msg->sol; |
| 1356 | return ptr; |
| 1357 | |
| 1358 | #ifdef DEBUG_FULL |
| 1359 | default: |
| 1360 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1361 | exit(1); |
| 1362 | #endif |
| 1363 | } |
| 1364 | |
| 1365 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1366 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1367 | if (ret_state) |
| 1368 | *ret_state = state; |
| 1369 | if (ret_ptr) |
| 1370 | *ret_ptr = (char *)ptr; |
| 1371 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1374 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1375 | * This function parses a request line between <ptr> and <end>, starting with |
| 1376 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1377 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1378 | * will give undefined results. |
| 1379 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1380 | * and that msg->sol points to the beginning of the request. |
| 1381 | * If a complete line is found (which implies that at least one CR or LF is |
| 1382 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1383 | * returned indicating an incomplete line (which does not mean that parts have |
| 1384 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1385 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1386 | * upon next call. |
| 1387 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1388 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1390 | * 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] | 1391 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1392 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1393 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1394 | unsigned int state, const char *ptr, const char *end, |
| 1395 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1396 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | switch (state) { |
| 1398 | http_msg_rqmeth: |
| 1399 | case HTTP_MSG_RQMETH: |
| 1400 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1401 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1404 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1406 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1409 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1410 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1412 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1414 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1416 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | msg->sl.rq.v_l = 0; |
| 1418 | goto http_msg_rqline_eol; |
| 1419 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1420 | state = HTTP_MSG_ERROR; |
| 1421 | break; |
| 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | http_msg_rqmeth_sp: |
| 1424 | case HTTP_MSG_RQMETH_SP: |
| 1425 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1426 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | goto http_msg_rquri; |
| 1428 | } |
| 1429 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1430 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1431 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1432 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1433 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | http_msg_rquri: |
| 1435 | case HTTP_MSG_RQURI: |
| 1436 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1437 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1440 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1441 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1442 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1443 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1444 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1445 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | http_msg_rquri_sp: |
| 1448 | case HTTP_MSG_RQURI_SP: |
| 1449 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1450 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | goto http_msg_rqver; |
| 1452 | } |
| 1453 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1454 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1455 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1456 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1457 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1458 | http_msg_rqver: |
| 1459 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1460 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1462 | |
| 1463 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1464 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1465 | http_msg_rqline_eol: |
| 1466 | /* We have seen the end of line. Note that we do not |
| 1467 | * necessarily have the \n yet, but at least we know that we |
| 1468 | * have EITHER \r OR \n, otherwise the request would not be |
| 1469 | * complete. We can then record the request length and return |
| 1470 | * to the caller which will be able to register it. |
| 1471 | */ |
| 1472 | msg->sl.rq.l = ptr - msg->sol; |
| 1473 | return ptr; |
| 1474 | } |
| 1475 | |
| 1476 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1477 | state = HTTP_MSG_ERROR; |
| 1478 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1479 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1480 | #ifdef DEBUG_FULL |
| 1481 | default: |
| 1482 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1483 | exit(1); |
| 1484 | #endif |
| 1485 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1488 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1489 | if (ret_state) |
| 1490 | *ret_state = state; |
| 1491 | if (ret_ptr) |
| 1492 | *ret_ptr = (char *)ptr; |
| 1493 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1494 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1495 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1496 | /* |
| 1497 | * Returns the data from Authorization header. Function may be called more |
| 1498 | * than once so data is stored in txn->auth_data. When no header is found |
| 1499 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1500 | * searching again for something we are unable to find anyway. |
| 1501 | */ |
| 1502 | |
| 1503 | char get_http_auth_buff[BUFSIZE]; |
| 1504 | |
| 1505 | int |
| 1506 | get_http_auth(struct session *s) |
| 1507 | { |
| 1508 | |
| 1509 | struct http_txn *txn = &s->txn; |
| 1510 | struct chunk auth_method; |
| 1511 | struct hdr_ctx ctx; |
| 1512 | char *h, *p; |
| 1513 | int len; |
| 1514 | |
| 1515 | #ifdef DEBUG_AUTH |
| 1516 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1517 | #endif |
| 1518 | |
| 1519 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1520 | return 0; |
| 1521 | |
| 1522 | if (txn->auth.method) |
| 1523 | return 1; |
| 1524 | |
| 1525 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1526 | |
| 1527 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1528 | |
| 1529 | if (txn->flags & TX_USE_PX_CONN) { |
| 1530 | h = "Proxy-Authorization"; |
| 1531 | len = strlen(h); |
| 1532 | } else { |
| 1533 | h = "Authorization"; |
| 1534 | len = strlen(h); |
| 1535 | } |
| 1536 | |
| 1537 | if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1538 | return 0; |
| 1539 | |
| 1540 | h = ctx.line + ctx.val; |
| 1541 | |
| 1542 | p = memchr(h, ' ', ctx.vlen); |
| 1543 | if (!p || p == h) |
| 1544 | return 0; |
| 1545 | |
| 1546 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1547 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1548 | |
| 1549 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1550 | |
| 1551 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1552 | get_http_auth_buff, BUFSIZE - 1); |
| 1553 | |
| 1554 | if (len < 0) |
| 1555 | return 0; |
| 1556 | |
| 1557 | |
| 1558 | get_http_auth_buff[len] = '\0'; |
| 1559 | |
| 1560 | p = strchr(get_http_auth_buff, ':'); |
| 1561 | |
| 1562 | if (!p) |
| 1563 | return 0; |
| 1564 | |
| 1565 | txn->auth.user = get_http_auth_buff; |
| 1566 | *p = '\0'; |
| 1567 | txn->auth.pass = p+1; |
| 1568 | |
| 1569 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1570 | return 1; |
| 1571 | } |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1576 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1577 | /* |
| 1578 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1579 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1580 | * when data are missing and recalled at the exact same location with no |
| 1581 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1582 | * 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] | 1583 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1584 | * the first state, so that none of the msg pointers has to be initialized |
| 1585 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1586 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1587 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1588 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1589 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1591 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1592 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | ptr = buf->lr; |
| 1594 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1595 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | if (unlikely(ptr >= end)) |
| 1597 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1598 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1599 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1600 | /* |
| 1601 | * First, states that are specific to the response only. |
| 1602 | * We check them first so that request and headers are |
| 1603 | * closer to each other (accessed more often). |
| 1604 | */ |
| 1605 | http_msg_rpbefore: |
| 1606 | case HTTP_MSG_RPBEFORE: |
| 1607 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1608 | /* we have a start of message, but we have to check |
| 1609 | * first if we need to remove some CRLF. We can only |
| 1610 | * do this when send_max=0. |
| 1611 | */ |
| 1612 | char *beg = buf->w + buf->send_max; |
| 1613 | if (beg >= buf->data + buf->size) |
| 1614 | beg -= buf->size; |
| 1615 | if (unlikely(ptr != beg)) { |
| 1616 | if (buf->send_max) |
| 1617 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1618 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1619 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1620 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1621 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1622 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1623 | hdr_idx_init(idx); |
| 1624 | state = HTTP_MSG_RPVER; |
| 1625 | goto http_msg_rpver; |
| 1626 | } |
| 1627 | |
| 1628 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1629 | goto http_msg_invalid; |
| 1630 | |
| 1631 | if (unlikely(*ptr == '\n')) |
| 1632 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1633 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1634 | /* stop here */ |
| 1635 | |
| 1636 | http_msg_rpbefore_cr: |
| 1637 | case HTTP_MSG_RPBEFORE_CR: |
| 1638 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1639 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1640 | /* stop here */ |
| 1641 | |
| 1642 | http_msg_rpver: |
| 1643 | case HTTP_MSG_RPVER: |
| 1644 | case HTTP_MSG_RPVER_SP: |
| 1645 | case HTTP_MSG_RPCODE: |
| 1646 | case HTTP_MSG_RPCODE_SP: |
| 1647 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1648 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1649 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1650 | if (unlikely(!ptr)) |
| 1651 | return; |
| 1652 | |
| 1653 | /* we have a full response and we know that we have either a CR |
| 1654 | * or an LF at <ptr>. |
| 1655 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1656 | //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] | 1657 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1658 | |
| 1659 | msg->sol = ptr; |
| 1660 | if (likely(*ptr == '\r')) |
| 1661 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1662 | goto http_msg_rpline_end; |
| 1663 | |
| 1664 | http_msg_rpline_end: |
| 1665 | case HTTP_MSG_RPLINE_END: |
| 1666 | /* msg->sol must point to the first of CR or LF. */ |
| 1667 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1668 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1669 | /* stop here */ |
| 1670 | |
| 1671 | /* |
| 1672 | * Second, states that are specific to the request only |
| 1673 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | http_msg_rqbefore: |
| 1675 | case HTTP_MSG_RQBEFORE: |
| 1676 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1677 | /* we have a start of message, but we have to check |
| 1678 | * first if we need to remove some CRLF. We can only |
| 1679 | * do this when send_max=0. |
| 1680 | */ |
| 1681 | char *beg = buf->w + buf->send_max; |
| 1682 | if (beg >= buf->data + buf->size) |
| 1683 | beg -= buf->size; |
| 1684 | if (likely(ptr != beg)) { |
| 1685 | if (buf->send_max) |
| 1686 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1687 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1688 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1690 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1691 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1692 | /* we will need this when keep-alive will be supported |
| 1693 | hdr_idx_init(idx); |
| 1694 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1695 | state = HTTP_MSG_RQMETH; |
| 1696 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1697 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1698 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1699 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1700 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1701 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | if (unlikely(*ptr == '\n')) |
| 1703 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1704 | 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] | 1705 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1706 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1707 | http_msg_rqbefore_cr: |
| 1708 | case HTTP_MSG_RQBEFORE_CR: |
| 1709 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1710 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1711 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1712 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1713 | http_msg_rqmeth: |
| 1714 | case HTTP_MSG_RQMETH: |
| 1715 | case HTTP_MSG_RQMETH_SP: |
| 1716 | case HTTP_MSG_RQURI: |
| 1717 | case HTTP_MSG_RQURI_SP: |
| 1718 | case HTTP_MSG_RQVER: |
| 1719 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1720 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | if (unlikely(!ptr)) |
| 1722 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1723 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | /* we have a full request and we know that we have either a CR |
| 1725 | * or an LF at <ptr>. |
| 1726 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1727 | //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] | 1728 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1729 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | msg->sol = ptr; |
| 1731 | if (likely(*ptr == '\r')) |
| 1732 | 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] | 1733 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1734 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1735 | http_msg_rqline_end: |
| 1736 | case HTTP_MSG_RQLINE_END: |
| 1737 | /* check for HTTP/0.9 request : no version information available. |
| 1738 | * msg->sol must point to the first of CR or LF. |
| 1739 | */ |
| 1740 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1741 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1742 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1744 | 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] | 1745 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1746 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1747 | /* |
| 1748 | * Common states below |
| 1749 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1750 | http_msg_hdr_first: |
| 1751 | case HTTP_MSG_HDR_FIRST: |
| 1752 | msg->sol = ptr; |
| 1753 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1754 | goto http_msg_hdr_name; |
| 1755 | } |
| 1756 | |
| 1757 | if (likely(*ptr == '\r')) |
| 1758 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1759 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1761 | http_msg_hdr_name: |
| 1762 | case HTTP_MSG_HDR_NAME: |
| 1763 | /* assumes msg->sol points to the first char */ |
| 1764 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1765 | 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] | 1766 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1767 | if (likely(*ptr == ':')) { |
| 1768 | msg->col = ptr - buf->data; |
| 1769 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1770 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1771 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1772 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1773 | goto http_msg_invalid; |
| 1774 | |
| 1775 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1776 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1777 | |
| 1778 | /* and we still accept this non-token character */ |
| 1779 | 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] | 1780 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1781 | http_msg_hdr_l1_sp: |
| 1782 | case HTTP_MSG_HDR_L1_SP: |
| 1783 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1784 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1785 | 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] | 1786 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1787 | /* header value can be basically anything except CR/LF */ |
| 1788 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1789 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1791 | goto http_msg_hdr_val; |
| 1792 | } |
| 1793 | |
| 1794 | if (likely(*ptr == '\r')) |
| 1795 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1796 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1797 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1798 | http_msg_hdr_l1_lf: |
| 1799 | case HTTP_MSG_HDR_L1_LF: |
| 1800 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1801 | 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] | 1802 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | http_msg_hdr_l1_lws: |
| 1804 | case HTTP_MSG_HDR_L1_LWS: |
| 1805 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1806 | /* replace HT,CR,LF with spaces */ |
| 1807 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1808 | buf->data[msg->sov] = ' '; |
| 1809 | goto http_msg_hdr_l1_sp; |
| 1810 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1811 | /* we had a header consisting only in spaces ! */ |
| 1812 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1813 | goto http_msg_complete_header; |
| 1814 | |
| 1815 | http_msg_hdr_val: |
| 1816 | case HTTP_MSG_HDR_VAL: |
| 1817 | /* assumes msg->sol points to the first char, msg->col to the |
| 1818 | * colon, and msg->sov points to the first character of the |
| 1819 | * value. |
| 1820 | */ |
| 1821 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1822 | 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] | 1823 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1824 | msg->eol = ptr; |
| 1825 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1826 | * real header end in case it ends with lots of LWS, but is this |
| 1827 | * really needed ? |
| 1828 | */ |
| 1829 | if (likely(*ptr == '\r')) |
| 1830 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1831 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1832 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1833 | http_msg_hdr_l2_lf: |
| 1834 | case HTTP_MSG_HDR_L2_LF: |
| 1835 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1836 | 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] | 1837 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | http_msg_hdr_l2_lws: |
| 1839 | case HTTP_MSG_HDR_L2_LWS: |
| 1840 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1841 | /* LWS: replace HT,CR,LF with spaces */ |
| 1842 | for (; msg->eol < ptr; msg->eol++) |
| 1843 | *msg->eol = ' '; |
| 1844 | goto http_msg_hdr_val; |
| 1845 | } |
| 1846 | http_msg_complete_header: |
| 1847 | /* |
| 1848 | * It was a new header, so the last one is finished. |
| 1849 | * Assumes msg->sol points to the first char, msg->col to the |
| 1850 | * colon, msg->sov points to the first character of the value |
| 1851 | * and msg->eol to the first CR or LF so we know how the line |
| 1852 | * ends. We insert last header into the index. |
| 1853 | */ |
| 1854 | /* |
| 1855 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1856 | write(2, msg->sol, msg->eol-msg->sol); |
| 1857 | fprintf(stderr,"\n"); |
| 1858 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1859 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1860 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1861 | idx, idx->tail) < 0)) |
| 1862 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1863 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1864 | msg->sol = ptr; |
| 1865 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1866 | goto http_msg_hdr_name; |
| 1867 | } |
| 1868 | |
| 1869 | if (likely(*ptr == '\r')) |
| 1870 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1871 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1872 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1873 | http_msg_last_lf: |
| 1874 | case HTTP_MSG_LAST_LF: |
| 1875 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1876 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1877 | ptr++; |
| 1878 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1879 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1880 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1881 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1882 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | return; |
| 1884 | #ifdef DEBUG_FULL |
| 1885 | default: |
| 1886 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1887 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1888 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1889 | } |
| 1890 | http_msg_ood: |
| 1891 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1892 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1893 | buf->lr = ptr; |
| 1894 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1895 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | http_msg_invalid: |
| 1897 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1898 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1899 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1900 | return; |
| 1901 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1902 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1903 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1904 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1905 | * nothing is done and 1 is returned. |
| 1906 | */ |
| 1907 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1908 | { |
| 1909 | int delta; |
| 1910 | char *cur_end; |
| 1911 | |
| 1912 | if (msg->sl.rq.v_l != 0) |
| 1913 | return 1; |
| 1914 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1915 | cur_end = msg->sol + msg->sl.rq.l; |
| 1916 | delta = 0; |
| 1917 | |
| 1918 | if (msg->sl.rq.u_l == 0) { |
| 1919 | /* if no URI was set, add "/" */ |
| 1920 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1921 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1922 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1923 | } |
| 1924 | /* add HTTP version */ |
| 1925 | 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] | 1926 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1927 | cur_end += delta; |
| 1928 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1929 | HTTP_MSG_RQMETH, |
| 1930 | msg->sol, cur_end + 1, |
| 1931 | NULL, NULL); |
| 1932 | if (unlikely(!cur_end)) |
| 1933 | return 0; |
| 1934 | |
| 1935 | /* we have a full HTTP/1.0 request now and we know that |
| 1936 | * we have either a CR or an LF at <ptr>. |
| 1937 | */ |
| 1938 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1939 | return 1; |
| 1940 | } |
| 1941 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1942 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1943 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1944 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1945 | * are used for that : |
| 1946 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1947 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1948 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1949 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1950 | * harmless combinations may be removed. Do not call that after changes have |
| 1951 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1952 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1953 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1954 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1955 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1956 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1957 | const char *hdr_val = "Connection"; |
| 1958 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1959 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1960 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1961 | return; |
| 1962 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1963 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1964 | hdr_val = "Proxy-Connection"; |
| 1965 | hdr_len = 16; |
| 1966 | } |
| 1967 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1968 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1969 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1970 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1971 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1972 | txn->flags |= TX_HDR_CONN_KAL; |
| 1973 | if ((to_del & 2) && buf) |
| 1974 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1975 | else |
| 1976 | txn->flags |= TX_CON_KAL_SET; |
| 1977 | } |
| 1978 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1979 | txn->flags |= TX_HDR_CONN_CLO; |
| 1980 | if ((to_del & 1) && buf) |
| 1981 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1982 | else |
| 1983 | txn->flags |= TX_CON_CLO_SET; |
| 1984 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1985 | } |
| 1986 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1987 | txn->flags |= TX_HDR_CONN_PRS; |
| 1988 | return; |
| 1989 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1990 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1991 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1992 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1993 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1994 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1995 | */ |
| 1996 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1997 | { |
| 1998 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1999 | const char *hdr_val = "Connection"; |
| 2000 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2001 | |
| 2002 | ctx.idx = 0; |
| 2003 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2004 | |
| 2005 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2006 | hdr_val = "Proxy-Connection"; |
| 2007 | hdr_len = 16; |
| 2008 | } |
| 2009 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2010 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2011 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2012 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2013 | if (wanted & TX_CON_KAL_SET) |
| 2014 | txn->flags |= TX_CON_KAL_SET; |
| 2015 | else |
| 2016 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2017 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2018 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2019 | if (wanted & TX_CON_CLO_SET) |
| 2020 | txn->flags |= TX_CON_CLO_SET; |
| 2021 | else |
| 2022 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2023 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2024 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2025 | |
| 2026 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2027 | return; |
| 2028 | |
| 2029 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2030 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2031 | hdr_val = "Connection: close"; |
| 2032 | hdr_len = 17; |
| 2033 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2034 | hdr_val = "Proxy-Connection: close"; |
| 2035 | hdr_len = 23; |
| 2036 | } |
| 2037 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2041 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2042 | hdr_val = "Connection: keep-alive"; |
| 2043 | hdr_len = 22; |
| 2044 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2045 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2046 | hdr_len = 28; |
| 2047 | } |
| 2048 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2049 | } |
| 2050 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2051 | } |
| 2052 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2053 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2054 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2055 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2056 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2057 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2058 | * 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] | 2059 | * 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] | 2060 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2061 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2062 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2063 | char *ptr = buf->lr; |
| 2064 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2065 | unsigned int chunk = 0; |
| 2066 | |
| 2067 | /* The chunk size is in the following form, though we are only |
| 2068 | * interested in the size and CRLF : |
| 2069 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2070 | */ |
| 2071 | while (1) { |
| 2072 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2073 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2074 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2075 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2076 | if (c < 0) /* not a hex digit anymore */ |
| 2077 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2078 | if (++ptr >= end) |
| 2079 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2080 | if (chunk & 0xF000000) /* overflow will occur */ |
| 2081 | return -1; |
| 2082 | chunk = (chunk << 4) + c; |
| 2083 | } |
| 2084 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2085 | /* empty size not allowed */ |
| 2086 | if (ptr == buf->lr) |
| 2087 | return -1; |
| 2088 | |
| 2089 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2090 | if (++ptr >= end) |
| 2091 | ptr = buf->data; |
| 2092 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2093 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2096 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2097 | * for the end of chunk size. |
| 2098 | */ |
| 2099 | while (1) { |
| 2100 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2101 | /* we now have a CR or an LF at ptr */ |
| 2102 | if (likely(*ptr == '\r')) { |
| 2103 | if (++ptr >= end) |
| 2104 | ptr = buf->data; |
| 2105 | if (ptr == buf->r) |
| 2106 | return 0; |
| 2107 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2108 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2109 | if (*ptr != '\n') |
| 2110 | return -1; |
| 2111 | if (++ptr >= end) |
| 2112 | ptr = buf->data; |
| 2113 | /* done */ |
| 2114 | break; |
| 2115 | } |
| 2116 | else if (*ptr == ';') { |
| 2117 | /* chunk extension, ends at next CRLF */ |
| 2118 | if (++ptr >= end) |
| 2119 | ptr = buf->data; |
| 2120 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2121 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2122 | |
| 2123 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2124 | if (++ptr >= end) |
| 2125 | ptr = buf->data; |
| 2126 | if (ptr == buf->r) |
| 2127 | return 0; |
| 2128 | } |
| 2129 | /* we have a CRLF now, loop above */ |
| 2130 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2131 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2132 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2133 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2134 | } |
| 2135 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2136 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2137 | * which may or may not be present. We save that into ->lr and |
| 2138 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2139 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2140 | msg->sov += ptr - buf->lr; |
| 2141 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2142 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2143 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2144 | return 1; |
| 2145 | } |
| 2146 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2147 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2148 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2149 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2150 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2151 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2152 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2153 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2154 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2155 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2156 | * which implies that all non-trailers data have already been scheduled for |
| 2157 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2158 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2159 | * important to note that this function is designed to be able to parse wrapped |
| 2160 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2161 | */ |
| 2162 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2163 | { |
| 2164 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2165 | while (1) { |
| 2166 | char *p1 = NULL, *p2 = NULL; |
| 2167 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2168 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2169 | |
| 2170 | /* scan current line and stop at LF or CRLF */ |
| 2171 | while (1) { |
| 2172 | if (ptr == buf->r) |
| 2173 | return 0; |
| 2174 | |
| 2175 | if (*ptr == '\n') { |
| 2176 | if (!p1) |
| 2177 | p1 = ptr; |
| 2178 | p2 = ptr; |
| 2179 | break; |
| 2180 | } |
| 2181 | |
| 2182 | if (*ptr == '\r') { |
| 2183 | if (p1) |
| 2184 | return -1; |
| 2185 | p1 = ptr; |
| 2186 | } |
| 2187 | |
| 2188 | ptr++; |
| 2189 | if (ptr >= buf->data + buf->size) |
| 2190 | ptr = buf->data; |
| 2191 | } |
| 2192 | |
| 2193 | /* after LF; point to beginning of next line */ |
| 2194 | p2++; |
| 2195 | if (p2 >= buf->data + buf->size) |
| 2196 | p2 = buf->data; |
| 2197 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2198 | bytes = p2 - buf->lr; |
| 2199 | if (bytes < 0) |
| 2200 | bytes += buf->size; |
| 2201 | |
| 2202 | /* schedule this line for forwarding */ |
| 2203 | msg->sov += bytes; |
| 2204 | if (msg->sov >= buf->size) |
| 2205 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2206 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2207 | if (p1 == buf->lr) { |
| 2208 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2209 | * Everything was scheduled for forwarding, there's nothing |
| 2210 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2211 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2212 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2213 | msg->msg_state = HTTP_MSG_DONE; |
| 2214 | return 1; |
| 2215 | } |
| 2216 | /* OK, next line then */ |
| 2217 | buf->lr = p2; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2222 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2223 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2224 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2225 | * not enough data are available, the function does not change anything and |
| 2226 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2227 | * does not change anything. Note: this function is designed to parse wrapped |
| 2228 | * CRLF at the end of the buffer. |
| 2229 | */ |
| 2230 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2231 | { |
| 2232 | char *ptr; |
| 2233 | int bytes; |
| 2234 | |
| 2235 | /* NB: we'll check data availabilty at the end. It's not a |
| 2236 | * problem because whatever we match first will be checked |
| 2237 | * against the correct length. |
| 2238 | */ |
| 2239 | bytes = 1; |
| 2240 | ptr = buf->lr; |
| 2241 | if (*ptr == '\r') { |
| 2242 | bytes++; |
| 2243 | ptr++; |
| 2244 | if (ptr >= buf->data + buf->size) |
| 2245 | ptr = buf->data; |
| 2246 | } |
| 2247 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 2248 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2249 | return 0; |
| 2250 | |
| 2251 | if (*ptr != '\n') |
| 2252 | return -1; |
| 2253 | |
| 2254 | ptr++; |
| 2255 | if (ptr >= buf->data + buf->size) |
| 2256 | ptr = buf->data; |
| 2257 | buf->lr = ptr; |
| 2258 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2259 | msg->sov = ptr - buf->data; |
| 2260 | msg->som = msg->sov - bytes; |
| 2261 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2262 | return 1; |
| 2263 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2264 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2265 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2266 | { |
| 2267 | char *end = buf->data + buf->size; |
| 2268 | int off = buf->data + buf->size - buf->w; |
| 2269 | |
| 2270 | /* two possible cases : |
| 2271 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2272 | * - the buffer is in two blocks, we move it via the swap_buffer |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2273 | */ |
| 2274 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2275 | int block1 = buf->l; |
| 2276 | int block2 = 0; |
| 2277 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2278 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2279 | block1 = buf->data + buf->size - buf->w; |
| 2280 | block2 = buf->r - buf->data; |
| 2281 | } |
| 2282 | if (block2) |
| 2283 | memcpy(swap_buffer, buf->data, block2); |
| 2284 | memmove(buf->data, buf->w, block1); |
| 2285 | if (block2) |
| 2286 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | /* adjust all known pointers */ |
| 2290 | buf->w = buf->data; |
| 2291 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2292 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2293 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2294 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2295 | |
| 2296 | /* adjust relative pointers */ |
| 2297 | msg->som = 0; |
| 2298 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2299 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2300 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2301 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2302 | if (msg->err_pos >= 0) { |
| 2303 | msg->err_pos += off; |
| 2304 | if (msg->err_pos >= buf->size) |
| 2305 | msg->err_pos -= buf->size; |
| 2306 | } |
| 2307 | |
| 2308 | buf->flags &= ~BF_FULL; |
| 2309 | if (buf->l >= buffer_max_len(buf)) |
| 2310 | buf->flags |= BF_FULL; |
| 2311 | } |
| 2312 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2313 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2314 | * processing can continue on next analysers, or zero if it either needs more |
| 2315 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2316 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2317 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2318 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2319 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2320 | 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] | 2321 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2322 | /* |
| 2323 | * We will parse the partial (or complete) lines. |
| 2324 | * We will check the request syntax, and also join multi-line |
| 2325 | * headers. An index of all the lines will be elaborated while |
| 2326 | * parsing. |
| 2327 | * |
| 2328 | * For the parsing, we use a 28 states FSM. |
| 2329 | * |
| 2330 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2331 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2332 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2333 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2334 | * req->lr = first non-visited byte |
| 2335 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2336 | * |
| 2337 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2338 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2339 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2340 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2341 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2342 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2343 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2344 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2345 | struct http_txn *txn = &s->txn; |
| 2346 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2347 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2348 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2349 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2350 | now_ms, __FUNCTION__, |
| 2351 | s, |
| 2352 | req, |
| 2353 | req->rex, req->wex, |
| 2354 | req->flags, |
| 2355 | req->l, |
| 2356 | req->analysers); |
| 2357 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2358 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2359 | s->srv_error = http_return_srv_error; |
| 2360 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2361 | /* There's a protected area at the end of the buffer for rewriting |
| 2362 | * purposes. We don't want to start to parse the request if the |
| 2363 | * protected area is affected, because we may have to move processed |
| 2364 | * data later, which is much more complicated. |
| 2365 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2366 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2367 | if ((txn->flags & TX_NOT_FIRST) && |
| 2368 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2369 | req->r < req->lr || |
| 2370 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2371 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2372 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2373 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2374 | /* some data has still not left the buffer, wake us once that's done */ |
| 2375 | buffer_dont_connect(req); |
| 2376 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2377 | return 0; |
| 2378 | } |
| 2379 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2380 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2381 | } |
| 2382 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2383 | /* Note that we have the same problem with the response ; we |
| 2384 | * may want to send a redirect, error or anything which requires |
| 2385 | * some spare space. So we'll ensure that we have at least |
| 2386 | * maxrewrite bytes available in the response buffer before |
| 2387 | * processing that one. This will only affect pipelined |
| 2388 | * keep-alive requests. |
| 2389 | */ |
| 2390 | if ((txn->flags & TX_NOT_FIRST) && |
| 2391 | unlikely((s->rep->flags & BF_FULL) || |
| 2392 | s->rep->r < s->rep->lr || |
| 2393 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2394 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2395 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2396 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2397 | /* don't let a connection request be initiated */ |
| 2398 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2399 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2400 | return 0; |
| 2401 | } |
| 2402 | } |
| 2403 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2404 | if (likely(req->lr < req->r)) |
| 2405 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2408 | /* 1: we might have to print this header in debug mode */ |
| 2409 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2410 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2411 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2412 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2413 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2414 | sol = msg->sol; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2415 | eol = sol + msg->sl.rq.l; |
| 2416 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2417 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2418 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2419 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2420 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2421 | while (cur_idx) { |
| 2422 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2423 | debug_hdr("clihdr", s, sol, eol); |
| 2424 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2425 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2426 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2427 | } |
| 2428 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2429 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2430 | /* |
| 2431 | * Now we quickly check if we have found a full valid request. |
| 2432 | * If not so, we check the FD and buffer states before leaving. |
| 2433 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2434 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2435 | * requests are checked first. When waiting for a second request |
| 2436 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2437 | * we note the error in the session flags but don't set any state. |
| 2438 | * Since the error will be noted there, it will not be counted by |
| 2439 | * process_session() as a frontend error. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2440 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2441 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2442 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2443 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2444 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2445 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2446 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2447 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2448 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2449 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2450 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2451 | /* 1: Since we are in header mode, if there's no space |
| 2452 | * left for headers, we won't be able to free more |
| 2453 | * later, so the session will never terminate. We |
| 2454 | * must terminate it now. |
| 2455 | */ |
| 2456 | if (unlikely(req->flags & BF_FULL)) { |
| 2457 | /* FIXME: check if URI is set and return Status |
| 2458 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2459 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2460 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2461 | goto return_bad_req; |
| 2462 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2463 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2464 | /* 2: have we encountered a read error ? */ |
| 2465 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2466 | if (!(s->flags & SN_ERR_MASK)) |
| 2467 | s->flags |= SN_ERR_CLICL; |
| 2468 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2469 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2470 | goto failed_keep_alive; |
| 2471 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2472 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2473 | if (msg->err_pos >= 0) |
| 2474 | 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] | 2475 | msg->msg_state = HTTP_MSG_ERROR; |
| 2476 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2477 | |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2478 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2479 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2480 | if (s->listener->counters) |
| 2481 | s->listener->counters->failed_req++; |
| 2482 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2483 | if (!(s->flags & SN_FINST_MASK)) |
| 2484 | s->flags |= SN_FINST_R; |
| 2485 | return 0; |
| 2486 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2487 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2488 | /* 3: has the read timeout expired ? */ |
| 2489 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2490 | if (!(s->flags & SN_ERR_MASK)) |
| 2491 | s->flags |= SN_ERR_CLITO; |
| 2492 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2493 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2494 | goto failed_keep_alive; |
| 2495 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2496 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2497 | if (msg->err_pos >= 0) |
| 2498 | 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] | 2499 | txn->status = 408; |
| 2500 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2501 | msg->msg_state = HTTP_MSG_ERROR; |
| 2502 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2503 | |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2504 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2505 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2506 | if (s->listener->counters) |
| 2507 | s->listener->counters->failed_req++; |
| 2508 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2509 | if (!(s->flags & SN_FINST_MASK)) |
| 2510 | s->flags |= SN_FINST_R; |
| 2511 | return 0; |
| 2512 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2513 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | /* 4: have we encountered a close ? */ |
| 2515 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2516 | if (!(s->flags & SN_ERR_MASK)) |
| 2517 | s->flags |= SN_ERR_CLICL; |
| 2518 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2519 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2520 | goto failed_keep_alive; |
| 2521 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2522 | if (msg->err_pos >= 0) |
| 2523 | 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] | 2524 | txn->status = 400; |
| 2525 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2526 | msg->msg_state = HTTP_MSG_ERROR; |
| 2527 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2528 | |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2529 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2530 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2531 | if (s->listener->counters) |
| 2532 | s->listener->counters->failed_req++; |
| 2533 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2534 | if (!(s->flags & SN_FINST_MASK)) |
| 2535 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2536 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2537 | } |
| 2538 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2539 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2540 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2541 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2542 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2543 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2544 | /* If the client starts to talk, let's fall back to |
| 2545 | * request timeout processing. |
| 2546 | */ |
| 2547 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2548 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2549 | } |
| 2550 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2551 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2552 | if (!tick_isset(req->analyse_exp)) { |
| 2553 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2554 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2555 | tick_isset(s->be->timeout.httpka)) |
| 2556 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2557 | else |
| 2558 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2559 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2560 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2561 | /* we're not ready yet */ |
| 2562 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2563 | |
| 2564 | failed_keep_alive: |
| 2565 | /* Here we process low-level errors for keep-alive requests. In |
| 2566 | * short, if the request is not the first one and it experiences |
| 2567 | * a timeout, read error or shutdown, we just silently close so |
| 2568 | * that the client can try again. |
| 2569 | */ |
| 2570 | txn->status = 0; |
| 2571 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2572 | req->analysers = 0; |
| 2573 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2574 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2575 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2576 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2577 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2578 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2579 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2580 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2581 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2582 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2583 | * points to the CRLF of the request line. req->lr points to the first |
| 2584 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2585 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2586 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2587 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2588 | |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2589 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2590 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2591 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2592 | /* kill the pending keep-alive timeout */ |
| 2593 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2594 | req->analyse_exp = TICK_ETERNITY; |
| 2595 | } |
| 2596 | |
| 2597 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2598 | /* Maybe we found in invalid header name while we were configured not |
| 2599 | * to block on that, so we have to capture it now. |
| 2600 | */ |
| 2601 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2602 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | /* |
| 2605 | * 1: identify the method |
| 2606 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2607 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2608 | |
| 2609 | /* we can make use of server redirect on GET and HEAD */ |
| 2610 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2611 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2612 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2613 | /* |
| 2614 | * 2: check if the URI matches the monitor_uri. |
| 2615 | * We have to do this for every request which gets in, because |
| 2616 | * the monitor-uri is defined by the frontend. |
| 2617 | */ |
| 2618 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2619 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2620 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2621 | s->fe->monitor_uri, |
| 2622 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2623 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2624 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2625 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2626 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2627 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2628 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2629 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2630 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2631 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2632 | 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] | 2633 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2634 | ret = acl_pass(ret); |
| 2635 | if (cond->pol == ACL_COND_UNLESS) |
| 2636 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2637 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2638 | if (ret) { |
| 2639 | /* we fail this request, let's return 503 service unavail */ |
| 2640 | txn->status = 503; |
| 2641 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2642 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2643 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2644 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2645 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2646 | /* nothing to fail, let's reply normaly */ |
| 2647 | txn->status = 200; |
| 2648 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2649 | goto return_prx_cond; |
| 2650 | } |
| 2651 | |
| 2652 | /* |
| 2653 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2654 | * Note: we cannot log anymore if the request has been |
| 2655 | * classified as invalid. |
| 2656 | */ |
| 2657 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2658 | /* we have a complete HTTP request that we must log */ |
| 2659 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2660 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2661 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2662 | if (urilen >= REQURI_LEN) |
| 2663 | urilen = REQURI_LEN - 1; |
| 2664 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2665 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2666 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2667 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2668 | s->do_log(s); |
| 2669 | } else { |
| 2670 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2671 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2672 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2673 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2674 | /* 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] | 2675 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2676 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2677 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2678 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2679 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2680 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2681 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2682 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2683 | txn->flags |= TX_REQ_VER_11; |
| 2684 | |
| 2685 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2686 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2687 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2688 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2689 | * we have what looks like a proxied connection instead of a connection, |
| 2690 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2691 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2692 | * happen to do that despite being non-standard :-( |
| 2693 | * We consider that a request not beginning with either '/' or '*' is |
| 2694 | * a proxied connection, which covers both "scheme://location" and |
| 2695 | * CONNECT ip:port. |
| 2696 | */ |
| 2697 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2698 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2699 | txn->flags |= TX_USE_PX_CONN; |
| 2700 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2701 | /* transfer length unknown*/ |
| 2702 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2703 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2704 | /* 5: we may need to capture headers */ |
| 2705 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2706 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2707 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2708 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2709 | /* 6: determine the transfer-length. |
| 2710 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2711 | * the presence of a message-body in a REQUEST and its transfer length |
| 2712 | * must be determined that way (in order of precedence) : |
| 2713 | * 1. The presence of a message-body in a request is signaled by the |
| 2714 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2715 | * in the request's header fields. When a request message contains |
| 2716 | * both a message-body of non-zero length and a method that does |
| 2717 | * not define any semantics for that request message-body, then an |
| 2718 | * origin server SHOULD either ignore the message-body or respond |
| 2719 | * with an appropriate error message (e.g., 413). A proxy or |
| 2720 | * gateway, when presented the same request, SHOULD either forward |
| 2721 | * the request inbound with the message- body or ignore the |
| 2722 | * message-body when determining a response. |
| 2723 | * |
| 2724 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2725 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2726 | * transfer-length is defined by the use of this transfer-coding. |
| 2727 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2728 | * transfer-coding is not present, the transfer-length is defined |
| 2729 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2730 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2731 | * 3. If a Content-Length header field is present, its decimal value in |
| 2732 | * OCTETs represents both the entity-length and the transfer-length. |
| 2733 | * If a message is received with both a Transfer-Encoding header |
| 2734 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2735 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2736 | * 4. By the server closing the connection. (Closing the connection |
| 2737 | * cannot be used to indicate the end of a request body, since that |
| 2738 | * would leave no possibility for the server to send back a response.) |
| 2739 | * |
| 2740 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2741 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2742 | * it is terminated by closing the connection. When the "chunked" |
| 2743 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2744 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2745 | */ |
| 2746 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2747 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2748 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2749 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2750 | while ((txn->flags & TX_REQ_VER_11) && |
| 2751 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2752 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2753 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2754 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2755 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2756 | use_close_only = 1; |
| 2757 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2758 | break; |
| 2759 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2760 | } |
| 2761 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2762 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2763 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2764 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2765 | signed long long cl; |
| 2766 | |
| 2767 | if (!ctx.vlen) |
| 2768 | goto return_bad_req; |
| 2769 | |
| 2770 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2771 | goto return_bad_req; /* parse failure */ |
| 2772 | |
| 2773 | if (cl < 0) |
| 2774 | goto return_bad_req; |
| 2775 | |
| 2776 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2777 | goto return_bad_req; /* already specified, was different */ |
| 2778 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2779 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2780 | msg->hdr_content_len = cl; |
| 2781 | } |
| 2782 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2783 | /* bodyless requests have a known length */ |
| 2784 | if (!use_close_only) |
| 2785 | txn->flags |= TX_REQ_XFER_LEN; |
| 2786 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2787 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2788 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2789 | req->analyse_exp = TICK_ETERNITY; |
| 2790 | return 1; |
| 2791 | |
| 2792 | return_bad_req: |
| 2793 | /* We centralize bad requests processing here */ |
| 2794 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2795 | /* we detected a parsing error. We want to archive this request |
| 2796 | * in the dedicated proxy area for later troubleshooting. |
| 2797 | */ |
| 2798 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2799 | } |
| 2800 | |
| 2801 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2802 | txn->status = 400; |
| 2803 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2804 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2805 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2806 | if (s->listener->counters) |
| 2807 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2808 | |
| 2809 | return_prx_cond: |
| 2810 | if (!(s->flags & SN_ERR_MASK)) |
| 2811 | s->flags |= SN_ERR_PRXCOND; |
| 2812 | if (!(s->flags & SN_FINST_MASK)) |
| 2813 | s->flags |= SN_FINST_R; |
| 2814 | |
| 2815 | req->analysers = 0; |
| 2816 | req->analyse_exp = TICK_ETERNITY; |
| 2817 | return 0; |
| 2818 | } |
| 2819 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2820 | /* This stream analyser runs all HTTP request processing which is common to |
| 2821 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2822 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2823 | * 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] | 2824 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2825 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2826 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2827 | 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] | 2828 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2829 | struct http_txn *txn = &s->txn; |
| 2830 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2831 | struct acl_cond *cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2832 | struct req_acl_rule *req_acl, *req_acl_final = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2833 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2834 | struct cond_wordlist *wl; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2835 | int del_ka, del_cl, do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2836 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2837 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2838 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2839 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2840 | return 0; |
| 2841 | } |
| 2842 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2843 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2844 | req->analyse_exp = TICK_ETERNITY; |
| 2845 | |
| 2846 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2847 | now_ms, __FUNCTION__, |
| 2848 | s, |
| 2849 | req, |
| 2850 | req->rex, req->wex, |
| 2851 | req->flags, |
| 2852 | req->l, |
| 2853 | req->analysers); |
| 2854 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2855 | /* first check whether we have some ACLs set to block this request */ |
| 2856 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2857 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2858 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2859 | ret = acl_pass(ret); |
| 2860 | if (cond->pol == ACL_COND_UNLESS) |
| 2861 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2862 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2863 | if (ret) { |
| 2864 | txn->status = 403; |
| 2865 | /* let's log the request time */ |
| 2866 | s->logs.tv_request = now; |
| 2867 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2868 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2869 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2870 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2871 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2872 | do_stats = stats_check_uri(s, px); |
| 2873 | |
| 2874 | list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2875 | int ret = 1; |
| 2876 | |
| 2877 | if (req_acl->action >= PR_REQ_ACL_ACT_MAX) |
| 2878 | continue; |
| 2879 | |
| 2880 | /* check condition, but only if attached */ |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 2881 | if (req_acl->cond) { |
| 2882 | ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ); |
| 2883 | ret = acl_pass(ret); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2884 | |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 2885 | if (req_acl->cond->pol == ACL_COND_UNLESS) |
| 2886 | ret = !ret; |
| 2887 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2888 | |
| 2889 | if (ret) { |
| 2890 | req_acl_final = req_acl; |
| 2891 | break; |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) { |
| 2896 | txn->status = 403; |
| 2897 | s->logs.tv_request = now; |
| 2898 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2899 | goto return_prx_cond; |
| 2900 | } |
| 2901 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2902 | /* try headers filters */ |
| 2903 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2904 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2905 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2906 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2907 | /* has the request been denied ? */ |
| 2908 | if (txn->flags & TX_CLDENY) { |
| 2909 | /* no need to go further */ |
| 2910 | txn->status = 403; |
| 2911 | /* let's log the request time */ |
| 2912 | s->logs.tv_request = now; |
| 2913 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2914 | goto return_prx_cond; |
| 2915 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2916 | |
| 2917 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2918 | * which may be the same as the connect timeout if unspecified. |
| 2919 | * If unset, then set it to zero because we really want it to |
| 2920 | * eventually expire. We build the tarpit as an analyser. |
| 2921 | */ |
| 2922 | if (txn->flags & TX_CLTARPIT) { |
| 2923 | buffer_erase(s->req); |
| 2924 | /* wipe the request out so that we can drop the connection early |
| 2925 | * if the client closes first. |
| 2926 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2927 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2928 | req->analysers = 0; /* remove switching rules etc... */ |
| 2929 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2930 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2931 | if (!req->analyse_exp) |
| 2932 | req->analyse_exp = tick_add(now_ms, 0); |
| 2933 | return 1; |
| 2934 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2935 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2936 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2937 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2938 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2939 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2940 | * in which headers are mangled. However, if another mode is set, it will |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2941 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2942 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2943 | * The method consists in checking if options changed between the two calls |
| 2944 | * (implying that either one is non-null, or one of them is non-null and we |
| 2945 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2946 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2947 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2948 | del_cl = del_ka = 0; |
| 2949 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2950 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2951 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2952 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2953 | (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] | 2954 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2955 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2956 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2957 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2958 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2959 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2960 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2961 | tmp = TX_CON_WANT_CLO; |
| 2962 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2963 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2964 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2965 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2966 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2967 | /* parse the Connection header and possibly clean it */ |
| 2968 | int to_del = 0; |
| 2969 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame^] | 2970 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2971 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2972 | to_del |= 2; /* remove "keep-alive" */ |
| 2973 | if (!(txn->flags & TX_REQ_VER_11)) |
| 2974 | to_del |= 1; /* remove "close" */ |
| 2975 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2976 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2977 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2978 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2979 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2980 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2981 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2982 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
| 2983 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2984 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 2985 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2986 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2987 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2988 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2989 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2990 | list_for_each_entry(wl, &px->req_add, list) { |
Willy Tarreau | 8abd4cd | 2010-01-31 14:30:44 +0100 | [diff] [blame] | 2991 | if (wl->cond) { |
| 2992 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 2993 | ret = acl_pass(ret); |
| 2994 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2995 | ret = !ret; |
| 2996 | if (!ret) |
| 2997 | continue; |
| 2998 | } |
| 2999 | |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 3000 | 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] | 3001 | goto return_bad_req; |
| 3002 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3003 | |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3004 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) { |
| 3005 | struct chunk msg; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3006 | char *realm = req_acl->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3007 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3008 | if (!realm) |
| 3009 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3010 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 3011 | sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3012 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 3013 | txn->status = 401; |
| 3014 | stream_int_retnclose(req->prod, &msg); |
| 3015 | goto return_prx_cond; |
| 3016 | } |
| 3017 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3018 | if (do_stats) { |
| 3019 | /* We need to provied stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3020 | * FIXME!!! that one is rather dangerous, we want to |
| 3021 | * make it follow standard rules (eg: clear req->analysers). |
| 3022 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3023 | |
| 3024 | s->logs.tv_request = now; |
| 3025 | s->data_source = DATA_SRC_STATS; |
| 3026 | s->data_state = DATA_ST_INIT; |
| 3027 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3028 | stream_int_register_handler(s->rep->prod, http_stats_io_handler); |
| 3029 | s->rep->prod->private = s; |
| 3030 | s->rep->prod->st0 = s->rep->prod->st1 = 0; |
| 3031 | req->analysers = 0; |
| 3032 | |
| 3033 | return 0; |
| 3034 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3035 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3036 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3037 | /* check whether we have some ACLs set to redirect this request */ |
| 3038 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3039 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3040 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3041 | if (rule->cond) { |
| 3042 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3043 | ret = acl_pass(ret); |
| 3044 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3045 | ret = !ret; |
| 3046 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3047 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3048 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3049 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3050 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3051 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3052 | /* build redirect message */ |
| 3053 | switch(rule->code) { |
| 3054 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3055 | msg_fmt = HTTP_303; |
| 3056 | break; |
| 3057 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3058 | msg_fmt = HTTP_301; |
| 3059 | break; |
| 3060 | case 302: |
| 3061 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3062 | msg_fmt = HTTP_302; |
| 3063 | break; |
| 3064 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3065 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3066 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3067 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3068 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3069 | switch(rule->type) { |
| 3070 | case REDIRECT_TYPE_PREFIX: { |
| 3071 | const char *path; |
| 3072 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3073 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3074 | path = http_get_path(txn); |
| 3075 | /* build message using path */ |
| 3076 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3077 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3078 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3079 | int qs = 0; |
| 3080 | while (qs < pathlen) { |
| 3081 | if (path[qs] == '?') { |
| 3082 | pathlen = qs; |
| 3083 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3084 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3085 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3086 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3087 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3088 | } else { |
| 3089 | path = "/"; |
| 3090 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3091 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3092 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3093 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3094 | goto return_bad_req; |
| 3095 | |
| 3096 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3097 | * add anything, otherwise it makes it hard for the user to |
| 3098 | * configure a self-redirection. |
| 3099 | */ |
| 3100 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3101 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3102 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3103 | } |
| 3104 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3105 | /* add path */ |
| 3106 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3107 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3108 | |
| 3109 | /* append a slash at the end of the location is needed and missing */ |
| 3110 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3111 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3112 | if (rdr.len > rdr.size - 5) |
| 3113 | goto return_bad_req; |
| 3114 | rdr.str[rdr.len] = '/'; |
| 3115 | rdr.len++; |
| 3116 | } |
| 3117 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3118 | break; |
| 3119 | } |
| 3120 | case REDIRECT_TYPE_LOCATION: |
| 3121 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3122 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3123 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3124 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3125 | /* add location */ |
| 3126 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3127 | rdr.len += rule->rdr_len; |
| 3128 | break; |
| 3129 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3130 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3131 | if (rule->cookie_len) { |
| 3132 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3133 | rdr.len += 14; |
| 3134 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3135 | rdr.len += rule->cookie_len; |
| 3136 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3137 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3138 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3139 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3140 | /* add end of headers and the keep-alive/close status. |
| 3141 | * We may choose to set keep-alive if the Location begins |
| 3142 | * with a slash, because the client will come back to the |
| 3143 | * same server. |
| 3144 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3145 | txn->status = rule->code; |
| 3146 | /* let's log the request time */ |
| 3147 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3148 | |
| 3149 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3150 | (txn->flags & TX_REQ_XFER_LEN) && |
| 3151 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 3152 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3153 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3154 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3155 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3156 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3157 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3158 | rdr.len += 30; |
| 3159 | } else { |
| 3160 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3161 | rdr.len += 24; |
| 3162 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3163 | } |
| 3164 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3165 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3166 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3167 | /* "eat" the request */ |
| 3168 | buffer_ignore(req, msg->sov - msg->som); |
| 3169 | msg->som = msg->sov; |
| 3170 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3171 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3172 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3173 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3174 | break; |
| 3175 | } else { |
| 3176 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3177 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3178 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3179 | rdr.len += 29; |
| 3180 | } else { |
| 3181 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3182 | rdr.len += 23; |
| 3183 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3184 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3185 | goto return_prx_cond; |
| 3186 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3187 | } |
| 3188 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3189 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3190 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3191 | * If this happens, then the data will not come immediately, so we must |
| 3192 | * send all what we have without waiting. Note that due to the small gain |
| 3193 | * in waiting for the body of the request, it's easier to simply put the |
| 3194 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3195 | * itself once used. |
| 3196 | */ |
| 3197 | req->flags |= BF_SEND_DONTWAIT; |
| 3198 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3199 | /* that's OK for us now, let's move on to next analysers */ |
| 3200 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3201 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3202 | return_bad_req: |
| 3203 | /* We centralize bad requests processing here */ |
| 3204 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3205 | /* we detected a parsing error. We want to archive this request |
| 3206 | * in the dedicated proxy area for later troubleshooting. |
| 3207 | */ |
| 3208 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 3209 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3210 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3211 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3212 | txn->status = 400; |
| 3213 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3214 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3215 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3216 | if (s->listener->counters) |
| 3217 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3218 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3219 | return_prx_cond: |
| 3220 | if (!(s->flags & SN_ERR_MASK)) |
| 3221 | s->flags |= SN_ERR_PRXCOND; |
| 3222 | if (!(s->flags & SN_FINST_MASK)) |
| 3223 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3224 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3225 | req->analysers = 0; |
| 3226 | req->analyse_exp = TICK_ETERNITY; |
| 3227 | return 0; |
| 3228 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3229 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3230 | /* This function performs all the processing enabled for the current request. |
| 3231 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3232 | * needs more data, encounters an error, or wants to immediately abort the |
| 3233 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3234 | */ |
| 3235 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3236 | { |
| 3237 | struct http_txn *txn = &s->txn; |
| 3238 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3239 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3240 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3241 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3242 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3243 | return 0; |
| 3244 | } |
| 3245 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3246 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3247 | now_ms, __FUNCTION__, |
| 3248 | s, |
| 3249 | req, |
| 3250 | req->rex, req->wex, |
| 3251 | req->flags, |
| 3252 | req->l, |
| 3253 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3254 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3255 | /* |
| 3256 | * Right now, we know that we have processed the entire headers |
| 3257 | * and that unwanted requests have been filtered out. We can do |
| 3258 | * whatever we want with the remaining request. Also, now we |
| 3259 | * may have separate values for ->fe, ->be. |
| 3260 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3261 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3262 | /* |
| 3263 | * If HTTP PROXY is set we simply get remote server address |
| 3264 | * parsing incoming request. |
| 3265 | */ |
| 3266 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3267 | url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3268 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3269 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3270 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3271 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3272 | * Note that doing so might move headers in the request, but |
| 3273 | * the fields will stay coherent and the URI will not move. |
| 3274 | * This should only be performed in the backend. |
| 3275 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3276 | 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] | 3277 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3278 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3279 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3280 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3281 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3282 | * so let's do the same now. |
| 3283 | */ |
| 3284 | |
| 3285 | /* It needs to look into the URI */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 3286 | if ((txn->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3287 | get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3291 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3292 | * asks for it. |
| 3293 | */ |
| 3294 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 3295 | if (s->cli_addr.ss_family == AF_INET) { |
| 3296 | /* Add an X-Forwarded-For header unless the source IP is |
| 3297 | * in the 'except' network range. |
| 3298 | */ |
| 3299 | if ((!s->fe->except_mask.s_addr || |
| 3300 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 3301 | != s->fe->except_net.s_addr) && |
| 3302 | (!s->be->except_mask.s_addr || |
| 3303 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 3304 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3305 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3306 | unsigned char *pn; |
| 3307 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3308 | |
| 3309 | /* Note: we rely on the backend to get the header name to be used for |
| 3310 | * x-forwarded-for, because the header is really meant for the backends. |
| 3311 | * However, if the backend did not specify any option, we have to rely |
| 3312 | * on the frontend's header name. |
| 3313 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3314 | if (s->be->fwdfor_hdr_len) { |
| 3315 | len = s->be->fwdfor_hdr_len; |
| 3316 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3317 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3318 | len = s->fe->fwdfor_hdr_len; |
| 3319 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3320 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3321 | 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] | 3322 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3323 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3324 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3325 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3326 | } |
| 3327 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3328 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3329 | /* FIXME: for the sake of completeness, we should also support |
| 3330 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3331 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3332 | int len; |
| 3333 | char pn[INET6_ADDRSTRLEN]; |
| 3334 | inet_ntop(AF_INET6, |
| 3335 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3336 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3337 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3338 | /* Note: we rely on the backend to get the header name to be used for |
| 3339 | * x-forwarded-for, because the header is really meant for the backends. |
| 3340 | * However, if the backend did not specify any option, we have to rely |
| 3341 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3342 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3343 | if (s->be->fwdfor_hdr_len) { |
| 3344 | len = s->be->fwdfor_hdr_len; |
| 3345 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3346 | } else { |
| 3347 | len = s->fe->fwdfor_hdr_len; |
| 3348 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3349 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3350 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3351 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3352 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3353 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3354 | goto return_bad_req; |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3359 | * 10: add X-Original-To if either the frontend or the backend |
| 3360 | * asks for it. |
| 3361 | */ |
| 3362 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3363 | |
| 3364 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3365 | if (s->cli_addr.ss_family == AF_INET) { |
| 3366 | /* Add an X-Original-To header unless the destination IP is |
| 3367 | * in the 'except' network range. |
| 3368 | */ |
| 3369 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3370 | get_frt_addr(s); |
| 3371 | |
| 3372 | if ((!s->fe->except_mask_to.s_addr || |
| 3373 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3374 | != s->fe->except_to.s_addr) && |
| 3375 | (!s->be->except_mask_to.s_addr || |
| 3376 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3377 | != s->be->except_to.s_addr)) { |
| 3378 | int len; |
| 3379 | unsigned char *pn; |
| 3380 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3381 | |
| 3382 | /* Note: we rely on the backend to get the header name to be used for |
| 3383 | * x-original-to, because the header is really meant for the backends. |
| 3384 | * However, if the backend did not specify any option, we have to rely |
| 3385 | * on the frontend's header name. |
| 3386 | */ |
| 3387 | if (s->be->orgto_hdr_len) { |
| 3388 | len = s->be->orgto_hdr_len; |
| 3389 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3390 | } else { |
| 3391 | len = s->fe->orgto_hdr_len; |
| 3392 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3393 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3394 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3395 | |
| 3396 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3397 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3398 | goto return_bad_req; |
| 3399 | } |
| 3400 | } |
| 3401 | } |
| 3402 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3403 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3404 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3405 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
| 3406 | unsigned int want_flags = 0; |
| 3407 | |
| 3408 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame^] | 3409 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3410 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) || |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3411 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) |
| 3412 | want_flags |= TX_CON_CLO_SET; |
| 3413 | } else { |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame^] | 3414 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3415 | (((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA) && |
| 3416 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3417 | want_flags |= TX_CON_KAL_SET; |
| 3418 | } |
| 3419 | |
| 3420 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3421 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3422 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3423 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3424 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3425 | /* If we have no server assigned yet and we're balancing on url_param |
| 3426 | * with a POST request, we may be interested in checking the body for |
| 3427 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3428 | */ |
| 3429 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3430 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3431 | s->be->url_param_post_limit != 0 && |
| 3432 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3433 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3434 | buffer_dont_connect(req); |
| 3435 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3436 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3437 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3438 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3439 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3440 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3441 | /************************************************************* |
| 3442 | * OK, that's finished for the headers. We have done what we * |
| 3443 | * could. Let's switch to the DATA state. * |
| 3444 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3445 | req->analyse_exp = TICK_ETERNITY; |
| 3446 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3447 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3448 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3449 | /* OK let's go on with the BODY now */ |
| 3450 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3451 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3452 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3453 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3454 | /* we detected a parsing error. We want to archive this request |
| 3455 | * in the dedicated proxy area for later troubleshooting. |
| 3456 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3457 | 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] | 3458 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3459 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3460 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3461 | txn->status = 400; |
| 3462 | req->analysers = 0; |
| 3463 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3464 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3465 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3466 | if (s->listener->counters) |
| 3467 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3468 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3469 | if (!(s->flags & SN_ERR_MASK)) |
| 3470 | s->flags |= SN_ERR_PRXCOND; |
| 3471 | if (!(s->flags & SN_FINST_MASK)) |
| 3472 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3473 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3474 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3475 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3476 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3477 | * returns zero, at the beginning because it prevents any other processing |
| 3478 | * from occurring, and at the end because it terminates the request. |
| 3479 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3480 | 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] | 3481 | { |
| 3482 | struct http_txn *txn = &s->txn; |
| 3483 | |
| 3484 | /* This connection is being tarpitted. The CLIENT side has |
| 3485 | * already set the connect expiration date to the right |
| 3486 | * timeout. We just have to check that the client is still |
| 3487 | * there and that the timeout has not expired. |
| 3488 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3489 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3490 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3491 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3492 | return 0; |
| 3493 | |
| 3494 | /* We will set the queue timer to the time spent, just for |
| 3495 | * logging purposes. We fake a 500 server error, so that the |
| 3496 | * attacker will not suspect his connection has been tarpitted. |
| 3497 | * It will not cause trouble to the logs because we can exclude |
| 3498 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3499 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3500 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3501 | |
| 3502 | txn->status = 500; |
| 3503 | if (req->flags != BF_READ_ERROR) |
| 3504 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3505 | |
| 3506 | req->analysers = 0; |
| 3507 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3508 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3509 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3510 | if (s->listener->counters) |
| 3511 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3512 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3513 | if (!(s->flags & SN_ERR_MASK)) |
| 3514 | s->flags |= SN_ERR_PRXCOND; |
| 3515 | if (!(s->flags & SN_FINST_MASK)) |
| 3516 | s->flags |= SN_FINST_T; |
| 3517 | return 0; |
| 3518 | } |
| 3519 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3520 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3521 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3522 | * must only be called after the standard HTTP request processing has occurred, |
| 3523 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3524 | * read more data, or 1 once it has completed its analysis. |
| 3525 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3526 | 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] | 3527 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3528 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3529 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3530 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3531 | |
| 3532 | /* We have to parse the HTTP request body to find any required data. |
| 3533 | * "balance url_param check_post" should have been the only way to get |
| 3534 | * into this. We were brought here after HTTP header analysis, so all |
| 3535 | * related structures are ready. |
| 3536 | */ |
| 3537 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3538 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3539 | goto missing_data; |
| 3540 | |
| 3541 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3542 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3543 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3544 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3545 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3546 | struct hdr_ctx ctx; |
| 3547 | ctx.idx = 0; |
| 3548 | /* Expect is allowed in 1.1, look for it */ |
| 3549 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3550 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3551 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3552 | } |
| 3553 | } |
| 3554 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3555 | } |
| 3556 | |
| 3557 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3558 | /* we have msg->col and msg->sov which both point to the first |
| 3559 | * byte of message body. msg->som still points to the beginning |
| 3560 | * of the message. We must save the body in req->lr because it |
| 3561 | * survives buffer re-alignments. |
| 3562 | */ |
| 3563 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3564 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3565 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3566 | else |
| 3567 | msg->msg_state = HTTP_MSG_DATA; |
| 3568 | } |
| 3569 | |
| 3570 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3571 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3572 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3573 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3574 | */ |
| 3575 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3576 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3577 | if (!ret) |
| 3578 | goto missing_data; |
| 3579 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3580 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3581 | } |
| 3582 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3583 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3584 | * We have the first non-header byte in msg->col, which is either the |
| 3585 | * beginning of the chunk size or of the data. The first data byte is in |
| 3586 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3587 | * 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] | 3588 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3589 | |
| 3590 | if (msg->hdr_content_len < limit) |
| 3591 | limit = msg->hdr_content_len; |
| 3592 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3593 | 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] | 3594 | goto http_end; |
| 3595 | |
| 3596 | missing_data: |
| 3597 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3598 | if (req->flags & BF_FULL) |
| 3599 | goto return_bad_req; |
| 3600 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3601 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3602 | txn->status = 408; |
| 3603 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3604 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3605 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3606 | |
| 3607 | /* we get here if we need to wait for more data */ |
| 3608 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3609 | /* Not enough data. We'll re-use the http-request |
| 3610 | * timeout here. Ideally, we should set the timeout |
| 3611 | * relative to the accept() date. We just set the |
| 3612 | * request timeout once at the beginning of the |
| 3613 | * request. |
| 3614 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3615 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3616 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3617 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3618 | return 0; |
| 3619 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3620 | |
| 3621 | http_end: |
| 3622 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3623 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3624 | req->analysers &= ~an_bit; |
| 3625 | req->analyse_exp = TICK_ETERNITY; |
| 3626 | return 1; |
| 3627 | |
| 3628 | return_bad_req: /* let's centralize all bad requests */ |
| 3629 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3630 | txn->status = 400; |
| 3631 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3632 | |
| 3633 | return_err_msg: |
| 3634 | req->analysers = 0; |
| 3635 | s->fe->counters.failed_req++; |
| 3636 | if (s->listener->counters) |
| 3637 | s->listener->counters->failed_req++; |
| 3638 | |
| 3639 | if (!(s->flags & SN_ERR_MASK)) |
| 3640 | s->flags |= SN_ERR_PRXCOND; |
| 3641 | if (!(s->flags & SN_FINST_MASK)) |
| 3642 | s->flags |= SN_FINST_R; |
| 3643 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3644 | } |
| 3645 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3646 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3647 | * right now but it works. |
| 3648 | */ |
| 3649 | void http_end_txn_clean_session(struct session *s) |
| 3650 | { |
| 3651 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3652 | * server's connections. We need a safer way to reinitialize buffer |
| 3653 | * flags. We also need a more accurate method for computing per-request |
| 3654 | * data. |
| 3655 | */ |
| 3656 | http_silent_debug(__LINE__, s); |
| 3657 | |
| 3658 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3659 | s->req->cons->shutr(s->req->cons); |
| 3660 | s->req->cons->shutw(s->req->cons); |
| 3661 | |
| 3662 | http_silent_debug(__LINE__, s); |
| 3663 | |
| 3664 | if (s->flags & SN_BE_ASSIGNED) |
| 3665 | s->be->beconn--; |
| 3666 | |
| 3667 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3668 | session_process_counters(s); |
| 3669 | |
| 3670 | if (s->txn.status) { |
| 3671 | int n; |
| 3672 | |
| 3673 | n = s->txn.status / 100; |
| 3674 | if (n < 1 || n > 5) |
| 3675 | n = 0; |
| 3676 | |
| 3677 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3678 | s->fe->counters.fe.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3679 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3680 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3681 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3682 | s->be->counters.be.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | /* don't count other requests' data */ |
| 3686 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3687 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3688 | |
| 3689 | /* let's do a final log if we need it */ |
| 3690 | if (s->logs.logwait && |
| 3691 | !(s->flags & SN_MONITOR) && |
| 3692 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3693 | s->do_log(s); |
| 3694 | } |
| 3695 | |
| 3696 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3697 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3698 | tv_zero(&s->logs.tv_request); |
| 3699 | s->logs.t_queue = -1; |
| 3700 | s->logs.t_connect = -1; |
| 3701 | s->logs.t_data = -1; |
| 3702 | s->logs.t_close = 0; |
| 3703 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3704 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3705 | |
| 3706 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3707 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3708 | |
| 3709 | if (s->pend_pos) |
| 3710 | pendconn_free(s->pend_pos); |
| 3711 | |
| 3712 | if (s->srv) { |
| 3713 | if (s->flags & SN_CURR_SESS) { |
| 3714 | s->flags &= ~SN_CURR_SESS; |
| 3715 | s->srv->cur_sess--; |
| 3716 | } |
| 3717 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3718 | process_srv_queue(s->srv); |
| 3719 | } |
| 3720 | |
| 3721 | if (unlikely(s->srv_conn)) |
| 3722 | sess_change_server(s, NULL); |
| 3723 | s->srv = NULL; |
| 3724 | |
| 3725 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3726 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3727 | s->req->cons->err_type = SI_ET_NONE; |
| 3728 | s->req->cons->err_loc = NULL; |
| 3729 | s->req->cons->exp = TICK_ETERNITY; |
| 3730 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3731 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3732 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL); |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 3733 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3734 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3735 | s->txn.meth = 0; |
| 3736 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3737 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3738 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3739 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3740 | |
| 3741 | /* if the request buffer is not empty, it means we're |
| 3742 | * about to process another request, so send pending |
| 3743 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3744 | * Just don't do this if the buffer is close to be full, |
| 3745 | * because the request will wait for it to flush a little |
| 3746 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3747 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3748 | if (s->req->l > s->req->send_max) { |
| 3749 | if (s->rep->send_max && |
| 3750 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3751 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3752 | s->rep->flags |= BF_EXPECT_MORE; |
| 3753 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3754 | |
| 3755 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3756 | buffer_auto_read(s->req); |
| 3757 | buffer_auto_close(s->req); |
| 3758 | buffer_auto_read(s->rep); |
| 3759 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3760 | |
| 3761 | /* make ->lr point to the first non-forwarded byte */ |
| 3762 | s->req->lr = s->req->w + s->req->send_max; |
| 3763 | if (s->req->lr >= s->req->data + s->req->size) |
| 3764 | s->req->lr -= s->req->size; |
| 3765 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3766 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3767 | s->rep->lr -= s->req->size; |
| 3768 | |
| 3769 | s->req->analysers |= s->fe->fe_req_ana; |
| 3770 | s->rep->analysers = 0; |
| 3771 | |
| 3772 | http_silent_debug(__LINE__, s); |
| 3773 | } |
| 3774 | |
| 3775 | |
| 3776 | /* This function updates the request state machine according to the response |
| 3777 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3778 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3779 | * it is only used to find when a request/response couple is complete. Both |
| 3780 | * this function and its equivalent should loop until both return zero. It |
| 3781 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3782 | */ |
| 3783 | int http_sync_req_state(struct session *s) |
| 3784 | { |
| 3785 | struct buffer *buf = s->req; |
| 3786 | struct http_txn *txn = &s->txn; |
| 3787 | unsigned int old_flags = buf->flags; |
| 3788 | unsigned int old_state = txn->req.msg_state; |
| 3789 | |
| 3790 | http_silent_debug(__LINE__, s); |
| 3791 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3792 | return 0; |
| 3793 | |
| 3794 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3795 | /* No need to read anymore, the request was completely parsed. |
| 3796 | * We can shut the read side unless we want to abort_on_close. |
| 3797 | */ |
| 3798 | if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)) |
| 3799 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3800 | |
| 3801 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3802 | goto wait_other_side; |
| 3803 | |
| 3804 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3805 | /* The server has not finished to respond, so we |
| 3806 | * don't want to move in order not to upset it. |
| 3807 | */ |
| 3808 | goto wait_other_side; |
| 3809 | } |
| 3810 | |
| 3811 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3812 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3813 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3814 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3815 | goto wait_other_side; |
| 3816 | } |
| 3817 | |
| 3818 | /* When we get here, it means that both the request and the |
| 3819 | * response have finished receiving. Depending on the connection |
| 3820 | * mode, we'll have to wait for the last bytes to leave in either |
| 3821 | * direction, and sometimes for a close to be effective. |
| 3822 | */ |
| 3823 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3824 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3825 | /* Server-close mode : queue a connection close to the server */ |
| 3826 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3827 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3828 | } |
| 3829 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3830 | /* Option forceclose is set, or either side wants to close, |
| 3831 | * let's enforce it now that we're not expecting any new |
| 3832 | * data to come. The caller knows the session is complete |
| 3833 | * once both states are CLOSED. |
| 3834 | */ |
| 3835 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3836 | buffer_shutr_now(buf); |
| 3837 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3838 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3839 | } |
| 3840 | else { |
| 3841 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3842 | * mode does not set the body analyser, we can't reach this place |
| 3843 | * in tunnel mode, so we're left with keep-alive only. |
| 3844 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3845 | */ |
| 3846 | buffer_auto_read(buf); |
| 3847 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3851 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3852 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3853 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3854 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3855 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3856 | goto http_msg_closing; |
| 3857 | } |
| 3858 | else { |
| 3859 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3860 | goto http_msg_closed; |
| 3861 | } |
| 3862 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3863 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3867 | http_msg_closing: |
| 3868 | /* nothing else to forward, just waiting for the output buffer |
| 3869 | * to be empty and for the shutw_now to take effect. |
| 3870 | */ |
| 3871 | if (buf->flags & BF_OUT_EMPTY) { |
| 3872 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3873 | goto http_msg_closed; |
| 3874 | } |
| 3875 | else if (buf->flags & BF_SHUTW) { |
| 3876 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3877 | goto wait_other_side; |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3882 | http_msg_closed: |
| 3883 | goto wait_other_side; |
| 3884 | } |
| 3885 | |
| 3886 | wait_other_side: |
| 3887 | http_silent_debug(__LINE__, s); |
| 3888 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3889 | } |
| 3890 | |
| 3891 | |
| 3892 | /* This function updates the response state machine according to the request |
| 3893 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3894 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3895 | * it is only used to find when a request/response couple is complete. Both |
| 3896 | * this function and its equivalent should loop until both return zero. It |
| 3897 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3898 | */ |
| 3899 | int http_sync_res_state(struct session *s) |
| 3900 | { |
| 3901 | struct buffer *buf = s->rep; |
| 3902 | struct http_txn *txn = &s->txn; |
| 3903 | unsigned int old_flags = buf->flags; |
| 3904 | unsigned int old_state = txn->rsp.msg_state; |
| 3905 | |
| 3906 | http_silent_debug(__LINE__, s); |
| 3907 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3908 | return 0; |
| 3909 | |
| 3910 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3911 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3912 | * still monitor the server connection for a possible close |
| 3913 | * while the request is being uploaded, so we don't disable |
| 3914 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3915 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3916 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3917 | |
| 3918 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3919 | goto wait_other_side; |
| 3920 | |
| 3921 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3922 | /* The client seems to still be sending data, probably |
| 3923 | * because we got an error response during an upload. |
| 3924 | * We have the choice of either breaking the connection |
| 3925 | * or letting it pass through. Let's do the later. |
| 3926 | */ |
| 3927 | goto wait_other_side; |
| 3928 | } |
| 3929 | |
| 3930 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3931 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3932 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3933 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3934 | goto wait_other_side; |
| 3935 | } |
| 3936 | |
| 3937 | /* When we get here, it means that both the request and the |
| 3938 | * response have finished receiving. Depending on the connection |
| 3939 | * mode, we'll have to wait for the last bytes to leave in either |
| 3940 | * direction, and sometimes for a close to be effective. |
| 3941 | */ |
| 3942 | |
| 3943 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3944 | /* Server-close mode : shut read and wait for the request |
| 3945 | * side to close its output buffer. The caller will detect |
| 3946 | * when we're in DONE and the other is in CLOSED and will |
| 3947 | * catch that for the final cleanup. |
| 3948 | */ |
| 3949 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3950 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3951 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3952 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3953 | /* Option forceclose is set, or either side wants to close, |
| 3954 | * let's enforce it now that we're not expecting any new |
| 3955 | * data to come. The caller knows the session is complete |
| 3956 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3957 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3958 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3959 | buffer_shutr_now(buf); |
| 3960 | buffer_shutw_now(buf); |
| 3961 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3962 | } |
| 3963 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3964 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3965 | * mode does not set the body analyser, we can't reach this place |
| 3966 | * in tunnel mode, so we're left with keep-alive only. |
| 3967 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3968 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3969 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3970 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3974 | /* if we've just closed an output, let's switch */ |
| 3975 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3976 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3977 | goto http_msg_closing; |
| 3978 | } |
| 3979 | else { |
| 3980 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3981 | goto http_msg_closed; |
| 3982 | } |
| 3983 | } |
| 3984 | goto wait_other_side; |
| 3985 | } |
| 3986 | |
| 3987 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3988 | http_msg_closing: |
| 3989 | /* nothing else to forward, just waiting for the output buffer |
| 3990 | * to be empty and for the shutw_now to take effect. |
| 3991 | */ |
| 3992 | if (buf->flags & BF_OUT_EMPTY) { |
| 3993 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3994 | goto http_msg_closed; |
| 3995 | } |
| 3996 | else if (buf->flags & BF_SHUTW) { |
| 3997 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 3998 | s->be->counters.cli_aborts++; |
| 3999 | if (s->srv) |
| 4000 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4001 | goto wait_other_side; |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4006 | http_msg_closed: |
| 4007 | /* drop any pending data */ |
| 4008 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4009 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4010 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4011 | goto wait_other_side; |
| 4012 | } |
| 4013 | |
| 4014 | wait_other_side: |
| 4015 | http_silent_debug(__LINE__, s); |
| 4016 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4017 | } |
| 4018 | |
| 4019 | |
| 4020 | /* Resync the request and response state machines. Return 1 if either state |
| 4021 | * changes. |
| 4022 | */ |
| 4023 | int http_resync_states(struct session *s) |
| 4024 | { |
| 4025 | struct http_txn *txn = &s->txn; |
| 4026 | int old_req_state = txn->req.msg_state; |
| 4027 | int old_res_state = txn->rsp.msg_state; |
| 4028 | |
| 4029 | http_silent_debug(__LINE__, s); |
| 4030 | http_sync_req_state(s); |
| 4031 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4032 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | if (!http_sync_res_state(s)) |
| 4034 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4035 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4036 | if (!http_sync_req_state(s)) |
| 4037 | break; |
| 4038 | } |
| 4039 | http_silent_debug(__LINE__, s); |
| 4040 | /* OK, both state machines agree on a compatible state. |
| 4041 | * There are a few cases we're interested in : |
| 4042 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4043 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4044 | * directions, so let's simply disable both analysers. |
| 4045 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4046 | * request. |
| 4047 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4048 | * with server-close mode means we've completed one request and we |
| 4049 | * must re-initialize the server connection. |
| 4050 | */ |
| 4051 | |
| 4052 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4053 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4054 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4055 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4056 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4057 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4058 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4059 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4060 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4061 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4062 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4063 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4064 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
| 4065 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4066 | s->rep->analysers = 0; |
| 4067 | buffer_auto_close(s->rep); |
| 4068 | buffer_auto_read(s->rep); |
| 4069 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4070 | buffer_abort(s->req); |
| 4071 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4072 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4073 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4074 | } |
| 4075 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4076 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4077 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4078 | /* server-close: terminate this server connection and |
| 4079 | * reinitialize a fresh-new transaction. |
| 4080 | */ |
| 4081 | http_end_txn_clean_session(s); |
| 4082 | } |
| 4083 | |
| 4084 | http_silent_debug(__LINE__, s); |
| 4085 | return txn->req.msg_state != old_req_state || |
| 4086 | txn->rsp.msg_state != old_res_state; |
| 4087 | } |
| 4088 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4089 | /* This function is an analyser which forwards request body (including chunk |
| 4090 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4091 | * zero byte. The only situation where it must not be called is when we're in |
| 4092 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4093 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4094 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4095 | * read more data, or 1 once we can go on with next request or end the session. |
| 4096 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4097 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4098 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4099 | */ |
| 4100 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4101 | { |
| 4102 | struct http_txn *txn = &s->txn; |
| 4103 | struct http_msg *msg = &s->txn.req; |
| 4104 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4105 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4106 | return 0; |
| 4107 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4108 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4109 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
| 4110 | /* Output closed while we were sending data. We must abort. */ |
| 4111 | buffer_ignore(req, req->l - req->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4112 | buffer_auto_read(req); |
| 4113 | buffer_auto_close(req); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4114 | req->analysers &= ~an_bit; |
| 4115 | return 1; |
| 4116 | } |
| 4117 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4118 | buffer_dont_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4119 | |
| 4120 | /* Note that we don't have to send 100-continue back because we don't |
| 4121 | * need the data to complete our job, and it's up to the server to |
| 4122 | * decide whether to return 100, 417 or anything else in return of |
| 4123 | * an "Expect: 100-continue" header. |
| 4124 | */ |
| 4125 | |
| 4126 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4127 | /* we have msg->col and msg->sov which both point to the first |
| 4128 | * byte of message body. msg->som still points to the beginning |
| 4129 | * of the message. We must save the body in req->lr because it |
| 4130 | * survives buffer re-alignments. |
| 4131 | */ |
| 4132 | req->lr = req->data + msg->sov; |
| 4133 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4134 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4135 | else { |
| 4136 | msg->msg_state = HTTP_MSG_DATA; |
| 4137 | } |
| 4138 | } |
| 4139 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4140 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4141 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4142 | /* we may have some data pending */ |
| 4143 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4144 | int bytes = msg->sov - msg->som; |
| 4145 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4146 | bytes += req->size; |
| 4147 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 4148 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4149 | msg->som = msg->sov; |
| 4150 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4151 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4152 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4153 | /* must still forward */ |
| 4154 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4155 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4156 | |
| 4157 | /* nothing left to forward */ |
| 4158 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4159 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4160 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4161 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4162 | } |
| 4163 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4164 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4165 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4166 | * TRAILERS state. |
| 4167 | */ |
| 4168 | int ret = http_parse_chunk_size(req, msg); |
| 4169 | |
| 4170 | if (!ret) |
| 4171 | goto missing_data; |
| 4172 | else if (ret < 0) |
| 4173 | goto return_bad_req; |
| 4174 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4175 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4176 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4177 | /* we want the CRLF after the data */ |
| 4178 | int ret; |
| 4179 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4180 | req->lr = req->w + req->send_max; |
| 4181 | if (req->lr >= req->data + req->size) |
| 4182 | req->lr -= req->size; |
| 4183 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4184 | ret = http_skip_chunk_crlf(req, msg); |
| 4185 | |
| 4186 | if (ret == 0) |
| 4187 | goto missing_data; |
| 4188 | else if (ret < 0) |
| 4189 | goto return_bad_req; |
| 4190 | /* we're in MSG_CHUNK_SIZE now */ |
| 4191 | } |
| 4192 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4193 | int ret = http_forward_trailers(req, msg); |
| 4194 | |
| 4195 | if (ret == 0) |
| 4196 | goto missing_data; |
| 4197 | else if (ret < 0) |
| 4198 | goto return_bad_req; |
| 4199 | /* we're in HTTP_MSG_DONE now */ |
| 4200 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4201 | else { |
| 4202 | /* other states, DONE...TUNNEL */ |
| 4203 | if (http_resync_states(s)) { |
| 4204 | /* some state changes occurred, maybe the analyser |
| 4205 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4206 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4207 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4208 | goto return_bad_req; |
| 4209 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4210 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4211 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4212 | } |
| 4213 | } |
| 4214 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4215 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4216 | /* stop waiting for data if the input is closed before the end */ |
| 4217 | if (req->flags & BF_SHUTR) |
| 4218 | goto return_bad_req; |
| 4219 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4220 | /* waiting for the last bits to leave the buffer */ |
| 4221 | if (req->flags & BF_SHUTW) |
| 4222 | goto return_bad_req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4223 | |
| 4224 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4225 | return 0; |
| 4226 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4227 | return_bad_req: /* let's centralize all bad requests */ |
| 4228 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4229 | txn->status = 400; |
| 4230 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 4231 | stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4232 | req->analysers = 0; |
| 4233 | s->fe->counters.failed_req++; |
| 4234 | if (s->listener->counters) |
| 4235 | s->listener->counters->failed_req++; |
| 4236 | |
| 4237 | if (!(s->flags & SN_ERR_MASK)) |
| 4238 | s->flags |= SN_ERR_PRXCOND; |
| 4239 | if (!(s->flags & SN_FINST_MASK)) |
| 4240 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4241 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4242 | return 0; |
| 4243 | } |
| 4244 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4245 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4246 | * processing can continue on next analysers, or zero if it either needs more |
| 4247 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4248 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4249 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4250 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4251 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4252 | 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] | 4253 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4254 | struct http_txn *txn = &s->txn; |
| 4255 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4256 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4257 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4258 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4259 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4260 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4261 | 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] | 4262 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4263 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4264 | rep, |
| 4265 | rep->rex, rep->wex, |
| 4266 | rep->flags, |
| 4267 | rep->l, |
| 4268 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4269 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4270 | /* |
| 4271 | * Now parse the partial (or complete) lines. |
| 4272 | * We will check the response syntax, and also join multi-line |
| 4273 | * headers. An index of all the lines will be elaborated while |
| 4274 | * parsing. |
| 4275 | * |
| 4276 | * For the parsing, we use a 28 states FSM. |
| 4277 | * |
| 4278 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4279 | * rep->data + msg->som = beginning of response |
| 4280 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4281 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4282 | * rep->lr = first non-visited byte |
| 4283 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4284 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4285 | */ |
| 4286 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4287 | /* There's a protected area at the end of the buffer for rewriting |
| 4288 | * purposes. We don't want to start to parse the request if the |
| 4289 | * protected area is affected, because we may have to move processed |
| 4290 | * data later, which is much more complicated. |
| 4291 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4292 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4293 | if (unlikely((rep->flags & BF_FULL) || |
| 4294 | rep->r < rep->lr || |
| 4295 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4296 | if (rep->send_max) { |
| 4297 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4298 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4299 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4300 | buffer_dont_close(rep); |
| 4301 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4302 | return 0; |
| 4303 | } |
| 4304 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4305 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4306 | } |
| 4307 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4308 | if (likely(rep->lr < rep->r)) |
| 4309 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4310 | } |
| 4311 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4312 | /* 1: we might have to print this header in debug mode */ |
| 4313 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4314 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4315 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4316 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4317 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4318 | sol = msg->sol; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4319 | eol = sol + msg->sl.rq.l; |
| 4320 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4321 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4322 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4323 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4324 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4325 | while (cur_idx) { |
| 4326 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4327 | debug_hdr("srvhdr", s, sol, eol); |
| 4328 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4329 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4330 | } |
| 4331 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4332 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4333 | /* |
| 4334 | * Now we quickly check if we have found a full valid response. |
| 4335 | * If not so, we check the FD and buffer states before leaving. |
| 4336 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4337 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4338 | * responses are checked first. |
| 4339 | * |
| 4340 | * Depending on whether the client is still there or not, we |
| 4341 | * may send an error response back or not. Note that normally |
| 4342 | * we should only check for HTTP status there, and check I/O |
| 4343 | * errors somewhere else. |
| 4344 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4345 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4346 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4347 | /* Invalid response */ |
| 4348 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4349 | /* we detected a parsing error. We want to archive this response |
| 4350 | * in the dedicated proxy area for later troubleshooting. |
| 4351 | */ |
| 4352 | hdr_response_bad: |
| 4353 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4354 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4355 | |
| 4356 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4357 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4358 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4359 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4360 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4361 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4362 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4363 | rep->analysers = 0; |
| 4364 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4365 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4366 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4367 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4368 | |
| 4369 | if (!(s->flags & SN_ERR_MASK)) |
| 4370 | s->flags |= SN_ERR_PRXCOND; |
| 4371 | if (!(s->flags & SN_FINST_MASK)) |
| 4372 | s->flags |= SN_FINST_H; |
| 4373 | |
| 4374 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4375 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4376 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4377 | /* too large response does not fit in buffer. */ |
| 4378 | else if (rep->flags & BF_FULL) { |
| 4379 | goto hdr_response_bad; |
| 4380 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4381 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4382 | /* read error */ |
| 4383 | else if (rep->flags & BF_READ_ERROR) { |
| 4384 | if (msg->err_pos >= 0) |
| 4385 | 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] | 4386 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4387 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4388 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4389 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4390 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4391 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4392 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4393 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4394 | rep->analysers = 0; |
| 4395 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4396 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4397 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4398 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4399 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4400 | if (!(s->flags & SN_ERR_MASK)) |
| 4401 | s->flags |= SN_ERR_SRVCL; |
| 4402 | if (!(s->flags & SN_FINST_MASK)) |
| 4403 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4404 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4405 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4406 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4407 | /* read timeout : return a 504 to the client. */ |
| 4408 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4409 | if (msg->err_pos >= 0) |
| 4410 | 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] | 4411 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4412 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4413 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4414 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4415 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4416 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4417 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4418 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4419 | rep->analysers = 0; |
| 4420 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4421 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4422 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4423 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4424 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4425 | if (!(s->flags & SN_ERR_MASK)) |
| 4426 | s->flags |= SN_ERR_SRVTO; |
| 4427 | if (!(s->flags & SN_FINST_MASK)) |
| 4428 | s->flags |= SN_FINST_H; |
| 4429 | return 0; |
| 4430 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4431 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4432 | /* close from server */ |
| 4433 | else if (rep->flags & BF_SHUTR) { |
| 4434 | if (msg->err_pos >= 0) |
| 4435 | 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] | 4436 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4437 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4438 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4439 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4440 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4441 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4442 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4443 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4444 | rep->analysers = 0; |
| 4445 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4446 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4447 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4448 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4449 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4450 | if (!(s->flags & SN_ERR_MASK)) |
| 4451 | s->flags |= SN_ERR_SRVCL; |
| 4452 | if (!(s->flags & SN_FINST_MASK)) |
| 4453 | s->flags |= SN_FINST_H; |
| 4454 | return 0; |
| 4455 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4456 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4457 | /* write error to client (we don't send any message then) */ |
| 4458 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4459 | if (msg->err_pos >= 0) |
| 4460 | 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] | 4461 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4462 | s->be->counters.failed_resp++; |
| 4463 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4464 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4465 | |
| 4466 | if (!(s->flags & SN_ERR_MASK)) |
| 4467 | s->flags |= SN_ERR_CLICL; |
| 4468 | if (!(s->flags & SN_FINST_MASK)) |
| 4469 | s->flags |= SN_FINST_H; |
| 4470 | |
| 4471 | /* process_session() will take care of the error */ |
| 4472 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4473 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4474 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4475 | buffer_dont_close(rep); |
| 4476 | return 0; |
| 4477 | } |
| 4478 | |
| 4479 | /* More interesting part now : we know that we have a complete |
| 4480 | * response which at least looks like HTTP. We have an indicator |
| 4481 | * of each header's length, so we can parse them quickly. |
| 4482 | */ |
| 4483 | |
| 4484 | if (unlikely(msg->err_pos >= 0)) |
| 4485 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4486 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4487 | /* |
| 4488 | * 1: get the status code |
| 4489 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4490 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4491 | if (n < 1 || n > 5) |
| 4492 | n = 0; |
| 4493 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4494 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4495 | /* check if the response is HTTP/1.1 or above */ |
| 4496 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4497 | ((msg->sol[5] > '1') || |
| 4498 | ((msg->sol[5] == '1') && |
| 4499 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4500 | txn->flags |= TX_RES_VER_11; |
| 4501 | |
| 4502 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4503 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4504 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4505 | /* transfer length unknown*/ |
| 4506 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4507 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4508 | txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4509 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4510 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4511 | * and 505 are triggered on demand by client request, so we must not |
| 4512 | * count them as server failures. |
| 4513 | */ |
| 4514 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4515 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4516 | else |
| 4517 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4518 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4519 | /* |
| 4520 | * 2: check for cacheability. |
| 4521 | */ |
| 4522 | |
| 4523 | switch (txn->status) { |
| 4524 | case 200: |
| 4525 | case 203: |
| 4526 | case 206: |
| 4527 | case 300: |
| 4528 | case 301: |
| 4529 | case 410: |
| 4530 | /* RFC2616 @13.4: |
| 4531 | * "A response received with a status code of |
| 4532 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4533 | * by a cache (...) unless a cache-control |
| 4534 | * directive prohibits caching." |
| 4535 | * |
| 4536 | * RFC2616 @9.5: POST method : |
| 4537 | * "Responses to this method are not cacheable, |
| 4538 | * unless the response includes appropriate |
| 4539 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4540 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4541 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4542 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4543 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4544 | break; |
| 4545 | default: |
| 4546 | break; |
| 4547 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4548 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4549 | /* |
| 4550 | * 3: we may need to capture headers |
| 4551 | */ |
| 4552 | s->logs.logwait &= ~LW_RESP; |
| 4553 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4554 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4555 | txn->rsp.cap, s->fe->rsp_cap); |
| 4556 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4557 | /* 4: determine the transfer-length. |
| 4558 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4559 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4560 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4561 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4562 | * All responses to the HEAD request method MUST NOT include a |
| 4563 | * message-body, even though the presence of entity-header fields |
| 4564 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4565 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4566 | * message-body. All other responses do include a message-body, |
| 4567 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4568 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4569 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4570 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4571 | * always terminated by the first empty line after the header fields, |
| 4572 | * regardless of the entity-header fields present in the message. |
| 4573 | * |
| 4574 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4575 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4576 | * transfer-length is defined by the use of this transfer-coding. |
| 4577 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4578 | * transfer-coding is not present, the transfer-length is defined by |
| 4579 | * the sender closing the connection. |
| 4580 | * |
| 4581 | * 3. If a Content-Length header field is present, its decimal value in |
| 4582 | * OCTETs represents both the entity-length and the transfer-length. |
| 4583 | * If a message is received with both a Transfer-Encoding header |
| 4584 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4585 | * |
| 4586 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4587 | * the transfer-length is not otherwise specified, then this self- |
| 4588 | * delimiting media type defines the transfer-length. This media |
| 4589 | * type MUST NOT be used unless the sender knows that the recipient |
| 4590 | * can parse it; the presence in a request of a Range header with |
| 4591 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4592 | * client can parse multipart/byteranges responses. |
| 4593 | * |
| 4594 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4595 | */ |
| 4596 | |
| 4597 | /* Skip parsing if no content length is possible. The response flags |
| 4598 | * 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] | 4599 | * 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] | 4600 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4601 | */ |
| 4602 | if (txn->meth == HTTP_METH_HEAD || |
| 4603 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4604 | txn->status == 204 || txn->status == 304) { |
| 4605 | txn->flags |= TX_RES_XFER_LEN; |
| 4606 | goto skip_content_length; |
| 4607 | } |
| 4608 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4609 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4610 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4611 | while ((txn->flags & TX_RES_VER_11) && |
| 4612 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4613 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4614 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4615 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4616 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4617 | use_close_only = 1; |
| 4618 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4619 | break; |
| 4620 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4624 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4625 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4626 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4627 | signed long long cl; |
| 4628 | |
| 4629 | if (!ctx.vlen) |
| 4630 | goto hdr_response_bad; |
| 4631 | |
| 4632 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4633 | goto hdr_response_bad; /* parse failure */ |
| 4634 | |
| 4635 | if (cl < 0) |
| 4636 | goto hdr_response_bad; |
| 4637 | |
| 4638 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4639 | goto hdr_response_bad; /* already specified, was different */ |
| 4640 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4641 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4642 | msg->hdr_content_len = cl; |
| 4643 | } |
| 4644 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4645 | /* FIXME: we should also implement the multipart/byterange method. |
| 4646 | * For now on, we resort to close mode in this case (unknown length). |
| 4647 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4648 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4649 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4650 | /* end of job, return OK */ |
| 4651 | rep->analysers &= ~an_bit; |
| 4652 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4653 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4654 | return 1; |
| 4655 | } |
| 4656 | |
| 4657 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4658 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4659 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4660 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4661 | */ |
| 4662 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4663 | { |
| 4664 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4665 | struct http_msg *msg = &txn->rsp; |
| 4666 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4667 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4668 | |
| 4669 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4670 | now_ms, __FUNCTION__, |
| 4671 | t, |
| 4672 | rep, |
| 4673 | rep->rex, rep->wex, |
| 4674 | rep->flags, |
| 4675 | rep->l, |
| 4676 | rep->analysers); |
| 4677 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4678 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4679 | return 0; |
| 4680 | |
| 4681 | rep->analysers &= ~an_bit; |
| 4682 | rep->analyse_exp = TICK_ETERNITY; |
| 4683 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4684 | /* Now we have to check if we need to modify the Connection header. |
| 4685 | * This is more difficult on the response than it is on the request, |
| 4686 | * because we can have two different HTTP versions and we don't know |
| 4687 | * how the client will interprete a response. For instance, let's say |
| 4688 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4689 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4690 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4691 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4692 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4693 | * indicating how a request MAY be understood by the client. In case |
| 4694 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4695 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4696 | * will fall back to explicit close. Note that we won't take risks with |
| 4697 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4698 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4699 | */ |
| 4700 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4701 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4702 | txn->status == 101)) { |
| 4703 | /* Either we've established an explicit tunnel, or we're |
| 4704 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4705 | * to understand the next protocols. We have to switch to tunnel |
| 4706 | * mode, so that we transfer the request and responses then let |
| 4707 | * this protocol pass unmodified. When we later implement specific |
| 4708 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4709 | * header which contains information about that protocol for |
| 4710 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4711 | */ |
| 4712 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4713 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4714 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4715 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4716 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4717 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4718 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4719 | /* on unknown transfer length, we must close */ |
| 4720 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 4721 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4722 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4723 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4724 | /* now adjust header transformations depending on current state */ |
| 4725 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4726 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4727 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 4728 | if (!(txn->flags & TX_RES_VER_11)) |
| 4729 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4730 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4731 | else { /* SCL / KAL */ |
| 4732 | to_del |= 1; /* remove "close" on any response */ |
| 4733 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 4734 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4735 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4736 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4737 | /* Parse and remove some headers from the connection header */ |
| 4738 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4739 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4740 | /* Some keep-alive responses are converted to Server-close if |
| 4741 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4742 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4743 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4744 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 4745 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 4746 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4747 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4748 | } |
| 4749 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4750 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4751 | /* |
| 4752 | * 3: we will have to evaluate the filters. |
| 4753 | * As opposed to version 1.2, now they will be evaluated in the |
| 4754 | * filters order and not in the header order. This means that |
| 4755 | * each filter has to be validated among all headers. |
| 4756 | * |
| 4757 | * Filters are tried with ->be first, then with ->fe if it is |
| 4758 | * different from ->be. |
| 4759 | */ |
| 4760 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4761 | cur_proxy = t->be; |
| 4762 | while (1) { |
| 4763 | struct proxy *rule_set = cur_proxy; |
| 4764 | |
| 4765 | /* try headers filters */ |
| 4766 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4767 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4768 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4769 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4770 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4771 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4772 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4773 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4774 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4775 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4776 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4777 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4778 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4779 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4780 | if (!(t->flags & SN_ERR_MASK)) |
| 4781 | t->flags |= SN_ERR_PRXCOND; |
| 4782 | if (!(t->flags & SN_FINST_MASK)) |
| 4783 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4784 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4785 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4786 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4787 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4788 | /* has the response been denied ? */ |
| 4789 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4790 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4791 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4792 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4793 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4794 | if (t->listener->counters) |
| 4795 | t->listener->counters->denied_resp++; |
| 4796 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4797 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4798 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4799 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4800 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4801 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4802 | if (txn->status < 200) |
| 4803 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4804 | if (wl->cond) { |
| 4805 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 4806 | ret = acl_pass(ret); |
| 4807 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4808 | ret = !ret; |
| 4809 | if (!ret) |
| 4810 | continue; |
| 4811 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4812 | 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] | 4813 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4814 | } |
| 4815 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4816 | /* check whether we're already working on the frontend */ |
| 4817 | if (cur_proxy == t->fe) |
| 4818 | break; |
| 4819 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4820 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4821 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4822 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4823 | * We may be facing a 100-continue response, in which case this |
| 4824 | * is not the right response, and we're waiting for the next one. |
| 4825 | * Let's allow this response to go to the client and wait for the |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4826 | * next one. |
| 4827 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4828 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4829 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4830 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4831 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4832 | txn->status = 0; |
| 4833 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4834 | return 1; |
| 4835 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4836 | else if (unlikely(txn->status < 200)) |
| 4837 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4838 | |
| 4839 | /* we don't have any 1xx status code now */ |
| 4840 | |
| 4841 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4842 | * 4: check for server cookie. |
| 4843 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4844 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4845 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4846 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4847 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4848 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4849 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4850 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4851 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4852 | 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] | 4853 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4854 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4855 | /* |
| 4856 | * 6: add server cookie in the response if needed |
| 4857 | */ |
| 4858 | 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] | 4859 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4860 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4861 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4862 | /* the server is known, it's not the one the client requested, we have to |
| 4863 | * insert a set-cookie here, except if we want to insert only on POST |
| 4864 | * requests and this one isn't. Note that servers which don't have cookies |
| 4865 | * (eg: some backup servers) will return a full cookie removal request. |
| 4866 | */ |
| 4867 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4868 | t->be->cookie_name, |
| 4869 | 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] | 4870 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4871 | if (t->be->cookie_domain) |
| 4872 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4873 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4874 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4875 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4876 | goto return_bad_resp; |
| 4877 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4878 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4879 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4880 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4881 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4882 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4883 | */ |
| 4884 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4885 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4886 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4887 | |
| 4888 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4889 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4890 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4891 | } |
| 4892 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4893 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4894 | /* |
| 4895 | * 7: check if result will be cacheable with a cookie. |
| 4896 | * We'll block the response if security checks have caught |
| 4897 | * nasty things such as a cacheable cookie. |
| 4898 | */ |
| 4899 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4900 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4901 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4902 | |
| 4903 | /* we're in presence of a cacheable response containing |
| 4904 | * a set-cookie header. We'll block it as requested by |
| 4905 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4906 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4907 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4908 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4909 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4910 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4911 | if (t->listener->counters) |
| 4912 | t->listener->counters->denied_resp++; |
| 4913 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4914 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4915 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4916 | send_log(t->be, LOG_ALERT, |
| 4917 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4918 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4919 | goto return_srv_prx_502; |
| 4920 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4921 | |
| 4922 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4923 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4924 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4925 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 4926 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 4927 | unsigned int want_flags = 0; |
| 4928 | |
| 4929 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4930 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4931 | /* we want a keep-alive response here. Keep-alive header |
| 4932 | * required if either side is not 1.1. |
| 4933 | */ |
| 4934 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 4935 | want_flags |= TX_CON_KAL_SET; |
| 4936 | } |
| 4937 | else { |
| 4938 | /* we want a close response here. Close header required if |
| 4939 | * the server is 1.1, regardless of the client. |
| 4940 | */ |
| 4941 | if (txn->flags & TX_RES_VER_11) |
| 4942 | want_flags |= TX_CON_CLO_SET; |
| 4943 | } |
| 4944 | |
| 4945 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 4946 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4947 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4948 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4949 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4950 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 4951 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4952 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4953 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4954 | /************************************************************* |
| 4955 | * OK, that's finished for the headers. We have done what we * |
| 4956 | * could. Let's switch to the DATA state. * |
| 4957 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4958 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4959 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4960 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4961 | /* if the user wants to log as soon as possible, without counting |
| 4962 | * bytes from the server, then this is the right moment. We have |
| 4963 | * to temporarily assign bytes_out to log what we currently have. |
| 4964 | */ |
| 4965 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4966 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4967 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4968 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4969 | t->logs.bytes_out = 0; |
| 4970 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4971 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4972 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4973 | * otherwise we would not let the client side wake up. |
| 4974 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4975 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4976 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4977 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4978 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4979 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4980 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4981 | /* This function is an analyser which forwards response body (including chunk |
| 4982 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4983 | * zero byte. The only situation where it must not be called is when we're in |
| 4984 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4985 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4986 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4987 | * read more data, or 1 once we can go on with next request or end the session. |
| 4988 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4989 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4990 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4991 | */ |
| 4992 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4993 | { |
| 4994 | struct http_txn *txn = &s->txn; |
| 4995 | struct http_msg *msg = &s->txn.rsp; |
| 4996 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4997 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4998 | return 0; |
| 4999 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5000 | 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] | 5001 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5002 | !s->req->analysers) { |
| 5003 | /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */ |
| 5004 | buffer_ignore(res, res->l - res->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5005 | buffer_auto_read(res); |
| 5006 | buffer_auto_close(res); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5007 | res->analysers &= ~an_bit; |
| 5008 | return 1; |
| 5009 | } |
| 5010 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5011 | buffer_dont_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5012 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5013 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5014 | /* we have msg->col and msg->sov which both point to the first |
| 5015 | * byte of message body. msg->som still points to the beginning |
| 5016 | * of the message. We must save the body in req->lr because it |
| 5017 | * survives buffer re-alignments. |
| 5018 | */ |
| 5019 | res->lr = res->data + msg->sov; |
| 5020 | if (txn->flags & TX_RES_TE_CHNK) |
| 5021 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5022 | else { |
| 5023 | msg->msg_state = HTTP_MSG_DATA; |
| 5024 | } |
| 5025 | } |
| 5026 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5027 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5028 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5029 | /* we may have some data pending */ |
| 5030 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 5031 | int bytes = msg->sov - msg->som; |
| 5032 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 5033 | bytes += res->size; |
| 5034 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 5035 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 5036 | msg->som = msg->sov; |
| 5037 | } |
| 5038 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5039 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5040 | /* must still forward */ |
| 5041 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5042 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5043 | |
| 5044 | /* nothing left to forward */ |
| 5045 | if (txn->flags & TX_RES_TE_CHNK) |
| 5046 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5047 | else |
| 5048 | msg->msg_state = HTTP_MSG_DONE; |
| 5049 | } |
| 5050 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5051 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 5052 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5053 | */ |
| 5054 | int ret = http_parse_chunk_size(res, msg); |
| 5055 | |
| 5056 | if (!ret) |
| 5057 | goto missing_data; |
| 5058 | else if (ret < 0) |
| 5059 | goto return_bad_res; |
| 5060 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5061 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5062 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5063 | /* we want the CRLF after the data */ |
| 5064 | int ret; |
| 5065 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5066 | res->lr = res->w + res->send_max; |
| 5067 | if (res->lr >= res->data + res->size) |
| 5068 | res->lr -= res->size; |
| 5069 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5070 | ret = http_skip_chunk_crlf(res, msg); |
| 5071 | |
| 5072 | if (!ret) |
| 5073 | goto missing_data; |
| 5074 | else if (ret < 0) |
| 5075 | goto return_bad_res; |
| 5076 | /* we're in MSG_CHUNK_SIZE now */ |
| 5077 | } |
| 5078 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5079 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5080 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5081 | if (ret == 0) |
| 5082 | goto missing_data; |
| 5083 | else if (ret < 0) |
| 5084 | goto return_bad_res; |
| 5085 | /* we're in HTTP_MSG_DONE now */ |
| 5086 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5087 | else { |
| 5088 | /* other states, DONE...TUNNEL */ |
| 5089 | if (http_resync_states(s)) { |
| 5090 | http_silent_debug(__LINE__, s); |
| 5091 | /* some state changes occurred, maybe the analyser |
| 5092 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5093 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5094 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 5095 | goto return_bad_res; |
| 5096 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5097 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5098 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5102 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5103 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5104 | if (res->flags & BF_SHUTR) { |
| 5105 | if (!(s->flags & SN_ERR_MASK)) |
| 5106 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 5107 | s->be->counters.srv_aborts++; |
| 5108 | if (s->srv) |
| 5109 | s->srv->counters.srv_aborts++; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5110 | goto return_bad_res; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5111 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5112 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5113 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5114 | if (!s->req->analysers) |
| 5115 | goto return_bad_res; |
| 5116 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5117 | /* forward the chunk size as well as any pending data */ |
| 5118 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 5119 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 5120 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 5121 | msg->som = msg->sov; |
| 5122 | } |
| 5123 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5124 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5125 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5126 | return 0; |
| 5127 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5128 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5129 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5130 | /* don't send any error message as we're in the body */ |
| 5131 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5132 | res->analysers = 0; |
| 5133 | s->be->counters.failed_resp++; |
| 5134 | if (s->srv) { |
| 5135 | s->srv->counters.failed_resp++; |
| 5136 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 5137 | } |
| 5138 | |
| 5139 | if (!(s->flags & SN_ERR_MASK)) |
| 5140 | s->flags |= SN_ERR_PRXCOND; |
| 5141 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5142 | s->flags |= SN_FINST_D; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5143 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5144 | return 0; |
| 5145 | } |
| 5146 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5147 | /* Iterate the same filter through all request headers. |
| 5148 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5149 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5150 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5151 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5152 | 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] | 5153 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5154 | char term; |
| 5155 | char *cur_ptr, *cur_end, *cur_next; |
| 5156 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5157 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5158 | struct hdr_idx_elem *cur_hdr; |
| 5159 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5160 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5161 | last_hdr = 0; |
| 5162 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5163 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5164 | old_idx = 0; |
| 5165 | |
| 5166 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5167 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5168 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5169 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5170 | (exp->action == ACT_ALLOW || |
| 5171 | exp->action == ACT_DENY || |
| 5172 | exp->action == ACT_TARPIT)) |
| 5173 | return 0; |
| 5174 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5175 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5176 | if (!cur_idx) |
| 5177 | break; |
| 5178 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5179 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5180 | cur_ptr = cur_next; |
| 5181 | cur_end = cur_ptr + cur_hdr->len; |
| 5182 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5183 | |
| 5184 | /* Now we have one header between cur_ptr and cur_end, |
| 5185 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5186 | */ |
| 5187 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5188 | /* The annoying part is that pattern matching needs |
| 5189 | * that we modify the contents to null-terminate all |
| 5190 | * strings before testing them. |
| 5191 | */ |
| 5192 | |
| 5193 | term = *cur_end; |
| 5194 | *cur_end = '\0'; |
| 5195 | |
| 5196 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5197 | switch (exp->action) { |
| 5198 | case ACT_SETBE: |
| 5199 | /* It is not possible to jump a second time. |
| 5200 | * FIXME: should we return an HTTP/500 here so that |
| 5201 | * the admin knows there's a problem ? |
| 5202 | */ |
| 5203 | if (t->be != t->fe) |
| 5204 | break; |
| 5205 | |
| 5206 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5207 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5208 | last_hdr = 1; |
| 5209 | break; |
| 5210 | |
| 5211 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5212 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5213 | last_hdr = 1; |
| 5214 | break; |
| 5215 | |
| 5216 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5217 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5218 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5219 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5220 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5221 | if (t->listener->counters) |
| 5222 | t->listener->counters->denied_resp++; |
| 5223 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5224 | break; |
| 5225 | |
| 5226 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5227 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5228 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5229 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5230 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5231 | if (t->listener->counters) |
| 5232 | t->listener->counters->denied_resp++; |
| 5233 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5234 | break; |
| 5235 | |
| 5236 | case ACT_REPLACE: |
| 5237 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5238 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5239 | /* FIXME: if the user adds a newline in the replacement, the |
| 5240 | * index will not be recalculated for now, and the new line |
| 5241 | * will not be counted as a new header. |
| 5242 | */ |
| 5243 | |
| 5244 | cur_end += delta; |
| 5245 | cur_next += delta; |
| 5246 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5247 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5248 | break; |
| 5249 | |
| 5250 | case ACT_REMOVE: |
| 5251 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5252 | cur_next += delta; |
| 5253 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5254 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5255 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5256 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5257 | cur_hdr->len = 0; |
| 5258 | cur_end = NULL; /* null-term has been rewritten */ |
| 5259 | break; |
| 5260 | |
| 5261 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5262 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5263 | if (cur_end) |
| 5264 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5265 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5266 | /* keep the link from this header to next one in case of later |
| 5267 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5268 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5269 | old_idx = cur_idx; |
| 5270 | } |
| 5271 | return 0; |
| 5272 | } |
| 5273 | |
| 5274 | |
| 5275 | /* Apply the filter to the request line. |
| 5276 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5277 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5278 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5279 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5280 | */ |
| 5281 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5282 | { |
| 5283 | char term; |
| 5284 | char *cur_ptr, *cur_end; |
| 5285 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5286 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5287 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5288 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5289 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5290 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5291 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5292 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5293 | (exp->action == ACT_ALLOW || |
| 5294 | exp->action == ACT_DENY || |
| 5295 | exp->action == ACT_TARPIT)) |
| 5296 | return 0; |
| 5297 | else if (exp->action == ACT_REMOVE) |
| 5298 | return 0; |
| 5299 | |
| 5300 | done = 0; |
| 5301 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5302 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5303 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5304 | |
| 5305 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5306 | |
| 5307 | /* The annoying part is that pattern matching needs |
| 5308 | * that we modify the contents to null-terminate all |
| 5309 | * strings before testing them. |
| 5310 | */ |
| 5311 | |
| 5312 | term = *cur_end; |
| 5313 | *cur_end = '\0'; |
| 5314 | |
| 5315 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5316 | switch (exp->action) { |
| 5317 | case ACT_SETBE: |
| 5318 | /* It is not possible to jump a second time. |
| 5319 | * FIXME: should we return an HTTP/500 here so that |
| 5320 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5321 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5322 | if (t->be != t->fe) |
| 5323 | break; |
| 5324 | |
| 5325 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5326 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5327 | done = 1; |
| 5328 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5329 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5330 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5331 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5332 | done = 1; |
| 5333 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5334 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5335 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5336 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5337 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5338 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5339 | if (t->listener->counters) |
| 5340 | t->listener->counters->denied_resp++; |
| 5341 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5342 | done = 1; |
| 5343 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5344 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5345 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5346 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5347 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5348 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5349 | if (t->listener->counters) |
| 5350 | t->listener->counters->denied_resp++; |
| 5351 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5352 | done = 1; |
| 5353 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5354 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5355 | case ACT_REPLACE: |
| 5356 | *cur_end = term; /* restore the string terminator */ |
| 5357 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5358 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5359 | /* FIXME: if the user adds a newline in the replacement, the |
| 5360 | * index will not be recalculated for now, and the new line |
| 5361 | * will not be counted as a new header. |
| 5362 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5363 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5364 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5365 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5366 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5367 | HTTP_MSG_RQMETH, |
| 5368 | cur_ptr, cur_end + 1, |
| 5369 | NULL, NULL); |
| 5370 | if (unlikely(!cur_end)) |
| 5371 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5372 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5373 | /* we have a full request and we know that we have either a CR |
| 5374 | * or an LF at <ptr>. |
| 5375 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5376 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5377 | 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] | 5378 | /* there is no point trying this regex on headers */ |
| 5379 | return 1; |
| 5380 | } |
| 5381 | } |
| 5382 | *cur_end = term; /* restore the string terminator */ |
| 5383 | return done; |
| 5384 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5385 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5386 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5388 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5389 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5390 | * 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] | 5391 | * unparsable request. Since it can manage the switch to another backend, it |
| 5392 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5393 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5394 | int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5395 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5396 | struct http_txn *txn = &s->txn; |
| 5397 | struct hdr_exp *exp; |
| 5398 | |
| 5399 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5400 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5401 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5402 | /* |
| 5403 | * The interleaving of transformations and verdicts |
| 5404 | * makes it difficult to decide to continue or stop |
| 5405 | * the evaluation. |
| 5406 | */ |
| 5407 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5408 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5409 | break; |
| 5410 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5411 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5412 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5413 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5414 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5415 | |
| 5416 | /* if this filter had a condition, evaluate it now and skip to |
| 5417 | * next filter if the condition does not match. |
| 5418 | */ |
| 5419 | if (exp->cond) { |
| 5420 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5421 | ret = acl_pass(ret); |
| 5422 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5423 | ret = !ret; |
| 5424 | |
| 5425 | if (!ret) |
| 5426 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5430 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5431 | if (unlikely(ret < 0)) |
| 5432 | return -1; |
| 5433 | |
| 5434 | if (likely(ret == 0)) { |
| 5435 | /* The filter did not match the request, it can be |
| 5436 | * iterated through all headers. |
| 5437 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5438 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5439 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5440 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5441 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5445 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5446 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5447 | * Try to retrieve the server associated to the appsession. |
| 5448 | * If the server is found, it's assigned to the session. |
| 5449 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5450 | 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] | 5451 | struct http_txn *txn = &t->txn; |
| 5452 | appsess *asession = NULL; |
| 5453 | char *sessid_temp = NULL; |
| 5454 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5455 | if (len > t->be->appsession_len) { |
| 5456 | len = t->be->appsession_len; |
| 5457 | } |
| 5458 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5459 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5460 | /* request-learn option is enabled : store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5461 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5462 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5463 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5464 | } |
| 5465 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5466 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5467 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5468 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5469 | return; |
| 5470 | } |
| 5471 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5472 | memcpy(txn->sessid, buf, len); |
| 5473 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5477 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5478 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5479 | return; |
| 5480 | } |
| 5481 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5482 | memcpy(sessid_temp, buf, len); |
| 5483 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5484 | |
| 5485 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5486 | /* free previously allocated memory */ |
| 5487 | pool_free2(apools.sessid, sessid_temp); |
| 5488 | |
| 5489 | if (asession != NULL) { |
| 5490 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5491 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5492 | asession->request_count++; |
| 5493 | |
| 5494 | if (asession->serverid != NULL) { |
| 5495 | struct server *srv = t->be->srv; |
| 5496 | while (srv) { |
| 5497 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5498 | if ((srv->state & SRV_RUNNING) || |
| 5499 | (t->be->options & PR_O_PERSIST) || |
| 5500 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5501 | /* we found the server and it's usable */ |
| 5502 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5503 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5504 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5505 | t->srv = srv; |
| 5506 | break; |
| 5507 | } else { |
| 5508 | txn->flags &= ~TX_CK_MASK; |
| 5509 | txn->flags |= TX_CK_DOWN; |
| 5510 | } |
| 5511 | } |
| 5512 | srv = srv->next; |
| 5513 | } |
| 5514 | } |
| 5515 | } |
| 5516 | } |
| 5517 | |
| 5518 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5519 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5520 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5521 | */ |
| 5522 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5523 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5524 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5525 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5526 | char *del_colon, *del_cookie, *colon; |
| 5527 | int app_cookies; |
| 5528 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5529 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5530 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5531 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5532 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5533 | * we start with the start line. |
| 5534 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5535 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5536 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5537 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5538 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5539 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5540 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5541 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5542 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5543 | cur_ptr = cur_next; |
| 5544 | cur_end = cur_ptr + cur_hdr->len; |
| 5545 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5546 | |
| 5547 | /* We have one full header between cur_ptr and cur_end, and the |
| 5548 | * next header starts at cur_next. We're only interested in |
| 5549 | * "Cookie:" headers. |
| 5550 | */ |
| 5551 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5552 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5553 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5554 | old_idx = cur_idx; |
| 5555 | continue; |
| 5556 | } |
| 5557 | |
| 5558 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5559 | * attributes whose name begin with a '$', and associate them with |
| 5560 | * the right cookie, if we want to delete this cookie. |
| 5561 | * So there are 3 cases for each cookie read : |
| 5562 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5563 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5564 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5565 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5566 | * "special" cookie. |
| 5567 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5568 | * remove it. If no application cookie persists in the header, we |
| 5569 | * *MUST* delete it |
| 5570 | */ |
| 5571 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5572 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5573 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5574 | /* del_cookie == NULL => nothing to be deleted */ |
| 5575 | del_colon = del_cookie = NULL; |
| 5576 | app_cookies = 0; |
| 5577 | |
| 5578 | while (p1 < cur_end) { |
| 5579 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5580 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5581 | while (p1 < cur_end) { |
| 5582 | if (*p1 == ';' || *p1 == ',') |
| 5583 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5584 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5585 | break; |
| 5586 | p1++; |
| 5587 | } |
| 5588 | |
| 5589 | if (p1 == cur_end) |
| 5590 | break; |
| 5591 | |
| 5592 | /* p1 is at the beginning of the cookie name */ |
| 5593 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5594 | while (p2 < cur_end && *p2 != '=') { |
| 5595 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5596 | /* oops, the cookie name was truncated, resync */ |
| 5597 | p1 = p2; |
| 5598 | goto resync_name; |
| 5599 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5600 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5601 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5602 | |
| 5603 | if (p2 == cur_end) |
| 5604 | break; |
| 5605 | |
| 5606 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5607 | if (p3 == cur_end) |
| 5608 | break; |
| 5609 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5610 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5611 | p5 = p4 = p3; |
| 5612 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5613 | if (!isspace((unsigned char)*p5)) |
| 5614 | p4 = p5 + 1; |
| 5615 | p5++; |
| 5616 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5617 | |
| 5618 | /* here, we have the cookie name between p1 and p2, |
| 5619 | * and its value between p3 and p4. |
| 5620 | * we can process it : |
| 5621 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5622 | * Cookie: NAME=VALUE ; |
| 5623 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5624 | * | || || +--> p4 |
| 5625 | * | || |+-------> p3 |
| 5626 | * | || +--------> p2 |
| 5627 | * | |+------------> p1 |
| 5628 | * | +-------------> colon |
| 5629 | * +--------------------> cur_ptr |
| 5630 | */ |
| 5631 | |
| 5632 | if (*p1 == '$') { |
| 5633 | /* skip this one */ |
| 5634 | } |
| 5635 | else { |
| 5636 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5637 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5638 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5639 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5640 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5641 | int log_len = p4 - p1; |
| 5642 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5643 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5644 | Alert("HTTP logging : out of memory.\n"); |
| 5645 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5646 | if (log_len > t->fe->capture_len) |
| 5647 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5648 | memcpy(txn->cli_cookie, p1, log_len); |
| 5649 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5650 | } |
| 5651 | } |
| 5652 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5653 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5654 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5655 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5656 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5657 | char *delim; |
| 5658 | |
| 5659 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5660 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5661 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5662 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5663 | * Cookie: NAME=SRV~VALUE ; |
| 5664 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5665 | * | || || | +--> p4 |
| 5666 | * | || || +--------> delim |
| 5667 | * | || |+-----------> p3 |
| 5668 | * | || +------------> p2 |
| 5669 | * | |+----------------> p1 |
| 5670 | * | +-----------------> colon |
| 5671 | * +------------------------> cur_ptr |
| 5672 | */ |
| 5673 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5674 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5675 | for (delim = p3; delim < p4; delim++) |
| 5676 | if (*delim == COOKIE_DELIM) |
| 5677 | break; |
| 5678 | } |
| 5679 | else |
| 5680 | delim = p4; |
| 5681 | |
| 5682 | |
| 5683 | /* Here, we'll look for the first running server which supports the cookie. |
| 5684 | * This allows to share a same cookie between several servers, for example |
| 5685 | * to dedicate backup servers to specific servers only. |
| 5686 | * However, to prevent clients from sticking to cookie-less backup server |
| 5687 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5688 | * empty cookies and mark them as invalid. |
| 5689 | */ |
| 5690 | if (delim == p3) |
| 5691 | srv = NULL; |
| 5692 | |
| 5693 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5694 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5695 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5696 | if ((srv->state & SRV_RUNNING) || |
| 5697 | (t->be->options & PR_O_PERSIST) || |
| 5698 | (t->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5699 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5700 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5701 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5702 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5703 | t->srv = srv; |
| 5704 | break; |
| 5705 | } else { |
| 5706 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5707 | txn->flags &= ~TX_CK_MASK; |
| 5708 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5709 | } |
| 5710 | } |
| 5711 | srv = srv->next; |
| 5712 | } |
| 5713 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5714 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5715 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5716 | txn->flags &= ~TX_CK_MASK; |
| 5717 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | /* depending on the cookie mode, we may have to either : |
| 5721 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5722 | * the server never sees it ; |
| 5723 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5724 | * application cookie so that it does not get accidentely removed later, |
| 5725 | * if we're in cookie prefix mode |
| 5726 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5727 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5728 | int delta; /* negative */ |
| 5729 | |
| 5730 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5731 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5732 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5733 | cur_end += delta; |
| 5734 | cur_next += delta; |
| 5735 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5736 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5737 | |
| 5738 | del_cookie = del_colon = NULL; |
| 5739 | app_cookies++; /* protect the header from deletion */ |
| 5740 | } |
| 5741 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5742 | (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] | 5743 | del_cookie = p1; |
| 5744 | del_colon = colon; |
| 5745 | } |
| 5746 | } else { |
| 5747 | /* now we know that we must keep this cookie since it's |
| 5748 | * not ours. But if we wanted to delete our cookie |
| 5749 | * earlier, we cannot remove the complete header, but we |
| 5750 | * can remove the previous block itself. |
| 5751 | */ |
| 5752 | app_cookies++; |
| 5753 | |
| 5754 | if (del_cookie != NULL) { |
| 5755 | int delta; /* negative */ |
| 5756 | |
| 5757 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5758 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5759 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5760 | cur_end += delta; |
| 5761 | cur_next += delta; |
| 5762 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5763 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5764 | del_cookie = del_colon = NULL; |
| 5765 | } |
| 5766 | } |
| 5767 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5768 | if (t->be->appsession_name != NULL) { |
| 5769 | int cmp_len, value_len; |
| 5770 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5771 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5772 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5773 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5774 | value_begin = p1 + t->be->appsession_name_len; |
| 5775 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5776 | } else { |
| 5777 | cmp_len = p2 - p1; |
| 5778 | value_begin = p3; |
| 5779 | value_len = p4 - p3; |
| 5780 | } |
| 5781 | |
| 5782 | /* let's see if the cookie is our appcookie */ |
| 5783 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5784 | /* Cool... it's the right one */ |
| 5785 | manage_client_side_appsession(t, value_begin, value_len); |
| 5786 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5787 | #if defined(DEBUG_HASH) |
| 5788 | Alert("manage_client_side_cookies\n"); |
| 5789 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5790 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5791 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5792 | } |
| 5793 | |
| 5794 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5795 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5796 | } /* while (p1 < cur_end) */ |
| 5797 | |
| 5798 | /* There's no more cookie on this line. |
| 5799 | * We may have marked the last one(s) for deletion. |
| 5800 | * We must do this now in two ways : |
| 5801 | * - if there is no app cookie, we simply delete the header ; |
| 5802 | * - if there are app cookies, we must delete the end of the |
| 5803 | * string properly, including the colon/semi-colon before |
| 5804 | * the cookie name. |
| 5805 | */ |
| 5806 | if (del_cookie != NULL) { |
| 5807 | int delta; |
| 5808 | if (app_cookies) { |
| 5809 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5810 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5811 | cur_hdr->len += delta; |
| 5812 | } else { |
| 5813 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5816 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5817 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5818 | cur_hdr->len = 0; |
| 5819 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5820 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5821 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | /* keep the link from this header to next one */ |
| 5825 | old_idx = cur_idx; |
| 5826 | } /* end of cookie processing on this header */ |
| 5827 | } |
| 5828 | |
| 5829 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5830 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5831 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5832 | */ |
| 5833 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5834 | { |
| 5835 | char term; |
| 5836 | char *cur_ptr, *cur_end, *cur_next; |
| 5837 | int cur_idx, old_idx, last_hdr; |
| 5838 | struct http_txn *txn = &t->txn; |
| 5839 | struct hdr_idx_elem *cur_hdr; |
| 5840 | int len, delta; |
| 5841 | |
| 5842 | last_hdr = 0; |
| 5843 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5844 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5845 | old_idx = 0; |
| 5846 | |
| 5847 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5848 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5849 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5850 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5851 | (exp->action == ACT_ALLOW || |
| 5852 | exp->action == ACT_DENY)) |
| 5853 | return 0; |
| 5854 | |
| 5855 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5856 | if (!cur_idx) |
| 5857 | break; |
| 5858 | |
| 5859 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5860 | cur_ptr = cur_next; |
| 5861 | cur_end = cur_ptr + cur_hdr->len; |
| 5862 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5863 | |
| 5864 | /* Now we have one header between cur_ptr and cur_end, |
| 5865 | * and the next header starts at cur_next. |
| 5866 | */ |
| 5867 | |
| 5868 | /* The annoying part is that pattern matching needs |
| 5869 | * that we modify the contents to null-terminate all |
| 5870 | * strings before testing them. |
| 5871 | */ |
| 5872 | |
| 5873 | term = *cur_end; |
| 5874 | *cur_end = '\0'; |
| 5875 | |
| 5876 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5877 | switch (exp->action) { |
| 5878 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5879 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5880 | last_hdr = 1; |
| 5881 | break; |
| 5882 | |
| 5883 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5884 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5885 | last_hdr = 1; |
| 5886 | break; |
| 5887 | |
| 5888 | case ACT_REPLACE: |
| 5889 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5890 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5891 | /* FIXME: if the user adds a newline in the replacement, the |
| 5892 | * index will not be recalculated for now, and the new line |
| 5893 | * will not be counted as a new header. |
| 5894 | */ |
| 5895 | |
| 5896 | cur_end += delta; |
| 5897 | cur_next += delta; |
| 5898 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5899 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5900 | break; |
| 5901 | |
| 5902 | case ACT_REMOVE: |
| 5903 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5904 | cur_next += delta; |
| 5905 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5906 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5907 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5908 | txn->hdr_idx.used--; |
| 5909 | cur_hdr->len = 0; |
| 5910 | cur_end = NULL; /* null-term has been rewritten */ |
| 5911 | break; |
| 5912 | |
| 5913 | } |
| 5914 | } |
| 5915 | if (cur_end) |
| 5916 | *cur_end = term; /* restore the string terminator */ |
| 5917 | |
| 5918 | /* keep the link from this header to next one in case of later |
| 5919 | * removal of next header. |
| 5920 | */ |
| 5921 | old_idx = cur_idx; |
| 5922 | } |
| 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | |
| 5927 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5928 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5929 | * or -1 if a replacement resulted in an invalid status line. |
| 5930 | */ |
| 5931 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5932 | { |
| 5933 | char term; |
| 5934 | char *cur_ptr, *cur_end; |
| 5935 | int done; |
| 5936 | struct http_txn *txn = &t->txn; |
| 5937 | int len, delta; |
| 5938 | |
| 5939 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5940 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5941 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5942 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5943 | (exp->action == ACT_ALLOW || |
| 5944 | exp->action == ACT_DENY)) |
| 5945 | return 0; |
| 5946 | else if (exp->action == ACT_REMOVE) |
| 5947 | return 0; |
| 5948 | |
| 5949 | done = 0; |
| 5950 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5951 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5952 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5953 | |
| 5954 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5955 | |
| 5956 | /* The annoying part is that pattern matching needs |
| 5957 | * that we modify the contents to null-terminate all |
| 5958 | * strings before testing them. |
| 5959 | */ |
| 5960 | |
| 5961 | term = *cur_end; |
| 5962 | *cur_end = '\0'; |
| 5963 | |
| 5964 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5965 | switch (exp->action) { |
| 5966 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5967 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5968 | done = 1; |
| 5969 | break; |
| 5970 | |
| 5971 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5972 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5973 | done = 1; |
| 5974 | break; |
| 5975 | |
| 5976 | case ACT_REPLACE: |
| 5977 | *cur_end = term; /* restore the string terminator */ |
| 5978 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5979 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5980 | /* FIXME: if the user adds a newline in the replacement, the |
| 5981 | * index will not be recalculated for now, and the new line |
| 5982 | * will not be counted as a new header. |
| 5983 | */ |
| 5984 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5985 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5986 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5987 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5988 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5989 | cur_ptr, cur_end + 1, |
| 5990 | NULL, NULL); |
| 5991 | if (unlikely(!cur_end)) |
| 5992 | return -1; |
| 5993 | |
| 5994 | /* we have a full respnse and we know that we have either a CR |
| 5995 | * or an LF at <ptr>. |
| 5996 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5997 | txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5998 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5999 | /* there is no point trying this regex on headers */ |
| 6000 | return 1; |
| 6001 | } |
| 6002 | } |
| 6003 | *cur_end = term; /* restore the string terminator */ |
| 6004 | return done; |
| 6005 | } |
| 6006 | |
| 6007 | |
| 6008 | |
| 6009 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6010 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6011 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6012 | * unparsable response. |
| 6013 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6014 | int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6015 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6016 | struct http_txn *txn = &s->txn; |
| 6017 | struct hdr_exp *exp; |
| 6018 | |
| 6019 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6020 | int ret; |
| 6021 | |
| 6022 | /* |
| 6023 | * The interleaving of transformations and verdicts |
| 6024 | * makes it difficult to decide to continue or stop |
| 6025 | * the evaluation. |
| 6026 | */ |
| 6027 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6028 | if (txn->flags & TX_SVDENY) |
| 6029 | break; |
| 6030 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6031 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6032 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6033 | exp->action == ACT_PASS)) { |
| 6034 | exp = exp->next; |
| 6035 | continue; |
| 6036 | } |
| 6037 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6038 | /* if this filter had a condition, evaluate it now and skip to |
| 6039 | * next filter if the condition does not match. |
| 6040 | */ |
| 6041 | if (exp->cond) { |
| 6042 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6043 | ret = acl_pass(ret); |
| 6044 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6045 | ret = !ret; |
| 6046 | if (!ret) |
| 6047 | continue; |
| 6048 | } |
| 6049 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6050 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6051 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6052 | if (unlikely(ret < 0)) |
| 6053 | return -1; |
| 6054 | |
| 6055 | if (likely(ret == 0)) { |
| 6056 | /* The filter did not match the response, it can be |
| 6057 | * iterated through all headers. |
| 6058 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6059 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6060 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6061 | } |
| 6062 | return 0; |
| 6063 | } |
| 6064 | |
| 6065 | |
| 6066 | |
| 6067 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6068 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 6069 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6070 | */ |
| 6071 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 6072 | { |
| 6073 | struct http_txn *txn = &t->txn; |
| 6074 | char *p1, *p2, *p3, *p4; |
| 6075 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6076 | char *cur_ptr, *cur_end, *cur_next; |
| 6077 | int cur_idx, old_idx, delta; |
| 6078 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6079 | /* Iterate through the headers. |
| 6080 | * we start with the start line. |
| 6081 | */ |
| 6082 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6083 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6084 | |
| 6085 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6086 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6087 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6088 | |
| 6089 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6090 | cur_ptr = cur_next; |
| 6091 | cur_end = cur_ptr + cur_hdr->len; |
| 6092 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6093 | |
| 6094 | /* We have one full header between cur_ptr and cur_end, and the |
| 6095 | * next header starts at cur_next. We're only interested in |
| 6096 | * "Cookie:" headers. |
| 6097 | */ |
| 6098 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6099 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 6100 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6101 | old_idx = cur_idx; |
| 6102 | continue; |
| 6103 | } |
| 6104 | |
| 6105 | /* 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] | 6106 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6107 | |
| 6108 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6109 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 6110 | * the cookie capture is declared in the fronend. |
| 6111 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6112 | if (t->be->cookie_name == NULL && |
| 6113 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6114 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6115 | return; |
| 6116 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6117 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6118 | |
| 6119 | 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] | 6120 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 6121 | break; |
| 6122 | |
| 6123 | /* p1 is at the beginning of the cookie name */ |
| 6124 | p2 = p1; |
| 6125 | |
| 6126 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 6127 | p2++; |
| 6128 | |
| 6129 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 6130 | break; |
| 6131 | |
| 6132 | p3 = p2 + 1; /* skip the '=' sign */ |
| 6133 | if (p3 == cur_end) |
| 6134 | break; |
| 6135 | |
| 6136 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6137 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6138 | p4++; |
| 6139 | |
| 6140 | /* here, we have the cookie name between p1 and p2, |
| 6141 | * and its value between p3 and p4. |
| 6142 | * we can process it. |
| 6143 | */ |
| 6144 | |
| 6145 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6146 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6147 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6148 | (p4 - p1 >= t->fe->capture_namelen) && |
| 6149 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6150 | int log_len = p4 - p1; |
| 6151 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6152 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6153 | Alert("HTTP logging : out of memory.\n"); |
| 6154 | } |
| 6155 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6156 | if (log_len > t->fe->capture_len) |
| 6157 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6158 | memcpy(txn->srv_cookie, p1, log_len); |
| 6159 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6163 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6164 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6165 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6166 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6167 | |
| 6168 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6169 | * this occurrence because we'll insert another one later. |
| 6170 | * We'll delete it too if the "indirect" option is set and we're in |
| 6171 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6172 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 6173 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6174 | /* this header must be deleted */ |
| 6175 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6176 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6177 | txn->hdr_idx.used--; |
| 6178 | cur_hdr->len = 0; |
| 6179 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6180 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6181 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6182 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6183 | } |
| 6184 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6185 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6186 | /* replace bytes p3->p4 with the cookie name associated |
| 6187 | * with this server since we know it. |
| 6188 | */ |
| 6189 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 6190 | cur_hdr->len += delta; |
| 6191 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6192 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6193 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6194 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6195 | } |
| 6196 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6197 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6198 | /* insert the cookie name associated with this server |
| 6199 | * before existing cookie, and insert a delimitor between them.. |
| 6200 | */ |
| 6201 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 6202 | cur_hdr->len += delta; |
| 6203 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6204 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6205 | |
| 6206 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6207 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6208 | } |
| 6209 | } |
| 6210 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6211 | else if (t->be->appsession_name != NULL) { |
| 6212 | int cmp_len, value_len; |
| 6213 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6214 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6215 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6216 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 6217 | value_begin = p1 + t->be->appsession_name_len; |
| 6218 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 6219 | } else { |
| 6220 | cmp_len = p2 - p1; |
| 6221 | value_begin = p3; |
| 6222 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6223 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6224 | |
| 6225 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 6226 | /* Cool... it's the right one */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6227 | if (txn->sessid != NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6228 | /* free previously allocated memory as we don't need it anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6229 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6230 | } |
| 6231 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6232 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6233 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6234 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6235 | return; |
| 6236 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6237 | memcpy(txn->sessid, value_begin, value_len); |
| 6238 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6239 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6240 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6241 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 6242 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6243 | /* keep the link from this header to next one */ |
| 6244 | old_idx = cur_idx; |
| 6245 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6246 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6247 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6248 | appsess *asession = NULL; |
| 6249 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6250 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6251 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6252 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6253 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6254 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6255 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6256 | return; |
| 6257 | } |
| 6258 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6259 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6260 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 6261 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6262 | return; |
| 6263 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6264 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6265 | asession->sessid[t->be->appsession_len] = 0; |
| 6266 | |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6267 | server_id_len = strlen(t->srv->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6268 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 6269 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6270 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 6271 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6272 | return; |
| 6273 | } |
| 6274 | asession->serverid[0] = '\0'; |
| 6275 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 6276 | |
| 6277 | asession->request_count = 0; |
| 6278 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6279 | } |
| 6280 | |
| 6281 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6282 | asession->request_count++; |
| 6283 | } |
| 6284 | |
| 6285 | #if defined(DEBUG_HASH) |
| 6286 | Alert("manage_server_side_cookies\n"); |
| 6287 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 6288 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6289 | } |
| 6290 | |
| 6291 | |
| 6292 | |
| 6293 | /* |
| 6294 | * Check if response is cacheable or not. Updates t->flags. |
| 6295 | */ |
| 6296 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6297 | { |
| 6298 | struct http_txn *txn = &t->txn; |
| 6299 | char *p1, *p2; |
| 6300 | |
| 6301 | char *cur_ptr, *cur_end, *cur_next; |
| 6302 | int cur_idx; |
| 6303 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6304 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6305 | return; |
| 6306 | |
| 6307 | /* Iterate through the headers. |
| 6308 | * we start with the start line. |
| 6309 | */ |
| 6310 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6311 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6312 | |
| 6313 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6314 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6315 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6316 | |
| 6317 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6318 | cur_ptr = cur_next; |
| 6319 | cur_end = cur_ptr + cur_hdr->len; |
| 6320 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6321 | |
| 6322 | /* We have one full header between cur_ptr and cur_end, and the |
| 6323 | * next header starts at cur_next. We're only interested in |
| 6324 | * "Cookie:" headers. |
| 6325 | */ |
| 6326 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6327 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6328 | if (val) { |
| 6329 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6330 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6331 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6332 | return; |
| 6333 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6334 | } |
| 6335 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6336 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6337 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6338 | continue; |
| 6339 | |
| 6340 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6341 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6342 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6343 | |
| 6344 | if (p1 >= cur_end) /* no more info */ |
| 6345 | continue; |
| 6346 | |
| 6347 | /* p1 is at the beginning of the value */ |
| 6348 | p2 = p1; |
| 6349 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6350 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6351 | p2++; |
| 6352 | |
| 6353 | /* we have a complete value between p1 and p2 */ |
| 6354 | if (p2 < cur_end && *p2 == '=') { |
| 6355 | /* we have something of the form no-cache="set-cookie" */ |
| 6356 | if ((cur_end - p1 >= 21) && |
| 6357 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6358 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6359 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6360 | continue; |
| 6361 | } |
| 6362 | |
| 6363 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6364 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6365 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6366 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6367 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6368 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6369 | return; |
| 6370 | } |
| 6371 | |
| 6372 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6373 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6374 | continue; |
| 6375 | } |
| 6376 | } |
| 6377 | } |
| 6378 | |
| 6379 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6380 | /* |
| 6381 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6382 | * If the server is found, it's assigned to the session. |
| 6383 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6384 | 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] | 6385 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6386 | char *end_params, *first_param, *cur_param, *next_param; |
| 6387 | char separator; |
| 6388 | int value_len; |
| 6389 | |
| 6390 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6391 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6392 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6393 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6394 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6395 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6396 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6397 | first_param = NULL; |
| 6398 | switch (mode) { |
| 6399 | case PR_O2_AS_M_PP: |
| 6400 | first_param = memchr(begin, ';', len); |
| 6401 | break; |
| 6402 | case PR_O2_AS_M_QS: |
| 6403 | first_param = memchr(begin, '?', len); |
| 6404 | break; |
| 6405 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6406 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6407 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6408 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6409 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6410 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6411 | switch (mode) { |
| 6412 | case PR_O2_AS_M_PP: |
| 6413 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6414 | end_params = (char *) begin + len; |
| 6415 | } |
| 6416 | separator = ';'; |
| 6417 | break; |
| 6418 | case PR_O2_AS_M_QS: |
| 6419 | end_params = (char *) begin + len; |
| 6420 | separator = '&'; |
| 6421 | break; |
| 6422 | default: |
| 6423 | /* unknown mode, shouldn't happen */ |
| 6424 | return; |
| 6425 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6426 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6427 | cur_param = next_param = end_params; |
| 6428 | while (cur_param > first_param) { |
| 6429 | cur_param--; |
| 6430 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6431 | /* let's see if this is the appsession parameter */ |
| 6432 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6433 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6434 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6435 | /* Cool... it's the right one */ |
| 6436 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6437 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6438 | if (value_len > 0) { |
| 6439 | manage_client_side_appsession(t, cur_param, value_len); |
| 6440 | } |
| 6441 | break; |
| 6442 | } |
| 6443 | next_param = cur_param; |
| 6444 | } |
| 6445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6446 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6447 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6448 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6449 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6450 | } |
| 6451 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6452 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6453 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6454 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6455 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6456 | * It is assumed that the request is either a HEAD or GET and that the |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6457 | * t->be->uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6458 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6459 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6460 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6461 | int stats_check_uri(struct session *t, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6462 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6463 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6464 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6465 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6466 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6467 | if (!uri_auth) |
| 6468 | return 0; |
| 6469 | |
| 6470 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD) |
| 6471 | return 0; |
| 6472 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6473 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6475 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6476 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6477 | return 0; |
| 6478 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6479 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6480 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6481 | /* the URI is in h */ |
| 6482 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6483 | return 0; |
| 6484 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6485 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6486 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6487 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6488 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6489 | break; |
| 6490 | } |
| 6491 | h++; |
| 6492 | } |
| 6493 | |
| 6494 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6495 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6496 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6497 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6498 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6499 | break; |
| 6500 | } |
| 6501 | h++; |
| 6502 | } |
| 6503 | } |
| 6504 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6505 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6506 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6507 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6508 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6509 | break; |
| 6510 | } |
| 6511 | h++; |
| 6512 | } |
| 6513 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6514 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6515 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6516 | return 1; |
| 6517 | } |
| 6518 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6519 | /* |
| 6520 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6521 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 6522 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6523 | */ |
| 6524 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6525 | struct buffer *buf, struct http_msg *msg, |
| 6526 | struct proxy *other_end) |
| 6527 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6528 | es->len = buf->r - (buf->data + msg->som); |
| 6529 | 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] | 6530 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6531 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6532 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6533 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6534 | es->when = date; // user-visible date |
| 6535 | es->sid = s->uniq_id; |
| 6536 | es->srv = s->srv; |
| 6537 | es->oe = other_end; |
| 6538 | es->src = s->cli_addr; |
| 6539 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6540 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 6541 | /* return the IP address pointed to by occurrence <occ> of header <hname> in |
| 6542 | * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the |
| 6543 | * occurrence number corresponding to this value is returned. If <occ> is |
| 6544 | * strictly negative, the occurrence number before the end corresponding to |
| 6545 | * this value is returned. If <occ> is null, any value is returned, so it is |
| 6546 | * not recommended to use it that way. Negative occurrences are limited to |
| 6547 | * a small value because it is required to keep them in memory while scanning. |
| 6548 | * IP address 0.0.0.0 is returned if no match is found. |
| 6549 | */ |
| 6550 | unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ) |
| 6551 | { |
| 6552 | struct hdr_ctx ctx; |
| 6553 | unsigned int hdr_hist[MAX_HDR_HISTORY]; |
| 6554 | unsigned int hist_ptr; |
| 6555 | int found = 0; |
| 6556 | |
| 6557 | ctx.idx = 0; |
| 6558 | if (occ >= 0) { |
| 6559 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 6560 | occ--; |
| 6561 | if (occ <= 0) { |
| 6562 | found = 1; |
| 6563 | break; |
| 6564 | } |
| 6565 | } |
| 6566 | if (!found) |
| 6567 | return 0; |
| 6568 | return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 6569 | } |
| 6570 | |
| 6571 | /* negative occurrence, we scan all the list then walk back */ |
| 6572 | if (-occ > MAX_HDR_HISTORY) |
| 6573 | return 0; |
| 6574 | |
| 6575 | hist_ptr = 0; |
| 6576 | hdr_hist[hist_ptr] = 0; |
| 6577 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 6578 | hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 6579 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 6580 | hist_ptr = 0; |
| 6581 | found++; |
| 6582 | } |
| 6583 | if (-occ > found) |
| 6584 | return 0; |
| 6585 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 6586 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 6587 | */ |
| 6588 | hist_ptr += occ; |
| 6589 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 6590 | hist_ptr -= MAX_HDR_HISTORY; |
| 6591 | return hdr_hist[hist_ptr]; |
| 6592 | } |
| 6593 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6594 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6595 | * Print a debug line with a header |
| 6596 | */ |
| 6597 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6598 | { |
| 6599 | int len, max; |
| 6600 | 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] | 6601 | 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] | 6602 | max = end - start; |
| 6603 | UBOUND(max, sizeof(trash) - len - 1); |
| 6604 | len += strlcpy2(trash + len, start, max + 1); |
| 6605 | trash[len++] = '\n'; |
| 6606 | write(1, trash, len); |
| 6607 | } |
| 6608 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6609 | /* |
| 6610 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6611 | * the required fields are properly allocated and that we only need to (re)init |
| 6612 | * them. This should be used before processing any new request. |
| 6613 | */ |
| 6614 | void http_init_txn(struct session *s) |
| 6615 | { |
| 6616 | struct http_txn *txn = &s->txn; |
| 6617 | struct proxy *fe = s->fe; |
| 6618 | |
| 6619 | txn->flags = 0; |
| 6620 | txn->status = -1; |
| 6621 | |
| 6622 | txn->req.sol = txn->req.eol = NULL; |
| 6623 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6624 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6625 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6626 | txn->req.hdr_content_len = 0LL; |
| 6627 | txn->rsp.hdr_content_len = 0LL; |
| 6628 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6629 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 6630 | |
| 6631 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6632 | |
| 6633 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6634 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6635 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6636 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6637 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6638 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6639 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6640 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6641 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6642 | |
| 6643 | if (txn->hdr_idx.v) |
| 6644 | hdr_idx_init(&txn->hdr_idx); |
| 6645 | } |
| 6646 | |
| 6647 | /* to be used at the end of a transaction */ |
| 6648 | void http_end_txn(struct session *s) |
| 6649 | { |
| 6650 | struct http_txn *txn = &s->txn; |
| 6651 | |
| 6652 | /* these ones will have been dynamically allocated */ |
| 6653 | pool_free2(pool2_requri, txn->uri); |
| 6654 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6655 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6656 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 6657 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6658 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6659 | txn->uri = NULL; |
| 6660 | txn->srv_cookie = NULL; |
| 6661 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6662 | |
| 6663 | if (txn->req.cap) { |
| 6664 | struct cap_hdr *h; |
| 6665 | for (h = s->fe->req_cap; h; h = h->next) |
| 6666 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 6667 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 6668 | } |
| 6669 | |
| 6670 | if (txn->rsp.cap) { |
| 6671 | struct cap_hdr *h; |
| 6672 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 6673 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 6674 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 6675 | } |
| 6676 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | /* to be used at the end of a transaction to prepare a new one */ |
| 6680 | void http_reset_txn(struct session *s) |
| 6681 | { |
| 6682 | http_end_txn(s); |
| 6683 | http_init_txn(s); |
| 6684 | |
| 6685 | s->be = s->fe; |
| 6686 | s->req->analysers = s->listener->analysers; |
| 6687 | s->logs.logwait = s->fe->to_log; |
| 6688 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 6689 | /* re-init store persistence */ |
| 6690 | s->store_count = 0; |
| 6691 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6692 | s->pend_pos = NULL; |
| 6693 | s->conn_retries = s->be->conn_retries; |
| 6694 | |
| 6695 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6696 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 6697 | /* We must trim any excess data from the response buffer, because we |
| 6698 | * may have blocked an invalid response from a server that we don't |
| 6699 | * want to accidentely forward once we disable the analysers, nor do |
| 6700 | * we want those data to come along with next response. A typical |
| 6701 | * example of such data would be from a buggy server responding to |
| 6702 | * a HEAD with some data, or sending more than the advertised |
| 6703 | * content-length. |
| 6704 | */ |
| 6705 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 6706 | s->rep->l = s->rep->send_max; |
| 6707 | s->rep->r = s->rep->w + s->rep->l; |
| 6708 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 6709 | s->rep->r -= s->rep->size; |
| 6710 | } |
| 6711 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6712 | s->req->rto = s->fe->timeout.client; |
| 6713 | s->req->wto = s->be->timeout.server; |
| 6714 | s->req->cto = s->be->timeout.connect; |
| 6715 | |
| 6716 | s->rep->rto = s->be->timeout.server; |
| 6717 | s->rep->wto = s->fe->timeout.client; |
| 6718 | s->rep->cto = TICK_ETERNITY; |
| 6719 | |
| 6720 | s->req->rex = TICK_ETERNITY; |
| 6721 | s->req->wex = TICK_ETERNITY; |
| 6722 | s->req->analyse_exp = TICK_ETERNITY; |
| 6723 | s->rep->rex = TICK_ETERNITY; |
| 6724 | s->rep->wex = TICK_ETERNITY; |
| 6725 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6726 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6727 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6728 | /************************************************************************/ |
| 6729 | /* The code below is dedicated to ACL parsing and matching */ |
| 6730 | /************************************************************************/ |
| 6731 | |
| 6732 | |
| 6733 | |
| 6734 | |
| 6735 | /* 1. Check on METHOD |
| 6736 | * We use the pre-parsed method if it is known, and store its number as an |
| 6737 | * integer. If it is unknown, we use the pointer and the length. |
| 6738 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6739 | 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] | 6740 | { |
| 6741 | int len, meth; |
| 6742 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6743 | len = strlen(*text); |
| 6744 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6745 | |
| 6746 | pattern->val.i = meth; |
| 6747 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6748 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6749 | if (!pattern->ptr.str) |
| 6750 | return 0; |
| 6751 | pattern->len = len; |
| 6752 | } |
| 6753 | return 1; |
| 6754 | } |
| 6755 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6756 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6757 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6758 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6759 | { |
| 6760 | int meth; |
| 6761 | struct http_txn *txn = l7; |
| 6762 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6763 | if (!txn) |
| 6764 | return 0; |
| 6765 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6766 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6767 | return 0; |
| 6768 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6769 | meth = txn->meth; |
| 6770 | test->i = meth; |
| 6771 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6772 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6773 | /* ensure the indexes are not affected */ |
| 6774 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6775 | test->len = txn->req.sl.rq.m_l; |
| 6776 | test->ptr = txn->req.sol; |
| 6777 | } |
| 6778 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6779 | return 1; |
| 6780 | } |
| 6781 | |
| 6782 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6783 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6784 | int icase; |
| 6785 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6786 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6787 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6788 | |
| 6789 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6790 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6791 | |
| 6792 | /* Other method, we must compare the strings */ |
| 6793 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6794 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6795 | |
| 6796 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6797 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6798 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6799 | return ACL_PAT_FAIL; |
| 6800 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | /* 2. Check on Request/Status Version |
| 6804 | * We simply compare strings here. |
| 6805 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6806 | 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] | 6807 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6808 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6809 | if (!pattern->ptr.str) |
| 6810 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6811 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6812 | return 1; |
| 6813 | } |
| 6814 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6815 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6816 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6817 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6818 | { |
| 6819 | struct http_txn *txn = l7; |
| 6820 | char *ptr; |
| 6821 | int len; |
| 6822 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6823 | if (!txn) |
| 6824 | return 0; |
| 6825 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6826 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6827 | return 0; |
| 6828 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6829 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6830 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6831 | |
| 6832 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6833 | if (len <= 0) |
| 6834 | return 0; |
| 6835 | |
| 6836 | test->ptr = ptr; |
| 6837 | test->len = len; |
| 6838 | |
| 6839 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6840 | return 1; |
| 6841 | } |
| 6842 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6843 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6844 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6845 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6846 | { |
| 6847 | struct http_txn *txn = l7; |
| 6848 | char *ptr; |
| 6849 | int len; |
| 6850 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6851 | if (!txn) |
| 6852 | return 0; |
| 6853 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6854 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6855 | return 0; |
| 6856 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6857 | len = txn->rsp.sl.st.v_l; |
| 6858 | ptr = txn->rsp.sol; |
| 6859 | |
| 6860 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6861 | if (len <= 0) |
| 6862 | return 0; |
| 6863 | |
| 6864 | test->ptr = ptr; |
| 6865 | test->len = len; |
| 6866 | |
| 6867 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6868 | return 1; |
| 6869 | } |
| 6870 | |
| 6871 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6872 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6873 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6874 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6875 | { |
| 6876 | struct http_txn *txn = l7; |
| 6877 | char *ptr; |
| 6878 | int len; |
| 6879 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6880 | if (!txn) |
| 6881 | return 0; |
| 6882 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6883 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6884 | return 0; |
| 6885 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6886 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6887 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6888 | |
| 6889 | test->i = __strl2ui(ptr, len); |
| 6890 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6891 | return 1; |
| 6892 | } |
| 6893 | |
| 6894 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6895 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6896 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6897 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6898 | { |
| 6899 | struct http_txn *txn = l7; |
| 6900 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6901 | if (!txn) |
| 6902 | return 0; |
| 6903 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6904 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6905 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6906 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6907 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6908 | /* ensure the indexes are not affected */ |
| 6909 | return 0; |
| 6910 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6911 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6912 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6913 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6914 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6915 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6916 | return 1; |
| 6917 | } |
| 6918 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6919 | static int |
| 6920 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6921 | struct acl_expr *expr, struct acl_test *test) |
| 6922 | { |
| 6923 | struct http_txn *txn = l7; |
| 6924 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6925 | if (!txn) |
| 6926 | return 0; |
| 6927 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6928 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6929 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6930 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6931 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6932 | /* ensure the indexes are not affected */ |
| 6933 | return 0; |
| 6934 | |
| 6935 | /* Parse HTTP request */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6936 | url2sa(txn->req.sol + 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] | 6937 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6938 | test->i = AF_INET; |
| 6939 | |
| 6940 | /* |
| 6941 | * If we are parsing url in frontend space, we prepare backend stage |
| 6942 | * to not parse again the same url ! optimization lazyness... |
| 6943 | */ |
| 6944 | if (px->options & PR_O_HTTP_PROXY) |
| 6945 | l4->flags |= SN_ADDR_SET; |
| 6946 | |
| 6947 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6948 | return 1; |
| 6949 | } |
| 6950 | |
| 6951 | static int |
| 6952 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6953 | struct acl_expr *expr, struct acl_test *test) |
| 6954 | { |
| 6955 | struct http_txn *txn = l7; |
| 6956 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6957 | if (!txn) |
| 6958 | return 0; |
| 6959 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6960 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6961 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6962 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6963 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6964 | /* ensure the indexes are not affected */ |
| 6965 | return 0; |
| 6966 | |
| 6967 | /* Same optimization as url_ip */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6968 | url2sa(txn->req.sol + 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] | 6969 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6970 | |
| 6971 | if (px->options & PR_O_HTTP_PROXY) |
| 6972 | l4->flags |= SN_ADDR_SET; |
| 6973 | |
| 6974 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6975 | return 1; |
| 6976 | } |
| 6977 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6978 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6979 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6980 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6981 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6982 | 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] | 6983 | struct acl_expr *expr, struct acl_test *test) |
| 6984 | { |
| 6985 | struct http_txn *txn = l7; |
| 6986 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6987 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6988 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6989 | if (!txn) |
| 6990 | return 0; |
| 6991 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6992 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6993 | /* search for header from the beginning */ |
| 6994 | ctx->idx = 0; |
| 6995 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6996 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6997 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6998 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6999 | test->len = ctx->vlen; |
| 7000 | test->ptr = (char *)ctx->line + ctx->val; |
| 7001 | return 1; |
| 7002 | } |
| 7003 | |
| 7004 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7005 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7006 | return 0; |
| 7007 | } |
| 7008 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7009 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7010 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7011 | struct acl_expr *expr, struct acl_test *test) |
| 7012 | { |
| 7013 | struct http_txn *txn = l7; |
| 7014 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7015 | if (!txn) |
| 7016 | return 0; |
| 7017 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7018 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7019 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7020 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7021 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7022 | /* ensure the indexes are not affected */ |
| 7023 | return 0; |
| 7024 | |
| 7025 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 7026 | } |
| 7027 | |
| 7028 | static int |
| 7029 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7030 | struct acl_expr *expr, struct acl_test *test) |
| 7031 | { |
| 7032 | struct http_txn *txn = l7; |
| 7033 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7034 | if (!txn) |
| 7035 | return 0; |
| 7036 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7037 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7038 | return 0; |
| 7039 | |
| 7040 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 7041 | } |
| 7042 | |
| 7043 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 7044 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7045 | */ |
| 7046 | static int |
| 7047 | 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] | 7048 | struct acl_expr *expr, struct acl_test *test) |
| 7049 | { |
| 7050 | struct http_txn *txn = l7; |
| 7051 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7052 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7053 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7054 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7055 | if (!txn) |
| 7056 | return 0; |
| 7057 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7058 | ctx.idx = 0; |
| 7059 | cnt = 0; |
| 7060 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 7061 | cnt++; |
| 7062 | |
| 7063 | test->i = cnt; |
| 7064 | test->flags = ACL_TEST_F_VOL_HDR; |
| 7065 | return 1; |
| 7066 | } |
| 7067 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7068 | static int |
| 7069 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7070 | struct acl_expr *expr, struct acl_test *test) |
| 7071 | { |
| 7072 | struct http_txn *txn = l7; |
| 7073 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7074 | if (!txn) |
| 7075 | return 0; |
| 7076 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7077 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7078 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7079 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7080 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7081 | /* ensure the indexes are not affected */ |
| 7082 | return 0; |
| 7083 | |
| 7084 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 7085 | } |
| 7086 | |
| 7087 | static int |
| 7088 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7089 | struct acl_expr *expr, struct acl_test *test) |
| 7090 | { |
| 7091 | struct http_txn *txn = l7; |
| 7092 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7093 | if (!txn) |
| 7094 | return 0; |
| 7095 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7096 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7097 | return 0; |
| 7098 | |
| 7099 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 7100 | } |
| 7101 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7102 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7103 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7104 | * 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] | 7105 | */ |
| 7106 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7107 | 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] | 7108 | struct acl_expr *expr, struct acl_test *test) |
| 7109 | { |
| 7110 | struct http_txn *txn = l7; |
| 7111 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7112 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7113 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7114 | if (!txn) |
| 7115 | return 0; |
| 7116 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7117 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7118 | /* search for header from the beginning */ |
| 7119 | ctx->idx = 0; |
| 7120 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7121 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7122 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7123 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7124 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 7125 | return 1; |
| 7126 | } |
| 7127 | |
| 7128 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7129 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7130 | return 0; |
| 7131 | } |
| 7132 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7133 | static int |
| 7134 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7135 | struct acl_expr *expr, struct acl_test *test) |
| 7136 | { |
| 7137 | struct http_txn *txn = l7; |
| 7138 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7139 | if (!txn) |
| 7140 | return 0; |
| 7141 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7142 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7143 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7144 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7145 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7146 | /* ensure the indexes are not affected */ |
| 7147 | return 0; |
| 7148 | |
| 7149 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 7150 | } |
| 7151 | |
| 7152 | static int |
| 7153 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7154 | struct acl_expr *expr, struct acl_test *test) |
| 7155 | { |
| 7156 | struct http_txn *txn = l7; |
| 7157 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7158 | if (!txn) |
| 7159 | return 0; |
| 7160 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7161 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7162 | return 0; |
| 7163 | |
| 7164 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 7165 | } |
| 7166 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7167 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 7168 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7169 | */ |
| 7170 | static int |
| 7171 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 7172 | struct acl_expr *expr, struct acl_test *test) |
| 7173 | { |
| 7174 | struct http_txn *txn = l7; |
| 7175 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7176 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 7177 | |
| 7178 | if (!txn) |
| 7179 | return 0; |
| 7180 | |
| 7181 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7182 | /* search for header from the beginning */ |
| 7183 | ctx->idx = 0; |
| 7184 | |
| 7185 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7186 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7187 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7188 | /* Same optimization as url_ip */ |
| 7189 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 7190 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 7191 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 7192 | test->i = AF_INET; |
| 7193 | return 1; |
| 7194 | } |
| 7195 | |
| 7196 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7197 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7198 | return 0; |
| 7199 | } |
| 7200 | |
| 7201 | static int |
| 7202 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7203 | struct acl_expr *expr, struct acl_test *test) |
| 7204 | { |
| 7205 | struct http_txn *txn = l7; |
| 7206 | |
| 7207 | if (!txn) |
| 7208 | return 0; |
| 7209 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7210 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7211 | return 0; |
| 7212 | |
| 7213 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7214 | /* ensure the indexes are not affected */ |
| 7215 | return 0; |
| 7216 | |
| 7217 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 7218 | } |
| 7219 | |
| 7220 | static int |
| 7221 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7222 | struct acl_expr *expr, struct acl_test *test) |
| 7223 | { |
| 7224 | struct http_txn *txn = l7; |
| 7225 | |
| 7226 | if (!txn) |
| 7227 | return 0; |
| 7228 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7229 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7230 | return 0; |
| 7231 | |
| 7232 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 7233 | } |
| 7234 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7235 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 7236 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 7237 | */ |
| 7238 | static int |
| 7239 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7240 | struct acl_expr *expr, struct acl_test *test) |
| 7241 | { |
| 7242 | struct http_txn *txn = l7; |
| 7243 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7244 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7245 | if (!txn) |
| 7246 | return 0; |
| 7247 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7248 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7249 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7250 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7251 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7252 | /* ensure the indexes are not affected */ |
| 7253 | return 0; |
| 7254 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7255 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 7256 | ptr = http_get_path(txn); |
| 7257 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7258 | return 0; |
| 7259 | |
| 7260 | /* OK, we got the '/' ! */ |
| 7261 | test->ptr = ptr; |
| 7262 | |
| 7263 | while (ptr < end && *ptr != '?') |
| 7264 | ptr++; |
| 7265 | |
| 7266 | test->len = ptr - test->ptr; |
| 7267 | |
| 7268 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7269 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7270 | return 1; |
| 7271 | } |
| 7272 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7273 | static int |
| 7274 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 7275 | struct acl_expr *expr, struct acl_test *test) |
| 7276 | { |
| 7277 | struct buffer *req = s->req; |
| 7278 | struct http_txn *txn = &s->txn; |
| 7279 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7280 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7281 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7282 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7283 | */ |
| 7284 | |
| 7285 | if (!s || !req) |
| 7286 | return 0; |
| 7287 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7288 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7289 | /* Already decoded as OK */ |
| 7290 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7291 | return 1; |
| 7292 | } |
| 7293 | |
| 7294 | /* Try to decode HTTP request */ |
| 7295 | if (likely(req->lr < req->r)) |
| 7296 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 7297 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7298 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7299 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 7300 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7301 | return 1; |
| 7302 | } |
| 7303 | /* wait for final state */ |
| 7304 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 7305 | return 0; |
| 7306 | } |
| 7307 | |
| 7308 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 7309 | * perform so that further checks can rely on HTTP tests. |
| 7310 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7311 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7312 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7313 | s->flags |= SN_REDIRECTABLE; |
| 7314 | |
| 7315 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7316 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7317 | return 1; |
| 7318 | } |
| 7319 | |
| 7320 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7321 | return 1; |
| 7322 | } |
| 7323 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7324 | static int |
| 7325 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 7326 | struct acl_expr *expr, struct acl_test *test) |
| 7327 | { |
| 7328 | |
| 7329 | if (!s) |
| 7330 | return 0; |
| 7331 | |
| 7332 | if (!get_http_auth(s)) |
| 7333 | return 0; |
| 7334 | |
| 7335 | test->ctx.a[0] = expr->arg.ul; |
| 7336 | test->ctx.a[1] = s->txn.auth.user; |
| 7337 | test->ctx.a[2] = s->txn.auth.pass; |
| 7338 | |
| 7339 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 7340 | |
| 7341 | return 1; |
| 7342 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7343 | |
| 7344 | /************************************************************************/ |
| 7345 | /* All supported keywords must be declared here. */ |
| 7346 | /************************************************************************/ |
| 7347 | |
| 7348 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7349 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7350 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7351 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7352 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 7353 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7354 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7355 | { "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] | 7356 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7357 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7358 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7359 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7360 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7361 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7362 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7363 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7364 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 7365 | { "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] | 7366 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7367 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 7368 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7369 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7370 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7371 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7372 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7373 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7374 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7375 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 7376 | { "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] | 7377 | { "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] | 7378 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7379 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7380 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 7381 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 7382 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 7383 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 7384 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 7385 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 7386 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 7387 | { "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] | 7388 | { "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] | 7389 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7390 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7391 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7392 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7393 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7394 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7395 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7396 | { "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] | 7397 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7398 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7399 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7400 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7401 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7402 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7403 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7404 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7405 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7406 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7407 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7408 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7409 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7410 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7411 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7412 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7413 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7414 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7415 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7416 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7417 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth }, |
| 7418 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7419 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7420 | }}; |
| 7421 | |
| 7422 | |
| 7423 | __attribute__((constructor)) |
| 7424 | static void __http_protocol_init(void) |
| 7425 | { |
| 7426 | acl_register_keywords(&acl_kws); |
| 7427 | } |
| 7428 | |
| 7429 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7430 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7431 | * Local variables: |
| 7432 | * c-indent-level: 8 |
| 7433 | * c-basic-offset: 8 |
| 7434 | * End: |
| 7435 | */ |