Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 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> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 47 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 49 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 52 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 53 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 55 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 59 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 60 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/task.h> |
| 62 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 63 | const char HTTP_100[] = |
| 64 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 65 | |
| 66 | const struct chunk http_100_chunk = { |
| 67 | .str = (char *)&HTTP_100, |
| 68 | .len = sizeof(HTTP_100)-1 |
| 69 | }; |
| 70 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 71 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 72 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 73 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 74 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 75 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 77 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 78 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 79 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 83 | |
| 84 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 85 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [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 | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 90 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 92 | const char *HTTP_401_fmt = |
| 93 | "HTTP/1.0 401 Unauthorized\r\n" |
| 94 | "Cache-Control: no-cache\r\n" |
| 95 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 96 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 98 | "\r\n" |
| 99 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 100 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 101 | const char *HTTP_407_fmt = |
| 102 | "HTTP/1.0 407 Unauthorized\r\n" |
| 103 | "Cache-Control: no-cache\r\n" |
| 104 | "Connection: close\r\n" |
| 105 | "Content-Type: text/html\r\n" |
| 106 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 107 | "\r\n" |
| 108 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 109 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 110 | |
| 111 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 112 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 113 | [HTTP_ERR_400] = 400, |
| 114 | [HTTP_ERR_403] = 403, |
| 115 | [HTTP_ERR_408] = 408, |
| 116 | [HTTP_ERR_500] = 500, |
| 117 | [HTTP_ERR_502] = 502, |
| 118 | [HTTP_ERR_503] = 503, |
| 119 | [HTTP_ERR_504] = 504, |
| 120 | }; |
| 121 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 122 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 123 | [HTTP_ERR_200] = |
| 124 | "HTTP/1.0 200 OK\r\n" |
| 125 | "Cache-Control: no-cache\r\n" |
| 126 | "Connection: close\r\n" |
| 127 | "Content-Type: text/html\r\n" |
| 128 | "\r\n" |
| 129 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 130 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 132 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | "Cache-Control: no-cache\r\n" |
| 134 | "Connection: close\r\n" |
| 135 | "Content-Type: text/html\r\n" |
| 136 | "\r\n" |
| 137 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 138 | |
| 139 | [HTTP_ERR_403] = |
| 140 | "HTTP/1.0 403 Forbidden\r\n" |
| 141 | "Cache-Control: no-cache\r\n" |
| 142 | "Connection: close\r\n" |
| 143 | "Content-Type: text/html\r\n" |
| 144 | "\r\n" |
| 145 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 146 | |
| 147 | [HTTP_ERR_408] = |
| 148 | "HTTP/1.0 408 Request Time-out\r\n" |
| 149 | "Cache-Control: no-cache\r\n" |
| 150 | "Connection: close\r\n" |
| 151 | "Content-Type: text/html\r\n" |
| 152 | "\r\n" |
| 153 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 154 | |
| 155 | [HTTP_ERR_500] = |
| 156 | "HTTP/1.0 500 Server Error\r\n" |
| 157 | "Cache-Control: no-cache\r\n" |
| 158 | "Connection: close\r\n" |
| 159 | "Content-Type: text/html\r\n" |
| 160 | "\r\n" |
| 161 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 162 | |
| 163 | [HTTP_ERR_502] = |
| 164 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 165 | "Cache-Control: no-cache\r\n" |
| 166 | "Connection: close\r\n" |
| 167 | "Content-Type: text/html\r\n" |
| 168 | "\r\n" |
| 169 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 170 | |
| 171 | [HTTP_ERR_503] = |
| 172 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 173 | "Cache-Control: no-cache\r\n" |
| 174 | "Connection: close\r\n" |
| 175 | "Content-Type: text/html\r\n" |
| 176 | "\r\n" |
| 177 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 178 | |
| 179 | [HTTP_ERR_504] = |
| 180 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 181 | "Cache-Control: no-cache\r\n" |
| 182 | "Connection: close\r\n" |
| 183 | "Content-Type: text/html\r\n" |
| 184 | "\r\n" |
| 185 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 186 | |
| 187 | }; |
| 188 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 189 | /* We must put the messages here since GCC cannot initialize consts depending |
| 190 | * on strlen(). |
| 191 | */ |
| 192 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 193 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 194 | #define FD_SETS_ARE_BITFIELDS |
| 195 | #ifdef FD_SETS_ARE_BITFIELDS |
| 196 | /* |
| 197 | * This map is used with all the FD_* macros to check whether a particular bit |
| 198 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 199 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 200 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 201 | * exclusively to the macros. |
| 202 | */ |
| 203 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 204 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 205 | |
| 206 | #else |
| 207 | #error "Check if your OS uses bitfields for fd_sets" |
| 208 | #endif |
| 209 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 210 | void init_proto_http() |
| 211 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 212 | int i; |
| 213 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 216 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 217 | if (!http_err_msgs[msg]) { |
| 218 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 219 | abort(); |
| 220 | } |
| 221 | |
| 222 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 223 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 224 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 225 | |
| 226 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 227 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 228 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 229 | * printable characters above. |
| 230 | */ |
| 231 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 232 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 233 | for (i = 0; i < 32; i++) { |
| 234 | FD_SET(i, hdr_encode_map); |
| 235 | FD_SET(i, url_encode_map); |
| 236 | } |
| 237 | for (i = 127; i < 256; i++) { |
| 238 | FD_SET(i, hdr_encode_map); |
| 239 | FD_SET(i, url_encode_map); |
| 240 | } |
| 241 | |
| 242 | tmp = "\"#{|}"; |
| 243 | while (*tmp) { |
| 244 | FD_SET(*tmp, hdr_encode_map); |
| 245 | tmp++; |
| 246 | } |
| 247 | |
| 248 | tmp = "\"#"; |
| 249 | while (*tmp) { |
| 250 | FD_SET(*tmp, url_encode_map); |
| 251 | tmp++; |
| 252 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 253 | |
| 254 | /* memory allocations */ |
| 255 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 256 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 257 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 258 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 259 | /* |
| 260 | * We have 26 list of methods (1 per first letter), each of which can have |
| 261 | * up to 3 entries (2 valid, 1 null). |
| 262 | */ |
| 263 | struct http_method_desc { |
| 264 | http_meth_t meth; |
| 265 | int len; |
| 266 | const char text[8]; |
| 267 | }; |
| 268 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 269 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 270 | ['C' - 'A'] = { |
| 271 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 272 | }, |
| 273 | ['D' - 'A'] = { |
| 274 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 275 | }, |
| 276 | ['G' - 'A'] = { |
| 277 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 278 | }, |
| 279 | ['H' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 281 | }, |
| 282 | ['P' - 'A'] = { |
| 283 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 284 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 285 | }, |
| 286 | ['T' - 'A'] = { |
| 287 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 288 | }, |
| 289 | /* rest is empty like this : |
| 290 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 291 | */ |
| 292 | }; |
| 293 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 294 | /* 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] | 295 | * 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] | 296 | * RFC2616 for those chars. |
| 297 | */ |
| 298 | |
| 299 | const char http_is_spht[256] = { |
| 300 | [' '] = 1, ['\t'] = 1, |
| 301 | }; |
| 302 | |
| 303 | const char http_is_crlf[256] = { |
| 304 | ['\r'] = 1, ['\n'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_lws[256] = { |
| 308 | [' '] = 1, ['\t'] = 1, |
| 309 | ['\r'] = 1, ['\n'] = 1, |
| 310 | }; |
| 311 | |
| 312 | const char http_is_sep[256] = { |
| 313 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 314 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 315 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 316 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 317 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 318 | }; |
| 319 | |
| 320 | const char http_is_ctl[256] = { |
| 321 | [0 ... 31] = 1, |
| 322 | [127] = 1, |
| 323 | }; |
| 324 | |
| 325 | /* |
| 326 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 327 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 328 | * them correctly. Instead, define every non-CTL char's status. |
| 329 | */ |
| 330 | const char http_is_token[256] = { |
| 331 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 332 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 333 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 334 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 335 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 336 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 337 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 338 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 339 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 340 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 341 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 342 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 343 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 344 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 345 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 346 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 347 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 348 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 349 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 350 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 351 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 352 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 353 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 354 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 355 | }; |
| 356 | |
| 357 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 358 | /* |
| 359 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 360 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 361 | */ |
| 362 | const char http_is_ver_token[256] = { |
| 363 | ['.'] = 1, ['/'] = 1, |
| 364 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 365 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 366 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 367 | }; |
| 368 | |
| 369 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 370 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 371 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 372 | */ |
| 373 | #if defined(DEBUG_FSM) |
| 374 | static void http_silent_debug(int line, struct session *s) |
| 375 | { |
| 376 | int size = 0; |
| 377 | size += snprintf(trash + size, sizeof(trash) - size, |
| 378 | "[%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", |
| 379 | line, |
| 380 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 381 | 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); |
| 382 | write(-1, trash, size); |
| 383 | size = 0; |
| 384 | size += snprintf(trash + size, sizeof(trash) - size, |
| 385 | " %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", |
| 386 | line, |
| 387 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 388 | 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); |
| 389 | |
| 390 | write(-1, trash, size); |
| 391 | } |
| 392 | #else |
| 393 | #define http_silent_debug(l,s) do { } while (0) |
| 394 | #endif |
| 395 | |
| 396 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 397 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 398 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 399 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 400 | * of headers is automatically adjusted. The number of bytes added is returned |
| 401 | * on success, otherwise <0 is returned indicating an error. |
| 402 | */ |
| 403 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 404 | struct hdr_idx *hdr_idx, const char *text) |
| 405 | { |
| 406 | int bytes, len; |
| 407 | |
| 408 | len = strlen(text); |
| 409 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 410 | if (!bytes) |
| 411 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 412 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 413 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 418 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 419 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 420 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 421 | * of headers is automatically adjusted. The number of bytes added is returned |
| 422 | * on success, otherwise <0 is returned indicating an error. |
| 423 | */ |
| 424 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 425 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 426 | { |
| 427 | int bytes; |
| 428 | |
| 429 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 430 | if (!bytes) |
| 431 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 432 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 433 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 434 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 435 | |
| 436 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 437 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 438 | * If so, returns the position of the first non-space character relative to |
| 439 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 440 | * to return a pointer to the place after the first space. Returns 0 if the |
| 441 | * header name does not match. Checks are case-insensitive. |
| 442 | */ |
| 443 | int http_header_match2(const char *hdr, const char *end, |
| 444 | const char *name, int len) |
| 445 | { |
| 446 | const char *val; |
| 447 | |
| 448 | if (hdr + len >= end) |
| 449 | return 0; |
| 450 | if (hdr[len] != ':') |
| 451 | return 0; |
| 452 | if (strncasecmp(hdr, name, len) != 0) |
| 453 | return 0; |
| 454 | val = hdr + len + 1; |
| 455 | while (val < end && HTTP_IS_SPHT(*val)) |
| 456 | val++; |
| 457 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 458 | return len + 2; /* we may replace starting from second space */ |
| 459 | return val - hdr; |
| 460 | } |
| 461 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 462 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 463 | * par 2.2 for more information. Note that it requires a valid header to return |
| 464 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 465 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 466 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 467 | { |
| 468 | int quoted, qdpair; |
| 469 | |
| 470 | quoted = qdpair = 0; |
| 471 | for (; s < e; s++) { |
| 472 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 473 | else if (quoted) { |
| 474 | if (*s == '\\') qdpair = 1; |
| 475 | else if (*s == '"') quoted = 0; |
| 476 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 477 | else if (*s == '"') quoted = 1; |
| 478 | else if (*s == ',') return s; |
| 479 | } |
| 480 | return s; |
| 481 | } |
| 482 | |
| 483 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 484 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 485 | * structure holds everything necessary to use the header and find next |
| 486 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 487 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 488 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 489 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 490 | * is NULL when searching for a new values of a header, the current header is |
| 491 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 492 | */ |
| 493 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 494 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 495 | struct hdr_ctx *ctx) |
| 496 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 497 | char *eol, *sov; |
| 498 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 499 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 500 | cur_idx = ctx->idx; |
| 501 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 502 | /* We have previously returned a value, let's search |
| 503 | * another one on the same line. |
| 504 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 505 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 506 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 507 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | eol = sol + idx->v[cur_idx].len; |
| 509 | |
| 510 | if (sov >= eol) |
| 511 | /* no more values in this header */ |
| 512 | goto next_hdr; |
| 513 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 514 | /* values remaining for this header, skip the comma but save it |
| 515 | * for later use (eg: for header deletion). |
| 516 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 517 | sov++; |
| 518 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 519 | sov++; |
| 520 | |
| 521 | goto return_hdr; |
| 522 | } |
| 523 | |
| 524 | /* first request for this header */ |
| 525 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 526 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 527 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 528 | while (cur_idx) { |
| 529 | eol = sol + idx->v[cur_idx].len; |
| 530 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 531 | if (len == 0) { |
| 532 | /* No argument was passed, we want any header. |
| 533 | * To achieve this, we simply build a fake request. */ |
| 534 | while (sol + len < eol && sol[len] != ':') |
| 535 | len++; |
| 536 | name = sol; |
| 537 | } |
| 538 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 539 | if ((len < eol - sol) && |
| 540 | (sol[len] == ':') && |
| 541 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 542 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 543 | sov = sol + len + 1; |
| 544 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 545 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 546 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 547 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 548 | ctx->prev = old_idx; |
| 549 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 550 | ctx->idx = cur_idx; |
| 551 | ctx->val = sov - sol; |
| 552 | |
| 553 | eol = find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 554 | ctx->tws = 0; |
Willy Tarreau | 275600b | 2011-09-16 08:11:26 +0200 | [diff] [blame] | 555 | while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 556 | eol--; |
| 557 | ctx->tws++; |
| 558 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 559 | ctx->vlen = eol - sov; |
| 560 | return 1; |
| 561 | } |
| 562 | next_hdr: |
| 563 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 564 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 565 | cur_idx = idx->v[cur_idx].next; |
| 566 | } |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 571 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 572 | struct hdr_ctx *ctx) |
| 573 | { |
| 574 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 575 | } |
| 576 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 577 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 578 | * the http_find_header functions. The value is removed, as well as surrounding |
| 579 | * commas if any. If the removed value was alone, the whole header is removed. |
| 580 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 581 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 582 | * no more header, so any processing may stop. The ctx is always left in a form |
| 583 | * that can be handled by http_find_header2() to find next occurrence. |
| 584 | */ |
| 585 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 586 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 587 | { |
| 588 | int cur_idx = ctx->idx; |
| 589 | char *sol = ctx->line; |
| 590 | struct hdr_idx_elem *hdr; |
| 591 | int delta, skip_comma; |
| 592 | |
| 593 | if (!cur_idx) |
| 594 | return 0; |
| 595 | |
| 596 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 597 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 598 | /* This was the only value of the header, we must now remove it entirely. */ |
| 599 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 600 | http_msg_move_end(msg, delta); |
| 601 | idx->used--; |
| 602 | hdr->len = 0; /* unused entry */ |
| 603 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 604 | if (idx->tail == ctx->idx) |
| 605 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 606 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 607 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 608 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 609 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 610 | return ctx->idx; |
| 611 | } |
| 612 | |
| 613 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 614 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 615 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 616 | */ |
| 617 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 618 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 619 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 620 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 621 | NULL, 0); |
| 622 | hdr->len += delta; |
| 623 | http_msg_move_end(msg, delta); |
| 624 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 625 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 626 | return ctx->idx; |
| 627 | } |
| 628 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 629 | /* This function handles a server error at the stream interface level. The |
| 630 | * stream interface is assumed to be already in a closed state. An optional |
| 631 | * message is copied into the input buffer, and an HTTP status code stored. |
| 632 | * 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] | 633 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 634 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 635 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 636 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 637 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 638 | buffer_auto_read(si->ob); |
| 639 | buffer_abort(si->ob); |
| 640 | buffer_auto_close(si->ob); |
| 641 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 642 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 643 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 644 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 645 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 646 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 647 | } |
| 648 | if (!(t->flags & SN_ERR_MASK)) |
| 649 | t->flags |= err; |
| 650 | if (!(t->flags & SN_FINST_MASK)) |
| 651 | t->flags |= finst; |
| 652 | } |
| 653 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 654 | /* This function returns the appropriate error location for the given session |
| 655 | * and message. |
| 656 | */ |
| 657 | |
| 658 | struct chunk *error_message(struct session *s, int msgnum) |
| 659 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 660 | if (s->be->errmsg[msgnum].str) |
| 661 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 662 | else if (s->fe->errmsg[msgnum].str) |
| 663 | return &s->fe->errmsg[msgnum]; |
| 664 | else |
| 665 | return &http_err_chunks[msgnum]; |
| 666 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 667 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 668 | /* |
| 669 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 670 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 671 | */ |
| 672 | static http_meth_t find_http_meth(const char *str, const int len) |
| 673 | { |
| 674 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 675 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 676 | |
| 677 | m = ((unsigned)*str - 'A'); |
| 678 | |
| 679 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 680 | for (h = http_methods[m]; h->len > 0; h++) { |
| 681 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 683 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 684 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 685 | }; |
| 686 | return HTTP_METH_OTHER; |
| 687 | } |
| 688 | return HTTP_METH_NONE; |
| 689 | |
| 690 | } |
| 691 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 692 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 693 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 694 | * It is returned otherwise. |
| 695 | */ |
| 696 | static char * |
| 697 | http_get_path(struct http_txn *txn) |
| 698 | { |
| 699 | char *ptr, *end; |
| 700 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 701 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 702 | end = ptr + txn->req.sl.rq.u_l; |
| 703 | |
| 704 | if (ptr >= end) |
| 705 | return NULL; |
| 706 | |
| 707 | /* RFC2616, par. 5.1.2 : |
| 708 | * Request-URI = "*" | absuri | abspath | authority |
| 709 | */ |
| 710 | |
| 711 | if (*ptr == '*') |
| 712 | return NULL; |
| 713 | |
| 714 | if (isalpha((unsigned char)*ptr)) { |
| 715 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 716 | ptr++; |
| 717 | while (ptr < end && |
| 718 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 719 | ptr++; |
| 720 | /* skip '://' */ |
| 721 | if (ptr == end || *ptr++ != ':') |
| 722 | return NULL; |
| 723 | if (ptr == end || *ptr++ != '/') |
| 724 | return NULL; |
| 725 | if (ptr == end || *ptr++ != '/') |
| 726 | return NULL; |
| 727 | } |
| 728 | /* skip [user[:passwd]@]host[:[port]] */ |
| 729 | |
| 730 | while (ptr < end && *ptr != '/') |
| 731 | ptr++; |
| 732 | |
| 733 | if (ptr == end) |
| 734 | return NULL; |
| 735 | |
| 736 | /* OK, we got the '/' ! */ |
| 737 | return ptr; |
| 738 | } |
| 739 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 740 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 741 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 742 | * left unchanged and will follow normal proxy processing. |
| 743 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 744 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 745 | { |
| 746 | struct http_txn *txn; |
| 747 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 748 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 749 | char *path; |
| 750 | int len; |
| 751 | |
| 752 | /* 1: create the response header */ |
| 753 | rdr.len = strlen(HTTP_302); |
| 754 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 755 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 756 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 757 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 758 | srv = target_srv(&s->target); |
| 759 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 760 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 761 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 762 | return; |
| 763 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 764 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 765 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 766 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 767 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 768 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 769 | |
| 770 | /* 3: add the request URI */ |
| 771 | txn = &s->txn; |
| 772 | path = http_get_path(txn); |
| 773 | if (!path) |
| 774 | return; |
| 775 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 776 | 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] | 777 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 778 | return; |
| 779 | |
| 780 | memcpy(rdr.str + rdr.len, path, len); |
| 781 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 782 | |
| 783 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 784 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 785 | rdr.len += 29; |
| 786 | } else { |
| 787 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 788 | rdr.len += 23; |
| 789 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 790 | |
| 791 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 792 | si->shutr(si); |
| 793 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 794 | si->err_type = SI_ET_NONE; |
| 795 | si->err_loc = NULL; |
| 796 | si->state = SI_ST_CLO; |
| 797 | |
| 798 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 799 | 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] | 800 | |
| 801 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 802 | if (srv) |
| 803 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 806 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 807 | * that the server side is closed. Note that err_type is actually a |
| 808 | * bitmask, where almost only aborts may be cumulated with other |
| 809 | * values. We consider that aborted operations are more important |
| 810 | * than timeouts or errors due to the fact that nobody else in the |
| 811 | * logs might explain incomplete retries. All others should avoid |
| 812 | * being cumulated. It should normally not be possible to have multiple |
| 813 | * aborts at once, but just in case, the first one in sequence is reported. |
| 814 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 815 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 816 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 817 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | |
| 819 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 820 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 821 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 822 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 823 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 824 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 825 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 826 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 827 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 828 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 829 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 830 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 831 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 832 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 833 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 834 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 835 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 836 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 837 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 838 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 839 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 840 | } |
| 841 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 842 | extern const char sess_term_cond[8]; |
| 843 | extern const char sess_fin_state[8]; |
| 844 | extern const char *monthname[12]; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 845 | const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */ |
| 846 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 847 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 848 | Set-cookie Updated, unknown, unknown */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 849 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 850 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 851 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 852 | void http_sess_clflog(struct session *s) |
| 853 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 854 | char pn[INET6_ADDRSTRLEN]; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 855 | struct proxy *fe = s->fe; |
| 856 | struct proxy *be = s->be; |
| 857 | struct proxy *prx_log; |
| 858 | struct http_txn *txn = &s->txn; |
| 859 | int tolog, level, err; |
| 860 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 861 | const char *svid; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 862 | struct tm tm; |
| 863 | static char tmpline[MAX_SYSLOG_LEN]; |
| 864 | int hdr; |
| 865 | size_t w; |
| 866 | int t_request; |
| 867 | |
| 868 | prx_log = fe; |
| 869 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 870 | (s->req->cons->conn_retries != be->conn_retries) || |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 871 | txn->status >= 500; |
| 872 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 873 | if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 874 | snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid); |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 875 | |
| 876 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 877 | |
| 878 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 879 | tolog = fe->to_log; |
| 880 | |
| 881 | h = tmpline; |
| 882 | |
| 883 | w = snprintf(h, sizeof(tmpline), |
| 884 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 885 | pn, |
| 886 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 887 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 888 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 889 | goto trunc; |
| 890 | h += w; |
| 891 | |
| 892 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 893 | goto trunc; |
| 894 | |
| 895 | *(h++) = ' '; |
| 896 | *(h++) = '\"'; |
| 897 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 898 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 899 | '#', url_encode_map, uri); |
| 900 | *(h++) = '\"'; |
| 901 | |
| 902 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 903 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 904 | goto trunc; |
| 905 | h += w; |
| 906 | |
| 907 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 908 | goto trunc; |
| 909 | memcpy(h, " \"-\" \"-\"", 8); |
| 910 | h += 8; |
| 911 | |
| 912 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 913 | " %d %03d", |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 914 | s->req->prod->addr.from.ss_family == AF_UNIX ? s->listener->luid : |
| 915 | get_host_port(&s->req->prod->addr.from), |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 916 | (int)s->logs.accept_date.tv_usec/1000); |
| 917 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 918 | goto trunc; |
| 919 | h += w; |
| 920 | |
| 921 | w = strlen(fe->id); |
| 922 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 923 | goto trunc; |
| 924 | *(h++) = ' '; |
| 925 | *(h++) = '\"'; |
| 926 | memcpy(h, fe->id, w); |
| 927 | h += w; |
| 928 | *(h++) = '\"'; |
| 929 | |
| 930 | w = strlen(be->id); |
| 931 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 932 | goto trunc; |
| 933 | *(h++) = ' '; |
| 934 | *(h++) = '\"'; |
| 935 | memcpy(h, be->id, w); |
| 936 | h += w; |
| 937 | *(h++) = '\"'; |
| 938 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 939 | if (!(tolog & LW_SVID)) |
| 940 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 941 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 942 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 943 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 944 | break; |
| 945 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 946 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 947 | break; |
| 948 | default: |
| 949 | svid = "<NOSRV>"; |
| 950 | break; |
| 951 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 952 | |
| 953 | w = strlen(svid); |
| 954 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 955 | goto trunc; |
| 956 | *(h++) = ' '; |
| 957 | *(h++) = '\"'; |
| 958 | memcpy(h, svid, w); |
| 959 | h += w; |
| 960 | *(h++) = '\"'; |
| 961 | |
| 962 | t_request = -1; |
| 963 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 964 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 965 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 966 | " %d %ld %ld %ld %ld", |
| 967 | t_request, |
| 968 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 969 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 970 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 971 | s->logs.t_close); |
| 972 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 973 | goto trunc; |
| 974 | h += w; |
| 975 | |
| 976 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 977 | goto trunc; |
| 978 | *(h++) = ' '; |
| 979 | *(h++) = '\"'; |
| 980 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 981 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 982 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 983 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 984 | *(h++) = '\"'; |
| 985 | |
| 986 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 987 | " %d %d %d %d %d %ld %ld", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 988 | actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 989 | (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 990 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 991 | |
| 992 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 993 | goto trunc; |
| 994 | h += w; |
| 995 | |
| 996 | if (txn->cli_cookie) { |
| 997 | w = strlen(txn->cli_cookie); |
| 998 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 999 | goto trunc; |
| 1000 | *(h++) = ' '; |
| 1001 | *(h++) = '\"'; |
| 1002 | memcpy(h, txn->cli_cookie, w); |
| 1003 | h += w; |
| 1004 | *(h++) = '\"'; |
| 1005 | } else { |
| 1006 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1007 | goto trunc; |
| 1008 | memcpy(h, " \"-\"", 4); |
| 1009 | h += 4; |
| 1010 | } |
| 1011 | |
| 1012 | if (txn->srv_cookie) { |
| 1013 | w = strlen(txn->srv_cookie); |
| 1014 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1015 | goto trunc; |
| 1016 | *(h++) = ' '; |
| 1017 | *(h++) = '\"'; |
| 1018 | memcpy(h, txn->srv_cookie, w); |
| 1019 | h += w; |
| 1020 | *(h++) = '\"'; |
| 1021 | } else { |
| 1022 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1023 | goto trunc; |
| 1024 | memcpy(h, " \"-\"", 4); |
| 1025 | h += 4; |
| 1026 | } |
| 1027 | |
| 1028 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1029 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1030 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1031 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1032 | if (txn->req.cap[hdr] != NULL) { |
| 1033 | *(h++) = ' '; |
| 1034 | *(h++) = '\"'; |
| 1035 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1036 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1037 | *(h++) = '\"'; |
| 1038 | } else { |
| 1039 | memcpy(h, " \"-\"", 4); |
| 1040 | h += 4; |
| 1041 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1046 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1047 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1048 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1049 | if (txn->rsp.cap[hdr] != NULL) { |
| 1050 | *(h++) = ' '; |
| 1051 | *(h++) = '\"'; |
| 1052 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1053 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1054 | *(h++) = '\"'; |
| 1055 | } else { |
| 1056 | memcpy(h, " \"-\"", 4); |
| 1057 | h += 4; |
| 1058 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | trunc: |
| 1063 | *h = '\0'; |
| 1064 | |
| 1065 | level = LOG_INFO; |
| 1066 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1067 | level = LOG_ERR; |
| 1068 | |
| 1069 | send_log(prx_log, level, "%s\n", tmpline); |
| 1070 | |
| 1071 | s->logs.logwait = 0; |
| 1072 | } |
| 1073 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1074 | /* |
| 1075 | * send a log for the session when we have enough info about it. |
| 1076 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1077 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1078 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1079 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 1080 | char pn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1081 | struct proxy *fe = s->fe; |
| 1082 | struct proxy *be = s->be; |
| 1083 | struct proxy *prx_log; |
| 1084 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1085 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1086 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1087 | const char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1088 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1089 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1090 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1091 | int hdr; |
| 1092 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1093 | /* if we don't want to log normal traffic, return now */ |
| 1094 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1095 | (s->req->cons->conn_retries != be->conn_retries) || |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1096 | txn->status >= 500; |
| 1097 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1098 | return; |
| 1099 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame^] | 1100 | if (LIST_ISEMPTY(&fe->logsrvs)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1101 | return; |
| 1102 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1103 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1104 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1105 | return http_sess_clflog(s); |
| 1106 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 1107 | if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX) |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1108 | snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1109 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1110 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1111 | |
| 1112 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1113 | tolog = fe->to_log; |
| 1114 | |
| 1115 | h = tmpline; |
| 1116 | if (fe->to_log & LW_REQHDR && |
| 1117 | txn->req.cap && |
| 1118 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1119 | *(h++) = ' '; |
| 1120 | *(h++) = '{'; |
| 1121 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1122 | if (hdr) |
| 1123 | *(h++) = '|'; |
| 1124 | if (txn->req.cap[hdr] != NULL) |
| 1125 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1126 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1127 | } |
| 1128 | *(h++) = '}'; |
| 1129 | } |
| 1130 | |
| 1131 | if (fe->to_log & LW_RSPHDR && |
| 1132 | txn->rsp.cap && |
| 1133 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1134 | *(h++) = ' '; |
| 1135 | *(h++) = '{'; |
| 1136 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1137 | if (hdr) |
| 1138 | *(h++) = '|'; |
| 1139 | if (txn->rsp.cap[hdr] != NULL) |
| 1140 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1141 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1142 | } |
| 1143 | *(h++) = '}'; |
| 1144 | } |
| 1145 | |
| 1146 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1147 | *(h++) = ' '; |
| 1148 | *(h++) = '"'; |
| 1149 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1150 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1151 | '#', url_encode_map, uri); |
| 1152 | *(h++) = '"'; |
| 1153 | } |
| 1154 | *h = '\0'; |
| 1155 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1156 | if (!(tolog & LW_SVID)) |
| 1157 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1158 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1159 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1160 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1161 | break; |
| 1162 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1163 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1164 | break; |
| 1165 | default: |
| 1166 | svid = "<NOSRV>"; |
| 1167 | break; |
| 1168 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1169 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1170 | t_request = -1; |
| 1171 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1172 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1173 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1174 | level = LOG_INFO; |
| 1175 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1176 | level = LOG_ERR; |
| 1177 | |
| 1178 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1179 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1180 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1181 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n", |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 1182 | (s->req->prod->addr.from.ss_family == AF_UNIX) ? "unix" : pn, |
| 1183 | (s->req->prod->addr.from.ss_family == AF_UNIX) ? s->listener->luid : |
| 1184 | get_host_port(&s->req->prod->addr.from), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1185 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1186 | 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] | 1187 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1188 | t_request, |
| 1189 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1190 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1191 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1192 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1193 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1194 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1195 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1196 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1197 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1198 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1199 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1200 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1201 | actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 1202 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1203 | (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1204 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1205 | |
| 1206 | s->logs.logwait = 0; |
| 1207 | } |
| 1208 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1209 | |
| 1210 | /* |
| 1211 | * Capture headers from message starting at <som> according to header list |
| 1212 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1213 | */ |
| 1214 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1215 | char **cap, struct cap_hdr *cap_hdr) |
| 1216 | { |
| 1217 | char *eol, *sol, *col, *sov; |
| 1218 | int cur_idx; |
| 1219 | struct cap_hdr *h; |
| 1220 | int len; |
| 1221 | |
| 1222 | sol = som + hdr_idx_first_pos(idx); |
| 1223 | cur_idx = hdr_idx_first_idx(idx); |
| 1224 | |
| 1225 | while (cur_idx) { |
| 1226 | eol = sol + idx->v[cur_idx].len; |
| 1227 | |
| 1228 | col = sol; |
| 1229 | while (col < eol && *col != ':') |
| 1230 | col++; |
| 1231 | |
| 1232 | sov = col + 1; |
| 1233 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1234 | sov++; |
| 1235 | |
| 1236 | for (h = cap_hdr; h; h = h->next) { |
| 1237 | if ((h->namelen == col - sol) && |
| 1238 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1239 | if (cap[h->index] == NULL) |
| 1240 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1241 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1242 | |
| 1243 | if (cap[h->index] == NULL) { |
| 1244 | Alert("HTTP capture : out of memory.\n"); |
| 1245 | continue; |
| 1246 | } |
| 1247 | |
| 1248 | len = eol - sov; |
| 1249 | if (len > h->len) |
| 1250 | len = h->len; |
| 1251 | |
| 1252 | memcpy(cap[h->index], sov, len); |
| 1253 | cap[h->index][len]=0; |
| 1254 | } |
| 1255 | } |
| 1256 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1257 | cur_idx = idx->v[cur_idx].next; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1262 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1263 | */ |
| 1264 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1265 | |
| 1266 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1267 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1268 | */ |
| 1269 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1270 | ptr++; \ |
| 1271 | if (likely(ptr < end)) \ |
| 1272 | goto good; \ |
| 1273 | else { \ |
| 1274 | state = (st); \ |
| 1275 | goto http_msg_ood; \ |
| 1276 | } \ |
| 1277 | } while (0) |
| 1278 | |
| 1279 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1280 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1281 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1282 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1283 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1284 | * will give undefined results. |
| 1285 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1286 | * and that msg->sol points to the beginning of the response. |
| 1287 | * If a complete line is found (which implies that at least one CR or LF is |
| 1288 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1289 | * returned indicating an incomplete line (which does not mean that parts have |
| 1290 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1291 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1292 | * upon next call. |
| 1293 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1294 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1296 | * 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] | 1297 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1299 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1300 | unsigned int state, const char *ptr, const char *end, |
| 1301 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1302 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1303 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1304 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1305 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1306 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1307 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1308 | |
| 1309 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1310 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1311 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1312 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1313 | state = HTTP_MSG_ERROR; |
| 1314 | break; |
| 1315 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1317 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1318 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1319 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1320 | goto http_msg_rpcode; |
| 1321 | } |
| 1322 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1323 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1324 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1325 | state = HTTP_MSG_ERROR; |
| 1326 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1327 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1328 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1329 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1331 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1332 | |
| 1333 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1334 | 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] | 1335 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1336 | } |
| 1337 | |
| 1338 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1339 | 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] | 1340 | http_msg_rsp_reason: |
| 1341 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1342 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1343 | msg->sl.st.r_l = 0; |
| 1344 | goto http_msg_rpline_eol; |
| 1345 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1347 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1349 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1350 | goto http_msg_rpreason; |
| 1351 | } |
| 1352 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1353 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1354 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1355 | goto http_msg_rsp_reason; |
| 1356 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1357 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1358 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1359 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1360 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1361 | 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] | 1362 | http_msg_rpline_eol: |
| 1363 | /* We have seen the end of line. Note that we do not |
| 1364 | * necessarily have the \n yet, but at least we know that we |
| 1365 | * have EITHER \r OR \n, otherwise the response would not be |
| 1366 | * complete. We can then record the response length and return |
| 1367 | * to the caller which will be able to register it. |
| 1368 | */ |
| 1369 | msg->sl.st.l = ptr - msg->sol; |
| 1370 | return ptr; |
| 1371 | |
| 1372 | #ifdef DEBUG_FULL |
| 1373 | default: |
| 1374 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1375 | exit(1); |
| 1376 | #endif |
| 1377 | } |
| 1378 | |
| 1379 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1380 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1381 | if (ret_state) |
| 1382 | *ret_state = state; |
| 1383 | if (ret_ptr) |
| 1384 | *ret_ptr = (char *)ptr; |
| 1385 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1388 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | * This function parses a request line between <ptr> and <end>, starting with |
| 1390 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1391 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1392 | * will give undefined results. |
| 1393 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1394 | * and that msg->sol points to the beginning of the request. |
| 1395 | * If a complete line is found (which implies that at least one CR or LF is |
| 1396 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1397 | * returned indicating an incomplete line (which does not mean that parts have |
| 1398 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1399 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1400 | * upon next call. |
| 1401 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1402 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1404 | * 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] | 1405 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1406 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1407 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1408 | unsigned int state, const char *ptr, const char *end, |
| 1409 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1410 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1413 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1415 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1416 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1418 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1419 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1420 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1421 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1423 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1424 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | http_msg_req09_uri: |
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 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1428 | 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] | 1429 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1430 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | msg->sl.rq.v_l = 0; |
| 1432 | goto http_msg_rqline_eol; |
| 1433 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1434 | state = HTTP_MSG_ERROR; |
| 1435 | break; |
| 1436 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1438 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1440 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1441 | goto http_msg_rquri; |
| 1442 | } |
| 1443 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1444 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1445 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1446 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1447 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1449 | http_msg_rquri: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1451 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1452 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1454 | 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] | 1455 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1456 | } |
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 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1459 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1462 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1464 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | goto http_msg_rqver; |
| 1466 | } |
| 1467 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1468 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1469 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1470 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1471 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1472 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1473 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1474 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1476 | |
| 1477 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1478 | 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] | 1479 | http_msg_rqline_eol: |
| 1480 | /* We have seen the end of line. Note that we do not |
| 1481 | * necessarily have the \n yet, but at least we know that we |
| 1482 | * have EITHER \r OR \n, otherwise the request would not be |
| 1483 | * complete. We can then record the request length and return |
| 1484 | * to the caller which will be able to register it. |
| 1485 | */ |
| 1486 | msg->sl.rq.l = ptr - msg->sol; |
| 1487 | return ptr; |
| 1488 | } |
| 1489 | |
| 1490 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1491 | state = HTTP_MSG_ERROR; |
| 1492 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1493 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1494 | #ifdef DEBUG_FULL |
| 1495 | default: |
| 1496 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1497 | exit(1); |
| 1498 | #endif |
| 1499 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1502 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | if (ret_state) |
| 1504 | *ret_state = state; |
| 1505 | if (ret_ptr) |
| 1506 | *ret_ptr = (char *)ptr; |
| 1507 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1509 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1510 | /* |
| 1511 | * Returns the data from Authorization header. Function may be called more |
| 1512 | * than once so data is stored in txn->auth_data. When no header is found |
| 1513 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1514 | * searching again for something we are unable to find anyway. |
| 1515 | */ |
| 1516 | |
| 1517 | char get_http_auth_buff[BUFSIZE]; |
| 1518 | |
| 1519 | int |
| 1520 | get_http_auth(struct session *s) |
| 1521 | { |
| 1522 | |
| 1523 | struct http_txn *txn = &s->txn; |
| 1524 | struct chunk auth_method; |
| 1525 | struct hdr_ctx ctx; |
| 1526 | char *h, *p; |
| 1527 | int len; |
| 1528 | |
| 1529 | #ifdef DEBUG_AUTH |
| 1530 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1531 | #endif |
| 1532 | |
| 1533 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1534 | return 0; |
| 1535 | |
| 1536 | if (txn->auth.method) |
| 1537 | return 1; |
| 1538 | |
| 1539 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1540 | |
| 1541 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1542 | |
| 1543 | if (txn->flags & TX_USE_PX_CONN) { |
| 1544 | h = "Proxy-Authorization"; |
| 1545 | len = strlen(h); |
| 1546 | } else { |
| 1547 | h = "Authorization"; |
| 1548 | len = strlen(h); |
| 1549 | } |
| 1550 | |
| 1551 | 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] | 1552 | return 0; |
| 1553 | |
| 1554 | h = ctx.line + ctx.val; |
| 1555 | |
| 1556 | p = memchr(h, ' ', ctx.vlen); |
| 1557 | if (!p || p == h) |
| 1558 | return 0; |
| 1559 | |
| 1560 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1561 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1562 | |
| 1563 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1564 | |
| 1565 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1566 | get_http_auth_buff, BUFSIZE - 1); |
| 1567 | |
| 1568 | if (len < 0) |
| 1569 | return 0; |
| 1570 | |
| 1571 | |
| 1572 | get_http_auth_buff[len] = '\0'; |
| 1573 | |
| 1574 | p = strchr(get_http_auth_buff, ':'); |
| 1575 | |
| 1576 | if (!p) |
| 1577 | return 0; |
| 1578 | |
| 1579 | txn->auth.user = get_http_auth_buff; |
| 1580 | *p = '\0'; |
| 1581 | txn->auth.pass = p+1; |
| 1582 | |
| 1583 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1584 | return 1; |
| 1585 | } |
| 1586 | |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1590 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1591 | /* |
| 1592 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1593 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1594 | * when data are missing and recalled at the exact same location with no |
| 1595 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1596 | * 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] | 1597 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1598 | * the first state, so that none of the msg pointers has to be initialized |
| 1599 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1600 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1601 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1602 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1603 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1604 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1605 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1606 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | ptr = buf->lr; |
| 1608 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1609 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | if (unlikely(ptr >= end)) |
| 1611 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1612 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1613 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1614 | /* |
| 1615 | * First, states that are specific to the response only. |
| 1616 | * We check them first so that request and headers are |
| 1617 | * closer to each other (accessed more often). |
| 1618 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1619 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1620 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1621 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1622 | /* we have a start of message, but we have to check |
| 1623 | * first if we need to remove some CRLF. We can only |
| 1624 | * do this when send_max=0. |
| 1625 | */ |
| 1626 | char *beg = buf->w + buf->send_max; |
| 1627 | if (beg >= buf->data + buf->size) |
| 1628 | beg -= buf->size; |
| 1629 | if (unlikely(ptr != beg)) { |
| 1630 | if (buf->send_max) |
| 1631 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1632 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1633 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1634 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1635 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1636 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1637 | hdr_idx_init(idx); |
| 1638 | state = HTTP_MSG_RPVER; |
| 1639 | goto http_msg_rpver; |
| 1640 | } |
| 1641 | |
| 1642 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1643 | goto http_msg_invalid; |
| 1644 | |
| 1645 | if (unlikely(*ptr == '\n')) |
| 1646 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1647 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1648 | /* stop here */ |
| 1649 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1650 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1651 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1653 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1654 | /* stop here */ |
| 1655 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1656 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1657 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1658 | case HTTP_MSG_RPVER_SP: |
| 1659 | case HTTP_MSG_RPCODE: |
| 1660 | case HTTP_MSG_RPCODE_SP: |
| 1661 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1662 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1663 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1664 | if (unlikely(!ptr)) |
| 1665 | return; |
| 1666 | |
| 1667 | /* we have a full response and we know that we have either a CR |
| 1668 | * or an LF at <ptr>. |
| 1669 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1670 | //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] | 1671 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1672 | |
| 1673 | msg->sol = ptr; |
| 1674 | if (likely(*ptr == '\r')) |
| 1675 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1676 | goto http_msg_rpline_end; |
| 1677 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1678 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1679 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1680 | /* msg->sol must point to the first of CR or LF. */ |
| 1681 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1682 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1683 | /* stop here */ |
| 1684 | |
| 1685 | /* |
| 1686 | * Second, states that are specific to the request only |
| 1687 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1688 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1689 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1691 | /* we have a start of message, but we have to check |
| 1692 | * first if we need to remove some CRLF. We can only |
| 1693 | * do this when send_max=0. |
| 1694 | */ |
| 1695 | char *beg = buf->w + buf->send_max; |
| 1696 | if (beg >= buf->data + buf->size) |
| 1697 | beg -= buf->size; |
| 1698 | if (likely(ptr != beg)) { |
| 1699 | if (buf->send_max) |
| 1700 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1701 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1702 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1704 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1705 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1706 | /* we will need this when keep-alive will be supported |
| 1707 | hdr_idx_init(idx); |
| 1708 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1709 | state = HTTP_MSG_RQMETH; |
| 1710 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1711 | } |
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 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1714 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1715 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | if (unlikely(*ptr == '\n')) |
| 1717 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1718 | 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] | 1719 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1720 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1722 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1723 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1724 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1725 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1726 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1728 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | case HTTP_MSG_RQMETH_SP: |
| 1730 | case HTTP_MSG_RQURI: |
| 1731 | case HTTP_MSG_RQURI_SP: |
| 1732 | case HTTP_MSG_RQVER: |
| 1733 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1734 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1735 | if (unlikely(!ptr)) |
| 1736 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1737 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | /* we have a full request and we know that we have either a CR |
| 1739 | * or an LF at <ptr>. |
| 1740 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1741 | //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] | 1742 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1743 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1744 | msg->sol = ptr; |
| 1745 | if (likely(*ptr == '\r')) |
| 1746 | 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] | 1747 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1748 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1749 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1750 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | /* check for HTTP/0.9 request : no version information available. |
| 1752 | * msg->sol must point to the first of CR or LF. |
| 1753 | */ |
| 1754 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1755 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1756 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1757 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1758 | 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] | 1759 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1761 | /* |
| 1762 | * Common states below |
| 1763 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1765 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1766 | msg->sol = ptr; |
| 1767 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1768 | goto http_msg_hdr_name; |
| 1769 | } |
| 1770 | |
| 1771 | if (likely(*ptr == '\r')) |
| 1772 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1773 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1776 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | /* assumes msg->sol points to the first char */ |
| 1778 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1779 | 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] | 1780 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1781 | if (likely(*ptr == ':')) { |
| 1782 | msg->col = ptr - buf->data; |
| 1783 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1784 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1785 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1786 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1787 | goto http_msg_invalid; |
| 1788 | |
| 1789 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1790 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1791 | |
| 1792 | /* and we still accept this non-token character */ |
| 1793 | 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] | 1794 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1795 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1796 | http_msg_hdr_l1_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1798 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1799 | 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] | 1800 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | /* header value can be basically anything except CR/LF */ |
| 1802 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1803 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1804 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1805 | goto http_msg_hdr_val; |
| 1806 | } |
| 1807 | |
| 1808 | if (likely(*ptr == '\r')) |
| 1809 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1810 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1811 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1812 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1813 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1815 | 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] | 1816 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1817 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1818 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1820 | /* replace HT,CR,LF with spaces */ |
| 1821 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1822 | buf->data[msg->sov] = ' '; |
| 1823 | goto http_msg_hdr_l1_sp; |
| 1824 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1825 | /* we had a header consisting only in spaces ! */ |
| 1826 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | goto http_msg_complete_header; |
| 1828 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1829 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1830 | http_msg_hdr_val: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | /* assumes msg->sol points to the first char, msg->col to the |
| 1832 | * colon, and msg->sov points to the first character of the |
| 1833 | * value. |
| 1834 | */ |
| 1835 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1836 | 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] | 1837 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | msg->eol = ptr; |
| 1839 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1840 | * real header end in case it ends with lots of LWS, but is this |
| 1841 | * really needed ? |
| 1842 | */ |
| 1843 | if (likely(*ptr == '\r')) |
| 1844 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1845 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1846 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1847 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1848 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1849 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1850 | 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] | 1851 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1852 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1853 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1855 | /* LWS: replace HT,CR,LF with spaces */ |
| 1856 | for (; msg->eol < ptr; msg->eol++) |
| 1857 | *msg->eol = ' '; |
| 1858 | goto http_msg_hdr_val; |
| 1859 | } |
| 1860 | http_msg_complete_header: |
| 1861 | /* |
| 1862 | * It was a new header, so the last one is finished. |
| 1863 | * Assumes msg->sol points to the first char, msg->col to the |
| 1864 | * colon, msg->sov points to the first character of the value |
| 1865 | * and msg->eol to the first CR or LF so we know how the line |
| 1866 | * ends. We insert last header into the index. |
| 1867 | */ |
| 1868 | /* |
| 1869 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1870 | write(2, msg->sol, msg->eol-msg->sol); |
| 1871 | fprintf(stderr,"\n"); |
| 1872 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1873 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1874 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1875 | idx, idx->tail) < 0)) |
| 1876 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1877 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1878 | msg->sol = ptr; |
| 1879 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1880 | goto http_msg_hdr_name; |
| 1881 | } |
| 1882 | |
| 1883 | if (likely(*ptr == '\r')) |
| 1884 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1885 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1887 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1888 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1889 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1890 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1891 | ptr++; |
| 1892 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1893 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1894 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1895 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1896 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1897 | return; |
| 1898 | #ifdef DEBUG_FULL |
| 1899 | default: |
| 1900 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1901 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1902 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1903 | } |
| 1904 | http_msg_ood: |
| 1905 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1906 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1907 | buf->lr = ptr; |
| 1908 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1909 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1910 | http_msg_invalid: |
| 1911 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1912 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1913 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1914 | return; |
| 1915 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1916 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1917 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1918 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1919 | * nothing is done and 1 is returned. |
| 1920 | */ |
| 1921 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1922 | { |
| 1923 | int delta; |
| 1924 | char *cur_end; |
| 1925 | |
| 1926 | if (msg->sl.rq.v_l != 0) |
| 1927 | return 1; |
| 1928 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1929 | cur_end = msg->sol + msg->sl.rq.l; |
| 1930 | delta = 0; |
| 1931 | |
| 1932 | if (msg->sl.rq.u_l == 0) { |
| 1933 | /* if no URI was set, add "/" */ |
| 1934 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1935 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1936 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1937 | } |
| 1938 | /* add HTTP version */ |
| 1939 | 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] | 1940 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1941 | cur_end += delta; |
| 1942 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1943 | HTTP_MSG_RQMETH, |
| 1944 | msg->sol, cur_end + 1, |
| 1945 | NULL, NULL); |
| 1946 | if (unlikely(!cur_end)) |
| 1947 | return 0; |
| 1948 | |
| 1949 | /* we have a full HTTP/1.0 request now and we know that |
| 1950 | * we have either a CR or an LF at <ptr>. |
| 1951 | */ |
| 1952 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1953 | return 1; |
| 1954 | } |
| 1955 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1956 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1957 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1958 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1959 | * are used for that : |
| 1960 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1961 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1962 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1963 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1964 | * harmless combinations may be removed. Do not call that after changes have |
| 1965 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1966 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1967 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1968 | 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] | 1969 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1970 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1971 | const char *hdr_val = "Connection"; |
| 1972 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1973 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1974 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1975 | return; |
| 1976 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1977 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1978 | hdr_val = "Proxy-Connection"; |
| 1979 | hdr_len = 16; |
| 1980 | } |
| 1981 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1982 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1983 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1984 | 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] | 1985 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1986 | txn->flags |= TX_HDR_CONN_KAL; |
| 1987 | if ((to_del & 2) && buf) |
| 1988 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1989 | else |
| 1990 | txn->flags |= TX_CON_KAL_SET; |
| 1991 | } |
| 1992 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1993 | txn->flags |= TX_HDR_CONN_CLO; |
| 1994 | if ((to_del & 1) && buf) |
| 1995 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1996 | else |
| 1997 | txn->flags |= TX_CON_CLO_SET; |
| 1998 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2001 | txn->flags |= TX_HDR_CONN_PRS; |
| 2002 | return; |
| 2003 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2004 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2005 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2006 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2007 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2008 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2009 | */ |
| 2010 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 2011 | { |
| 2012 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2013 | const char *hdr_val = "Connection"; |
| 2014 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2015 | |
| 2016 | ctx.idx = 0; |
| 2017 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2018 | |
| 2019 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2020 | hdr_val = "Proxy-Connection"; |
| 2021 | hdr_len = 16; |
| 2022 | } |
| 2023 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2024 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2025 | 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] | 2026 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2027 | if (wanted & TX_CON_KAL_SET) |
| 2028 | txn->flags |= TX_CON_KAL_SET; |
| 2029 | else |
| 2030 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2031 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2032 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2033 | if (wanted & TX_CON_CLO_SET) |
| 2034 | txn->flags |= TX_CON_CLO_SET; |
| 2035 | else |
| 2036 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2037 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2038 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2039 | |
| 2040 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2041 | return; |
| 2042 | |
| 2043 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2044 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2045 | hdr_val = "Connection: close"; |
| 2046 | hdr_len = 17; |
| 2047 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2048 | hdr_val = "Proxy-Connection: close"; |
| 2049 | hdr_len = 23; |
| 2050 | } |
| 2051 | 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] | 2052 | } |
| 2053 | |
| 2054 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2055 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2056 | hdr_val = "Connection: keep-alive"; |
| 2057 | hdr_len = 22; |
| 2058 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2059 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2060 | hdr_len = 28; |
| 2061 | } |
| 2062 | 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] | 2063 | } |
| 2064 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2065 | } |
| 2066 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2067 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2068 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2069 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2070 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2071 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2072 | * 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] | 2073 | * 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] | 2074 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2075 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2076 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2077 | char *ptr = buf->lr; |
| 2078 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2079 | unsigned int chunk = 0; |
| 2080 | |
| 2081 | /* The chunk size is in the following form, though we are only |
| 2082 | * interested in the size and CRLF : |
| 2083 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2084 | */ |
| 2085 | while (1) { |
| 2086 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2087 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2088 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2089 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2090 | if (c < 0) /* not a hex digit anymore */ |
| 2091 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2092 | if (++ptr >= end) |
| 2093 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2094 | if (chunk & 0xF000000) /* overflow will occur */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2095 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2096 | chunk = (chunk << 4) + c; |
| 2097 | } |
| 2098 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2099 | /* empty size not allowed */ |
| 2100 | if (ptr == buf->lr) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2101 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2102 | |
| 2103 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2104 | if (++ptr >= end) |
| 2105 | ptr = buf->data; |
| 2106 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2107 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2108 | } |
| 2109 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2110 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2111 | * for the end of chunk size. |
| 2112 | */ |
| 2113 | while (1) { |
| 2114 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2115 | /* we now have a CR or an LF at ptr */ |
| 2116 | if (likely(*ptr == '\r')) { |
| 2117 | if (++ptr >= end) |
| 2118 | ptr = buf->data; |
| 2119 | if (ptr == buf->r) |
| 2120 | return 0; |
| 2121 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2122 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2123 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2124 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2125 | if (++ptr >= end) |
| 2126 | ptr = buf->data; |
| 2127 | /* done */ |
| 2128 | break; |
| 2129 | } |
| 2130 | else if (*ptr == ';') { |
| 2131 | /* chunk extension, ends at next CRLF */ |
| 2132 | if (++ptr >= end) |
| 2133 | ptr = buf->data; |
| 2134 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2135 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2136 | |
| 2137 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2138 | if (++ptr >= end) |
| 2139 | ptr = buf->data; |
| 2140 | if (ptr == buf->r) |
| 2141 | return 0; |
| 2142 | } |
| 2143 | /* we have a CRLF now, loop above */ |
| 2144 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2145 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2146 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2147 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2150 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2151 | * which may or may not be present. We save that into ->lr and |
| 2152 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2153 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2154 | msg->sov += ptr - buf->lr; |
| 2155 | buf->lr = ptr; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2156 | msg->chunk_len = chunk; |
| 2157 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2158 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2159 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2160 | error: |
| 2161 | msg->err_pos = ptr - buf->data; |
| 2162 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2163 | } |
| 2164 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2165 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2166 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2167 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2168 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2169 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2170 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2171 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2172 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2173 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2174 | * which implies that all non-trailers data have already been scheduled for |
| 2175 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2176 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2177 | * important to note that this function is designed to be able to parse wrapped |
| 2178 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2179 | */ |
| 2180 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2181 | { |
| 2182 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2183 | while (1) { |
| 2184 | char *p1 = NULL, *p2 = NULL; |
| 2185 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2186 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2187 | |
| 2188 | /* scan current line and stop at LF or CRLF */ |
| 2189 | while (1) { |
| 2190 | if (ptr == buf->r) |
| 2191 | return 0; |
| 2192 | |
| 2193 | if (*ptr == '\n') { |
| 2194 | if (!p1) |
| 2195 | p1 = ptr; |
| 2196 | p2 = ptr; |
| 2197 | break; |
| 2198 | } |
| 2199 | |
| 2200 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2201 | if (p1) { |
| 2202 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2203 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2204 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2205 | p1 = ptr; |
| 2206 | } |
| 2207 | |
| 2208 | ptr++; |
| 2209 | if (ptr >= buf->data + buf->size) |
| 2210 | ptr = buf->data; |
| 2211 | } |
| 2212 | |
| 2213 | /* after LF; point to beginning of next line */ |
| 2214 | p2++; |
| 2215 | if (p2 >= buf->data + buf->size) |
| 2216 | p2 = buf->data; |
| 2217 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2218 | bytes = p2 - buf->lr; |
| 2219 | if (bytes < 0) |
| 2220 | bytes += buf->size; |
| 2221 | |
| 2222 | /* schedule this line for forwarding */ |
| 2223 | msg->sov += bytes; |
| 2224 | if (msg->sov >= buf->size) |
| 2225 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2226 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2227 | if (p1 == buf->lr) { |
| 2228 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2229 | * Everything was scheduled for forwarding, there's nothing |
| 2230 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2231 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2232 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2233 | msg->msg_state = HTTP_MSG_DONE; |
| 2234 | return 1; |
| 2235 | } |
| 2236 | /* OK, next line then */ |
| 2237 | buf->lr = p2; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2242 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2243 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2244 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2245 | * not enough data are available, the function does not change anything and |
| 2246 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2247 | * does not change anything. Note: this function is designed to parse wrapped |
| 2248 | * CRLF at the end of the buffer. |
| 2249 | */ |
| 2250 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2251 | { |
| 2252 | char *ptr; |
| 2253 | int bytes; |
| 2254 | |
| 2255 | /* NB: we'll check data availabilty at the end. It's not a |
| 2256 | * problem because whatever we match first will be checked |
| 2257 | * against the correct length. |
| 2258 | */ |
| 2259 | bytes = 1; |
| 2260 | ptr = buf->lr; |
| 2261 | if (*ptr == '\r') { |
| 2262 | bytes++; |
| 2263 | ptr++; |
| 2264 | if (ptr >= buf->data + buf->size) |
| 2265 | ptr = buf->data; |
| 2266 | } |
| 2267 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 2268 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2269 | return 0; |
| 2270 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2271 | if (*ptr != '\n') { |
| 2272 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2273 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2274 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2275 | |
| 2276 | ptr++; |
| 2277 | if (ptr >= buf->data + buf->size) |
| 2278 | ptr = buf->data; |
| 2279 | buf->lr = ptr; |
| 2280 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2281 | msg->sov = ptr - buf->data; |
| 2282 | msg->som = msg->sov - bytes; |
| 2283 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2284 | return 1; |
| 2285 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2286 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2287 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2288 | { |
| 2289 | char *end = buf->data + buf->size; |
| 2290 | int off = buf->data + buf->size - buf->w; |
| 2291 | |
| 2292 | /* two possible cases : |
| 2293 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2294 | * - 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] | 2295 | */ |
| 2296 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2297 | int block1 = buf->l; |
| 2298 | int block2 = 0; |
| 2299 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2300 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2301 | block1 = buf->data + buf->size - buf->w; |
| 2302 | block2 = buf->r - buf->data; |
| 2303 | } |
| 2304 | if (block2) |
| 2305 | memcpy(swap_buffer, buf->data, block2); |
| 2306 | memmove(buf->data, buf->w, block1); |
| 2307 | if (block2) |
| 2308 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | /* adjust all known pointers */ |
| 2312 | buf->w = buf->data; |
| 2313 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2314 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2315 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2316 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2317 | |
| 2318 | /* adjust relative pointers */ |
| 2319 | msg->som = 0; |
| 2320 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2321 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2322 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2323 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2324 | if (msg->err_pos >= 0) { |
| 2325 | msg->err_pos += off; |
| 2326 | if (msg->err_pos >= buf->size) |
| 2327 | msg->err_pos -= buf->size; |
| 2328 | } |
| 2329 | |
| 2330 | buf->flags &= ~BF_FULL; |
| 2331 | if (buf->l >= buffer_max_len(buf)) |
| 2332 | buf->flags |= BF_FULL; |
| 2333 | } |
| 2334 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2335 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2336 | * processing can continue on next analysers, or zero if it either needs more |
| 2337 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2338 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2339 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2340 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2341 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2342 | 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] | 2343 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2344 | /* |
| 2345 | * We will parse the partial (or complete) lines. |
| 2346 | * We will check the request syntax, and also join multi-line |
| 2347 | * headers. An index of all the lines will be elaborated while |
| 2348 | * parsing. |
| 2349 | * |
| 2350 | * For the parsing, we use a 28 states FSM. |
| 2351 | * |
| 2352 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2353 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2354 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2355 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2356 | * req->lr = first non-visited byte |
| 2357 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2358 | * |
| 2359 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2360 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2361 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2362 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2363 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2364 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2365 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2366 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2367 | struct http_txn *txn = &s->txn; |
| 2368 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2369 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2370 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2371 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2372 | now_ms, __FUNCTION__, |
| 2373 | s, |
| 2374 | req, |
| 2375 | req->rex, req->wex, |
| 2376 | req->flags, |
| 2377 | req->l, |
| 2378 | req->analysers); |
| 2379 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2380 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2381 | s->srv_error = http_return_srv_error; |
| 2382 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2383 | /* There's a protected area at the end of the buffer for rewriting |
| 2384 | * purposes. We don't want to start to parse the request if the |
| 2385 | * protected area is affected, because we may have to move processed |
| 2386 | * data later, which is much more complicated. |
| 2387 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2388 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2389 | if ((txn->flags & TX_NOT_FIRST) && |
| 2390 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2391 | req->r < req->lr || |
| 2392 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2393 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2394 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2395 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2396 | /* some data has still not left the buffer, wake us once that's done */ |
| 2397 | buffer_dont_connect(req); |
| 2398 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2399 | return 0; |
| 2400 | } |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2401 | if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2402 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2405 | /* Note that we have the same problem with the response ; we |
| 2406 | * may want to send a redirect, error or anything which requires |
| 2407 | * some spare space. So we'll ensure that we have at least |
| 2408 | * maxrewrite bytes available in the response buffer before |
| 2409 | * processing that one. This will only affect pipelined |
| 2410 | * keep-alive requests. |
| 2411 | */ |
| 2412 | if ((txn->flags & TX_NOT_FIRST) && |
| 2413 | unlikely((s->rep->flags & BF_FULL) || |
| 2414 | s->rep->r < s->rep->lr || |
| 2415 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2416 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2417 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2418 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2419 | /* don't let a connection request be initiated */ |
| 2420 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2421 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2422 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2423 | return 0; |
| 2424 | } |
| 2425 | } |
| 2426 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2427 | if (likely(req->lr < req->r)) |
| 2428 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2429 | } |
| 2430 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2431 | /* 1: we might have to print this header in debug mode */ |
| 2432 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2433 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2434 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2435 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2436 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2437 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2438 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2439 | eol = sol + msg->sl.rq.l; |
| 2440 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2441 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2442 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2443 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2444 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2445 | while (cur_idx) { |
| 2446 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2447 | debug_hdr("clihdr", s, sol, eol); |
| 2448 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2449 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2450 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2453 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2454 | /* |
| 2455 | * Now we quickly check if we have found a full valid request. |
| 2456 | * If not so, we check the FD and buffer states before leaving. |
| 2457 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2458 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2459 | * requests are checked first. When waiting for a second request |
| 2460 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2461 | * we note the error in the session flags but don't set any state. |
| 2462 | * Since the error will be noted there, it will not be counted by |
| 2463 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2464 | * Last, we may increase some tracked counters' http request errors on |
| 2465 | * the cases that are deliberately the client's fault. For instance, |
| 2466 | * a timeout or connection reset is not counted as an error. However |
| 2467 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2468 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2469 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2470 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2471 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2472 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2473 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2474 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2475 | session_inc_http_req_ctr(s); |
| 2476 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2477 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2478 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2479 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2480 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2481 | /* 1: Since we are in header mode, if there's no space |
| 2482 | * left for headers, we won't be able to free more |
| 2483 | * later, so the session will never terminate. We |
| 2484 | * must terminate it now. |
| 2485 | */ |
| 2486 | if (unlikely(req->flags & BF_FULL)) { |
| 2487 | /* FIXME: check if URI is set and return Status |
| 2488 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2489 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2490 | session_inc_http_req_ctr(s); |
| 2491 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2492 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2493 | if (msg->err_pos < 0) |
| 2494 | msg->err_pos = req->l; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2495 | goto return_bad_req; |
| 2496 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2497 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2498 | /* 2: have we encountered a read error ? */ |
| 2499 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2500 | if (!(s->flags & SN_ERR_MASK)) |
| 2501 | s->flags |= SN_ERR_CLICL; |
| 2502 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2503 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2504 | goto failed_keep_alive; |
| 2505 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2506 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2507 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2508 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2509 | session_inc_http_err_ctr(s); |
| 2510 | } |
| 2511 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2512 | msg->msg_state = HTTP_MSG_ERROR; |
| 2513 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2514 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2515 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2516 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2517 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2518 | if (s->listener->counters) |
| 2519 | s->listener->counters->failed_req++; |
| 2520 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2521 | if (!(s->flags & SN_FINST_MASK)) |
| 2522 | s->flags |= SN_FINST_R; |
| 2523 | return 0; |
| 2524 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2525 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2526 | /* 3: has the read timeout expired ? */ |
| 2527 | 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] | 2528 | if (!(s->flags & SN_ERR_MASK)) |
| 2529 | s->flags |= SN_ERR_CLITO; |
| 2530 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2531 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2532 | goto failed_keep_alive; |
| 2533 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2534 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2535 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2536 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2537 | session_inc_http_err_ctr(s); |
| 2538 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2539 | txn->status = 408; |
| 2540 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2541 | msg->msg_state = HTTP_MSG_ERROR; |
| 2542 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2543 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2544 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2545 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2546 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2547 | if (s->listener->counters) |
| 2548 | s->listener->counters->failed_req++; |
| 2549 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | if (!(s->flags & SN_FINST_MASK)) |
| 2551 | s->flags |= SN_FINST_R; |
| 2552 | return 0; |
| 2553 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2554 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2555 | /* 4: have we encountered a close ? */ |
| 2556 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2557 | if (!(s->flags & SN_ERR_MASK)) |
| 2558 | s->flags |= SN_ERR_CLICL; |
| 2559 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2560 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2561 | goto failed_keep_alive; |
| 2562 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2563 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2564 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2565 | txn->status = 400; |
| 2566 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2567 | msg->msg_state = HTTP_MSG_ERROR; |
| 2568 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2569 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2570 | session_inc_http_err_ctr(s); |
| 2571 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2572 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2573 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2574 | if (s->listener->counters) |
| 2575 | s->listener->counters->failed_req++; |
| 2576 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2577 | if (!(s->flags & SN_FINST_MASK)) |
| 2578 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2579 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2580 | } |
| 2581 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2582 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2583 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2584 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2585 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2586 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2587 | /* If the client starts to talk, let's fall back to |
| 2588 | * request timeout processing. |
| 2589 | */ |
| 2590 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2591 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2592 | } |
| 2593 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2594 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2595 | if (!tick_isset(req->analyse_exp)) { |
| 2596 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2597 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2598 | tick_isset(s->be->timeout.httpka)) |
| 2599 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2600 | else |
| 2601 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2602 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2603 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2604 | /* we're not ready yet */ |
| 2605 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2606 | |
| 2607 | failed_keep_alive: |
| 2608 | /* Here we process low-level errors for keep-alive requests. In |
| 2609 | * short, if the request is not the first one and it experiences |
| 2610 | * a timeout, read error or shutdown, we just silently close so |
| 2611 | * that the client can try again. |
| 2612 | */ |
| 2613 | txn->status = 0; |
| 2614 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2615 | req->analysers = 0; |
| 2616 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2617 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2618 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2619 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2620 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2621 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2622 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2623 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2624 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2625 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2626 | * points to the CRLF of the request line. req->lr points to the first |
| 2627 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2628 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2629 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2630 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2631 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2632 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2633 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2634 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2635 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2636 | /* kill the pending keep-alive timeout */ |
| 2637 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2638 | req->analyse_exp = TICK_ETERNITY; |
| 2639 | } |
| 2640 | |
| 2641 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2642 | /* Maybe we found in invalid header name while we were configured not |
| 2643 | * to block on that, so we have to capture it now. |
| 2644 | */ |
| 2645 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2646 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2647 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | /* |
| 2649 | * 1: identify the method |
| 2650 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2651 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | |
| 2653 | /* we can make use of server redirect on GET and HEAD */ |
| 2654 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2655 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2656 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2657 | /* |
| 2658 | * 2: check if the URI matches the monitor_uri. |
| 2659 | * We have to do this for every request which gets in, because |
| 2660 | * the monitor-uri is defined by the frontend. |
| 2661 | */ |
| 2662 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2663 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2664 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2665 | s->fe->monitor_uri, |
| 2666 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2667 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2668 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2669 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2670 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2671 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2672 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2673 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2674 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2676 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2677 | 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] | 2678 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2679 | ret = acl_pass(ret); |
| 2680 | if (cond->pol == ACL_COND_UNLESS) |
| 2681 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2682 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2683 | if (ret) { |
| 2684 | /* we fail this request, let's return 503 service unavail */ |
| 2685 | txn->status = 503; |
| 2686 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2687 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2688 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2689 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2690 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2691 | /* nothing to fail, let's reply normaly */ |
| 2692 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2693 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | goto return_prx_cond; |
| 2695 | } |
| 2696 | |
| 2697 | /* |
| 2698 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2699 | * Note: we cannot log anymore if the request has been |
| 2700 | * classified as invalid. |
| 2701 | */ |
| 2702 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2703 | /* we have a complete HTTP request that we must log */ |
| 2704 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2705 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2706 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2707 | if (urilen >= REQURI_LEN) |
| 2708 | urilen = REQURI_LEN - 1; |
| 2709 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2710 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2711 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2712 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2713 | s->do_log(s); |
| 2714 | } else { |
| 2715 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2716 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2717 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2718 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2719 | /* 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] | 2720 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2721 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2722 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2723 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2724 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2725 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2726 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2727 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2728 | txn->flags |= TX_REQ_VER_11; |
| 2729 | |
| 2730 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2731 | 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] | 2732 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2733 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2734 | * we have what looks like a proxied connection instead of a connection, |
| 2735 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2736 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2737 | * happen to do that despite being non-standard :-( |
| 2738 | * We consider that a request not beginning with either '/' or '*' is |
| 2739 | * a proxied connection, which covers both "scheme://location" and |
| 2740 | * CONNECT ip:port. |
| 2741 | */ |
| 2742 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2743 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2744 | txn->flags |= TX_USE_PX_CONN; |
| 2745 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2746 | /* transfer length unknown*/ |
| 2747 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2748 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2749 | /* 5: we may need to capture headers */ |
| 2750 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2751 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2752 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2753 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2754 | /* 6: determine the transfer-length. |
| 2755 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2756 | * the presence of a message-body in a REQUEST and its transfer length |
| 2757 | * must be determined that way (in order of precedence) : |
| 2758 | * 1. The presence of a message-body in a request is signaled by the |
| 2759 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2760 | * in the request's header fields. When a request message contains |
| 2761 | * both a message-body of non-zero length and a method that does |
| 2762 | * not define any semantics for that request message-body, then an |
| 2763 | * origin server SHOULD either ignore the message-body or respond |
| 2764 | * with an appropriate error message (e.g., 413). A proxy or |
| 2765 | * gateway, when presented the same request, SHOULD either forward |
| 2766 | * the request inbound with the message- body or ignore the |
| 2767 | * message-body when determining a response. |
| 2768 | * |
| 2769 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2770 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2771 | * transfer-length is defined by the use of this transfer-coding. |
| 2772 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2773 | * transfer-coding is not present, the transfer-length is defined |
| 2774 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2775 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2776 | * 3. If a Content-Length header field is present, its decimal value in |
| 2777 | * OCTETs represents both the entity-length and the transfer-length. |
| 2778 | * If a message is received with both a Transfer-Encoding header |
| 2779 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2780 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2781 | * 4. By the server closing the connection. (Closing the connection |
| 2782 | * cannot be used to indicate the end of a request body, since that |
| 2783 | * would leave no possibility for the server to send back a response.) |
| 2784 | * |
| 2785 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2786 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2787 | * it is terminated by closing the connection. When the "chunked" |
| 2788 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2789 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2790 | */ |
| 2791 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2792 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2793 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2794 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2795 | while ((txn->flags & TX_REQ_VER_11) && |
| 2796 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2797 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2798 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2799 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2800 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2801 | use_close_only = 1; |
| 2802 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2803 | break; |
| 2804 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2805 | } |
| 2806 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2807 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2808 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2809 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2810 | signed long long cl; |
| 2811 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2812 | if (!ctx.vlen) { |
| 2813 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2814 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2815 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2816 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2817 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 2818 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2819 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2820 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2821 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2822 | if (cl < 0) { |
| 2823 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2824 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2825 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2826 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2827 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) { |
| 2828 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2829 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2830 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2831 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2832 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2833 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2834 | } |
| 2835 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2836 | /* bodyless requests have a known length */ |
| 2837 | if (!use_close_only) |
| 2838 | txn->flags |= TX_REQ_XFER_LEN; |
| 2839 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2840 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2841 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2842 | req->analyse_exp = TICK_ETERNITY; |
| 2843 | return 1; |
| 2844 | |
| 2845 | return_bad_req: |
| 2846 | /* We centralize bad requests processing here */ |
| 2847 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2848 | /* we detected a parsing error. We want to archive this request |
| 2849 | * in the dedicated proxy area for later troubleshooting. |
| 2850 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2851 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2855 | txn->status = 400; |
| 2856 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2857 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2858 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2859 | if (s->listener->counters) |
| 2860 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2861 | |
| 2862 | return_prx_cond: |
| 2863 | if (!(s->flags & SN_ERR_MASK)) |
| 2864 | s->flags |= SN_ERR_PRXCOND; |
| 2865 | if (!(s->flags & SN_FINST_MASK)) |
| 2866 | s->flags |= SN_FINST_R; |
| 2867 | |
| 2868 | req->analysers = 0; |
| 2869 | req->analyse_exp = TICK_ETERNITY; |
| 2870 | return 0; |
| 2871 | } |
| 2872 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2873 | /* We reached the stats page through a POST request. |
| 2874 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2875 | * Returns 1 if request was parsed or zero if it needs more data. |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2876 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2877 | int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req) |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2878 | { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2879 | struct proxy *px; |
| 2880 | struct server *sv; |
| 2881 | |
| 2882 | char *backend = NULL; |
| 2883 | int action = 0; |
| 2884 | |
| 2885 | char *first_param, *cur_param, *next_param, *end_params; |
| 2886 | |
| 2887 | first_param = req->data + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2888 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2889 | |
| 2890 | cur_param = next_param = end_params; |
| 2891 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2892 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2893 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2894 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2895 | return 1; |
| 2896 | } |
| 2897 | else if (end_params > req->data + req->l) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2898 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2899 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2900 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | *end_params = '\0'; |
| 2904 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2905 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2906 | |
| 2907 | /* |
| 2908 | * Parse the parameters in reverse order to only store the last value. |
| 2909 | * From the html form, the backend and the action are at the end. |
| 2910 | */ |
| 2911 | while (cur_param > first_param) { |
| 2912 | char *key, *value; |
| 2913 | |
| 2914 | cur_param--; |
| 2915 | if ((*cur_param == '&') || (cur_param == first_param)) { |
| 2916 | /* Parse the key */ |
| 2917 | key = cur_param; |
| 2918 | if (cur_param != first_param) { |
| 2919 | /* delimit the string for the next loop */ |
| 2920 | *key++ = '\0'; |
| 2921 | } |
| 2922 | |
| 2923 | /* Parse the value */ |
| 2924 | value = key; |
| 2925 | while (*value != '\0' && *value != '=') { |
| 2926 | value++; |
| 2927 | } |
| 2928 | if (*value == '=') { |
| 2929 | /* Ok, a value is found, we can mark the end of the key */ |
| 2930 | *value++ = '\0'; |
| 2931 | } |
| 2932 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2933 | if (!url_decode(key) || !url_decode(value)) |
| 2934 | break; |
| 2935 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2936 | /* Now we can check the key to see what to do */ |
| 2937 | if (!backend && strcmp(key, "b") == 0) { |
| 2938 | backend = value; |
| 2939 | } |
| 2940 | else if (!action && strcmp(key, "action") == 0) { |
| 2941 | if (strcmp(value, "disable") == 0) { |
| 2942 | action = 1; |
| 2943 | } |
| 2944 | else if (strcmp(value, "enable") == 0) { |
| 2945 | action = 2; |
| 2946 | } else { |
| 2947 | /* unknown action, no need to continue */ |
| 2948 | break; |
| 2949 | } |
| 2950 | } |
| 2951 | else if (strcmp(key, "s") == 0) { |
| 2952 | if (backend && action && get_backend_server(backend, value, &px, &sv)) { |
| 2953 | switch (action) { |
| 2954 | case 1: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2955 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2956 | /* Not already in maintenance, we can change the server state */ |
| 2957 | sv->state |= SRV_MAINTAIN; |
| 2958 | set_server_down(sv); |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2959 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2960 | } |
| 2961 | break; |
| 2962 | case 2: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2963 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2964 | /* Already in maintenance, we can change the server state */ |
| 2965 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2966 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2967 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2968 | } |
| 2969 | break; |
| 2970 | } |
| 2971 | } |
| 2972 | } |
| 2973 | next_param = cur_param; |
| 2974 | } |
| 2975 | } |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2976 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2979 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2980 | * or NULL if everything's OK. |
| 2981 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2982 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2983 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2984 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2985 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2986 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2987 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2988 | int ret = 1; |
| 2989 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2990 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2991 | continue; |
| 2992 | |
| 2993 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2994 | if (rule->cond) { |
| 2995 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2996 | ret = acl_pass(ret); |
| 2997 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2998 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2999 | ret = !ret; |
| 3000 | } |
| 3001 | |
| 3002 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3003 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3004 | return NULL; /* no problem */ |
| 3005 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3006 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | return NULL; |
| 3010 | } |
| 3011 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3012 | /* This stream analyser runs all HTTP request processing which is common to |
| 3013 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3014 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3015 | * 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] | 3016 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3017 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3018 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3019 | 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] | 3020 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3021 | struct http_txn *txn = &s->txn; |
| 3022 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3023 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3024 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3025 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3026 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 3027 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3028 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3029 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3030 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3031 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3032 | return 0; |
| 3033 | } |
| 3034 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3035 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3036 | req->analyse_exp = TICK_ETERNITY; |
| 3037 | |
| 3038 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3039 | now_ms, __FUNCTION__, |
| 3040 | s, |
| 3041 | req, |
| 3042 | req->rex, req->wex, |
| 3043 | req->flags, |
| 3044 | req->l, |
| 3045 | req->analysers); |
| 3046 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3047 | /* first check whether we have some ACLs set to block this request */ |
| 3048 | list_for_each_entry(cond, &px->block_cond, list) { |
| 3049 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 3050 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3051 | ret = acl_pass(ret); |
| 3052 | if (cond->pol == ACL_COND_UNLESS) |
| 3053 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3054 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3055 | if (ret) { |
| 3056 | txn->status = 403; |
| 3057 | /* let's log the request time */ |
| 3058 | s->logs.tv_request = now; |
| 3059 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3060 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3061 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3062 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3063 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3064 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3065 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3066 | http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3067 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3068 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3069 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3070 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3071 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3072 | http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3073 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3074 | else |
| 3075 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3076 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3077 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3078 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3079 | txn->status = 403; |
| 3080 | s->logs.tv_request = now; |
| 3081 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3082 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 3083 | s->fe->fe_counters.denied_req++; |
| 3084 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 3085 | s->be->be_counters.denied_req++; |
| 3086 | if (s->listener->counters) |
| 3087 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3088 | goto return_prx_cond; |
| 3089 | } |
| 3090 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3091 | /* try headers filters */ |
| 3092 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3093 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3094 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3095 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3096 | /* has the request been denied ? */ |
| 3097 | if (txn->flags & TX_CLDENY) { |
| 3098 | /* no need to go further */ |
| 3099 | txn->status = 403; |
| 3100 | /* let's log the request time */ |
| 3101 | s->logs.tv_request = now; |
| 3102 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3103 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3104 | goto return_prx_cond; |
| 3105 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3106 | |
| 3107 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3108 | * which may be the same as the connect timeout if unspecified. |
| 3109 | * If unset, then set it to zero because we really want it to |
| 3110 | * eventually expire. We build the tarpit as an analyser. |
| 3111 | */ |
| 3112 | if (txn->flags & TX_CLTARPIT) { |
| 3113 | buffer_erase(s->req); |
| 3114 | /* wipe the request out so that we can drop the connection early |
| 3115 | * if the client closes first. |
| 3116 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3117 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3118 | req->analysers = 0; /* remove switching rules etc... */ |
| 3119 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3120 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3121 | if (!req->analyse_exp) |
| 3122 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3123 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3124 | return 1; |
| 3125 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3126 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3127 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3128 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3129 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3130 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3131 | * 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] | 3132 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3133 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3134 | * The method consists in checking if options changed between the two calls |
| 3135 | * (implying that either one is non-null, or one of them is non-null and we |
| 3136 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3137 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3138 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3139 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3140 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3141 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3142 | (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] | 3143 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3144 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3145 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3146 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3147 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3148 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3149 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3150 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3151 | tmp = TX_CON_WANT_CLO; |
| 3152 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3153 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3154 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3156 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3157 | /* parse the Connection header and possibly clean it */ |
| 3158 | int to_del = 0; |
| 3159 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3160 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3161 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3162 | to_del |= 2; /* remove "keep-alive" */ |
| 3163 | if (!(txn->flags & TX_REQ_VER_11)) |
| 3164 | to_del |= 1; /* remove "close" */ |
| 3165 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3166 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3167 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3168 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3169 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3170 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3171 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 3172 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3173 | ((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] | 3174 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 3175 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3176 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3177 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3178 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3179 | /* we can be blocked here because the request needs to be authenticated, |
| 3180 | * either to pass or to access stats. |
| 3181 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3182 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3183 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3184 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3185 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3186 | if (!realm) |
| 3187 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3188 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 3189 | 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] | 3190 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 3191 | txn->status = 401; |
| 3192 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3193 | /* on 401 we still count one error, because normal browsing |
| 3194 | * won't significantly increase the counter but brute force |
| 3195 | * attempts will. |
| 3196 | */ |
| 3197 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3198 | goto return_prx_cond; |
| 3199 | } |
| 3200 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3201 | /* add request headers from the rule sets in the same order */ |
| 3202 | list_for_each_entry(wl, &px->req_add, list) { |
| 3203 | if (wl->cond) { |
| 3204 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 3205 | ret = acl_pass(ret); |
| 3206 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3207 | ret = !ret; |
| 3208 | if (!ret) |
| 3209 | continue; |
| 3210 | } |
| 3211 | |
| 3212 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
| 3213 | goto return_bad_req; |
| 3214 | } |
| 3215 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3216 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3217 | struct stats_admin_rule *stats_admin_rule; |
| 3218 | |
| 3219 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3220 | * FIXME!!! that one is rather dangerous, we want to |
| 3221 | * make it follow standard rules (eg: clear req->analysers). |
| 3222 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3223 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3224 | /* now check whether we have some admin rules for this request */ |
| 3225 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 3226 | int ret = 1; |
| 3227 | |
| 3228 | if (stats_admin_rule->cond) { |
| 3229 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 3230 | ret = acl_pass(ret); |
| 3231 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3232 | ret = !ret; |
| 3233 | } |
| 3234 | |
| 3235 | if (ret) { |
| 3236 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3237 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3238 | break; |
| 3239 | } |
| 3240 | } |
| 3241 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3242 | /* Was the status page requested with a POST ? */ |
| 3243 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3244 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3245 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3246 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3247 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3248 | */ |
| 3249 | if (txn->flags & TX_REQ_VER_11) { |
| 3250 | struct hdr_ctx ctx; |
| 3251 | ctx.idx = 0; |
| 3252 | /* Expect is allowed in 1.1, look for it */ |
| 3253 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3254 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3255 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3256 | } |
| 3257 | } |
| 3258 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3259 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3260 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3261 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3262 | /* we need more data */ |
| 3263 | req->analysers |= an_bit; |
| 3264 | buffer_dont_connect(req); |
| 3265 | return 0; |
| 3266 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3267 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3268 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3269 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3270 | } |
| 3271 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3272 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3273 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3274 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3275 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3276 | s->rep->prod->applet.private = s; |
| 3277 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3278 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3279 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3280 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3281 | |
| 3282 | return 0; |
| 3283 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3284 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3285 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3286 | /* check whether we have some ACLs set to redirect this request */ |
| 3287 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3288 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3289 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3290 | if (rule->cond) { |
| 3291 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3292 | ret = acl_pass(ret); |
| 3293 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3294 | ret = !ret; |
| 3295 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3296 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3297 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3298 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3299 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3300 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3301 | /* build redirect message */ |
| 3302 | switch(rule->code) { |
| 3303 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3304 | msg_fmt = HTTP_303; |
| 3305 | break; |
| 3306 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3307 | msg_fmt = HTTP_301; |
| 3308 | break; |
| 3309 | case 302: |
| 3310 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3311 | msg_fmt = HTTP_302; |
| 3312 | break; |
| 3313 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3314 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3315 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3316 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3317 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3318 | switch(rule->type) { |
| 3319 | case REDIRECT_TYPE_PREFIX: { |
| 3320 | const char *path; |
| 3321 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3322 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3323 | path = http_get_path(txn); |
| 3324 | /* build message using path */ |
| 3325 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3326 | 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] | 3327 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3328 | int qs = 0; |
| 3329 | while (qs < pathlen) { |
| 3330 | if (path[qs] == '?') { |
| 3331 | pathlen = qs; |
| 3332 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3333 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3334 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3335 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3336 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3337 | } else { |
| 3338 | path = "/"; |
| 3339 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3340 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3341 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3342 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3343 | goto return_bad_req; |
| 3344 | |
| 3345 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3346 | * add anything, otherwise it makes it hard for the user to |
| 3347 | * configure a self-redirection. |
| 3348 | */ |
| 3349 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3350 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3351 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3352 | } |
| 3353 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3354 | /* add path */ |
| 3355 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3356 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3357 | |
| 3358 | /* append a slash at the end of the location is needed and missing */ |
| 3359 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3360 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3361 | if (rdr.len > rdr.size - 5) |
| 3362 | goto return_bad_req; |
| 3363 | rdr.str[rdr.len] = '/'; |
| 3364 | rdr.len++; |
| 3365 | } |
| 3366 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3367 | break; |
| 3368 | } |
| 3369 | case REDIRECT_TYPE_LOCATION: |
| 3370 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3371 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3372 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3373 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3374 | /* add location */ |
| 3375 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3376 | rdr.len += rule->rdr_len; |
| 3377 | break; |
| 3378 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3379 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3380 | if (rule->cookie_len) { |
| 3381 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3382 | rdr.len += 14; |
| 3383 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3384 | rdr.len += rule->cookie_len; |
| 3385 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3386 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3387 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3388 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3389 | /* add end of headers and the keep-alive/close status. |
| 3390 | * We may choose to set keep-alive if the Location begins |
| 3391 | * with a slash, because the client will come back to the |
| 3392 | * same server. |
| 3393 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3394 | txn->status = rule->code; |
| 3395 | /* let's log the request time */ |
| 3396 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3397 | |
| 3398 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3399 | (txn->flags & TX_REQ_XFER_LEN) && |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3400 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3401 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3402 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3403 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3404 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3405 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3406 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3407 | rdr.len += 30; |
| 3408 | } else { |
| 3409 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3410 | rdr.len += 24; |
| 3411 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3412 | } |
| 3413 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3414 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3415 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3416 | /* "eat" the request */ |
| 3417 | buffer_ignore(req, msg->sov - msg->som); |
| 3418 | msg->som = msg->sov; |
| 3419 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3420 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3421 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3422 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3423 | break; |
| 3424 | } else { |
| 3425 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3426 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3427 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3428 | rdr.len += 29; |
| 3429 | } else { |
| 3430 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3431 | rdr.len += 23; |
| 3432 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3433 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3434 | goto return_prx_cond; |
| 3435 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3436 | } |
| 3437 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3438 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3439 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3440 | * If this happens, then the data will not come immediately, so we must |
| 3441 | * send all what we have without waiting. Note that due to the small gain |
| 3442 | * in waiting for the body of the request, it's easier to simply put the |
| 3443 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3444 | * itself once used. |
| 3445 | */ |
| 3446 | req->flags |= BF_SEND_DONTWAIT; |
| 3447 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3448 | /* that's OK for us now, let's move on to next analysers */ |
| 3449 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3450 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3451 | return_bad_req: |
| 3452 | /* We centralize bad requests processing here */ |
| 3453 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3454 | /* we detected a parsing error. We want to archive this request |
| 3455 | * in the dedicated proxy area for later troubleshooting. |
| 3456 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3457 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3458 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3459 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3460 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3461 | txn->status = 400; |
| 3462 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3463 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3464 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3465 | if (s->listener->counters) |
| 3466 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3467 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3468 | return_prx_cond: |
| 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 | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3473 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3474 | req->analysers = 0; |
| 3475 | req->analyse_exp = TICK_ETERNITY; |
| 3476 | return 0; |
| 3477 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3478 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3479 | /* This function performs all the processing enabled for the current request. |
| 3480 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3481 | * needs more data, encounters an error, or wants to immediately abort the |
| 3482 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3483 | */ |
| 3484 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3485 | { |
| 3486 | struct http_txn *txn = &s->txn; |
| 3487 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3488 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3489 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3490 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3491 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3492 | return 0; |
| 3493 | } |
| 3494 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3495 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3496 | now_ms, __FUNCTION__, |
| 3497 | s, |
| 3498 | req, |
| 3499 | req->rex, req->wex, |
| 3500 | req->flags, |
| 3501 | req->l, |
| 3502 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3503 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3504 | /* |
| 3505 | * Right now, we know that we have processed the entire headers |
| 3506 | * and that unwanted requests have been filtered out. We can do |
| 3507 | * whatever we want with the remaining request. Also, now we |
| 3508 | * may have separate values for ->fe, ->be. |
| 3509 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3510 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3511 | /* |
| 3512 | * If HTTP PROXY is set we simply get remote server address |
| 3513 | * parsing incoming request. |
| 3514 | */ |
| 3515 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3516 | url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3517 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3518 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3519 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3520 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3521 | * Note that doing so might move headers in the request, but |
| 3522 | * the fields will stay coherent and the URI will not move. |
| 3523 | * This should only be performed in the backend. |
| 3524 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3525 | 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] | 3526 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3527 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3528 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3529 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3530 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3531 | * so let's do the same now. |
| 3532 | */ |
| 3533 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3534 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3535 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3536 | 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] | 3537 | } |
| 3538 | |
| 3539 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3540 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3541 | * asks for it. |
| 3542 | */ |
| 3543 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3544 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3545 | |
| 3546 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Sagi Bashari | 1611e2d | 2011-10-08 22:48:48 +0200 | [diff] [blame] | 3547 | http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3548 | /* The header is set to be added only if none is present |
| 3549 | * and we found it, so don't do anything. |
| 3550 | */ |
| 3551 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3552 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3553 | /* Add an X-Forwarded-For header unless the source IP is |
| 3554 | * in the 'except' network range. |
| 3555 | */ |
| 3556 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3557 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3558 | != s->fe->except_net.s_addr) && |
| 3559 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3560 | (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3561 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3562 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3563 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3564 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3565 | |
| 3566 | /* Note: we rely on the backend to get the header name to be used for |
| 3567 | * x-forwarded-for, because the header is really meant for the backends. |
| 3568 | * However, if the backend did not specify any option, we have to rely |
| 3569 | * on the frontend's header name. |
| 3570 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3571 | if (s->be->fwdfor_hdr_len) { |
| 3572 | len = s->be->fwdfor_hdr_len; |
| 3573 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3574 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3575 | len = s->fe->fwdfor_hdr_len; |
| 3576 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3577 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3578 | 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] | 3579 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3580 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3581 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3582 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3583 | } |
| 3584 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3585 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3586 | /* FIXME: for the sake of completeness, we should also support |
| 3587 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3588 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3589 | int len; |
| 3590 | char pn[INET6_ADDRSTRLEN]; |
| 3591 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3592 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3593 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3594 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3595 | /* Note: we rely on the backend to get the header name to be used for |
| 3596 | * x-forwarded-for, because the header is really meant for the backends. |
| 3597 | * However, if the backend did not specify any option, we have to rely |
| 3598 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3599 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3600 | if (s->be->fwdfor_hdr_len) { |
| 3601 | len = s->be->fwdfor_hdr_len; |
| 3602 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3603 | } else { |
| 3604 | len = s->fe->fwdfor_hdr_len; |
| 3605 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3606 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3607 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3608 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3609 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3610 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3611 | goto return_bad_req; |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3616 | * 10: add X-Original-To if either the frontend or the backend |
| 3617 | * asks for it. |
| 3618 | */ |
| 3619 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3620 | |
| 3621 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3622 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3623 | /* Add an X-Original-To header unless the destination IP is |
| 3624 | * in the 'except' network range. |
| 3625 | */ |
| 3626 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3627 | get_frt_addr(s); |
| 3628 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3629 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3630 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3631 | (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3632 | != s->fe->except_to.s_addr) && |
| 3633 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3634 | (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3635 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3636 | int len; |
| 3637 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3638 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3639 | |
| 3640 | /* Note: we rely on the backend to get the header name to be used for |
| 3641 | * x-original-to, because the header is really meant for the backends. |
| 3642 | * However, if the backend did not specify any option, we have to rely |
| 3643 | * on the frontend's header name. |
| 3644 | */ |
| 3645 | if (s->be->orgto_hdr_len) { |
| 3646 | len = s->be->orgto_hdr_len; |
| 3647 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3648 | } else { |
| 3649 | len = s->fe->orgto_hdr_len; |
| 3650 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3651 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3652 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3653 | |
| 3654 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3655 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3656 | goto return_bad_req; |
| 3657 | } |
| 3658 | } |
| 3659 | } |
| 3660 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3661 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3662 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3663 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3664 | unsigned int want_flags = 0; |
| 3665 | |
| 3666 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3667 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3668 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3669 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3670 | want_flags |= TX_CON_CLO_SET; |
| 3671 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3672 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3673 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3674 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3675 | want_flags |= TX_CON_KAL_SET; |
| 3676 | } |
| 3677 | |
| 3678 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3679 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3680 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3681 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3682 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3683 | /* If we have no server assigned yet and we're balancing on url_param |
| 3684 | * with a POST request, we may be interested in checking the body for |
| 3685 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3686 | */ |
| 3687 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3688 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3689 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | 61a21a3 | 2011-03-01 20:35:49 +0100 | [diff] [blame] | 3690 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3691 | buffer_dont_connect(req); |
| 3692 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3693 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3694 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3695 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3696 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3697 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3698 | /************************************************************* |
| 3699 | * OK, that's finished for the headers. We have done what we * |
| 3700 | * could. Let's switch to the DATA state. * |
| 3701 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3702 | req->analyse_exp = TICK_ETERNITY; |
| 3703 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3705 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3706 | /* OK let's go on with the BODY now */ |
| 3707 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3708 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3709 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3710 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3711 | /* we detected a parsing error. We want to archive this request |
| 3712 | * in the dedicated proxy area for later troubleshooting. |
| 3713 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3714 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3715 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3716 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3717 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3718 | txn->status = 400; |
| 3719 | req->analysers = 0; |
| 3720 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3721 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3722 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3723 | if (s->listener->counters) |
| 3724 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3725 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3726 | if (!(s->flags & SN_ERR_MASK)) |
| 3727 | s->flags |= SN_ERR_PRXCOND; |
| 3728 | if (!(s->flags & SN_FINST_MASK)) |
| 3729 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3730 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3731 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3732 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3733 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3734 | * returns zero, at the beginning because it prevents any other processing |
| 3735 | * from occurring, and at the end because it terminates the request. |
| 3736 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3737 | 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] | 3738 | { |
| 3739 | struct http_txn *txn = &s->txn; |
| 3740 | |
| 3741 | /* This connection is being tarpitted. The CLIENT side has |
| 3742 | * already set the connect expiration date to the right |
| 3743 | * timeout. We just have to check that the client is still |
| 3744 | * there and that the timeout has not expired. |
| 3745 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3746 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3747 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3748 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3749 | return 0; |
| 3750 | |
| 3751 | /* We will set the queue timer to the time spent, just for |
| 3752 | * logging purposes. We fake a 500 server error, so that the |
| 3753 | * attacker will not suspect his connection has been tarpitted. |
| 3754 | * It will not cause trouble to the logs because we can exclude |
| 3755 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3756 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3757 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3758 | |
| 3759 | txn->status = 500; |
| 3760 | if (req->flags != BF_READ_ERROR) |
| 3761 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3762 | |
| 3763 | req->analysers = 0; |
| 3764 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3765 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3766 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3767 | if (s->listener->counters) |
| 3768 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3769 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3770 | if (!(s->flags & SN_ERR_MASK)) |
| 3771 | s->flags |= SN_ERR_PRXCOND; |
| 3772 | if (!(s->flags & SN_FINST_MASK)) |
| 3773 | s->flags |= SN_FINST_T; |
| 3774 | return 0; |
| 3775 | } |
| 3776 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3777 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3778 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3779 | * must only be called after the standard HTTP request processing has occurred, |
| 3780 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3781 | * read more data, or 1 once it has completed its analysis. |
| 3782 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3783 | 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] | 3784 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3785 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3786 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3787 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3788 | |
| 3789 | /* We have to parse the HTTP request body to find any required data. |
| 3790 | * "balance url_param check_post" should have been the only way to get |
| 3791 | * into this. We were brought here after HTTP header analysis, so all |
| 3792 | * related structures are ready. |
| 3793 | */ |
| 3794 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3795 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3796 | goto missing_data; |
| 3797 | |
| 3798 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3799 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3800 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3801 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3802 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3803 | struct hdr_ctx ctx; |
| 3804 | ctx.idx = 0; |
| 3805 | /* Expect is allowed in 1.1, look for it */ |
| 3806 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3807 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3808 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3809 | } |
| 3810 | } |
| 3811 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3812 | } |
| 3813 | |
| 3814 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3815 | /* we have msg->col and msg->sov which both point to the first |
| 3816 | * byte of message body. msg->som still points to the beginning |
| 3817 | * of the message. We must save the body in req->lr because it |
| 3818 | * survives buffer re-alignments. |
| 3819 | */ |
| 3820 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3821 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3822 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3823 | else |
| 3824 | msg->msg_state = HTTP_MSG_DATA; |
| 3825 | } |
| 3826 | |
| 3827 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3828 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3829 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3830 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3831 | */ |
| 3832 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3833 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3834 | if (!ret) |
| 3835 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3836 | else if (ret < 0) { |
| 3837 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3838 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3839 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3840 | } |
| 3841 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3842 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3843 | * We have the first non-header byte in msg->col, which is either the |
| 3844 | * beginning of the chunk size or of the data. The first data byte is in |
| 3845 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3846 | * 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] | 3847 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3848 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3849 | if (msg->body_len < limit) |
| 3850 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3851 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3852 | 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] | 3853 | goto http_end; |
| 3854 | |
| 3855 | missing_data: |
| 3856 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3857 | if (req->flags & BF_FULL) { |
| 3858 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3859 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3860 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3861 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3862 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3863 | txn->status = 408; |
| 3864 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3865 | |
| 3866 | if (!(s->flags & SN_ERR_MASK)) |
| 3867 | s->flags |= SN_ERR_CLITO; |
| 3868 | if (!(s->flags & SN_FINST_MASK)) |
| 3869 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3870 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3871 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3872 | |
| 3873 | /* we get here if we need to wait for more data */ |
| 3874 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3875 | /* Not enough data. We'll re-use the http-request |
| 3876 | * timeout here. Ideally, we should set the timeout |
| 3877 | * relative to the accept() date. We just set the |
| 3878 | * request timeout once at the beginning of the |
| 3879 | * request. |
| 3880 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3881 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3882 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3883 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3884 | return 0; |
| 3885 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3886 | |
| 3887 | http_end: |
| 3888 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3889 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3890 | req->analysers &= ~an_bit; |
| 3891 | req->analyse_exp = TICK_ETERNITY; |
| 3892 | return 1; |
| 3893 | |
| 3894 | return_bad_req: /* let's centralize all bad requests */ |
| 3895 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3896 | txn->status = 400; |
| 3897 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3898 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3899 | if (!(s->flags & SN_ERR_MASK)) |
| 3900 | s->flags |= SN_ERR_PRXCOND; |
| 3901 | if (!(s->flags & SN_FINST_MASK)) |
| 3902 | s->flags |= SN_FINST_R; |
| 3903 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3904 | return_err_msg: |
| 3905 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3906 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3907 | if (s->listener->counters) |
| 3908 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3909 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3910 | } |
| 3911 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3912 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3913 | * right now but it works. |
| 3914 | */ |
| 3915 | void http_end_txn_clean_session(struct session *s) |
| 3916 | { |
| 3917 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3918 | * server's connections. We need a safer way to reinitialize buffer |
| 3919 | * flags. We also need a more accurate method for computing per-request |
| 3920 | * data. |
| 3921 | */ |
| 3922 | http_silent_debug(__LINE__, s); |
| 3923 | |
| 3924 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3925 | s->req->cons->shutr(s->req->cons); |
| 3926 | s->req->cons->shutw(s->req->cons); |
| 3927 | |
| 3928 | http_silent_debug(__LINE__, s); |
| 3929 | |
| 3930 | if (s->flags & SN_BE_ASSIGNED) |
| 3931 | s->be->beconn--; |
| 3932 | |
| 3933 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3934 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3935 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3936 | |
| 3937 | if (s->txn.status) { |
| 3938 | int n; |
| 3939 | |
| 3940 | n = s->txn.status / 100; |
| 3941 | if (n < 1 || n > 5) |
| 3942 | n = 0; |
| 3943 | |
| 3944 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3945 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3946 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3947 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3948 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3949 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | /* don't count other requests' data */ |
| 3953 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3954 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3955 | |
| 3956 | /* let's do a final log if we need it */ |
| 3957 | if (s->logs.logwait && |
| 3958 | !(s->flags & SN_MONITOR) && |
| 3959 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3960 | s->do_log(s); |
| 3961 | } |
| 3962 | |
| 3963 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3964 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3965 | tv_zero(&s->logs.tv_request); |
| 3966 | s->logs.t_queue = -1; |
| 3967 | s->logs.t_connect = -1; |
| 3968 | s->logs.t_data = -1; |
| 3969 | s->logs.t_close = 0; |
| 3970 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3971 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3972 | |
| 3973 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3974 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3975 | |
| 3976 | if (s->pend_pos) |
| 3977 | pendconn_free(s->pend_pos); |
| 3978 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3979 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3980 | if (s->flags & SN_CURR_SESS) { |
| 3981 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3982 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3983 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3984 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3985 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | if (unlikely(s->srv_conn)) |
| 3989 | sess_change_server(s, NULL); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3990 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3991 | |
| 3992 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3993 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3994 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3995 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3996 | s->req->cons->err_loc = NULL; |
| 3997 | s->req->cons->exp = TICK_ETERNITY; |
| 3998 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3999 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 4000 | 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|BF_NEVER_WAIT); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4001 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4002 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 4003 | s->txn.meth = 0; |
| 4004 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4005 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4006 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4007 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4008 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4009 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 4010 | s->req->flags |= BF_NEVER_WAIT; |
| 4011 | s->rep->flags |= BF_NEVER_WAIT; |
| 4012 | } |
| 4013 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4014 | /* if the request buffer is not empty, it means we're |
| 4015 | * about to process another request, so send pending |
| 4016 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4017 | * Just don't do this if the buffer is close to be full, |
| 4018 | * because the request will wait for it to flush a little |
| 4019 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4020 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4021 | if (s->req->l > s->req->send_max) { |
| 4022 | if (s->rep->send_max && |
| 4023 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4024 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 4025 | s->rep->flags |= BF_EXPECT_MORE; |
| 4026 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4027 | |
| 4028 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 4029 | buffer_auto_read(s->req); |
| 4030 | buffer_auto_close(s->req); |
| 4031 | buffer_auto_read(s->rep); |
| 4032 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | |
| 4034 | /* make ->lr point to the first non-forwarded byte */ |
| 4035 | s->req->lr = s->req->w + s->req->send_max; |
| 4036 | if (s->req->lr >= s->req->data + s->req->size) |
| 4037 | s->req->lr -= s->req->size; |
| 4038 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 4039 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 4040 | s->rep->lr -= s->req->size; |
| 4041 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4042 | s->req->analysers = s->listener->analysers; |
| 4043 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4044 | s->rep->analysers = 0; |
| 4045 | |
| 4046 | http_silent_debug(__LINE__, s); |
| 4047 | } |
| 4048 | |
| 4049 | |
| 4050 | /* This function updates the request state machine according to the response |
| 4051 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4052 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4053 | * it is only used to find when a request/response couple is complete. Both |
| 4054 | * this function and its equivalent should loop until both return zero. It |
| 4055 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4056 | */ |
| 4057 | int http_sync_req_state(struct session *s) |
| 4058 | { |
| 4059 | struct buffer *buf = s->req; |
| 4060 | struct http_txn *txn = &s->txn; |
| 4061 | unsigned int old_flags = buf->flags; |
| 4062 | unsigned int old_state = txn->req.msg_state; |
| 4063 | |
| 4064 | http_silent_debug(__LINE__, s); |
| 4065 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4066 | return 0; |
| 4067 | |
| 4068 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4069 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4070 | * We can shut the read side unless we want to abort_on_close, |
| 4071 | * or we have a POST request. The issue with POST requests is |
| 4072 | * that some browsers still send a CRLF after the request, and |
| 4073 | * this CRLF must be read so that it does not remain in the kernel |
| 4074 | * buffers, otherwise a close could cause an RST on some systems |
| 4075 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4076 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4077 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4078 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4079 | |
| 4080 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4081 | goto wait_other_side; |
| 4082 | |
| 4083 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4084 | /* The server has not finished to respond, so we |
| 4085 | * don't want to move in order not to upset it. |
| 4086 | */ |
| 4087 | goto wait_other_side; |
| 4088 | } |
| 4089 | |
| 4090 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4091 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4092 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4093 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4094 | goto wait_other_side; |
| 4095 | } |
| 4096 | |
| 4097 | /* When we get here, it means that both the request and the |
| 4098 | * response have finished receiving. Depending on the connection |
| 4099 | * mode, we'll have to wait for the last bytes to leave in either |
| 4100 | * direction, and sometimes for a close to be effective. |
| 4101 | */ |
| 4102 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4103 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4104 | /* Server-close mode : queue a connection close to the server */ |
| 4105 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4106 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4107 | } |
| 4108 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4109 | /* Option forceclose is set, or either side wants to close, |
| 4110 | * let's enforce it now that we're not expecting any new |
| 4111 | * data to come. The caller knows the session is complete |
| 4112 | * once both states are CLOSED. |
| 4113 | */ |
| 4114 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4115 | buffer_shutr_now(buf); |
| 4116 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4117 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4118 | } |
| 4119 | else { |
| 4120 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4121 | * mode does not set the body analyser, we can't reach this place |
| 4122 | * in tunnel mode, so we're left with keep-alive only. |
| 4123 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4124 | */ |
| 4125 | buffer_auto_read(buf); |
| 4126 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4130 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4131 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4132 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4133 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4134 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4135 | goto http_msg_closing; |
| 4136 | } |
| 4137 | else { |
| 4138 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4139 | goto http_msg_closed; |
| 4140 | } |
| 4141 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4142 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4146 | http_msg_closing: |
| 4147 | /* nothing else to forward, just waiting for the output buffer |
| 4148 | * to be empty and for the shutw_now to take effect. |
| 4149 | */ |
| 4150 | if (buf->flags & BF_OUT_EMPTY) { |
| 4151 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4152 | goto http_msg_closed; |
| 4153 | } |
| 4154 | else if (buf->flags & BF_SHUTW) { |
| 4155 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4156 | goto wait_other_side; |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4161 | http_msg_closed: |
| 4162 | goto wait_other_side; |
| 4163 | } |
| 4164 | |
| 4165 | wait_other_side: |
| 4166 | http_silent_debug(__LINE__, s); |
| 4167 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 4168 | } |
| 4169 | |
| 4170 | |
| 4171 | /* This function updates the response state machine according to the request |
| 4172 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4173 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4174 | * it is only used to find when a request/response couple is complete. Both |
| 4175 | * this function and its equivalent should loop until both return zero. It |
| 4176 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4177 | */ |
| 4178 | int http_sync_res_state(struct session *s) |
| 4179 | { |
| 4180 | struct buffer *buf = s->rep; |
| 4181 | struct http_txn *txn = &s->txn; |
| 4182 | unsigned int old_flags = buf->flags; |
| 4183 | unsigned int old_state = txn->rsp.msg_state; |
| 4184 | |
| 4185 | http_silent_debug(__LINE__, s); |
| 4186 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4187 | return 0; |
| 4188 | |
| 4189 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4190 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4191 | * still monitor the server connection for a possible close |
| 4192 | * while the request is being uploaded, so we don't disable |
| 4193 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4194 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4195 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4196 | |
| 4197 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4198 | goto wait_other_side; |
| 4199 | |
| 4200 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4201 | /* The client seems to still be sending data, probably |
| 4202 | * because we got an error response during an upload. |
| 4203 | * We have the choice of either breaking the connection |
| 4204 | * or letting it pass through. Let's do the later. |
| 4205 | */ |
| 4206 | goto wait_other_side; |
| 4207 | } |
| 4208 | |
| 4209 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4210 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4211 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4212 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4213 | goto wait_other_side; |
| 4214 | } |
| 4215 | |
| 4216 | /* When we get here, it means that both the request and the |
| 4217 | * response have finished receiving. Depending on the connection |
| 4218 | * mode, we'll have to wait for the last bytes to leave in either |
| 4219 | * direction, and sometimes for a close to be effective. |
| 4220 | */ |
| 4221 | |
| 4222 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4223 | /* Server-close mode : shut read and wait for the request |
| 4224 | * side to close its output buffer. The caller will detect |
| 4225 | * when we're in DONE and the other is in CLOSED and will |
| 4226 | * catch that for the final cleanup. |
| 4227 | */ |
| 4228 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4229 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4230 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4231 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4232 | /* Option forceclose is set, or either side wants to close, |
| 4233 | * let's enforce it now that we're not expecting any new |
| 4234 | * data to come. The caller knows the session is complete |
| 4235 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4236 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4237 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4238 | buffer_shutr_now(buf); |
| 4239 | buffer_shutw_now(buf); |
| 4240 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4241 | } |
| 4242 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4243 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4244 | * mode does not set the body analyser, we can't reach this place |
| 4245 | * in tunnel mode, so we're left with keep-alive only. |
| 4246 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4247 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4248 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4249 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4253 | /* if we've just closed an output, let's switch */ |
| 4254 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4255 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4256 | goto http_msg_closing; |
| 4257 | } |
| 4258 | else { |
| 4259 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4260 | goto http_msg_closed; |
| 4261 | } |
| 4262 | } |
| 4263 | goto wait_other_side; |
| 4264 | } |
| 4265 | |
| 4266 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4267 | http_msg_closing: |
| 4268 | /* nothing else to forward, just waiting for the output buffer |
| 4269 | * to be empty and for the shutw_now to take effect. |
| 4270 | */ |
| 4271 | if (buf->flags & BF_OUT_EMPTY) { |
| 4272 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4273 | goto http_msg_closed; |
| 4274 | } |
| 4275 | else if (buf->flags & BF_SHUTW) { |
| 4276 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4277 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4278 | if (target_srv(&s->target)) |
| 4279 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4280 | goto wait_other_side; |
| 4281 | } |
| 4282 | } |
| 4283 | |
| 4284 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4285 | http_msg_closed: |
| 4286 | /* drop any pending data */ |
| 4287 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4288 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4289 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4290 | goto wait_other_side; |
| 4291 | } |
| 4292 | |
| 4293 | wait_other_side: |
| 4294 | http_silent_debug(__LINE__, s); |
| 4295 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4296 | } |
| 4297 | |
| 4298 | |
| 4299 | /* Resync the request and response state machines. Return 1 if either state |
| 4300 | * changes. |
| 4301 | */ |
| 4302 | int http_resync_states(struct session *s) |
| 4303 | { |
| 4304 | struct http_txn *txn = &s->txn; |
| 4305 | int old_req_state = txn->req.msg_state; |
| 4306 | int old_res_state = txn->rsp.msg_state; |
| 4307 | |
| 4308 | http_silent_debug(__LINE__, s); |
| 4309 | http_sync_req_state(s); |
| 4310 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4311 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4312 | if (!http_sync_res_state(s)) |
| 4313 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4314 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4315 | if (!http_sync_req_state(s)) |
| 4316 | break; |
| 4317 | } |
| 4318 | http_silent_debug(__LINE__, s); |
| 4319 | /* OK, both state machines agree on a compatible state. |
| 4320 | * There are a few cases we're interested in : |
| 4321 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4322 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4323 | * directions, so let's simply disable both analysers. |
| 4324 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4325 | * request. |
| 4326 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4327 | * with server-close mode means we've completed one request and we |
| 4328 | * must re-initialize the server connection. |
| 4329 | */ |
| 4330 | |
| 4331 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4332 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4333 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4334 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4335 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4336 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4337 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4338 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4339 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4340 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4341 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4342 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4343 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4344 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4345 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4346 | s->rep->analysers = 0; |
| 4347 | buffer_auto_close(s->rep); |
| 4348 | buffer_auto_read(s->rep); |
| 4349 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4350 | buffer_abort(s->req); |
| 4351 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4352 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4353 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4354 | } |
| 4355 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4356 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4357 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4358 | /* server-close: terminate this server connection and |
| 4359 | * reinitialize a fresh-new transaction. |
| 4360 | */ |
| 4361 | http_end_txn_clean_session(s); |
| 4362 | } |
| 4363 | |
| 4364 | http_silent_debug(__LINE__, s); |
| 4365 | return txn->req.msg_state != old_req_state || |
| 4366 | txn->rsp.msg_state != old_res_state; |
| 4367 | } |
| 4368 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4369 | /* This function is an analyser which forwards request body (including chunk |
| 4370 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4371 | * zero byte. The only situation where it must not be called is when we're in |
| 4372 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4373 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4374 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4375 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4376 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4377 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4378 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4379 | */ |
| 4380 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4381 | { |
| 4382 | struct http_txn *txn = &s->txn; |
| 4383 | struct http_msg *msg = &s->txn.req; |
| 4384 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4385 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4386 | return 0; |
| 4387 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4388 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4389 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4390 | /* Output closed while we were sending data. We must abort and |
| 4391 | * wake the other side up. |
| 4392 | */ |
| 4393 | msg->msg_state = HTTP_MSG_ERROR; |
| 4394 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4395 | return 1; |
| 4396 | } |
| 4397 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4398 | /* in most states, we should abort in case of early close */ |
| 4399 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4400 | |
| 4401 | /* Note that we don't have to send 100-continue back because we don't |
| 4402 | * need the data to complete our job, and it's up to the server to |
| 4403 | * decide whether to return 100, 417 or anything else in return of |
| 4404 | * an "Expect: 100-continue" header. |
| 4405 | */ |
| 4406 | |
| 4407 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4408 | /* we have msg->col and msg->sov which both point to the first |
| 4409 | * byte of message body. msg->som still points to the beginning |
| 4410 | * of the message. We must save the body in req->lr because it |
| 4411 | * survives buffer re-alignments. |
| 4412 | */ |
| 4413 | req->lr = req->data + msg->sov; |
| 4414 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4415 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4416 | else { |
| 4417 | msg->msg_state = HTTP_MSG_DATA; |
| 4418 | } |
| 4419 | } |
| 4420 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4421 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4422 | int bytes; |
| 4423 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4424 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4425 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4426 | bytes = msg->sov - msg->som; |
| 4427 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4428 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4429 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4430 | bytes += req->size; |
| 4431 | msg->chunk_len += (unsigned int)bytes; |
| 4432 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4433 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4434 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4435 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4436 | /* must still forward */ |
| 4437 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4438 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4439 | |
| 4440 | /* nothing left to forward */ |
| 4441 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4442 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4443 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4444 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4445 | } |
| 4446 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4447 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4448 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4449 | * TRAILERS state. |
| 4450 | */ |
| 4451 | int ret = http_parse_chunk_size(req, msg); |
| 4452 | |
| 4453 | if (!ret) |
| 4454 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4455 | else if (ret < 0) { |
| 4456 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4457 | if (msg->err_pos >= 0) |
| 4458 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4459 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4460 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4461 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4462 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4463 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4464 | /* we want the CRLF after the data */ |
| 4465 | int ret; |
| 4466 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4467 | req->lr = req->w + req->send_max; |
| 4468 | if (req->lr >= req->data + req->size) |
| 4469 | req->lr -= req->size; |
| 4470 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4471 | ret = http_skip_chunk_crlf(req, msg); |
| 4472 | |
| 4473 | if (ret == 0) |
| 4474 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4475 | else if (ret < 0) { |
| 4476 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4477 | if (msg->err_pos >= 0) |
| 4478 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4479 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4480 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4481 | /* we're in MSG_CHUNK_SIZE now */ |
| 4482 | } |
| 4483 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4484 | int ret = http_forward_trailers(req, msg); |
| 4485 | |
| 4486 | if (ret == 0) |
| 4487 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4488 | else if (ret < 0) { |
| 4489 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4490 | if (msg->err_pos >= 0) |
| 4491 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4492 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4493 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4494 | /* we're in HTTP_MSG_DONE now */ |
| 4495 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4496 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4497 | int old_state = msg->msg_state; |
| 4498 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4499 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4500 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4501 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4502 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4503 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4504 | if (http_resync_states(s)) { |
| 4505 | /* some state changes occurred, maybe the analyser |
| 4506 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4507 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4508 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4509 | if (req->flags & BF_SHUTW) { |
| 4510 | /* request errors are most likely due to |
| 4511 | * the server aborting the transfer. |
| 4512 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4513 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4514 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4515 | if (msg->err_pos >= 0) |
| 4516 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4517 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4518 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4519 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4520 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4521 | |
| 4522 | /* If "option abortonclose" is set on the backend, we |
| 4523 | * want to monitor the client's connection and forward |
| 4524 | * any shutdown notification to the server, which will |
| 4525 | * decide whether to close or to go on processing the |
| 4526 | * request. |
| 4527 | */ |
| 4528 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4529 | buffer_auto_read(req); |
| 4530 | buffer_auto_close(req); |
| 4531 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4532 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4533 | /* POST requests may require to read extra CRLF |
| 4534 | * sent by broken browsers and which could cause |
| 4535 | * an RST to be sent upon close on some systems |
| 4536 | * (eg: Linux). |
| 4537 | */ |
| 4538 | buffer_auto_read(req); |
| 4539 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4540 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4541 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4542 | } |
| 4543 | } |
| 4544 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4545 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4546 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4547 | if (req->flags & BF_SHUTR) { |
| 4548 | if (!(s->flags & SN_ERR_MASK)) |
| 4549 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4550 | if (!(s->flags & SN_FINST_MASK)) { |
| 4551 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4552 | s->flags |= SN_FINST_H; |
| 4553 | else |
| 4554 | s->flags |= SN_FINST_D; |
| 4555 | } |
| 4556 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4557 | s->fe->fe_counters.cli_aborts++; |
| 4558 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4559 | if (target_srv(&s->target)) |
| 4560 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4561 | |
| 4562 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4563 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4564 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4565 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4566 | if (req->flags & BF_SHUTW) |
| 4567 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4568 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4569 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4570 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4571 | */ |
| 4572 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4573 | buffer_dont_close(req); |
| 4574 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4575 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4576 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4577 | * system knows it must not set a PUSH on this first part. Interactive |
| 4578 | * modes are already handled by the stream sock layer. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4579 | */ |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4580 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4581 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4582 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4583 | return 0; |
| 4584 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4585 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4586 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4587 | if (s->listener->counters) |
| 4588 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4589 | return_bad_req_stats_ok: |
| 4590 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4591 | if (txn->status) { |
| 4592 | /* Note: we don't send any error if some data were already sent */ |
| 4593 | stream_int_retnclose(req->prod, NULL); |
| 4594 | } else { |
| 4595 | txn->status = 400; |
| 4596 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4597 | } |
| 4598 | req->analysers = 0; |
| 4599 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4600 | |
| 4601 | if (!(s->flags & SN_ERR_MASK)) |
| 4602 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4603 | if (!(s->flags & SN_FINST_MASK)) { |
| 4604 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4605 | s->flags |= SN_FINST_H; |
| 4606 | else |
| 4607 | s->flags |= SN_FINST_D; |
| 4608 | } |
| 4609 | return 0; |
| 4610 | |
| 4611 | aborted_xfer: |
| 4612 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4613 | if (txn->status) { |
| 4614 | /* Note: we don't send any error if some data were already sent */ |
| 4615 | stream_int_retnclose(req->prod, NULL); |
| 4616 | } else { |
| 4617 | txn->status = 502; |
| 4618 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4619 | } |
| 4620 | req->analysers = 0; |
| 4621 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4622 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4623 | s->fe->fe_counters.srv_aborts++; |
| 4624 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4625 | if (target_srv(&s->target)) |
| 4626 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4627 | |
| 4628 | if (!(s->flags & SN_ERR_MASK)) |
| 4629 | s->flags |= SN_ERR_SRVCL; |
| 4630 | if (!(s->flags & SN_FINST_MASK)) { |
| 4631 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4632 | s->flags |= SN_FINST_H; |
| 4633 | else |
| 4634 | s->flags |= SN_FINST_D; |
| 4635 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4636 | return 0; |
| 4637 | } |
| 4638 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4639 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4640 | * processing can continue on next analysers, or zero if it either needs more |
| 4641 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4642 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4643 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4644 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4645 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4646 | 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] | 4647 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4648 | struct http_txn *txn = &s->txn; |
| 4649 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4650 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4651 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4652 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4653 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4654 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4655 | 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] | 4656 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4657 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4658 | rep, |
| 4659 | rep->rex, rep->wex, |
| 4660 | rep->flags, |
| 4661 | rep->l, |
| 4662 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4663 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4664 | /* |
| 4665 | * Now parse the partial (or complete) lines. |
| 4666 | * We will check the response syntax, and also join multi-line |
| 4667 | * headers. An index of all the lines will be elaborated while |
| 4668 | * parsing. |
| 4669 | * |
| 4670 | * For the parsing, we use a 28 states FSM. |
| 4671 | * |
| 4672 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4673 | * rep->data + msg->som = beginning of response |
| 4674 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4675 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4676 | * rep->lr = first non-visited byte |
| 4677 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4678 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4679 | */ |
| 4680 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4681 | /* There's a protected area at the end of the buffer for rewriting |
| 4682 | * purposes. We don't want to start to parse the request if the |
| 4683 | * protected area is affected, because we may have to move processed |
| 4684 | * data later, which is much more complicated. |
| 4685 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4686 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4687 | if (unlikely((rep->flags & BF_FULL) || |
| 4688 | rep->r < rep->lr || |
| 4689 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4690 | if (rep->send_max) { |
| 4691 | /* 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] | 4692 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4693 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4694 | buffer_dont_close(rep); |
| 4695 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4696 | return 0; |
| 4697 | } |
| 4698 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4699 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4700 | } |
| 4701 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4702 | if (likely(rep->lr < rep->r)) |
| 4703 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4704 | } |
| 4705 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4706 | /* 1: we might have to print this header in debug mode */ |
| 4707 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4708 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4709 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4710 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4711 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4712 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4713 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4714 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4715 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4716 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4717 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4718 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4719 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4720 | while (cur_idx) { |
| 4721 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4722 | debug_hdr("srvhdr", s, sol, eol); |
| 4723 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4724 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4725 | } |
| 4726 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4727 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4728 | /* |
| 4729 | * Now we quickly check if we have found a full valid response. |
| 4730 | * If not so, we check the FD and buffer states before leaving. |
| 4731 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4732 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4733 | * responses are checked first. |
| 4734 | * |
| 4735 | * Depending on whether the client is still there or not, we |
| 4736 | * may send an error response back or not. Note that normally |
| 4737 | * we should only check for HTTP status there, and check I/O |
| 4738 | * errors somewhere else. |
| 4739 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4740 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4741 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4742 | /* Invalid response */ |
| 4743 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4744 | /* we detected a parsing error. We want to archive this response |
| 4745 | * in the dedicated proxy area for later troubleshooting. |
| 4746 | */ |
| 4747 | hdr_response_bad: |
| 4748 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4749 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4750 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4751 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4752 | if (target_srv(&s->target)) { |
| 4753 | target_srv(&s->target)->counters.failed_resp++; |
| 4754 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4755 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4756 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4757 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4758 | rep->analysers = 0; |
| 4759 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4760 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4761 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4762 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4763 | |
| 4764 | if (!(s->flags & SN_ERR_MASK)) |
| 4765 | s->flags |= SN_ERR_PRXCOND; |
| 4766 | if (!(s->flags & SN_FINST_MASK)) |
| 4767 | s->flags |= SN_FINST_H; |
| 4768 | |
| 4769 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4770 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4771 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4772 | /* too large response does not fit in buffer. */ |
| 4773 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4774 | if (msg->err_pos < 0) |
| 4775 | msg->err_pos = rep->l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4776 | goto hdr_response_bad; |
| 4777 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4778 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4779 | /* read error */ |
| 4780 | else if (rep->flags & BF_READ_ERROR) { |
| 4781 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4782 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4783 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4784 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4785 | if (target_srv(&s->target)) { |
| 4786 | target_srv(&s->target)->counters.failed_resp++; |
| 4787 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4788 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4789 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4790 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4791 | rep->analysers = 0; |
| 4792 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4793 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4794 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4795 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4796 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4797 | if (!(s->flags & SN_ERR_MASK)) |
| 4798 | s->flags |= SN_ERR_SRVCL; |
| 4799 | if (!(s->flags & SN_FINST_MASK)) |
| 4800 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4801 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4802 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4803 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4804 | /* read timeout : return a 504 to the client. */ |
| 4805 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4806 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4807 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4808 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4809 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4810 | if (target_srv(&s->target)) { |
| 4811 | target_srv(&s->target)->counters.failed_resp++; |
| 4812 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4813 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4814 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4815 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4816 | rep->analysers = 0; |
| 4817 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4818 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4819 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4820 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4821 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4822 | if (!(s->flags & SN_ERR_MASK)) |
| 4823 | s->flags |= SN_ERR_SRVTO; |
| 4824 | if (!(s->flags & SN_FINST_MASK)) |
| 4825 | s->flags |= SN_FINST_H; |
| 4826 | return 0; |
| 4827 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4828 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4829 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4830 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4831 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4832 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4833 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4834 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4835 | if (target_srv(&s->target)) { |
| 4836 | target_srv(&s->target)->counters.failed_resp++; |
| 4837 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4838 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4839 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4840 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4841 | rep->analysers = 0; |
| 4842 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4843 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4844 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4845 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4846 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4847 | if (!(s->flags & SN_ERR_MASK)) |
| 4848 | s->flags |= SN_ERR_SRVCL; |
| 4849 | if (!(s->flags & SN_FINST_MASK)) |
| 4850 | s->flags |= SN_FINST_H; |
| 4851 | return 0; |
| 4852 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4853 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4854 | /* write error to client (we don't send any message then) */ |
| 4855 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4856 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4857 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4858 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4859 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4860 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4861 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4862 | |
| 4863 | if (!(s->flags & SN_ERR_MASK)) |
| 4864 | s->flags |= SN_ERR_CLICL; |
| 4865 | if (!(s->flags & SN_FINST_MASK)) |
| 4866 | s->flags |= SN_FINST_H; |
| 4867 | |
| 4868 | /* process_session() will take care of the error */ |
| 4869 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4870 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4871 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4872 | buffer_dont_close(rep); |
| 4873 | return 0; |
| 4874 | } |
| 4875 | |
| 4876 | /* More interesting part now : we know that we have a complete |
| 4877 | * response which at least looks like HTTP. We have an indicator |
| 4878 | * of each header's length, so we can parse them quickly. |
| 4879 | */ |
| 4880 | |
| 4881 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4882 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4883 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4884 | /* |
| 4885 | * 1: get the status code |
| 4886 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4887 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4888 | if (n < 1 || n > 5) |
| 4889 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4890 | /* when the client triggers a 4xx from the server, it's most often due |
| 4891 | * to a missing object or permission. These events should be tracked |
| 4892 | * because if they happen often, it may indicate a brute force or a |
| 4893 | * vulnerability scan. |
| 4894 | */ |
| 4895 | if (n == 4) |
| 4896 | session_inc_http_err_ctr(s); |
| 4897 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4898 | if (target_srv(&s->target)) |
| 4899 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4900 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4901 | /* check if the response is HTTP/1.1 or above */ |
| 4902 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4903 | ((msg->sol[5] > '1') || |
| 4904 | ((msg->sol[5] == '1') && |
| 4905 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4906 | txn->flags |= TX_RES_VER_11; |
| 4907 | |
| 4908 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4909 | 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] | 4910 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4911 | /* transfer length unknown*/ |
| 4912 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4913 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4914 | 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] | 4915 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4916 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4917 | * and 505 are triggered on demand by client request, so we must not |
| 4918 | * count them as server failures. |
| 4919 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4920 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4921 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4922 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4923 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4924 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4925 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4926 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4927 | /* |
| 4928 | * 2: check for cacheability. |
| 4929 | */ |
| 4930 | |
| 4931 | switch (txn->status) { |
| 4932 | case 200: |
| 4933 | case 203: |
| 4934 | case 206: |
| 4935 | case 300: |
| 4936 | case 301: |
| 4937 | case 410: |
| 4938 | /* RFC2616 @13.4: |
| 4939 | * "A response received with a status code of |
| 4940 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4941 | * by a cache (...) unless a cache-control |
| 4942 | * directive prohibits caching." |
| 4943 | * |
| 4944 | * RFC2616 @9.5: POST method : |
| 4945 | * "Responses to this method are not cacheable, |
| 4946 | * unless the response includes appropriate |
| 4947 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4948 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4949 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4950 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4951 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4952 | break; |
| 4953 | default: |
| 4954 | break; |
| 4955 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4956 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4957 | /* |
| 4958 | * 3: we may need to capture headers |
| 4959 | */ |
| 4960 | s->logs.logwait &= ~LW_RESP; |
| 4961 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4962 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4963 | txn->rsp.cap, s->fe->rsp_cap); |
| 4964 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4965 | /* 4: determine the transfer-length. |
| 4966 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4967 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4968 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4969 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4970 | * All responses to the HEAD request method MUST NOT include a |
| 4971 | * message-body, even though the presence of entity-header fields |
| 4972 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4973 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4974 | * message-body. All other responses do include a message-body, |
| 4975 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4976 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4977 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4978 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4979 | * always terminated by the first empty line after the header fields, |
| 4980 | * regardless of the entity-header fields present in the message. |
| 4981 | * |
| 4982 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4983 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4984 | * transfer-length is defined by the use of this transfer-coding. |
| 4985 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4986 | * transfer-coding is not present, the transfer-length is defined by |
| 4987 | * the sender closing the connection. |
| 4988 | * |
| 4989 | * 3. If a Content-Length header field is present, its decimal value in |
| 4990 | * OCTETs represents both the entity-length and the transfer-length. |
| 4991 | * If a message is received with both a Transfer-Encoding header |
| 4992 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4993 | * |
| 4994 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4995 | * the transfer-length is not otherwise specified, then this self- |
| 4996 | * delimiting media type defines the transfer-length. This media |
| 4997 | * type MUST NOT be used unless the sender knows that the recipient |
| 4998 | * can parse it; the presence in a request of a Range header with |
| 4999 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5000 | * client can parse multipart/byteranges responses. |
| 5001 | * |
| 5002 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5003 | */ |
| 5004 | |
| 5005 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5006 | * remain 0 as well as the chunk_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5007 | * 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] | 5008 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5009 | */ |
| 5010 | if (txn->meth == HTTP_METH_HEAD || |
| 5011 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5012 | txn->status == 204 || txn->status == 304) { |
| 5013 | txn->flags |= TX_RES_XFER_LEN; |
| 5014 | goto skip_content_length; |
| 5015 | } |
| 5016 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5017 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5018 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 5019 | while ((txn->flags & TX_RES_VER_11) && |
| 5020 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5021 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 5022 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5023 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 5024 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5025 | use_close_only = 1; |
| 5026 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5027 | break; |
| 5028 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5032 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5033 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5034 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 5035 | signed long long cl; |
| 5036 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5037 | if (!ctx.vlen) { |
| 5038 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5039 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5040 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5041 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5042 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 5043 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5044 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5045 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5046 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5047 | if (cl < 0) { |
| 5048 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5049 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5050 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5051 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5052 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) { |
| 5053 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5054 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5055 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5056 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5057 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5058 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5059 | } |
| 5060 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5061 | /* FIXME: we should also implement the multipart/byterange method. |
| 5062 | * For now on, we resort to close mode in this case (unknown length). |
| 5063 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5064 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5065 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5066 | /* end of job, return OK */ |
| 5067 | rep->analysers &= ~an_bit; |
| 5068 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5069 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5070 | return 1; |
| 5071 | } |
| 5072 | |
| 5073 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5074 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5075 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5076 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5077 | */ |
| 5078 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 5079 | { |
| 5080 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5081 | struct http_msg *msg = &txn->rsp; |
| 5082 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5083 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5084 | |
| 5085 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 5086 | now_ms, __FUNCTION__, |
| 5087 | t, |
| 5088 | rep, |
| 5089 | rep->rex, rep->wex, |
| 5090 | rep->flags, |
| 5091 | rep->l, |
| 5092 | rep->analysers); |
| 5093 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5094 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5095 | return 0; |
| 5096 | |
| 5097 | rep->analysers &= ~an_bit; |
| 5098 | rep->analyse_exp = TICK_ETERNITY; |
| 5099 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5100 | /* Now we have to check if we need to modify the Connection header. |
| 5101 | * This is more difficult on the response than it is on the request, |
| 5102 | * because we can have two different HTTP versions and we don't know |
| 5103 | * how the client will interprete a response. For instance, let's say |
| 5104 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5105 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5106 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5107 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5108 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5109 | * indicating how a request MAY be understood by the client. In case |
| 5110 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5111 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5112 | * will fall back to explicit close. Note that we won't take risks with |
| 5113 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5114 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5115 | */ |
| 5116 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5117 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5118 | txn->status == 101)) { |
| 5119 | /* Either we've established an explicit tunnel, or we're |
| 5120 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5121 | * to understand the next protocols. We have to switch to tunnel |
| 5122 | * mode, so that we transfer the request and responses then let |
| 5123 | * this protocol pass unmodified. When we later implement specific |
| 5124 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5125 | * header which contains information about that protocol for |
| 5126 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5127 | */ |
| 5128 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5129 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5130 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5131 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5132 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5133 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5134 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5135 | /* on unknown transfer length, we must close */ |
| 5136 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 5137 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5138 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5139 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5140 | /* now adjust header transformations depending on current state */ |
| 5141 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5142 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5143 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 5144 | if (!(txn->flags & TX_RES_VER_11)) |
| 5145 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5146 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5147 | else { /* SCL / KAL */ |
| 5148 | to_del |= 1; /* remove "close" on any response */ |
| 5149 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 5150 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5151 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5152 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5153 | /* Parse and remove some headers from the connection header */ |
| 5154 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5155 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5156 | /* Some keep-alive responses are converted to Server-close if |
| 5157 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5158 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5159 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5160 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 5161 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 5162 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5163 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5164 | } |
| 5165 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5166 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5167 | /* |
| 5168 | * 3: we will have to evaluate the filters. |
| 5169 | * As opposed to version 1.2, now they will be evaluated in the |
| 5170 | * filters order and not in the header order. This means that |
| 5171 | * each filter has to be validated among all headers. |
| 5172 | * |
| 5173 | * Filters are tried with ->be first, then with ->fe if it is |
| 5174 | * different from ->be. |
| 5175 | */ |
| 5176 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5177 | cur_proxy = t->be; |
| 5178 | while (1) { |
| 5179 | struct proxy *rule_set = cur_proxy; |
| 5180 | |
| 5181 | /* try headers filters */ |
| 5182 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5183 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5184 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5185 | if (target_srv(&t->target)) { |
| 5186 | target_srv(&t->target)->counters.failed_resp++; |
| 5187 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5188 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5189 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5190 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5191 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5192 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5193 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 5194 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 5195 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5196 | if (!(t->flags & SN_ERR_MASK)) |
| 5197 | t->flags |= SN_ERR_PRXCOND; |
| 5198 | if (!(t->flags & SN_FINST_MASK)) |
| 5199 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5200 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5201 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5202 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5203 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5204 | /* has the response been denied ? */ |
| 5205 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5206 | if (target_srv(&t->target)) |
| 5207 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5208 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5209 | t->be->be_counters.denied_resp++; |
| 5210 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5211 | if (t->listener->counters) |
| 5212 | t->listener->counters->denied_resp++; |
| 5213 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5214 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5215 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5216 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5217 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5218 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5219 | if (txn->status < 200) |
| 5220 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5221 | if (wl->cond) { |
| 5222 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5223 | ret = acl_pass(ret); |
| 5224 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5225 | ret = !ret; |
| 5226 | if (!ret) |
| 5227 | continue; |
| 5228 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5229 | 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] | 5230 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5231 | } |
| 5232 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5233 | /* check whether we're already working on the frontend */ |
| 5234 | if (cur_proxy == t->fe) |
| 5235 | break; |
| 5236 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5237 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5238 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5239 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5240 | * We may be facing a 100-continue response, in which case this |
| 5241 | * is not the right response, and we're waiting for the next one. |
| 5242 | * 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] | 5243 | * next one. |
| 5244 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5245 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5246 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5247 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5248 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5249 | txn->status = 0; |
| 5250 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5251 | return 1; |
| 5252 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5253 | else if (unlikely(txn->status < 200)) |
| 5254 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5255 | |
| 5256 | /* we don't have any 1xx status code now */ |
| 5257 | |
| 5258 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5259 | * 4: check for server cookie. |
| 5260 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5261 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5262 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5263 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5264 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5265 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5266 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5267 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5268 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5269 | 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] | 5270 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5271 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5272 | /* |
| 5273 | * 6: add server cookie in the response if needed |
| 5274 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5275 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5276 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5277 | (!(t->flags & SN_DIRECT) || |
| 5278 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5279 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5280 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5281 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5282 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5283 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5284 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5285 | /* the server is known, it's not the one the client requested, or the |
| 5286 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5287 | * insert a set-cookie here, except if we want to insert only on POST |
| 5288 | * requests and this one isn't. Note that servers which don't have cookies |
| 5289 | * (eg: some backup servers) will return a full cookie removal request. |
| 5290 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5291 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5292 | len = sprintf(trash, |
| 5293 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5294 | t->be->cookie_name); |
| 5295 | } |
| 5296 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5297 | len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5298 | |
| 5299 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5300 | /* emit last_date, which is mandatory */ |
| 5301 | trash[len++] = COOKIE_DELIM_DATE; |
| 5302 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5303 | if (t->be->cookie_maxlife) { |
| 5304 | /* emit first_date, which is either the original one or |
| 5305 | * the current date. |
| 5306 | */ |
| 5307 | trash[len++] = COOKIE_DELIM_DATE; |
| 5308 | s30tob64(txn->cookie_first_date ? |
| 5309 | txn->cookie_first_date >> 2 : |
| 5310 | (date.tv_sec+3) >> 2, trash + len); |
| 5311 | len += 5; |
| 5312 | } |
| 5313 | } |
| 5314 | len += sprintf(trash + len, "; path=/"); |
| 5315 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5316 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5317 | if (t->be->cookie_domain) |
| 5318 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5319 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5320 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5321 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5322 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5323 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5324 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5325 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5326 | /* the server did not change, only the date was updated */ |
| 5327 | txn->flags |= TX_SCK_UPDATED; |
| 5328 | else |
| 5329 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5330 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5331 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5332 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5333 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5334 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5335 | */ |
| 5336 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5337 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5338 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5339 | |
| 5340 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5341 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5342 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5343 | } |
| 5344 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5345 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5346 | /* |
| 5347 | * 7: check if result will be cacheable with a cookie. |
| 5348 | * We'll block the response if security checks have caught |
| 5349 | * nasty things such as a cacheable cookie. |
| 5350 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5351 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5352 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5353 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5354 | |
| 5355 | /* we're in presence of a cacheable response containing |
| 5356 | * a set-cookie header. We'll block it as requested by |
| 5357 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5358 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5359 | if (target_srv(&t->target)) |
| 5360 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5361 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5362 | t->be->be_counters.denied_resp++; |
| 5363 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5364 | if (t->listener->counters) |
| 5365 | t->listener->counters->denied_resp++; |
| 5366 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5367 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5368 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5369 | send_log(t->be, LOG_ALERT, |
| 5370 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5371 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5372 | goto return_srv_prx_502; |
| 5373 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5374 | |
| 5375 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5376 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5377 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5378 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5379 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5380 | unsigned int want_flags = 0; |
| 5381 | |
| 5382 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5383 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5384 | /* we want a keep-alive response here. Keep-alive header |
| 5385 | * required if either side is not 1.1. |
| 5386 | */ |
| 5387 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 5388 | want_flags |= TX_CON_KAL_SET; |
| 5389 | } |
| 5390 | else { |
| 5391 | /* we want a close response here. Close header required if |
| 5392 | * the server is 1.1, regardless of the client. |
| 5393 | */ |
| 5394 | if (txn->flags & TX_RES_VER_11) |
| 5395 | want_flags |= TX_CON_CLO_SET; |
| 5396 | } |
| 5397 | |
| 5398 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5399 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5400 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5401 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5402 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5403 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 5404 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5405 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5406 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5407 | /************************************************************* |
| 5408 | * OK, that's finished for the headers. We have done what we * |
| 5409 | * could. Let's switch to the DATA state. * |
| 5410 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5411 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5412 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5413 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5414 | /* if the user wants to log as soon as possible, without counting |
| 5415 | * bytes from the server, then this is the right moment. We have |
| 5416 | * to temporarily assign bytes_out to log what we currently have. |
| 5417 | */ |
| 5418 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5419 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5420 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5421 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5422 | t->logs.bytes_out = 0; |
| 5423 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5424 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5425 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5426 | * otherwise we would not let the client side wake up. |
| 5427 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5428 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5429 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5430 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5431 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5432 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5433 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5434 | /* This function is an analyser which forwards response body (including chunk |
| 5435 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5436 | * zero byte. The only situation where it must not be called is when we're in |
| 5437 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5438 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5439 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5440 | * read more data, or 1 once we can go on with next request or end the session. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5441 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5442 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5443 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5444 | */ |
| 5445 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5446 | { |
| 5447 | struct http_txn *txn = &s->txn; |
| 5448 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5449 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5450 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5451 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5452 | return 0; |
| 5453 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5454 | 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] | 5455 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5456 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5457 | /* Output closed while we were sending data. We must abort and |
| 5458 | * wake the other side up. |
| 5459 | */ |
| 5460 | msg->msg_state = HTTP_MSG_ERROR; |
| 5461 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5462 | return 1; |
| 5463 | } |
| 5464 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5465 | /* in most states, we should abort in case of early close */ |
| 5466 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5467 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5468 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5469 | /* we have msg->col and msg->sov which both point to the first |
| 5470 | * byte of message body. msg->som still points to the beginning |
| 5471 | * of the message. We must save the body in req->lr because it |
| 5472 | * survives buffer re-alignments. |
| 5473 | */ |
| 5474 | res->lr = res->data + msg->sov; |
| 5475 | if (txn->flags & TX_RES_TE_CHNK) |
| 5476 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5477 | else { |
| 5478 | msg->msg_state = HTTP_MSG_DATA; |
| 5479 | } |
| 5480 | } |
| 5481 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5482 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5483 | int bytes; |
| 5484 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5485 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5486 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5487 | bytes = msg->sov - msg->som; |
| 5488 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5489 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5490 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5491 | bytes += res->size; |
| 5492 | msg->chunk_len += (unsigned int)bytes; |
| 5493 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5494 | } |
| 5495 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5496 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5497 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5498 | /* must still forward */ |
| 5499 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5500 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5501 | |
| 5502 | /* nothing left to forward */ |
| 5503 | if (txn->flags & TX_RES_TE_CHNK) |
| 5504 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5505 | else |
| 5506 | msg->msg_state = HTTP_MSG_DONE; |
| 5507 | } |
| 5508 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5509 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5510 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5511 | */ |
| 5512 | int ret = http_parse_chunk_size(res, msg); |
| 5513 | |
| 5514 | if (!ret) |
| 5515 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5516 | else if (ret < 0) { |
| 5517 | if (msg->err_pos >= 0) |
| 5518 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5519 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5520 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5521 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5522 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5523 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5524 | /* we want the CRLF after the data */ |
| 5525 | int ret; |
| 5526 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5527 | res->lr = res->w + res->send_max; |
| 5528 | if (res->lr >= res->data + res->size) |
| 5529 | res->lr -= res->size; |
| 5530 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5531 | ret = http_skip_chunk_crlf(res, msg); |
| 5532 | |
| 5533 | if (!ret) |
| 5534 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5535 | else if (ret < 0) { |
| 5536 | if (msg->err_pos >= 0) |
| 5537 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5538 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5539 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5540 | /* we're in MSG_CHUNK_SIZE now */ |
| 5541 | } |
| 5542 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5543 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5544 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5545 | if (ret == 0) |
| 5546 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5547 | else if (ret < 0) { |
| 5548 | if (msg->err_pos >= 0) |
| 5549 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5550 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5551 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5552 | /* we're in HTTP_MSG_DONE now */ |
| 5553 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5554 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5555 | int old_state = msg->msg_state; |
| 5556 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5557 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5558 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5559 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5560 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5561 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5562 | if (http_resync_states(s)) { |
| 5563 | http_silent_debug(__LINE__, s); |
| 5564 | /* some state changes occurred, maybe the analyser |
| 5565 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5566 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5567 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5568 | if (res->flags & BF_SHUTW) { |
| 5569 | /* response errors are most likely due to |
| 5570 | * the client aborting the transfer. |
| 5571 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5572 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5573 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5574 | if (msg->err_pos >= 0) |
| 5575 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5576 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5577 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5578 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5579 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5580 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5581 | } |
| 5582 | } |
| 5583 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5584 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5585 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5586 | if (res->flags & BF_SHUTR) { |
| 5587 | if (!(s->flags & SN_ERR_MASK)) |
| 5588 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5589 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5590 | if (target_srv(&s->target)) |
| 5591 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5592 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5593 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5594 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5595 | if (res->flags & BF_SHUTW) |
| 5596 | goto aborted_xfer; |
| 5597 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5598 | /* 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] | 5599 | if (!s->req->analysers) |
| 5600 | goto return_bad_res; |
| 5601 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5602 | /* forward any pending data */ |
| 5603 | bytes = msg->sov - msg->som; |
| 5604 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5605 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5606 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5607 | bytes += res->size; |
| 5608 | msg->chunk_len += (unsigned int)bytes; |
| 5609 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5610 | } |
| 5611 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5612 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5613 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5614 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5615 | * close. |
| 5616 | */ |
| 5617 | if ((txn->flags & TX_RES_TE_CHNK) || |
| 5618 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5619 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5620 | buffer_dont_close(res); |
| 5621 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5622 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5623 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5624 | * system knows it must not set a PUSH on this first part. Interactive |
| 5625 | * modes are already handled by the stream sock layer. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5626 | */ |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5627 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5628 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5629 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5630 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5631 | return 0; |
| 5632 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5633 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5634 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5635 | if (target_srv(&s->target)) |
| 5636 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5637 | |
| 5638 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5639 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5640 | /* don't send any error message as we're in the body */ |
| 5641 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5642 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5643 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5644 | if (target_srv(&s->target)) |
| 5645 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5646 | |
| 5647 | if (!(s->flags & SN_ERR_MASK)) |
| 5648 | s->flags |= SN_ERR_PRXCOND; |
| 5649 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5650 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5651 | return 0; |
| 5652 | |
| 5653 | aborted_xfer: |
| 5654 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5655 | /* don't send any error message as we're in the body */ |
| 5656 | stream_int_retnclose(res->cons, NULL); |
| 5657 | res->analysers = 0; |
| 5658 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5659 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5660 | s->fe->fe_counters.cli_aborts++; |
| 5661 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5662 | if (target_srv(&s->target)) |
| 5663 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5664 | |
| 5665 | if (!(s->flags & SN_ERR_MASK)) |
| 5666 | s->flags |= SN_ERR_CLICL; |
| 5667 | if (!(s->flags & SN_FINST_MASK)) |
| 5668 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5669 | return 0; |
| 5670 | } |
| 5671 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5672 | /* Iterate the same filter through all request headers. |
| 5673 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5674 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5675 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5676 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5677 | 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] | 5678 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5679 | char term; |
| 5680 | char *cur_ptr, *cur_end, *cur_next; |
| 5681 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5682 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5683 | struct hdr_idx_elem *cur_hdr; |
| 5684 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5685 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5686 | last_hdr = 0; |
| 5687 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5688 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5689 | old_idx = 0; |
| 5690 | |
| 5691 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5692 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5693 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5694 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5695 | (exp->action == ACT_ALLOW || |
| 5696 | exp->action == ACT_DENY || |
| 5697 | exp->action == ACT_TARPIT)) |
| 5698 | return 0; |
| 5699 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5700 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5701 | if (!cur_idx) |
| 5702 | break; |
| 5703 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5704 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5705 | cur_ptr = cur_next; |
| 5706 | cur_end = cur_ptr + cur_hdr->len; |
| 5707 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5708 | |
| 5709 | /* Now we have one header between cur_ptr and cur_end, |
| 5710 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5711 | */ |
| 5712 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5713 | /* The annoying part is that pattern matching needs |
| 5714 | * that we modify the contents to null-terminate all |
| 5715 | * strings before testing them. |
| 5716 | */ |
| 5717 | |
| 5718 | term = *cur_end; |
| 5719 | *cur_end = '\0'; |
| 5720 | |
| 5721 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5722 | switch (exp->action) { |
| 5723 | case ACT_SETBE: |
| 5724 | /* It is not possible to jump a second time. |
| 5725 | * FIXME: should we return an HTTP/500 here so that |
| 5726 | * the admin knows there's a problem ? |
| 5727 | */ |
| 5728 | if (t->be != t->fe) |
| 5729 | break; |
| 5730 | |
| 5731 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5732 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5733 | last_hdr = 1; |
| 5734 | break; |
| 5735 | |
| 5736 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5737 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5738 | last_hdr = 1; |
| 5739 | break; |
| 5740 | |
| 5741 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5742 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5743 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5744 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5745 | t->fe->fe_counters.denied_req++; |
| 5746 | if (t->fe != t->be) |
| 5747 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5748 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5749 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5751 | break; |
| 5752 | |
| 5753 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5754 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5755 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5756 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5757 | t->fe->fe_counters.denied_req++; |
| 5758 | if (t->fe != t->be) |
| 5759 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5760 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5761 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5762 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5763 | break; |
| 5764 | |
| 5765 | case ACT_REPLACE: |
| 5766 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5767 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5768 | /* FIXME: if the user adds a newline in the replacement, the |
| 5769 | * index will not be recalculated for now, and the new line |
| 5770 | * will not be counted as a new header. |
| 5771 | */ |
| 5772 | |
| 5773 | cur_end += delta; |
| 5774 | cur_next += delta; |
| 5775 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5776 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5777 | break; |
| 5778 | |
| 5779 | case ACT_REMOVE: |
| 5780 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5781 | cur_next += delta; |
| 5782 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5783 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5784 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5785 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5786 | cur_hdr->len = 0; |
| 5787 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5788 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5789 | break; |
| 5790 | |
| 5791 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5792 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5793 | if (cur_end) |
| 5794 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5795 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5796 | /* keep the link from this header to next one in case of later |
| 5797 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5798 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5799 | old_idx = cur_idx; |
| 5800 | } |
| 5801 | return 0; |
| 5802 | } |
| 5803 | |
| 5804 | |
| 5805 | /* Apply the filter to the request line. |
| 5806 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5807 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5808 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5809 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5810 | */ |
| 5811 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5812 | { |
| 5813 | char term; |
| 5814 | char *cur_ptr, *cur_end; |
| 5815 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5816 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5817 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5818 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5819 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5820 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5821 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5822 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5823 | (exp->action == ACT_ALLOW || |
| 5824 | exp->action == ACT_DENY || |
| 5825 | exp->action == ACT_TARPIT)) |
| 5826 | return 0; |
| 5827 | else if (exp->action == ACT_REMOVE) |
| 5828 | return 0; |
| 5829 | |
| 5830 | done = 0; |
| 5831 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5832 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5833 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5834 | |
| 5835 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5836 | |
| 5837 | /* The annoying part is that pattern matching needs |
| 5838 | * that we modify the contents to null-terminate all |
| 5839 | * strings before testing them. |
| 5840 | */ |
| 5841 | |
| 5842 | term = *cur_end; |
| 5843 | *cur_end = '\0'; |
| 5844 | |
| 5845 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5846 | switch (exp->action) { |
| 5847 | case ACT_SETBE: |
| 5848 | /* It is not possible to jump a second time. |
| 5849 | * FIXME: should we return an HTTP/500 here so that |
| 5850 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5851 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5852 | if (t->be != t->fe) |
| 5853 | break; |
| 5854 | |
| 5855 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5856 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5857 | done = 1; |
| 5858 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5859 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5860 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5861 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5862 | done = 1; |
| 5863 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5864 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5865 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5866 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5867 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5868 | t->fe->fe_counters.denied_req++; |
| 5869 | if (t->fe != t->be) |
| 5870 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5871 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5872 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5873 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5874 | done = 1; |
| 5875 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5876 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5877 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5878 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5879 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5880 | t->fe->fe_counters.denied_req++; |
| 5881 | if (t->fe != t->be) |
| 5882 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5883 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5884 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5885 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5886 | done = 1; |
| 5887 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5888 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5889 | case ACT_REPLACE: |
| 5890 | *cur_end = term; /* restore the string terminator */ |
| 5891 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5892 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5893 | /* FIXME: if the user adds a newline in the replacement, the |
| 5894 | * index will not be recalculated for now, and the new line |
| 5895 | * will not be counted as a new header. |
| 5896 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5897 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5898 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5899 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5900 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5901 | HTTP_MSG_RQMETH, |
| 5902 | cur_ptr, cur_end + 1, |
| 5903 | NULL, NULL); |
| 5904 | if (unlikely(!cur_end)) |
| 5905 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5906 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5907 | /* we have a full request and we know that we have either a CR |
| 5908 | * or an LF at <ptr>. |
| 5909 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5910 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5911 | 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] | 5912 | /* there is no point trying this regex on headers */ |
| 5913 | return 1; |
| 5914 | } |
| 5915 | } |
| 5916 | *cur_end = term; /* restore the string terminator */ |
| 5917 | return done; |
| 5918 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5919 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5920 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5921 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5922 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5923 | * 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] | 5924 | * 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] | 5925 | * unparsable request. Since it can manage the switch to another backend, it |
| 5926 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5927 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5928 | 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] | 5929 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5930 | struct http_txn *txn = &s->txn; |
| 5931 | struct hdr_exp *exp; |
| 5932 | |
| 5933 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5934 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5935 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5936 | /* |
| 5937 | * The interleaving of transformations and verdicts |
| 5938 | * makes it difficult to decide to continue or stop |
| 5939 | * the evaluation. |
| 5940 | */ |
| 5941 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5942 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5943 | break; |
| 5944 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5945 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5946 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5947 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5948 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5949 | |
| 5950 | /* if this filter had a condition, evaluate it now and skip to |
| 5951 | * next filter if the condition does not match. |
| 5952 | */ |
| 5953 | if (exp->cond) { |
| 5954 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5955 | ret = acl_pass(ret); |
| 5956 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5957 | ret = !ret; |
| 5958 | |
| 5959 | if (!ret) |
| 5960 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5961 | } |
| 5962 | |
| 5963 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5964 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5965 | if (unlikely(ret < 0)) |
| 5966 | return -1; |
| 5967 | |
| 5968 | if (likely(ret == 0)) { |
| 5969 | /* The filter did not match the request, it can be |
| 5970 | * iterated through all headers. |
| 5971 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5972 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5973 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5974 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5975 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5976 | } |
| 5977 | |
| 5978 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5979 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5980 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5981 | * Try to retrieve the server associated to the appsession. |
| 5982 | * If the server is found, it's assigned to the session. |
| 5983 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5984 | 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] | 5985 | struct http_txn *txn = &t->txn; |
| 5986 | appsess *asession = NULL; |
| 5987 | char *sessid_temp = NULL; |
| 5988 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5989 | if (len > t->be->appsession_len) { |
| 5990 | len = t->be->appsession_len; |
| 5991 | } |
| 5992 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5993 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5994 | /* 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] | 5995 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5996 | /* 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] | 5997 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5998 | } |
| 5999 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6000 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6001 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6002 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6003 | return; |
| 6004 | } |
| 6005 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6006 | memcpy(txn->sessid, buf, len); |
| 6007 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6011 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6012 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6013 | return; |
| 6014 | } |
| 6015 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6016 | memcpy(sessid_temp, buf, len); |
| 6017 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6018 | |
| 6019 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6020 | /* free previously allocated memory */ |
| 6021 | pool_free2(apools.sessid, sessid_temp); |
| 6022 | |
| 6023 | if (asession != NULL) { |
| 6024 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6025 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6026 | asession->request_count++; |
| 6027 | |
| 6028 | if (asession->serverid != NULL) { |
| 6029 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6030 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6031 | while (srv) { |
| 6032 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6033 | if ((srv->state & SRV_RUNNING) || |
| 6034 | (t->be->options & PR_O_PERSIST) || |
| 6035 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6036 | /* we found the server and it's usable */ |
| 6037 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6038 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6039 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6040 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6041 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6042 | break; |
| 6043 | } else { |
| 6044 | txn->flags &= ~TX_CK_MASK; |
| 6045 | txn->flags |= TX_CK_DOWN; |
| 6046 | } |
| 6047 | } |
| 6048 | srv = srv->next; |
| 6049 | } |
| 6050 | } |
| 6051 | } |
| 6052 | } |
| 6053 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6054 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6055 | * same way as with headers above except that the semi-colon also ends a token. |
| 6056 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6057 | * return a valid result. |
| 6058 | */ |
| 6059 | char *find_cookie_value_end(char *s, const char *e) |
| 6060 | { |
| 6061 | int quoted, qdpair; |
| 6062 | |
| 6063 | quoted = qdpair = 0; |
| 6064 | for (; s < e; s++) { |
| 6065 | if (qdpair) qdpair = 0; |
| 6066 | else if (quoted) { |
| 6067 | if (*s == '\\') qdpair = 1; |
| 6068 | else if (*s == '"') quoted = 0; |
| 6069 | } |
| 6070 | else if (*s == '"') quoted = 1; |
| 6071 | else if (*s == ',' || *s == ';') return s; |
| 6072 | } |
| 6073 | return s; |
| 6074 | } |
| 6075 | |
| 6076 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6077 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6078 | * the first delimiter is updated if required. The function tries as much as |
| 6079 | * possible to respect the following principles : |
| 6080 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6081 | * colon, in which case <next> is simply removed |
| 6082 | * - set exactly one space character after the new first delimiter, unless |
| 6083 | * there are not enough characters in the block being moved to do so. |
| 6084 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6085 | * one. |
| 6086 | * |
| 6087 | * It is the caller's responsibility to ensure that : |
| 6088 | * - <from> points to a valid delimiter or the colon ; |
| 6089 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6090 | * - there are non-space chars before <from> ; |
| 6091 | * - there is a CR/LF at or after <next>. |
| 6092 | */ |
| 6093 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 6094 | { |
| 6095 | char *prev = *from; |
| 6096 | |
| 6097 | if (*prev == ':') { |
| 6098 | /* We're removing the first value, preserve the colon and add a |
| 6099 | * space if possible. |
| 6100 | */ |
| 6101 | if (!http_is_crlf[(unsigned char)*next]) |
| 6102 | next++; |
| 6103 | prev++; |
| 6104 | if (prev < next) |
| 6105 | *prev++ = ' '; |
| 6106 | |
| 6107 | while (http_is_spht[(unsigned char)*next]) |
| 6108 | next++; |
| 6109 | } else { |
| 6110 | /* Remove useless spaces before the old delimiter. */ |
| 6111 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6112 | prev--; |
| 6113 | *from = prev; |
| 6114 | |
| 6115 | /* copy the delimiter and if possible a space if we're |
| 6116 | * not at the end of the line. |
| 6117 | */ |
| 6118 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6119 | *prev++ = *next++; |
| 6120 | if (prev + 1 < next) |
| 6121 | *prev++ = ' '; |
| 6122 | while (http_is_spht[(unsigned char)*next]) |
| 6123 | next++; |
| 6124 | } |
| 6125 | } |
| 6126 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6127 | } |
| 6128 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6129 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6130 | * Manage client-side cookie. It can impact performance by about 2% so it is |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6131 | * desirable to call it only when needed. This code is quite complex because |
| 6132 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6133 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6134 | */ |
| 6135 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 6136 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6137 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6138 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6139 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6140 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6141 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6142 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6143 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6144 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6145 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6146 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6147 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6148 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6149 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6150 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6151 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6152 | hdr_beg = hdr_next; |
| 6153 | hdr_end = hdr_beg + cur_hdr->len; |
| 6154 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6155 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6156 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6157 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6158 | * "Cookie:" headers. |
| 6159 | */ |
| 6160 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6161 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6162 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6163 | old_idx = cur_idx; |
| 6164 | continue; |
| 6165 | } |
| 6166 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6167 | del_from = NULL; /* nothing to be deleted */ |
| 6168 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6169 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6170 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6171 | * attributes whose name begin with a '$', and associate them with |
| 6172 | * the right cookie, if we want to delete this cookie. |
| 6173 | * So there are 3 cases for each cookie read : |
| 6174 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6175 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6176 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6177 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6178 | * "special" cookie. |
| 6179 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6180 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6181 | * *MUST* delete it. |
| 6182 | * |
| 6183 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6184 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6185 | * the RFC explicitly allows spaces before it, and not within the |
| 6186 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6187 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6188 | * after the equal sign too, resulting in some (rare) buggy |
| 6189 | * implementations trying to do that. So let's do what servers do. |
| 6190 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6191 | * allowed quoted strings in values, with any possible character |
| 6192 | * after a backslash, including control chars and delimitors, which |
| 6193 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6194 | * within values even without quotes. |
| 6195 | * |
| 6196 | * We have to keep multiple pointers in order to support cookie |
| 6197 | * removal at the beginning, middle or end of header without |
| 6198 | * corrupting the header. All of these headers are valid : |
| 6199 | * |
| 6200 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6201 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6202 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6203 | * | | | | | | | | | |
| 6204 | * | | | | | | | | hdr_end <--+ |
| 6205 | * | | | | | | | +--> next |
| 6206 | * | | | | | | +----> val_end |
| 6207 | * | | | | | +-----------> val_beg |
| 6208 | * | | | | +--------------> equal |
| 6209 | * | | | +----------------> att_end |
| 6210 | * | | +---------------------> att_beg |
| 6211 | * | +--------------------------> prev |
| 6212 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6213 | */ |
| 6214 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6215 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6216 | /* Iterate through all cookies on this line */ |
| 6217 | |
| 6218 | /* find att_beg */ |
| 6219 | att_beg = prev + 1; |
| 6220 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6221 | att_beg++; |
| 6222 | |
| 6223 | /* find att_end : this is the first character after the last non |
| 6224 | * space before the equal. It may be equal to hdr_end. |
| 6225 | */ |
| 6226 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6227 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6228 | while (equal < hdr_end) { |
| 6229 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6230 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6231 | if (http_is_spht[(unsigned char)*equal++]) |
| 6232 | continue; |
| 6233 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6234 | } |
| 6235 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6236 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6237 | * is between <att_beg> and <equal>, both may be identical. |
| 6238 | */ |
| 6239 | |
| 6240 | /* look for end of cookie if there is an equal sign */ |
| 6241 | if (equal < hdr_end && *equal == '=') { |
| 6242 | /* look for the beginning of the value */ |
| 6243 | val_beg = equal + 1; |
| 6244 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6245 | val_beg++; |
| 6246 | |
| 6247 | /* find the end of the value, respecting quotes */ |
| 6248 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6249 | |
| 6250 | /* make val_end point to the first white space or delimitor after the value */ |
| 6251 | val_end = next; |
| 6252 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6253 | val_end--; |
| 6254 | } else { |
| 6255 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6256 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6257 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6258 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6259 | * they will automatically be removed if a header before them is removed, |
| 6260 | * since they're supposed to be linked together. |
| 6261 | */ |
| 6262 | if (*att_beg == '$') |
| 6263 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6264 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6265 | /* Ignore cookies with no equal sign */ |
| 6266 | if (equal == next) { |
| 6267 | /* This is not our cookie, so we must preserve it. But if we already |
| 6268 | * scheduled another cookie for removal, we cannot remove the |
| 6269 | * complete header, but we can remove the previous block itself. |
| 6270 | */ |
| 6271 | preserve_hdr = 1; |
| 6272 | if (del_from != NULL) { |
| 6273 | int delta = del_hdr_value(req, &del_from, prev); |
| 6274 | val_end += delta; |
| 6275 | next += delta; |
| 6276 | hdr_end += delta; |
| 6277 | hdr_next += delta; |
| 6278 | cur_hdr->len += delta; |
| 6279 | http_msg_move_end(&txn->req, delta); |
| 6280 | prev = del_from; |
| 6281 | del_from = NULL; |
| 6282 | } |
| 6283 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6284 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6285 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6286 | /* if there are spaces around the equal sign, we need to |
| 6287 | * strip them otherwise we'll get trouble for cookie captures, |
| 6288 | * or even for rewrites. Since this happens extremely rarely, |
| 6289 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6290 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6291 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6292 | int stripped_before = 0; |
| 6293 | int stripped_after = 0; |
| 6294 | |
| 6295 | if (att_end != equal) { |
| 6296 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6297 | equal += stripped_before; |
| 6298 | val_beg += stripped_before; |
| 6299 | } |
| 6300 | |
| 6301 | if (val_beg > equal + 1) { |
| 6302 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6303 | val_beg += stripped_after; |
| 6304 | stripped_before += stripped_after; |
| 6305 | } |
| 6306 | |
| 6307 | val_end += stripped_before; |
| 6308 | next += stripped_before; |
| 6309 | hdr_end += stripped_before; |
| 6310 | hdr_next += stripped_before; |
| 6311 | cur_hdr->len += stripped_before; |
| 6312 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6313 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6314 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6315 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6316 | /* First, let's see if we want to capture this cookie. We check |
| 6317 | * that we don't already have a client side cookie, because we |
| 6318 | * can only capture one. Also as an optimisation, we ignore |
| 6319 | * cookies shorter than the declared name. |
| 6320 | */ |
| 6321 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6322 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6323 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6324 | int log_len = val_end - att_beg; |
| 6325 | |
| 6326 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6327 | Alert("HTTP logging : out of memory.\n"); |
| 6328 | } else { |
| 6329 | if (log_len > t->fe->capture_len) |
| 6330 | log_len = t->fe->capture_len; |
| 6331 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6332 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6333 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6334 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6335 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6336 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6337 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6338 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6339 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6340 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6341 | * For cookies in prefix mode, the form is : |
| 6342 | * |
| 6343 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6344 | */ |
| 6345 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6346 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6347 | struct server *srv = t->be->srv; |
| 6348 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6349 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6350 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6351 | * have the server ID between val_beg and delim, and the original cookie between |
| 6352 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6353 | * |
| 6354 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6355 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6356 | * | || || | |+-> next |
| 6357 | * | || || | +--> val_end |
| 6358 | * | || || +---------> delim |
| 6359 | * | || |+------------> val_beg |
| 6360 | * | || +-------------> att_end = equal |
| 6361 | * | |+-----------------> att_beg |
| 6362 | * | +------------------> prev |
| 6363 | * +-------------------------> hdr_beg |
| 6364 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6365 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6366 | if (t->be->options & PR_O_COOK_PFX) { |
| 6367 | for (delim = val_beg; delim < val_end; delim++) |
| 6368 | if (*delim == COOKIE_DELIM) |
| 6369 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6370 | } else { |
| 6371 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6372 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6373 | /* Now check if the cookie contains a date field, which would |
| 6374 | * appear after a vertical bar ('|') just after the server name |
| 6375 | * and before the delimiter. |
| 6376 | */ |
| 6377 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6378 | if (vbar1) { |
| 6379 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6380 | * right is the last seen date. It is a base64 encoded |
| 6381 | * 30-bit value representing the UNIX date since the |
| 6382 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6383 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6384 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6385 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6386 | if (val_end - vbar1 >= 5) { |
| 6387 | val = b64tos30(vbar1); |
| 6388 | if (val > 0) |
| 6389 | txn->cookie_last_date = val << 2; |
| 6390 | } |
| 6391 | /* look for a second vertical bar */ |
| 6392 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6393 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6394 | val = b64tos30(vbar1 + 1); |
| 6395 | if (val > 0) |
| 6396 | txn->cookie_first_date = val << 2; |
| 6397 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6398 | } |
| 6399 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6400 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6401 | /* if the cookie has an expiration date and the proxy wants to check |
| 6402 | * it, then we do that now. We first check if the cookie is too old, |
| 6403 | * then only if it has expired. We detect strict overflow because the |
| 6404 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6405 | * in the future are ignored if their offset is beyond one day. This |
| 6406 | * allows an admin to fix timezone issues without expiring everyone |
| 6407 | * and at the same time avoids keeping unwanted side effects for too |
| 6408 | * long. |
| 6409 | */ |
| 6410 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6411 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6412 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6413 | txn->flags &= ~TX_CK_MASK; |
| 6414 | txn->flags |= TX_CK_OLD; |
| 6415 | delim = val_beg; // let's pretend we have not found the cookie |
| 6416 | txn->cookie_first_date = 0; |
| 6417 | txn->cookie_last_date = 0; |
| 6418 | } |
| 6419 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6420 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6421 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6422 | txn->flags &= ~TX_CK_MASK; |
| 6423 | txn->flags |= TX_CK_EXPIRED; |
| 6424 | delim = val_beg; // let's pretend we have not found the cookie |
| 6425 | txn->cookie_first_date = 0; |
| 6426 | txn->cookie_last_date = 0; |
| 6427 | } |
| 6428 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6429 | /* Here, we'll look for the first running server which supports the cookie. |
| 6430 | * This allows to share a same cookie between several servers, for example |
| 6431 | * to dedicate backup servers to specific servers only. |
| 6432 | * However, to prevent clients from sticking to cookie-less backup server |
| 6433 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6434 | * empty cookies and mark them as invalid. |
| 6435 | * The same behaviour is applied when persistence must be ignored. |
| 6436 | */ |
| 6437 | if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST)) |
| 6438 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6439 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6440 | while (srv) { |
| 6441 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6442 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6443 | if ((srv->state & SRV_RUNNING) || |
| 6444 | (t->be->options & PR_O_PERSIST) || |
| 6445 | (t->flags & SN_FORCE_PRST)) { |
| 6446 | /* we found the server and we can use it */ |
| 6447 | txn->flags &= ~TX_CK_MASK; |
| 6448 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6449 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6450 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6451 | break; |
| 6452 | } else { |
| 6453 | /* we found a server, but it's down, |
| 6454 | * mark it as such and go on in case |
| 6455 | * another one is available. |
| 6456 | */ |
| 6457 | txn->flags &= ~TX_CK_MASK; |
| 6458 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6459 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6460 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6461 | srv = srv->next; |
| 6462 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6463 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6464 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6465 | /* no server matched this cookie */ |
| 6466 | txn->flags &= ~TX_CK_MASK; |
| 6467 | txn->flags |= TX_CK_INVALID; |
| 6468 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6469 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6470 | /* depending on the cookie mode, we may have to either : |
| 6471 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6472 | * the server never sees it ; |
| 6473 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6474 | * application cookie so that it does not get accidentely removed later, |
| 6475 | * if we're in cookie prefix mode |
| 6476 | */ |
| 6477 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6478 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6479 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6480 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6481 | val_end += delta; |
| 6482 | next += delta; |
| 6483 | hdr_end += delta; |
| 6484 | hdr_next += delta; |
| 6485 | cur_hdr->len += delta; |
| 6486 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6487 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6488 | del_from = NULL; |
| 6489 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6490 | } |
| 6491 | else if (del_from == NULL && |
| 6492 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6493 | del_from = prev; |
| 6494 | } |
| 6495 | } else { |
| 6496 | /* This is not our cookie, so we must preserve it. But if we already |
| 6497 | * scheduled another cookie for removal, we cannot remove the |
| 6498 | * complete header, but we can remove the previous block itself. |
| 6499 | */ |
| 6500 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6501 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6502 | if (del_from != NULL) { |
| 6503 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6504 | if (att_beg >= del_from) |
| 6505 | att_beg += delta; |
| 6506 | if (att_end >= del_from) |
| 6507 | att_end += delta; |
| 6508 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6509 | val_end += delta; |
| 6510 | next += delta; |
| 6511 | hdr_end += delta; |
| 6512 | hdr_next += delta; |
| 6513 | cur_hdr->len += delta; |
| 6514 | http_msg_move_end(&txn->req, delta); |
| 6515 | prev = del_from; |
| 6516 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6517 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6518 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6519 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6520 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6521 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6522 | int cmp_len, value_len; |
| 6523 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6524 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6525 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6526 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6527 | value_begin = att_beg + t->be->appsession_name_len; |
| 6528 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6529 | } else { |
| 6530 | cmp_len = att_end - att_beg; |
| 6531 | value_begin = val_beg; |
| 6532 | value_len = val_end - val_beg; |
| 6533 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6534 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6535 | /* let's see if the cookie is our appcookie */ |
| 6536 | if (cmp_len == t->be->appsession_name_len && |
| 6537 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6538 | manage_client_side_appsession(t, value_begin, value_len); |
| 6539 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6540 | } |
| 6541 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6542 | /* continue with next cookie on this header line */ |
| 6543 | att_beg = next; |
| 6544 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6545 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6546 | /* There are no more cookies on this line. |
| 6547 | * We may still have one (or several) marked for deletion at the |
| 6548 | * end of the line. We must do this now in two ways : |
| 6549 | * - if some cookies must be preserved, we only delete from the |
| 6550 | * mark to the end of line ; |
| 6551 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6552 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6553 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6554 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6555 | if (preserve_hdr) { |
| 6556 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6557 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6558 | cur_hdr->len += delta; |
| 6559 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6560 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6561 | |
| 6562 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6563 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6564 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6565 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6566 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6567 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6568 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6569 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6570 | } |
| 6571 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6572 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6573 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6574 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6575 | } |
| 6576 | |
| 6577 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6578 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6579 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6580 | */ |
| 6581 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6582 | { |
| 6583 | char term; |
| 6584 | char *cur_ptr, *cur_end, *cur_next; |
| 6585 | int cur_idx, old_idx, last_hdr; |
| 6586 | struct http_txn *txn = &t->txn; |
| 6587 | struct hdr_idx_elem *cur_hdr; |
| 6588 | int len, delta; |
| 6589 | |
| 6590 | last_hdr = 0; |
| 6591 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6592 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6593 | old_idx = 0; |
| 6594 | |
| 6595 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6596 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6597 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6598 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6599 | (exp->action == ACT_ALLOW || |
| 6600 | exp->action == ACT_DENY)) |
| 6601 | return 0; |
| 6602 | |
| 6603 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6604 | if (!cur_idx) |
| 6605 | break; |
| 6606 | |
| 6607 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6608 | cur_ptr = cur_next; |
| 6609 | cur_end = cur_ptr + cur_hdr->len; |
| 6610 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6611 | |
| 6612 | /* Now we have one header between cur_ptr and cur_end, |
| 6613 | * and the next header starts at cur_next. |
| 6614 | */ |
| 6615 | |
| 6616 | /* The annoying part is that pattern matching needs |
| 6617 | * that we modify the contents to null-terminate all |
| 6618 | * strings before testing them. |
| 6619 | */ |
| 6620 | |
| 6621 | term = *cur_end; |
| 6622 | *cur_end = '\0'; |
| 6623 | |
| 6624 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6625 | switch (exp->action) { |
| 6626 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6627 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6628 | last_hdr = 1; |
| 6629 | break; |
| 6630 | |
| 6631 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6632 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6633 | last_hdr = 1; |
| 6634 | break; |
| 6635 | |
| 6636 | case ACT_REPLACE: |
| 6637 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6638 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6639 | /* FIXME: if the user adds a newline in the replacement, the |
| 6640 | * index will not be recalculated for now, and the new line |
| 6641 | * will not be counted as a new header. |
| 6642 | */ |
| 6643 | |
| 6644 | cur_end += delta; |
| 6645 | cur_next += delta; |
| 6646 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6647 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6648 | break; |
| 6649 | |
| 6650 | case ACT_REMOVE: |
| 6651 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6652 | cur_next += delta; |
| 6653 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6654 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6655 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6656 | txn->hdr_idx.used--; |
| 6657 | cur_hdr->len = 0; |
| 6658 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6659 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6660 | break; |
| 6661 | |
| 6662 | } |
| 6663 | } |
| 6664 | if (cur_end) |
| 6665 | *cur_end = term; /* restore the string terminator */ |
| 6666 | |
| 6667 | /* keep the link from this header to next one in case of later |
| 6668 | * removal of next header. |
| 6669 | */ |
| 6670 | old_idx = cur_idx; |
| 6671 | } |
| 6672 | return 0; |
| 6673 | } |
| 6674 | |
| 6675 | |
| 6676 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6677 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6678 | * or -1 if a replacement resulted in an invalid status line. |
| 6679 | */ |
| 6680 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6681 | { |
| 6682 | char term; |
| 6683 | char *cur_ptr, *cur_end; |
| 6684 | int done; |
| 6685 | struct http_txn *txn = &t->txn; |
| 6686 | int len, delta; |
| 6687 | |
| 6688 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6689 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6690 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6691 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6692 | (exp->action == ACT_ALLOW || |
| 6693 | exp->action == ACT_DENY)) |
| 6694 | return 0; |
| 6695 | else if (exp->action == ACT_REMOVE) |
| 6696 | return 0; |
| 6697 | |
| 6698 | done = 0; |
| 6699 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6700 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6701 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6702 | |
| 6703 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6704 | |
| 6705 | /* The annoying part is that pattern matching needs |
| 6706 | * that we modify the contents to null-terminate all |
| 6707 | * strings before testing them. |
| 6708 | */ |
| 6709 | |
| 6710 | term = *cur_end; |
| 6711 | *cur_end = '\0'; |
| 6712 | |
| 6713 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6714 | switch (exp->action) { |
| 6715 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6716 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6717 | done = 1; |
| 6718 | break; |
| 6719 | |
| 6720 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6721 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6722 | done = 1; |
| 6723 | break; |
| 6724 | |
| 6725 | case ACT_REPLACE: |
| 6726 | *cur_end = term; /* restore the string terminator */ |
| 6727 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6728 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6729 | /* FIXME: if the user adds a newline in the replacement, the |
| 6730 | * index will not be recalculated for now, and the new line |
| 6731 | * will not be counted as a new header. |
| 6732 | */ |
| 6733 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6734 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6735 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6736 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6737 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6738 | cur_ptr, cur_end + 1, |
| 6739 | NULL, NULL); |
| 6740 | if (unlikely(!cur_end)) |
| 6741 | return -1; |
| 6742 | |
| 6743 | /* we have a full respnse and we know that we have either a CR |
| 6744 | * or an LF at <ptr>. |
| 6745 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6746 | txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6747 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r'); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6748 | /* there is no point trying this regex on headers */ |
| 6749 | return 1; |
| 6750 | } |
| 6751 | } |
| 6752 | *cur_end = term; /* restore the string terminator */ |
| 6753 | return done; |
| 6754 | } |
| 6755 | |
| 6756 | |
| 6757 | |
| 6758 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6759 | * 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] | 6760 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6761 | * unparsable response. |
| 6762 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6763 | 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] | 6764 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6765 | struct http_txn *txn = &s->txn; |
| 6766 | struct hdr_exp *exp; |
| 6767 | |
| 6768 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6769 | int ret; |
| 6770 | |
| 6771 | /* |
| 6772 | * The interleaving of transformations and verdicts |
| 6773 | * makes it difficult to decide to continue or stop |
| 6774 | * the evaluation. |
| 6775 | */ |
| 6776 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6777 | if (txn->flags & TX_SVDENY) |
| 6778 | break; |
| 6779 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6780 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6781 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6782 | exp->action == ACT_PASS)) { |
| 6783 | exp = exp->next; |
| 6784 | continue; |
| 6785 | } |
| 6786 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6787 | /* if this filter had a condition, evaluate it now and skip to |
| 6788 | * next filter if the condition does not match. |
| 6789 | */ |
| 6790 | if (exp->cond) { |
| 6791 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6792 | ret = acl_pass(ret); |
| 6793 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6794 | ret = !ret; |
| 6795 | if (!ret) |
| 6796 | continue; |
| 6797 | } |
| 6798 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6799 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6800 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6801 | if (unlikely(ret < 0)) |
| 6802 | return -1; |
| 6803 | |
| 6804 | if (likely(ret == 0)) { |
| 6805 | /* The filter did not match the response, it can be |
| 6806 | * iterated through all headers. |
| 6807 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6808 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6809 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6810 | } |
| 6811 | return 0; |
| 6812 | } |
| 6813 | |
| 6814 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6815 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6816 | * Manage server-side cookies. It can impact performance by about 2% so it is |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6817 | * desirable to call it only when needed. This function is also used when we |
| 6818 | * just need to know if there is a cookie (eg: for check-cache). |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6819 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6820 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6821 | { |
| 6822 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6823 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6824 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6825 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6826 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6827 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6828 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6829 | /* Iterate through the headers. |
| 6830 | * we start with the start line. |
| 6831 | */ |
| 6832 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6833 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6834 | |
| 6835 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6836 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6837 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6838 | |
| 6839 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6840 | hdr_beg = hdr_next; |
| 6841 | hdr_end = hdr_beg + cur_hdr->len; |
| 6842 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6843 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6844 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6845 | * next header starts at hdr_next. We're only interested in |
| 6846 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6847 | */ |
| 6848 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6849 | is_cookie2 = 0; |
| 6850 | prev = hdr_beg + 10; |
| 6851 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6852 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6853 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6854 | if (!val) { |
| 6855 | old_idx = cur_idx; |
| 6856 | continue; |
| 6857 | } |
| 6858 | is_cookie2 = 1; |
| 6859 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6860 | } |
| 6861 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6862 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6863 | * <prev> points to the colon. |
| 6864 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6865 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6866 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6867 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6868 | * check-cache is enabled) and we are not interested in checking |
| 6869 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6870 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6871 | if (t->be->cookie_name == NULL && |
| 6872 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6873 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6874 | return; |
| 6875 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6876 | /* OK so now we know we have to process this response cookie. |
| 6877 | * The format of the Set-Cookie header is slightly different |
| 6878 | * from the format of the Cookie header in that it does not |
| 6879 | * support the comma as a cookie delimiter (thus the header |
| 6880 | * cannot be folded) because the Expires attribute described in |
| 6881 | * the original Netscape's spec may contain an unquoted date |
| 6882 | * with a comma inside. We have to live with this because |
| 6883 | * many browsers don't support Max-Age and some browsers don't |
| 6884 | * support quoted strings. However the Set-Cookie2 header is |
| 6885 | * clean. |
| 6886 | * |
| 6887 | * We have to keep multiple pointers in order to support cookie |
| 6888 | * removal at the beginning, middle or end of header without |
| 6889 | * corrupting the header (in case of set-cookie2). A special |
| 6890 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6891 | * fields after the first semi-colon. The <next> pointer points |
| 6892 | * either to the end of line (set-cookie) or next unquoted comma |
| 6893 | * (set-cookie2). All of these headers are valid : |
| 6894 | * |
| 6895 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6896 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6897 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6898 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6899 | * | | | | | | | | | | |
| 6900 | * | | | | | | | | +-> next hdr_end <--+ |
| 6901 | * | | | | | | | +------------> scav |
| 6902 | * | | | | | | +--------------> val_end |
| 6903 | * | | | | | +--------------------> val_beg |
| 6904 | * | | | | +----------------------> equal |
| 6905 | * | | | +------------------------> att_end |
| 6906 | * | | +----------------------------> att_beg |
| 6907 | * | +------------------------------> prev |
| 6908 | * +-----------------------------------------> hdr_beg |
| 6909 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6910 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6911 | for (; prev < hdr_end; prev = next) { |
| 6912 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6913 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6914 | /* find att_beg */ |
| 6915 | att_beg = prev + 1; |
| 6916 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6917 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6918 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6919 | /* find att_end : this is the first character after the last non |
| 6920 | * space before the equal. It may be equal to hdr_end. |
| 6921 | */ |
| 6922 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6923 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6924 | while (equal < hdr_end) { |
| 6925 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6926 | break; |
| 6927 | if (http_is_spht[(unsigned char)*equal++]) |
| 6928 | continue; |
| 6929 | att_end = equal; |
| 6930 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6931 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6932 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6933 | * is between <att_beg> and <equal>, both may be identical. |
| 6934 | */ |
| 6935 | |
| 6936 | /* look for end of cookie if there is an equal sign */ |
| 6937 | if (equal < hdr_end && *equal == '=') { |
| 6938 | /* look for the beginning of the value */ |
| 6939 | val_beg = equal + 1; |
| 6940 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6941 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6942 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6943 | /* find the end of the value, respecting quotes */ |
| 6944 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6945 | |
| 6946 | /* make val_end point to the first white space or delimitor after the value */ |
| 6947 | val_end = next; |
| 6948 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6949 | val_end--; |
| 6950 | } else { |
| 6951 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6952 | val_beg = val_end = next = equal; |
| 6953 | } |
| 6954 | |
| 6955 | if (next < hdr_end) { |
| 6956 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6957 | * a colon or semi-colon before the end. So skip all attr-value |
| 6958 | * pairs and look for the next comma. For Set-Cookie, since |
| 6959 | * commas are permitted in values, skip to the end. |
| 6960 | */ |
| 6961 | if (is_cookie2) |
| 6962 | next = find_hdr_value_end(next, hdr_end); |
| 6963 | else |
| 6964 | next = hdr_end; |
| 6965 | } |
| 6966 | |
| 6967 | /* Now everything is as on the diagram above */ |
| 6968 | |
| 6969 | /* Ignore cookies with no equal sign */ |
| 6970 | if (equal == val_end) |
| 6971 | continue; |
| 6972 | |
| 6973 | /* If there are spaces around the equal sign, we need to |
| 6974 | * strip them otherwise we'll get trouble for cookie captures, |
| 6975 | * or even for rewrites. Since this happens extremely rarely, |
| 6976 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6977 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6978 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6979 | int stripped_before = 0; |
| 6980 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6981 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6982 | if (att_end != equal) { |
| 6983 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6984 | equal += stripped_before; |
| 6985 | val_beg += stripped_before; |
| 6986 | } |
| 6987 | |
| 6988 | if (val_beg > equal + 1) { |
| 6989 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6990 | val_beg += stripped_after; |
| 6991 | stripped_before += stripped_after; |
| 6992 | } |
| 6993 | |
| 6994 | val_end += stripped_before; |
| 6995 | next += stripped_before; |
| 6996 | hdr_end += stripped_before; |
| 6997 | hdr_next += stripped_before; |
| 6998 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6999 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7000 | } |
| 7001 | |
| 7002 | /* First, let's see if we want to capture this cookie. We check |
| 7003 | * that we don't already have a server side cookie, because we |
| 7004 | * can only capture one. Also as an optimisation, we ignore |
| 7005 | * cookies shorter than the declared name. |
| 7006 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7007 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7008 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7009 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7010 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7011 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7012 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7013 | Alert("HTTP logging : out of memory.\n"); |
| 7014 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7015 | else { |
| 7016 | if (log_len > t->fe->capture_len) |
| 7017 | log_len = t->fe->capture_len; |
| 7018 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7019 | txn->srv_cookie[log_len] = 0; |
| 7020 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7021 | } |
| 7022 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7023 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7024 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7025 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7026 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7027 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7028 | /* assume passive cookie by default */ |
| 7029 | txn->flags &= ~TX_SCK_MASK; |
| 7030 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7031 | |
| 7032 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7033 | * this occurrence because we'll insert another one later. |
| 7034 | * We'll delete it too if the "indirect" option is set and we're in |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7035 | * a direct access. |
| 7036 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7037 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 7038 | /* The "preserve" flag was set, we don't want to touch the |
| 7039 | * server's cookie. |
| 7040 | */ |
| 7041 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7042 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7043 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7044 | /* this cookie must be deleted */ |
| 7045 | if (*prev == ':' && next == hdr_end) { |
| 7046 | /* whole header */ |
| 7047 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 7048 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7049 | txn->hdr_idx.used--; |
| 7050 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7051 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7052 | hdr_next += delta; |
| 7053 | http_msg_move_end(&txn->rsp, delta); |
| 7054 | /* note: while both invalid now, <next> and <hdr_end> |
| 7055 | * are still equal, so the for() will stop as expected. |
| 7056 | */ |
| 7057 | } else { |
| 7058 | /* just remove the value */ |
| 7059 | int delta = del_hdr_value(res, &prev, next); |
| 7060 | next = prev; |
| 7061 | hdr_end += delta; |
| 7062 | hdr_next += delta; |
| 7063 | cur_hdr->len += delta; |
| 7064 | http_msg_move_end(&txn->rsp, delta); |
| 7065 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7066 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7067 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7068 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7069 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7070 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7071 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7072 | * with this server since we know it. |
| 7073 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7074 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7075 | next += delta; |
| 7076 | hdr_end += delta; |
| 7077 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7078 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7079 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7080 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7081 | txn->flags &= ~TX_SCK_MASK; |
| 7082 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7083 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7084 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7085 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7086 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7087 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7088 | delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7089 | next += delta; |
| 7090 | hdr_end += delta; |
| 7091 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7092 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7093 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7094 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7095 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7096 | txn->flags &= ~TX_SCK_MASK; |
| 7097 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7098 | } |
| 7099 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7100 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7101 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7102 | int cmp_len, value_len; |
| 7103 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7104 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7105 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7106 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7107 | value_begin = att_beg + t->be->appsession_name_len; |
| 7108 | value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7109 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7110 | cmp_len = att_end - att_beg; |
| 7111 | value_begin = val_beg; |
| 7112 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7113 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7114 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7115 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7116 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7117 | /* free a possibly previously allocated memory */ |
| 7118 | pool_free2(apools.sessid, txn->sessid); |
| 7119 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7120 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7121 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7122 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7123 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7124 | return; |
| 7125 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7126 | memcpy(txn->sessid, value_begin, value_len); |
| 7127 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7128 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7129 | } |
| 7130 | /* that's done for this cookie, check the next one on the same |
| 7131 | * line when next != hdr_end (only if is_cookie2). |
| 7132 | */ |
| 7133 | } |
| 7134 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7135 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7136 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7137 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7138 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7139 | appsess *asession = NULL; |
| 7140 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7141 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7142 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7143 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7144 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7145 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7146 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7147 | return; |
| 7148 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7149 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7150 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7151 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7152 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7153 | 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] | 7154 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7155 | return; |
| 7156 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7157 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7158 | asession->sessid[t->be->appsession_len] = 0; |
| 7159 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7160 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7161 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7162 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7163 | 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] | 7164 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7165 | return; |
| 7166 | } |
| 7167 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7168 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7169 | |
| 7170 | asession->request_count = 0; |
| 7171 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7172 | } |
| 7173 | |
| 7174 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7175 | asession->request_count++; |
| 7176 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7177 | } |
| 7178 | |
| 7179 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7180 | /* |
| 7181 | * Check if response is cacheable or not. Updates t->flags. |
| 7182 | */ |
| 7183 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 7184 | { |
| 7185 | struct http_txn *txn = &t->txn; |
| 7186 | char *p1, *p2; |
| 7187 | |
| 7188 | char *cur_ptr, *cur_end, *cur_next; |
| 7189 | int cur_idx; |
| 7190 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7191 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7192 | return; |
| 7193 | |
| 7194 | /* Iterate through the headers. |
| 7195 | * we start with the start line. |
| 7196 | */ |
| 7197 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7198 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7199 | |
| 7200 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7201 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7202 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7203 | |
| 7204 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7205 | cur_ptr = cur_next; |
| 7206 | cur_end = cur_ptr + cur_hdr->len; |
| 7207 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7208 | |
| 7209 | /* We have one full header between cur_ptr and cur_end, and the |
| 7210 | * next header starts at cur_next. We're only interested in |
| 7211 | * "Cookie:" headers. |
| 7212 | */ |
| 7213 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7214 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7215 | if (val) { |
| 7216 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7217 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7218 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7219 | return; |
| 7220 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7221 | } |
| 7222 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7223 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7224 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7225 | continue; |
| 7226 | |
| 7227 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7228 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7229 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7230 | |
| 7231 | if (p1 >= cur_end) /* no more info */ |
| 7232 | continue; |
| 7233 | |
| 7234 | /* p1 is at the beginning of the value */ |
| 7235 | p2 = p1; |
| 7236 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7237 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7238 | p2++; |
| 7239 | |
| 7240 | /* we have a complete value between p1 and p2 */ |
| 7241 | if (p2 < cur_end && *p2 == '=') { |
| 7242 | /* we have something of the form no-cache="set-cookie" */ |
| 7243 | if ((cur_end - p1 >= 21) && |
| 7244 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7245 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7246 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7247 | continue; |
| 7248 | } |
| 7249 | |
| 7250 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7251 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7252 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7253 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7254 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7255 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7256 | return; |
| 7257 | } |
| 7258 | |
| 7259 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7260 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7261 | continue; |
| 7262 | } |
| 7263 | } |
| 7264 | } |
| 7265 | |
| 7266 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7267 | /* |
| 7268 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7269 | * If the server is found, it's assigned to the session. |
| 7270 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7271 | 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] | 7272 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7273 | char *end_params, *first_param, *cur_param, *next_param; |
| 7274 | char separator; |
| 7275 | int value_len; |
| 7276 | |
| 7277 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7278 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7279 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7280 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7281 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7282 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7283 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7284 | first_param = NULL; |
| 7285 | switch (mode) { |
| 7286 | case PR_O2_AS_M_PP: |
| 7287 | first_param = memchr(begin, ';', len); |
| 7288 | break; |
| 7289 | case PR_O2_AS_M_QS: |
| 7290 | first_param = memchr(begin, '?', len); |
| 7291 | break; |
| 7292 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7293 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7294 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7295 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7296 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7297 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7298 | switch (mode) { |
| 7299 | case PR_O2_AS_M_PP: |
| 7300 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7301 | end_params = (char *) begin + len; |
| 7302 | } |
| 7303 | separator = ';'; |
| 7304 | break; |
| 7305 | case PR_O2_AS_M_QS: |
| 7306 | end_params = (char *) begin + len; |
| 7307 | separator = '&'; |
| 7308 | break; |
| 7309 | default: |
| 7310 | /* unknown mode, shouldn't happen */ |
| 7311 | return; |
| 7312 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7313 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7314 | cur_param = next_param = end_params; |
| 7315 | while (cur_param > first_param) { |
| 7316 | cur_param--; |
| 7317 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7318 | /* let's see if this is the appsession parameter */ |
| 7319 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7320 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7321 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7322 | /* Cool... it's the right one */ |
| 7323 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7324 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7325 | if (value_len > 0) { |
| 7326 | manage_client_side_appsession(t, cur_param, value_len); |
| 7327 | } |
| 7328 | break; |
| 7329 | } |
| 7330 | next_param = cur_param; |
| 7331 | } |
| 7332 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7333 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7334 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7335 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7336 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7337 | } |
| 7338 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7339 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7340 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7341 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7342 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7343 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7344 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7345 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7346 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7347 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7348 | int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7349 | { |
| 7350 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7351 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7352 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7353 | if (!uri_auth) |
| 7354 | return 0; |
| 7355 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7356 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7357 | return 0; |
| 7358 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7359 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7360 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7361 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7362 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7363 | return 0; |
| 7364 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7365 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7366 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7367 | /* the URI is in h */ |
| 7368 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7369 | return 0; |
| 7370 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7371 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7372 | 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] | 7373 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7374 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7375 | break; |
| 7376 | } |
| 7377 | h++; |
| 7378 | } |
| 7379 | |
| 7380 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7381 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7382 | 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] | 7383 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7384 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7385 | break; |
| 7386 | } |
| 7387 | h++; |
| 7388 | } |
| 7389 | } |
| 7390 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7391 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7392 | 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] | 7393 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7394 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7395 | break; |
| 7396 | } |
| 7397 | h++; |
| 7398 | } |
| 7399 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7400 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7401 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7402 | if (memcmp(h, ";st=", 4) == 0) { |
| 7403 | h += 4; |
| 7404 | |
| 7405 | if (memcmp(h, STAT_STATUS_DONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7406 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7407 | else if (memcmp(h, STAT_STATUS_NONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7408 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7409 | else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7410 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 7411 | else if (memcmp(h, STAT_STATUS_DENY, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7412 | si->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7413 | else |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7414 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7415 | break; |
| 7416 | } |
| 7417 | h++; |
| 7418 | } |
| 7419 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7420 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7421 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7422 | return 1; |
| 7423 | } |
| 7424 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7425 | /* |
| 7426 | * 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] | 7427 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 7428 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7429 | */ |
| 7430 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7431 | struct buffer *buf, struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7432 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7433 | { |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7434 | if (buf->r <= (buf->data + msg->som)) { /* message wraps */ |
| 7435 | int len1 = buf->size - msg->som; |
| 7436 | es->len = buf->r - (buf->data + msg->som) + buf->size; |
| 7437 | memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf))); |
| 7438 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7439 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7440 | } |
| 7441 | else { |
| 7442 | es->len = buf->r - (buf->data + msg->som); |
| 7443 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
| 7444 | } |
| 7445 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7446 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7447 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7448 | else if (buf->lr >= (buf->data + msg->som)) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7449 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7450 | else |
| 7451 | es->pos = buf->lr - (buf->data + msg->som) + buf->size; |
| 7452 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7453 | es->when = date; // user-visible date |
| 7454 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7455 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7456 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7457 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7458 | es->state = state; |
| 7459 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7460 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7461 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7462 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7463 | /* return the IP address pointed to by occurrence <occ> of header <hname> in |
| 7464 | * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the |
| 7465 | * occurrence number corresponding to this value is returned. If <occ> is |
| 7466 | * strictly negative, the occurrence number before the end corresponding to |
| 7467 | * this value is returned. If <occ> is null, any value is returned, so it is |
| 7468 | * not recommended to use it that way. Negative occurrences are limited to |
| 7469 | * a small value because it is required to keep them in memory while scanning. |
| 7470 | * IP address 0.0.0.0 is returned if no match is found. |
| 7471 | */ |
| 7472 | unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ) |
| 7473 | { |
| 7474 | struct hdr_ctx ctx; |
| 7475 | unsigned int hdr_hist[MAX_HDR_HISTORY]; |
| 7476 | unsigned int hist_ptr; |
| 7477 | int found = 0; |
| 7478 | |
| 7479 | ctx.idx = 0; |
| 7480 | if (occ >= 0) { |
| 7481 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7482 | occ--; |
| 7483 | if (occ <= 0) { |
| 7484 | found = 1; |
| 7485 | break; |
| 7486 | } |
| 7487 | } |
| 7488 | if (!found) |
| 7489 | return 0; |
| 7490 | return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7491 | } |
| 7492 | |
| 7493 | /* negative occurrence, we scan all the list then walk back */ |
| 7494 | if (-occ > MAX_HDR_HISTORY) |
| 7495 | return 0; |
| 7496 | |
| 7497 | hist_ptr = 0; |
| 7498 | hdr_hist[hist_ptr] = 0; |
| 7499 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7500 | hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7501 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7502 | hist_ptr = 0; |
| 7503 | found++; |
| 7504 | } |
| 7505 | if (-occ > found) |
| 7506 | return 0; |
| 7507 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7508 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7509 | */ |
| 7510 | hist_ptr += occ; |
| 7511 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7512 | hist_ptr -= MAX_HDR_HISTORY; |
| 7513 | return hdr_hist[hist_ptr]; |
| 7514 | } |
| 7515 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7516 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7517 | * Print a debug line with a header |
| 7518 | */ |
| 7519 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7520 | { |
| 7521 | int len, max; |
| 7522 | 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] | 7523 | 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] | 7524 | max = end - start; |
| 7525 | UBOUND(max, sizeof(trash) - len - 1); |
| 7526 | len += strlcpy2(trash + len, start, max + 1); |
| 7527 | trash[len++] = '\n'; |
| 7528 | write(1, trash, len); |
| 7529 | } |
| 7530 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7531 | /* |
| 7532 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7533 | * the required fields are properly allocated and that we only need to (re)init |
| 7534 | * them. This should be used before processing any new request. |
| 7535 | */ |
| 7536 | void http_init_txn(struct session *s) |
| 7537 | { |
| 7538 | struct http_txn *txn = &s->txn; |
| 7539 | struct proxy *fe = s->fe; |
| 7540 | |
| 7541 | txn->flags = 0; |
| 7542 | txn->status = -1; |
| 7543 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7544 | txn->cookie_first_date = 0; |
| 7545 | txn->cookie_last_date = 0; |
| 7546 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7547 | txn->req.sol = txn->req.eol = NULL; |
| 7548 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 7549 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7550 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7551 | txn->req.chunk_len = 0LL; |
| 7552 | txn->req.body_len = 0LL; |
| 7553 | txn->rsp.chunk_len = 0LL; |
| 7554 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7555 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7556 | 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] | 7557 | |
| 7558 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7559 | |
| 7560 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7561 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7562 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7563 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7564 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7565 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7566 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7567 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7568 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7569 | |
| 7570 | if (txn->hdr_idx.v) |
| 7571 | hdr_idx_init(&txn->hdr_idx); |
| 7572 | } |
| 7573 | |
| 7574 | /* to be used at the end of a transaction */ |
| 7575 | void http_end_txn(struct session *s) |
| 7576 | { |
| 7577 | struct http_txn *txn = &s->txn; |
| 7578 | |
| 7579 | /* these ones will have been dynamically allocated */ |
| 7580 | pool_free2(pool2_requri, txn->uri); |
| 7581 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7582 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7583 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7584 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7585 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7586 | txn->uri = NULL; |
| 7587 | txn->srv_cookie = NULL; |
| 7588 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7589 | |
| 7590 | if (txn->req.cap) { |
| 7591 | struct cap_hdr *h; |
| 7592 | for (h = s->fe->req_cap; h; h = h->next) |
| 7593 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7594 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7595 | } |
| 7596 | |
| 7597 | if (txn->rsp.cap) { |
| 7598 | struct cap_hdr *h; |
| 7599 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7600 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7601 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7602 | } |
| 7603 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7604 | } |
| 7605 | |
| 7606 | /* to be used at the end of a transaction to prepare a new one */ |
| 7607 | void http_reset_txn(struct session *s) |
| 7608 | { |
| 7609 | http_end_txn(s); |
| 7610 | http_init_txn(s); |
| 7611 | |
| 7612 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7613 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7614 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7615 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7616 | /* re-init store persistence */ |
| 7617 | s->store_count = 0; |
| 7618 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7619 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7620 | |
| 7621 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7622 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7623 | /* We must trim any excess data from the response buffer, because we |
| 7624 | * may have blocked an invalid response from a server that we don't |
| 7625 | * want to accidentely forward once we disable the analysers, nor do |
| 7626 | * we want those data to come along with next response. A typical |
| 7627 | * example of such data would be from a buggy server responding to |
| 7628 | * a HEAD with some data, or sending more than the advertised |
| 7629 | * content-length. |
| 7630 | */ |
| 7631 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 7632 | s->rep->l = s->rep->send_max; |
| 7633 | s->rep->r = s->rep->w + s->rep->l; |
| 7634 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 7635 | s->rep->r -= s->rep->size; |
| 7636 | } |
| 7637 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7638 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7639 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7640 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7641 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7642 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7643 | |
| 7644 | s->req->rex = TICK_ETERNITY; |
| 7645 | s->req->wex = TICK_ETERNITY; |
| 7646 | s->req->analyse_exp = TICK_ETERNITY; |
| 7647 | s->rep->rex = TICK_ETERNITY; |
| 7648 | s->rep->wex = TICK_ETERNITY; |
| 7649 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7650 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7651 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7652 | void free_http_req_rules(struct list *r) { |
| 7653 | struct http_req_rule *tr, *pr; |
| 7654 | |
| 7655 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7656 | LIST_DEL(&pr->list); |
| 7657 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7658 | free(pr->http_auth.realm); |
| 7659 | |
| 7660 | free(pr); |
| 7661 | } |
| 7662 | } |
| 7663 | |
| 7664 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7665 | { |
| 7666 | struct http_req_rule *rule; |
| 7667 | int cur_arg; |
| 7668 | |
| 7669 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7670 | if (!rule) { |
| 7671 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7672 | return NULL; |
| 7673 | } |
| 7674 | |
| 7675 | if (!*args[0]) { |
| 7676 | goto req_error_parsing; |
| 7677 | } else if (!strcmp(args[0], "allow")) { |
| 7678 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7679 | cur_arg = 1; |
| 7680 | } else if (!strcmp(args[0], "deny")) { |
| 7681 | rule->action = HTTP_REQ_ACT_DENY; |
| 7682 | cur_arg = 1; |
| 7683 | } else if (!strcmp(args[0], "auth")) { |
| 7684 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7685 | cur_arg = 1; |
| 7686 | |
| 7687 | while(*args[cur_arg]) { |
| 7688 | if (!strcmp(args[cur_arg], "realm")) { |
| 7689 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7690 | cur_arg+=2; |
| 7691 | continue; |
| 7692 | } else |
| 7693 | break; |
| 7694 | } |
| 7695 | } else { |
| 7696 | req_error_parsing: |
| 7697 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7698 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7699 | return NULL; |
| 7700 | } |
| 7701 | |
| 7702 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7703 | struct acl_cond *cond; |
| 7704 | |
| 7705 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7706 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7707 | file, linenum, args[0]); |
| 7708 | return NULL; |
| 7709 | } |
| 7710 | rule->cond = cond; |
| 7711 | } |
| 7712 | else if (*args[cur_arg]) { |
| 7713 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7714 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7715 | file, linenum, args[0], args[cur_arg]); |
| 7716 | return NULL; |
| 7717 | } |
| 7718 | |
| 7719 | return rule; |
| 7720 | } |
| 7721 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7722 | /************************************************************************/ |
| 7723 | /* The code below is dedicated to ACL parsing and matching */ |
| 7724 | /************************************************************************/ |
| 7725 | |
| 7726 | |
| 7727 | |
| 7728 | |
| 7729 | /* 1. Check on METHOD |
| 7730 | * We use the pre-parsed method if it is known, and store its number as an |
| 7731 | * integer. If it is unknown, we use the pointer and the length. |
| 7732 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7733 | 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] | 7734 | { |
| 7735 | int len, meth; |
| 7736 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7737 | len = strlen(*text); |
| 7738 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7739 | |
| 7740 | pattern->val.i = meth; |
| 7741 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7742 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7743 | if (!pattern->ptr.str) |
| 7744 | return 0; |
| 7745 | pattern->len = len; |
| 7746 | } |
| 7747 | return 1; |
| 7748 | } |
| 7749 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7750 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7751 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7752 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7753 | { |
| 7754 | int meth; |
| 7755 | struct http_txn *txn = l7; |
| 7756 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7757 | if (!txn) |
| 7758 | return 0; |
| 7759 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7760 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7761 | return 0; |
| 7762 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7763 | meth = txn->meth; |
| 7764 | test->i = meth; |
| 7765 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7766 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7767 | /* ensure the indexes are not affected */ |
| 7768 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7769 | test->len = txn->req.sl.rq.m_l; |
| 7770 | test->ptr = txn->req.sol; |
| 7771 | } |
| 7772 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7773 | return 1; |
| 7774 | } |
| 7775 | |
| 7776 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7777 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7778 | int icase; |
| 7779 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7780 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7781 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7782 | |
| 7783 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7784 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7785 | |
| 7786 | /* Other method, we must compare the strings */ |
| 7787 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7788 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7789 | |
| 7790 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 7791 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 7792 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7793 | return ACL_PAT_FAIL; |
| 7794 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7795 | } |
| 7796 | |
| 7797 | /* 2. Check on Request/Status Version |
| 7798 | * We simply compare strings here. |
| 7799 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7800 | 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] | 7801 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7802 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7803 | if (!pattern->ptr.str) |
| 7804 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7805 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7806 | return 1; |
| 7807 | } |
| 7808 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7809 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7810 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7811 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7812 | { |
| 7813 | struct http_txn *txn = l7; |
| 7814 | char *ptr; |
| 7815 | int len; |
| 7816 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7817 | if (!txn) |
| 7818 | return 0; |
| 7819 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7820 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7821 | return 0; |
| 7822 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7823 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7824 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7825 | |
| 7826 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7827 | if (len <= 0) |
| 7828 | return 0; |
| 7829 | |
| 7830 | test->ptr = ptr; |
| 7831 | test->len = len; |
| 7832 | |
| 7833 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7834 | return 1; |
| 7835 | } |
| 7836 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7837 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7838 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7839 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7840 | { |
| 7841 | struct http_txn *txn = l7; |
| 7842 | char *ptr; |
| 7843 | int len; |
| 7844 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7845 | if (!txn) |
| 7846 | return 0; |
| 7847 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7848 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7849 | return 0; |
| 7850 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7851 | len = txn->rsp.sl.st.v_l; |
| 7852 | ptr = txn->rsp.sol; |
| 7853 | |
| 7854 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7855 | if (len <= 0) |
| 7856 | return 0; |
| 7857 | |
| 7858 | test->ptr = ptr; |
| 7859 | test->len = len; |
| 7860 | |
| 7861 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7862 | return 1; |
| 7863 | } |
| 7864 | |
| 7865 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7866 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7867 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7868 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7869 | { |
| 7870 | struct http_txn *txn = l7; |
| 7871 | char *ptr; |
| 7872 | int len; |
| 7873 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7874 | if (!txn) |
| 7875 | return 0; |
| 7876 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7877 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7878 | return 0; |
| 7879 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7880 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7881 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7882 | |
| 7883 | test->i = __strl2ui(ptr, len); |
| 7884 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7885 | return 1; |
| 7886 | } |
| 7887 | |
| 7888 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7889 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7890 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7891 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7892 | { |
| 7893 | struct http_txn *txn = l7; |
| 7894 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7895 | if (!txn) |
| 7896 | return 0; |
| 7897 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7898 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7899 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7900 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7901 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7902 | /* ensure the indexes are not affected */ |
| 7903 | return 0; |
| 7904 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7905 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7906 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7907 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7908 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7909 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7910 | return 1; |
| 7911 | } |
| 7912 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7913 | static int |
| 7914 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7915 | struct acl_expr *expr, struct acl_test *test) |
| 7916 | { |
| 7917 | struct http_txn *txn = l7; |
| 7918 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7919 | if (!txn) |
| 7920 | return 0; |
| 7921 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7922 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7923 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7924 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7925 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7926 | /* ensure the indexes are not affected */ |
| 7927 | return 0; |
| 7928 | |
| 7929 | /* Parse HTTP request */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7930 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
| 7931 | test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7932 | test->i = AF_INET; |
| 7933 | |
| 7934 | /* |
| 7935 | * If we are parsing url in frontend space, we prepare backend stage |
| 7936 | * to not parse again the same url ! optimization lazyness... |
| 7937 | */ |
| 7938 | if (px->options & PR_O_HTTP_PROXY) |
| 7939 | l4->flags |= SN_ADDR_SET; |
| 7940 | |
| 7941 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7942 | return 1; |
| 7943 | } |
| 7944 | |
| 7945 | static int |
| 7946 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7947 | struct acl_expr *expr, struct acl_test *test) |
| 7948 | { |
| 7949 | struct http_txn *txn = l7; |
| 7950 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7951 | if (!txn) |
| 7952 | return 0; |
| 7953 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7954 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7955 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7956 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7957 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7958 | /* ensure the indexes are not affected */ |
| 7959 | return 0; |
| 7960 | |
| 7961 | /* Same optimization as url_ip */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7962 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
| 7963 | test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7964 | |
| 7965 | if (px->options & PR_O_HTTP_PROXY) |
| 7966 | l4->flags |= SN_ADDR_SET; |
| 7967 | |
| 7968 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7969 | return 1; |
| 7970 | } |
| 7971 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7972 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7973 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7974 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7975 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7976 | 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] | 7977 | struct acl_expr *expr, struct acl_test *test) |
| 7978 | { |
| 7979 | struct http_txn *txn = l7; |
| 7980 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7981 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7982 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7983 | if (!txn) |
| 7984 | return 0; |
| 7985 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7986 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7987 | /* search for header from the beginning */ |
| 7988 | ctx->idx = 0; |
| 7989 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7990 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7991 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7992 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7993 | test->len = ctx->vlen; |
| 7994 | test->ptr = (char *)ctx->line + ctx->val; |
| 7995 | return 1; |
| 7996 | } |
| 7997 | |
| 7998 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7999 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8000 | return 0; |
| 8001 | } |
| 8002 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8003 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8004 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8005 | struct acl_expr *expr, struct acl_test *test) |
| 8006 | { |
| 8007 | struct http_txn *txn = l7; |
| 8008 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8009 | if (!txn) |
| 8010 | return 0; |
| 8011 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8012 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8013 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8014 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8015 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8016 | /* ensure the indexes are not affected */ |
| 8017 | return 0; |
| 8018 | |
| 8019 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 8020 | } |
| 8021 | |
| 8022 | static int |
| 8023 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8024 | struct acl_expr *expr, struct acl_test *test) |
| 8025 | { |
| 8026 | struct http_txn *txn = l7; |
| 8027 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8028 | if (!txn) |
| 8029 | return 0; |
| 8030 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8031 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8032 | return 0; |
| 8033 | |
| 8034 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 8035 | } |
| 8036 | |
| 8037 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 8038 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8039 | */ |
| 8040 | static int |
| 8041 | 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] | 8042 | struct acl_expr *expr, struct acl_test *test) |
| 8043 | { |
| 8044 | struct http_txn *txn = l7; |
| 8045 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8046 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8047 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8048 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8049 | if (!txn) |
| 8050 | return 0; |
| 8051 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8052 | ctx.idx = 0; |
| 8053 | cnt = 0; |
| 8054 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 8055 | cnt++; |
| 8056 | |
| 8057 | test->i = cnt; |
| 8058 | test->flags = ACL_TEST_F_VOL_HDR; |
| 8059 | return 1; |
| 8060 | } |
| 8061 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8062 | static int |
| 8063 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8064 | struct acl_expr *expr, struct acl_test *test) |
| 8065 | { |
| 8066 | struct http_txn *txn = l7; |
| 8067 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8068 | if (!txn) |
| 8069 | return 0; |
| 8070 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8071 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8072 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8073 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8074 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8075 | /* ensure the indexes are not affected */ |
| 8076 | return 0; |
| 8077 | |
| 8078 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 8079 | } |
| 8080 | |
| 8081 | static int |
| 8082 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8083 | struct acl_expr *expr, struct acl_test *test) |
| 8084 | { |
| 8085 | struct http_txn *txn = l7; |
| 8086 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8087 | if (!txn) |
| 8088 | return 0; |
| 8089 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8090 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8091 | return 0; |
| 8092 | |
| 8093 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 8094 | } |
| 8095 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8096 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 8097 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8098 | * 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] | 8099 | */ |
| 8100 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8101 | 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] | 8102 | struct acl_expr *expr, struct acl_test *test) |
| 8103 | { |
| 8104 | struct http_txn *txn = l7; |
| 8105 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8106 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8107 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8108 | if (!txn) |
| 8109 | return 0; |
| 8110 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8111 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8112 | /* search for header from the beginning */ |
| 8113 | ctx->idx = 0; |
| 8114 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8115 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8116 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8117 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8118 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 8119 | return 1; |
| 8120 | } |
| 8121 | |
| 8122 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8123 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8124 | return 0; |
| 8125 | } |
| 8126 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8127 | static int |
| 8128 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8129 | struct acl_expr *expr, struct acl_test *test) |
| 8130 | { |
| 8131 | struct http_txn *txn = l7; |
| 8132 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8133 | if (!txn) |
| 8134 | return 0; |
| 8135 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8136 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8137 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8138 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8139 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8140 | /* ensure the indexes are not affected */ |
| 8141 | return 0; |
| 8142 | |
| 8143 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 8144 | } |
| 8145 | |
| 8146 | static int |
| 8147 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8148 | struct acl_expr *expr, struct acl_test *test) |
| 8149 | { |
| 8150 | struct http_txn *txn = l7; |
| 8151 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8152 | if (!txn) |
| 8153 | return 0; |
| 8154 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8155 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8156 | return 0; |
| 8157 | |
| 8158 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 8159 | } |
| 8160 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8161 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 8162 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8163 | */ |
| 8164 | static int |
| 8165 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8166 | struct acl_expr *expr, struct acl_test *test) |
| 8167 | { |
| 8168 | struct http_txn *txn = l7; |
| 8169 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8170 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 8171 | |
| 8172 | if (!txn) |
| 8173 | return 0; |
| 8174 | |
| 8175 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8176 | /* search for header from the beginning */ |
| 8177 | ctx->idx = 0; |
| 8178 | |
| 8179 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8180 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8181 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8182 | /* Same optimization as url_ip */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 8183 | memset(&((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr, 0, sizeof(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr)); |
| 8184 | url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr); |
| 8185 | test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8186 | test->i = AF_INET; |
| 8187 | return 1; |
| 8188 | } |
| 8189 | |
| 8190 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8191 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8192 | return 0; |
| 8193 | } |
| 8194 | |
| 8195 | static int |
| 8196 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8197 | struct acl_expr *expr, struct acl_test *test) |
| 8198 | { |
| 8199 | struct http_txn *txn = l7; |
| 8200 | |
| 8201 | if (!txn) |
| 8202 | return 0; |
| 8203 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8204 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8205 | return 0; |
| 8206 | |
| 8207 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8208 | /* ensure the indexes are not affected */ |
| 8209 | return 0; |
| 8210 | |
| 8211 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 8212 | } |
| 8213 | |
| 8214 | static int |
| 8215 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8216 | struct acl_expr *expr, struct acl_test *test) |
| 8217 | { |
| 8218 | struct http_txn *txn = l7; |
| 8219 | |
| 8220 | if (!txn) |
| 8221 | return 0; |
| 8222 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8223 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8224 | return 0; |
| 8225 | |
| 8226 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 8227 | } |
| 8228 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8229 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8230 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8231 | */ |
| 8232 | static int |
| 8233 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8234 | struct acl_expr *expr, struct acl_test *test) |
| 8235 | { |
| 8236 | struct http_txn *txn = l7; |
| 8237 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8238 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8239 | if (!txn) |
| 8240 | return 0; |
| 8241 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8242 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8243 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8244 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8245 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8246 | /* ensure the indexes are not affected */ |
| 8247 | return 0; |
| 8248 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8249 | 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] | 8250 | ptr = http_get_path(txn); |
| 8251 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8252 | return 0; |
| 8253 | |
| 8254 | /* OK, we got the '/' ! */ |
| 8255 | test->ptr = ptr; |
| 8256 | |
| 8257 | while (ptr < end && *ptr != '?') |
| 8258 | ptr++; |
| 8259 | |
| 8260 | test->len = ptr - test->ptr; |
| 8261 | |
| 8262 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8263 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8264 | return 1; |
| 8265 | } |
| 8266 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8267 | static int |
| 8268 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 8269 | struct acl_expr *expr, struct acl_test *test) |
| 8270 | { |
| 8271 | struct buffer *req = s->req; |
| 8272 | struct http_txn *txn = &s->txn; |
| 8273 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8274 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8275 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8276 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8277 | */ |
| 8278 | |
| 8279 | if (!s || !req) |
| 8280 | return 0; |
| 8281 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8282 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8283 | /* Already decoded as OK */ |
| 8284 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8285 | return 1; |
| 8286 | } |
| 8287 | |
| 8288 | /* Try to decode HTTP request */ |
| 8289 | if (likely(req->lr < req->r)) |
| 8290 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 8291 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8292 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8293 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 8294 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8295 | return 1; |
| 8296 | } |
| 8297 | /* wait for final state */ |
| 8298 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 8299 | return 0; |
| 8300 | } |
| 8301 | |
| 8302 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 8303 | * perform so that further checks can rely on HTTP tests. |
| 8304 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8305 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8306 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8307 | s->flags |= SN_REDIRECTABLE; |
| 8308 | |
| 8309 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 8310 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8311 | return 1; |
| 8312 | } |
| 8313 | |
| 8314 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8315 | return 1; |
| 8316 | } |
| 8317 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8318 | /* return a valid test if the current request is the first one on the connection */ |
| 8319 | static int |
| 8320 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8321 | struct acl_expr *expr, struct acl_test *test) |
| 8322 | { |
| 8323 | if (!s) |
| 8324 | return 0; |
| 8325 | |
| 8326 | if (s->txn.flags & TX_NOT_FIRST) |
| 8327 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8328 | else |
| 8329 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8330 | |
| 8331 | return 1; |
| 8332 | } |
| 8333 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8334 | static int |
| 8335 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 8336 | struct acl_expr *expr, struct acl_test *test) |
| 8337 | { |
| 8338 | |
| 8339 | if (!s) |
| 8340 | return 0; |
| 8341 | |
| 8342 | if (!get_http_auth(s)) |
| 8343 | return 0; |
| 8344 | |
| 8345 | test->ctx.a[0] = expr->arg.ul; |
| 8346 | test->ctx.a[1] = s->txn.auth.user; |
| 8347 | test->ctx.a[2] = s->txn.auth.pass; |
| 8348 | |
| 8349 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8350 | |
| 8351 | return 1; |
| 8352 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8353 | |
| 8354 | /************************************************************************/ |
| 8355 | /* All supported keywords must be declared here. */ |
| 8356 | /************************************************************************/ |
| 8357 | |
| 8358 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8359 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8360 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8361 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8362 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8363 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8364 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8365 | { "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] | 8366 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8367 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8368 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8369 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8370 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8371 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8372 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8373 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8374 | { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8375 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8376 | { "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] | 8377 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8378 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8379 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8380 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8381 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8382 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8383 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8384 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8385 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8386 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8387 | { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8388 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8389 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8390 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8391 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8392 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8393 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8394 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8395 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8396 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8397 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8398 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8399 | { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8400 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8401 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8402 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8403 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8404 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8405 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8406 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8407 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8408 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8409 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 8410 | { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8411 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 8412 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8413 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 8414 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 8415 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 8416 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 8417 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 8418 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 8419 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 8420 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8421 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 8422 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 8423 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 8424 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 8425 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 8426 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 8427 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 8428 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8429 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8430 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8431 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8432 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8433 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8434 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8435 | }}; |
| 8436 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8437 | /************************************************************************/ |
| 8438 | /* The code below is dedicated to pattern fetching and matching */ |
| 8439 | /************************************************************************/ |
| 8440 | |
| 8441 | /* extract the IP address from the last occurrence of specified header. Note |
| 8442 | * that we should normally first extract the string then convert it to IP, |
| 8443 | * but right now we have all the functions to do this seemlessly, and we will |
| 8444 | * be able to change that later without touching the configuration. |
| 8445 | */ |
| 8446 | static int |
| 8447 | pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8448 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8449 | { |
| 8450 | struct http_txn *txn = l7; |
| 8451 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8452 | data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, -1)); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8453 | return data->ip.s_addr != 0; |
| 8454 | } |
| 8455 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8456 | /* |
| 8457 | * Given a path string and its length, find the position of beginning of the |
| 8458 | * query string. Returns NULL if no query string is found in the path. |
| 8459 | * |
| 8460 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8461 | * |
| 8462 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8463 | */ |
| 8464 | static inline char *find_query_string(char *path, size_t path_l) |
| 8465 | { |
| 8466 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8467 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8468 | p = memchr(path, '?', path_l); |
| 8469 | return p ? p + 1 : NULL; |
| 8470 | } |
| 8471 | |
| 8472 | static inline int is_param_delimiter(char c) |
| 8473 | { |
| 8474 | return c == '&' || c == ';'; |
| 8475 | } |
| 8476 | |
| 8477 | /* |
| 8478 | * Given a url parameter, find the starting position of the first occurence, |
| 8479 | * or NULL if the parameter is not found. |
| 8480 | * |
| 8481 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8482 | * the function will return query_string+8. |
| 8483 | */ |
| 8484 | static char* |
| 8485 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8486 | char* url_param_name, size_t url_param_name_l) |
| 8487 | { |
| 8488 | char *pos, *last; |
| 8489 | |
| 8490 | pos = query_string; |
| 8491 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8492 | |
| 8493 | while (pos <= last) { |
| 8494 | if (pos[url_param_name_l] == '=') { |
| 8495 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8496 | return pos; |
| 8497 | pos += url_param_name_l + 1; |
| 8498 | } |
| 8499 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8500 | pos++; |
| 8501 | pos++; |
| 8502 | } |
| 8503 | return NULL; |
| 8504 | } |
| 8505 | |
| 8506 | /* |
| 8507 | * Given a url parameter name, returns its value and size into *value and |
| 8508 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8509 | * zero is returned and value/value_l are not touched. |
| 8510 | */ |
| 8511 | static int |
| 8512 | find_url_param_value(char* path, size_t path_l, |
| 8513 | char* url_param_name, size_t url_param_name_l, |
| 8514 | char** value, size_t* value_l) |
| 8515 | { |
| 8516 | char *query_string, *qs_end; |
| 8517 | char *arg_start; |
| 8518 | char *value_start, *value_end; |
| 8519 | |
| 8520 | query_string = find_query_string(path, path_l); |
| 8521 | if (!query_string) |
| 8522 | return 0; |
| 8523 | |
| 8524 | qs_end = path + path_l; |
| 8525 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8526 | url_param_name, url_param_name_l); |
| 8527 | if (!arg_start) |
| 8528 | return 0; |
| 8529 | |
| 8530 | value_start = arg_start + url_param_name_l + 1; |
| 8531 | value_end = value_start; |
| 8532 | |
| 8533 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8534 | value_end++; |
| 8535 | |
| 8536 | *value = value_start; |
| 8537 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8538 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8539 | } |
| 8540 | |
| 8541 | static int |
| 8542 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8543 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8544 | { |
| 8545 | struct http_txn *txn = l7; |
| 8546 | struct http_msg *msg = &txn->req; |
| 8547 | char *url_param_value; |
| 8548 | size_t url_param_value_l; |
| 8549 | |
| 8550 | if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
| 8551 | arg_p->data.str.str, arg_p->data.str.len, |
| 8552 | &url_param_value, &url_param_value_l)) |
| 8553 | return 0; |
| 8554 | |
| 8555 | data->str.str = url_param_value; |
| 8556 | data->str.len = url_param_value_l; |
| 8557 | return 1; |
| 8558 | } |
| 8559 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8560 | /* Try to find the last occurrence of a cookie name in a cookie header value. |
| 8561 | * The pointer and size of the last occurrence of the cookie value is returned |
| 8562 | * into *value and *value_l, and the function returns non-zero if the value was |
| 8563 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8564 | * value nor value_l are touched. The input hdr string should begin at the |
| 8565 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8566 | * value may represent a list of values (cookie headers). |
| 8567 | */ |
| 8568 | static int |
| 8569 | extract_cookie_value(char *hdr, size_t hdr_l, |
| 8570 | char *cookie_name, size_t cookie_name_l, int list, |
| 8571 | char **value, size_t *value_l) |
| 8572 | { |
| 8573 | int found = 0; |
| 8574 | char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end; |
| 8575 | char *next; |
| 8576 | |
| 8577 | /* Note that multiple cookies may be delimited with semi-colons, so we |
| 8578 | * also have to loop on this. |
| 8579 | */ |
| 8580 | |
| 8581 | /* we search at least a cookie name followed by an equal, and more |
| 8582 | * generally something like this : |
| 8583 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8584 | */ |
| 8585 | if (hdr_l < cookie_name_l + 1) |
| 8586 | return 0; |
| 8587 | |
| 8588 | hdr_end = hdr + hdr_l; |
| 8589 | |
| 8590 | for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) { |
| 8591 | /* Iterate through all cookies on this line */ |
| 8592 | |
| 8593 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8594 | att_beg++; |
| 8595 | |
| 8596 | /* find att_end : this is the first character after the last non |
| 8597 | * space before the equal. It may be equal to hdr_end. |
| 8598 | */ |
| 8599 | equal = att_end = att_beg; |
| 8600 | |
| 8601 | while (equal < hdr_end) { |
| 8602 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8603 | break; |
| 8604 | if (http_is_spht[(unsigned char)*equal++]) |
| 8605 | continue; |
| 8606 | att_end = equal; |
| 8607 | } |
| 8608 | |
| 8609 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8610 | * is between <att_beg> and <equal>, both may be identical. |
| 8611 | */ |
| 8612 | |
| 8613 | /* look for end of cookie if there is an equal sign */ |
| 8614 | if (equal < hdr_end && *equal == '=') { |
| 8615 | /* look for the beginning of the value */ |
| 8616 | val_beg = equal + 1; |
| 8617 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8618 | val_beg++; |
| 8619 | |
| 8620 | /* find the end of the value, respecting quotes */ |
| 8621 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8622 | |
| 8623 | /* make val_end point to the first white space or delimitor after the value */ |
| 8624 | val_end = next; |
| 8625 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8626 | val_end--; |
| 8627 | } else { |
| 8628 | val_beg = val_end = next = equal; |
| 8629 | } |
| 8630 | |
| 8631 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8632 | * they will automatically be removed if a header before them is removed, |
| 8633 | * since they're supposed to be linked together. |
| 8634 | */ |
| 8635 | if (*att_beg == '$') |
| 8636 | continue; |
| 8637 | |
| 8638 | /* Ignore cookies with no equal sign */ |
| 8639 | if (equal == next) |
| 8640 | continue; |
| 8641 | |
| 8642 | /* Now we have the cookie name between att_beg and att_end, and |
| 8643 | * its value between val_beg and val_end. |
| 8644 | */ |
| 8645 | |
| 8646 | if (att_end - att_beg == cookie_name_l && |
| 8647 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8648 | found = 1; |
| 8649 | *value = val_beg; |
| 8650 | *value_l = val_end - val_beg; |
| 8651 | /* right now we want to catch the last occurrence |
| 8652 | * of the cookie, so let's go on searching. |
| 8653 | */ |
| 8654 | } |
| 8655 | |
| 8656 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8657 | if (!list) |
| 8658 | break; |
| 8659 | } |
| 8660 | |
| 8661 | return found; |
| 8662 | } |
| 8663 | |
| 8664 | /* Try to find in request or response message is in <msg> and whose transaction |
| 8665 | * is in <txn> the last occurrence of a cookie name in all cookie header values |
| 8666 | * whose header name is <hdr_name> with name of length <hdr_name_len>. The |
| 8667 | * pointer and size of the last occurrence of the cookie value is returned into |
| 8668 | * <value> and <value_l>, and the function returns non-zero if the value was |
| 8669 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8670 | * value nor value_l are touched. The input hdr string should begin at the |
| 8671 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8672 | * value may represent a list of values (cookie headers). |
| 8673 | */ |
| 8674 | |
| 8675 | static int |
| 8676 | find_cookie_value(struct http_msg *msg, struct http_txn *txn, |
| 8677 | const char *hdr_name, int hdr_name_len, |
| 8678 | char *cookie_name, size_t cookie_name_l, int list, |
| 8679 | char **value, size_t *value_l) |
| 8680 | { |
| 8681 | struct hdr_ctx ctx; |
| 8682 | int found = 0; |
| 8683 | |
| 8684 | ctx.idx = 0; |
| 8685 | while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { |
| 8686 | if (ctx.vlen < cookie_name_l + 1) |
| 8687 | continue; |
| 8688 | |
| 8689 | found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen, |
| 8690 | cookie_name, cookie_name_l, 1, |
| 8691 | value, value_l); |
| 8692 | } |
| 8693 | return found; |
| 8694 | } |
| 8695 | |
| 8696 | static int |
| 8697 | pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8698 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8699 | { |
| 8700 | struct http_txn *txn = l7; |
| 8701 | struct http_msg *msg = &txn->req; |
| 8702 | char *cookie_value; |
| 8703 | size_t cookie_value_l; |
| 8704 | int found = 0; |
| 8705 | |
| 8706 | found = find_cookie_value(msg, txn, "Cookie", 6, |
| 8707 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8708 | &cookie_value, &cookie_value_l); |
| 8709 | if (found) { |
| 8710 | data->str.str = cookie_value; |
| 8711 | data->str.len = cookie_value_l; |
| 8712 | } |
| 8713 | |
| 8714 | return found; |
| 8715 | } |
| 8716 | |
| 8717 | |
| 8718 | static int |
| 8719 | pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8720 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8721 | { |
| 8722 | struct http_txn *txn = l7; |
| 8723 | struct http_msg *msg = &txn->rsp; |
| 8724 | char *cookie_value; |
| 8725 | size_t cookie_value_l; |
| 8726 | int found = 0; |
| 8727 | |
| 8728 | found = find_cookie_value(msg, txn, "Set-Cookie", 10, |
| 8729 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8730 | &cookie_value, &cookie_value_l); |
| 8731 | if (found) { |
| 8732 | data->str.str = cookie_value; |
| 8733 | data->str.len = cookie_value_l; |
| 8734 | } |
| 8735 | |
| 8736 | return found; |
| 8737 | } |
| 8738 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8739 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8740 | /************************************************************************/ |
| 8741 | /* All supported keywords must be declared here. */ |
| 8742 | /************************************************************************/ |
| 8743 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8744 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8745 | { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8746 | { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8747 | { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
| 8748 | { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8749 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8750 | }}; |
| 8751 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8752 | |
| 8753 | __attribute__((constructor)) |
| 8754 | static void __http_protocol_init(void) |
| 8755 | { |
| 8756 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8757 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8758 | } |
| 8759 | |
| 8760 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8761 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8762 | * Local variables: |
| 8763 | * c-indent-level: 8 |
| 8764 | * c-basic-offset: 8 |
| 8765 | * End: |
| 8766 | */ |