Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 27 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
| 29 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 30 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/memory.h> |
| 32 | #include <common/mini-clist.h> |
| 33 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 34 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/time.h> |
| 36 | #include <common/uri_auth.h> |
| 37 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
| 39 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 42 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 43 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <proto/backend.h> |
| 45 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 46 | #include <proto/checks.h> |
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 | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 71 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 72 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 73 | "HTTP/1.0 200 OK\r\n" |
| 74 | "Cache-Control: no-cache\r\n" |
| 75 | "Connection: close\r\n" |
| 76 | "Content-Type: text/html\r\n" |
| 77 | "\r\n" |
| 78 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const struct chunk http_200_chunk = { |
| 81 | .str = (char *)&HTTP_200, |
| 82 | .len = sizeof(HTTP_200)-1 |
| 83 | }; |
| 84 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 85 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 86 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 90 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 91 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 95 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 96 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 97 | |
| 98 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 99 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 100 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 101 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 102 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 103 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 106 | const char *HTTP_401_fmt = |
| 107 | "HTTP/1.0 401 Unauthorized\r\n" |
| 108 | "Cache-Control: no-cache\r\n" |
| 109 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 110 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 112 | "\r\n" |
| 113 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 114 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 115 | const char *HTTP_407_fmt = |
| 116 | "HTTP/1.0 407 Unauthorized\r\n" |
| 117 | "Cache-Control: no-cache\r\n" |
| 118 | "Connection: close\r\n" |
| 119 | "Content-Type: text/html\r\n" |
| 120 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 121 | "\r\n" |
| 122 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 123 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | |
| 125 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 126 | [HTTP_ERR_400] = 400, |
| 127 | [HTTP_ERR_403] = 403, |
| 128 | [HTTP_ERR_408] = 408, |
| 129 | [HTTP_ERR_500] = 500, |
| 130 | [HTTP_ERR_502] = 502, |
| 131 | [HTTP_ERR_503] = 503, |
| 132 | [HTTP_ERR_504] = 504, |
| 133 | }; |
| 134 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 135 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 137 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | "Cache-Control: no-cache\r\n" |
| 139 | "Connection: close\r\n" |
| 140 | "Content-Type: text/html\r\n" |
| 141 | "\r\n" |
| 142 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 143 | |
| 144 | [HTTP_ERR_403] = |
| 145 | "HTTP/1.0 403 Forbidden\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 151 | |
| 152 | [HTTP_ERR_408] = |
| 153 | "HTTP/1.0 408 Request Time-out\r\n" |
| 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_500] = |
| 161 | "HTTP/1.0 500 Server Error\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 167 | |
| 168 | [HTTP_ERR_502] = |
| 169 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 175 | |
| 176 | [HTTP_ERR_503] = |
| 177 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 183 | |
| 184 | [HTTP_ERR_504] = |
| 185 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 186 | "Cache-Control: no-cache\r\n" |
| 187 | "Connection: close\r\n" |
| 188 | "Content-Type: text/html\r\n" |
| 189 | "\r\n" |
| 190 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 191 | |
| 192 | }; |
| 193 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 194 | /* We must put the messages here since GCC cannot initialize consts depending |
| 195 | * on strlen(). |
| 196 | */ |
| 197 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 198 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 199 | #define FD_SETS_ARE_BITFIELDS |
| 200 | #ifdef FD_SETS_ARE_BITFIELDS |
| 201 | /* |
| 202 | * This map is used with all the FD_* macros to check whether a particular bit |
| 203 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 204 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 205 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 206 | * exclusively to the macros. |
| 207 | */ |
| 208 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 209 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 210 | |
| 211 | #else |
| 212 | #error "Check if your OS uses bitfields for fd_sets" |
| 213 | #endif |
| 214 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 215 | void init_proto_http() |
| 216 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 217 | int i; |
| 218 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 221 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 222 | if (!http_err_msgs[msg]) { |
| 223 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 224 | abort(); |
| 225 | } |
| 226 | |
| 227 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 228 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 229 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 230 | |
| 231 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 232 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 233 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 234 | * printable characters above. |
| 235 | */ |
| 236 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 237 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 238 | for (i = 0; i < 32; i++) { |
| 239 | FD_SET(i, hdr_encode_map); |
| 240 | FD_SET(i, url_encode_map); |
| 241 | } |
| 242 | for (i = 127; i < 256; i++) { |
| 243 | FD_SET(i, hdr_encode_map); |
| 244 | FD_SET(i, url_encode_map); |
| 245 | } |
| 246 | |
| 247 | tmp = "\"#{|}"; |
| 248 | while (*tmp) { |
| 249 | FD_SET(*tmp, hdr_encode_map); |
| 250 | tmp++; |
| 251 | } |
| 252 | |
| 253 | tmp = "\"#"; |
| 254 | while (*tmp) { |
| 255 | FD_SET(*tmp, url_encode_map); |
| 256 | tmp++; |
| 257 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 258 | |
| 259 | /* memory allocations */ |
| 260 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 261 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 262 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 264 | /* |
| 265 | * We have 26 list of methods (1 per first letter), each of which can have |
| 266 | * up to 3 entries (2 valid, 1 null). |
| 267 | */ |
| 268 | struct http_method_desc { |
| 269 | http_meth_t meth; |
| 270 | int len; |
| 271 | const char text[8]; |
| 272 | }; |
| 273 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 274 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 275 | ['C' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 277 | }, |
| 278 | ['D' - 'A'] = { |
| 279 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 280 | }, |
| 281 | ['G' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 283 | }, |
| 284 | ['H' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 286 | }, |
| 287 | ['P' - 'A'] = { |
| 288 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 289 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 290 | }, |
| 291 | ['T' - 'A'] = { |
| 292 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 293 | }, |
| 294 | /* rest is empty like this : |
| 295 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 296 | */ |
| 297 | }; |
| 298 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 299 | /* 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] | 300 | * 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] | 301 | * RFC2616 for those chars. |
| 302 | */ |
| 303 | |
| 304 | const char http_is_spht[256] = { |
| 305 | [' '] = 1, ['\t'] = 1, |
| 306 | }; |
| 307 | |
| 308 | const char http_is_crlf[256] = { |
| 309 | ['\r'] = 1, ['\n'] = 1, |
| 310 | }; |
| 311 | |
| 312 | const char http_is_lws[256] = { |
| 313 | [' '] = 1, ['\t'] = 1, |
| 314 | ['\r'] = 1, ['\n'] = 1, |
| 315 | }; |
| 316 | |
| 317 | const char http_is_sep[256] = { |
| 318 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 319 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 320 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 321 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 322 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 323 | }; |
| 324 | |
| 325 | const char http_is_ctl[256] = { |
| 326 | [0 ... 31] = 1, |
| 327 | [127] = 1, |
| 328 | }; |
| 329 | |
| 330 | /* |
| 331 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 332 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 333 | * them correctly. Instead, define every non-CTL char's status. |
| 334 | */ |
| 335 | const char http_is_token[256] = { |
| 336 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 337 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 338 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 339 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 340 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 341 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 342 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 343 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 344 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 345 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 346 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 347 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 348 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 349 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 350 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 351 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 352 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 353 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 354 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 355 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 356 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 357 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 358 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 359 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 363 | /* |
| 364 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 365 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 366 | */ |
| 367 | const char http_is_ver_token[256] = { |
| 368 | ['.'] = 1, ['/'] = 1, |
| 369 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 370 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 371 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 372 | }; |
| 373 | |
| 374 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 375 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 376 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 377 | */ |
| 378 | #if defined(DEBUG_FSM) |
| 379 | static void http_silent_debug(int line, struct session *s) |
| 380 | { |
| 381 | int size = 0; |
| 382 | size += snprintf(trash + size, sizeof(trash) - size, |
| 383 | "[%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", |
| 384 | line, |
| 385 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 386 | 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); |
| 387 | write(-1, trash, size); |
| 388 | size = 0; |
| 389 | size += snprintf(trash + size, sizeof(trash) - size, |
| 390 | " %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", |
| 391 | line, |
| 392 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 393 | 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); |
| 394 | |
| 395 | write(-1, trash, size); |
| 396 | } |
| 397 | #else |
| 398 | #define http_silent_debug(l,s) do { } while (0) |
| 399 | #endif |
| 400 | |
| 401 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 402 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 403 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 404 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 405 | * of headers is automatically adjusted. The number of bytes added is returned |
| 406 | * on success, otherwise <0 is returned indicating an error. |
| 407 | */ |
| 408 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 409 | struct hdr_idx *hdr_idx, const char *text) |
| 410 | { |
| 411 | int bytes, len; |
| 412 | |
| 413 | len = strlen(text); |
| 414 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 415 | if (!bytes) |
| 416 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 417 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 418 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 423 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 424 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 425 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 426 | * of headers is automatically adjusted. The number of bytes added is returned |
| 427 | * on success, otherwise <0 is returned indicating an error. |
| 428 | */ |
| 429 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 430 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 431 | { |
| 432 | int bytes; |
| 433 | |
| 434 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 435 | if (!bytes) |
| 436 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 437 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 438 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 439 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 440 | |
| 441 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 442 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 443 | * If so, returns the position of the first non-space character relative to |
| 444 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 445 | * to return a pointer to the place after the first space. Returns 0 if the |
| 446 | * header name does not match. Checks are case-insensitive. |
| 447 | */ |
| 448 | int http_header_match2(const char *hdr, const char *end, |
| 449 | const char *name, int len) |
| 450 | { |
| 451 | const char *val; |
| 452 | |
| 453 | if (hdr + len >= end) |
| 454 | return 0; |
| 455 | if (hdr[len] != ':') |
| 456 | return 0; |
| 457 | if (strncasecmp(hdr, name, len) != 0) |
| 458 | return 0; |
| 459 | val = hdr + len + 1; |
| 460 | while (val < end && HTTP_IS_SPHT(*val)) |
| 461 | val++; |
| 462 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 463 | return len + 2; /* we may replace starting from second space */ |
| 464 | return val - hdr; |
| 465 | } |
| 466 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 467 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 468 | * par 2.2 for more information. Note that it requires a valid header to return |
| 469 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 470 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 471 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 472 | { |
| 473 | int quoted, qdpair; |
| 474 | |
| 475 | quoted = qdpair = 0; |
| 476 | for (; s < e; s++) { |
| 477 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 478 | else if (quoted) { |
| 479 | if (*s == '\\') qdpair = 1; |
| 480 | else if (*s == '"') quoted = 0; |
| 481 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 482 | else if (*s == '"') quoted = 1; |
| 483 | else if (*s == ',') return s; |
| 484 | } |
| 485 | return s; |
| 486 | } |
| 487 | |
| 488 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 489 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 490 | * structure holds everything necessary to use the header and find next |
| 491 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 492 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 493 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 494 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 495 | * is NULL when searching for a new values of a header, the current header is |
| 496 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | */ |
| 498 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 499 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 500 | struct hdr_ctx *ctx) |
| 501 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | char *eol, *sov; |
| 503 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 505 | cur_idx = ctx->idx; |
| 506 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | /* We have previously returned a value, let's search |
| 508 | * another one on the same line. |
| 509 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 511 | ctx->del = ctx->val + ctx->vlen; |
| 512 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 513 | eol = sol + idx->v[cur_idx].len; |
| 514 | |
| 515 | if (sov >= eol) |
| 516 | /* no more values in this header */ |
| 517 | goto next_hdr; |
| 518 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 519 | /* values remaining for this header, skip the comma but save it |
| 520 | * for later use (eg: for header deletion). |
| 521 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 522 | sov++; |
| 523 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 524 | sov++; |
| 525 | |
| 526 | goto return_hdr; |
| 527 | } |
| 528 | |
| 529 | /* first request for this header */ |
| 530 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 531 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 532 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 533 | while (cur_idx) { |
| 534 | eol = sol + idx->v[cur_idx].len; |
| 535 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 536 | if (len == 0) { |
| 537 | /* No argument was passed, we want any header. |
| 538 | * To achieve this, we simply build a fake request. */ |
| 539 | while (sol + len < eol && sol[len] != ':') |
| 540 | len++; |
| 541 | name = sol; |
| 542 | } |
| 543 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 544 | if ((len < eol - sol) && |
| 545 | (sol[len] == ':') && |
| 546 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 547 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 548 | sov = sol + len + 1; |
| 549 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 550 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 551 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 552 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 553 | ctx->prev = old_idx; |
| 554 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 555 | ctx->idx = cur_idx; |
| 556 | ctx->val = sov - sol; |
| 557 | |
| 558 | eol = find_hdr_value_end(sov, eol); |
| 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]; |
| 597 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 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; |
| 604 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 605 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 606 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 607 | ctx->vlen = 0; |
| 608 | return ctx->idx; |
| 609 | } |
| 610 | |
| 611 | /* This was not the only value of this header. We have to remove between |
| 612 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 613 | * of the list, we remove the last separator. |
| 614 | */ |
| 615 | |
| 616 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 617 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 618 | sol + ctx->val + ctx->vlen + skip_comma, |
| 619 | NULL, 0); |
| 620 | hdr->len += delta; |
| 621 | http_msg_move_end(msg, delta); |
| 622 | ctx->val = ctx->del; |
| 623 | ctx->vlen = 0; |
| 624 | return ctx->idx; |
| 625 | } |
| 626 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 627 | /* This function handles a server error at the stream interface level. The |
| 628 | * stream interface is assumed to be already in a closed state. An optional |
| 629 | * message is copied into the input buffer, and an HTTP status code stored. |
| 630 | * 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] | 631 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 632 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 633 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 634 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 635 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 636 | buffer_auto_read(si->ob); |
| 637 | buffer_abort(si->ob); |
| 638 | buffer_auto_close(si->ob); |
| 639 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 640 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 641 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 642 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 643 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 644 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 645 | } |
| 646 | if (!(t->flags & SN_ERR_MASK)) |
| 647 | t->flags |= err; |
| 648 | if (!(t->flags & SN_FINST_MASK)) |
| 649 | t->flags |= finst; |
| 650 | } |
| 651 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 652 | /* This function returns the appropriate error location for the given session |
| 653 | * and message. |
| 654 | */ |
| 655 | |
| 656 | struct chunk *error_message(struct session *s, int msgnum) |
| 657 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 658 | if (s->be->errmsg[msgnum].str) |
| 659 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 660 | else if (s->fe->errmsg[msgnum].str) |
| 661 | return &s->fe->errmsg[msgnum]; |
| 662 | else |
| 663 | return &http_err_chunks[msgnum]; |
| 664 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 665 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 666 | /* |
| 667 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 668 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 669 | */ |
| 670 | static http_meth_t find_http_meth(const char *str, const int len) |
| 671 | { |
| 672 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 673 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 674 | |
| 675 | m = ((unsigned)*str - 'A'); |
| 676 | |
| 677 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 678 | for (h = http_methods[m]; h->len > 0; h++) { |
| 679 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 680 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 681 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 683 | }; |
| 684 | return HTTP_METH_OTHER; |
| 685 | } |
| 686 | return HTTP_METH_NONE; |
| 687 | |
| 688 | } |
| 689 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 690 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 691 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 692 | * It is returned otherwise. |
| 693 | */ |
| 694 | static char * |
| 695 | http_get_path(struct http_txn *txn) |
| 696 | { |
| 697 | char *ptr, *end; |
| 698 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 699 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 700 | end = ptr + txn->req.sl.rq.u_l; |
| 701 | |
| 702 | if (ptr >= end) |
| 703 | return NULL; |
| 704 | |
| 705 | /* RFC2616, par. 5.1.2 : |
| 706 | * Request-URI = "*" | absuri | abspath | authority |
| 707 | */ |
| 708 | |
| 709 | if (*ptr == '*') |
| 710 | return NULL; |
| 711 | |
| 712 | if (isalpha((unsigned char)*ptr)) { |
| 713 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 714 | ptr++; |
| 715 | while (ptr < end && |
| 716 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 717 | ptr++; |
| 718 | /* skip '://' */ |
| 719 | if (ptr == end || *ptr++ != ':') |
| 720 | return NULL; |
| 721 | if (ptr == end || *ptr++ != '/') |
| 722 | return NULL; |
| 723 | if (ptr == end || *ptr++ != '/') |
| 724 | return NULL; |
| 725 | } |
| 726 | /* skip [user[:passwd]@]host[:[port]] */ |
| 727 | |
| 728 | while (ptr < end && *ptr != '/') |
| 729 | ptr++; |
| 730 | |
| 731 | if (ptr == end) |
| 732 | return NULL; |
| 733 | |
| 734 | /* OK, we got the '/' ! */ |
| 735 | return ptr; |
| 736 | } |
| 737 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 738 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 739 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 740 | * left unchanged and will follow normal proxy processing. |
| 741 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 742 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 743 | { |
| 744 | struct http_txn *txn; |
| 745 | struct chunk rdr; |
| 746 | char *path; |
| 747 | int len; |
| 748 | |
| 749 | /* 1: create the response header */ |
| 750 | rdr.len = strlen(HTTP_302); |
| 751 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 752 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 753 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 754 | |
| 755 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 756 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 757 | return; |
| 758 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 759 | /* special prefix "/" means don't change URL */ |
| 760 | if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') { |
| 761 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 762 | rdr.len += s->srv->rdr_len; |
| 763 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 764 | |
| 765 | /* 3: add the request URI */ |
| 766 | txn = &s->txn; |
| 767 | path = http_get_path(txn); |
| 768 | if (!path) |
| 769 | return; |
| 770 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 771 | 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] | 772 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 773 | return; |
| 774 | |
| 775 | memcpy(rdr.str + rdr.len, path, len); |
| 776 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 777 | |
| 778 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 779 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 780 | rdr.len += 29; |
| 781 | } else { |
| 782 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 783 | rdr.len += 23; |
| 784 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 785 | |
| 786 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 787 | si->shutr(si); |
| 788 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 789 | si->err_type = SI_ET_NONE; |
| 790 | si->err_loc = NULL; |
| 791 | si->state = SI_ST_CLO; |
| 792 | |
| 793 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 794 | 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] | 795 | |
| 796 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 797 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 798 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 799 | } |
| 800 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 801 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 802 | * that the server side is closed. Note that err_type is actually a |
| 803 | * bitmask, where almost only aborts may be cumulated with other |
| 804 | * values. We consider that aborted operations are more important |
| 805 | * than timeouts or errors due to the fact that nobody else in the |
| 806 | * logs might explain incomplete retries. All others should avoid |
| 807 | * being cumulated. It should normally not be possible to have multiple |
| 808 | * aborts at once, but just in case, the first one in sequence is reported. |
| 809 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 810 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 811 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 812 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 813 | |
| 814 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 815 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 816 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 817 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 818 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 819 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 820 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 821 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 822 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 823 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 824 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 825 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 826 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 827 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 828 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 829 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 830 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 831 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 833 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 834 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 837 | extern const char sess_term_cond[8]; |
| 838 | extern const char sess_fin_state[8]; |
| 839 | extern const char *monthname[12]; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 840 | const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */ |
| 841 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 842 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 843 | Set-cookie Updated, unknown, unknown */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 844 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 845 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 846 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 847 | void http_sess_clflog(struct session *s) |
| 848 | { |
| 849 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 850 | struct proxy *fe = s->fe; |
| 851 | struct proxy *be = s->be; |
| 852 | struct proxy *prx_log; |
| 853 | struct http_txn *txn = &s->txn; |
| 854 | int tolog, level, err; |
| 855 | char *uri, *h; |
| 856 | char *svid; |
| 857 | struct tm tm; |
| 858 | static char tmpline[MAX_SYSLOG_LEN]; |
| 859 | int hdr; |
| 860 | size_t w; |
| 861 | int t_request; |
| 862 | |
| 863 | prx_log = fe; |
| 864 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 865 | (s->req->cons->conn_retries != be->conn_retries) || |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 866 | txn->status >= 500; |
| 867 | |
| 868 | if (s->cli_addr.ss_family == AF_INET) |
| 869 | inet_ntop(AF_INET, |
| 870 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 871 | pn, sizeof(pn)); |
| 872 | else |
| 873 | inet_ntop(AF_INET6, |
| 874 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 875 | pn, sizeof(pn)); |
| 876 | |
| 877 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 878 | |
| 879 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 880 | tolog = fe->to_log; |
| 881 | |
| 882 | h = tmpline; |
| 883 | |
| 884 | w = snprintf(h, sizeof(tmpline), |
| 885 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 886 | pn, |
| 887 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 888 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 889 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 890 | goto trunc; |
| 891 | h += w; |
| 892 | |
| 893 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 894 | goto trunc; |
| 895 | |
| 896 | *(h++) = ' '; |
| 897 | *(h++) = '\"'; |
| 898 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 899 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 900 | '#', url_encode_map, uri); |
| 901 | *(h++) = '\"'; |
| 902 | |
| 903 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 904 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 905 | goto trunc; |
| 906 | h += w; |
| 907 | |
| 908 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 909 | goto trunc; |
| 910 | memcpy(h, " \"-\" \"-\"", 8); |
| 911 | h += 8; |
| 912 | |
| 913 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 914 | " %d %03d", |
| 915 | (s->cli_addr.ss_family == AF_INET) ? |
| 916 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 917 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 918 | (int)s->logs.accept_date.tv_usec/1000); |
| 919 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 920 | goto trunc; |
| 921 | h += w; |
| 922 | |
| 923 | w = strlen(fe->id); |
| 924 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 925 | goto trunc; |
| 926 | *(h++) = ' '; |
| 927 | *(h++) = '\"'; |
| 928 | memcpy(h, fe->id, w); |
| 929 | h += w; |
| 930 | *(h++) = '\"'; |
| 931 | |
| 932 | w = strlen(be->id); |
| 933 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 934 | goto trunc; |
| 935 | *(h++) = ' '; |
| 936 | *(h++) = '\"'; |
| 937 | memcpy(h, be->id, w); |
| 938 | h += w; |
| 939 | *(h++) = '\"'; |
| 940 | |
| 941 | svid = (tolog & LW_SVID) ? |
| 942 | (s->data_source != DATA_SRC_STATS) ? |
| 943 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 944 | |
| 945 | w = strlen(svid); |
| 946 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 947 | goto trunc; |
| 948 | *(h++) = ' '; |
| 949 | *(h++) = '\"'; |
| 950 | memcpy(h, svid, w); |
| 951 | h += w; |
| 952 | *(h++) = '\"'; |
| 953 | |
| 954 | t_request = -1; |
| 955 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 956 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 957 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 958 | " %d %ld %ld %ld %ld", |
| 959 | t_request, |
| 960 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 961 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 962 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 963 | s->logs.t_close); |
| 964 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 965 | goto trunc; |
| 966 | h += w; |
| 967 | |
| 968 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 969 | goto trunc; |
| 970 | *(h++) = ' '; |
| 971 | *(h++) = '\"'; |
| 972 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 973 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 974 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 975 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 976 | *(h++) = '\"'; |
| 977 | |
| 978 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 979 | " %d %d %d %d %d %ld %ld", |
| 980 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 981 | (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] | 982 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 983 | |
| 984 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 985 | goto trunc; |
| 986 | h += w; |
| 987 | |
| 988 | if (txn->cli_cookie) { |
| 989 | w = strlen(txn->cli_cookie); |
| 990 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 991 | goto trunc; |
| 992 | *(h++) = ' '; |
| 993 | *(h++) = '\"'; |
| 994 | memcpy(h, txn->cli_cookie, w); |
| 995 | h += w; |
| 996 | *(h++) = '\"'; |
| 997 | } else { |
| 998 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 999 | goto trunc; |
| 1000 | memcpy(h, " \"-\"", 4); |
| 1001 | h += 4; |
| 1002 | } |
| 1003 | |
| 1004 | if (txn->srv_cookie) { |
| 1005 | w = strlen(txn->srv_cookie); |
| 1006 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1007 | goto trunc; |
| 1008 | *(h++) = ' '; |
| 1009 | *(h++) = '\"'; |
| 1010 | memcpy(h, txn->srv_cookie, w); |
| 1011 | h += w; |
| 1012 | *(h++) = '\"'; |
| 1013 | } else { |
| 1014 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1015 | goto trunc; |
| 1016 | memcpy(h, " \"-\"", 4); |
| 1017 | h += 4; |
| 1018 | } |
| 1019 | |
| 1020 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1021 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1022 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1023 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1024 | if (txn->req.cap[hdr] != NULL) { |
| 1025 | *(h++) = ' '; |
| 1026 | *(h++) = '\"'; |
| 1027 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1028 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1029 | *(h++) = '\"'; |
| 1030 | } else { |
| 1031 | memcpy(h, " \"-\"", 4); |
| 1032 | h += 4; |
| 1033 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1038 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1039 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1040 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1041 | if (txn->rsp.cap[hdr] != NULL) { |
| 1042 | *(h++) = ' '; |
| 1043 | *(h++) = '\"'; |
| 1044 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1045 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1046 | *(h++) = '\"'; |
| 1047 | } else { |
| 1048 | memcpy(h, " \"-\"", 4); |
| 1049 | h += 4; |
| 1050 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | trunc: |
| 1055 | *h = '\0'; |
| 1056 | |
| 1057 | level = LOG_INFO; |
| 1058 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1059 | level = LOG_ERR; |
| 1060 | |
| 1061 | send_log(prx_log, level, "%s\n", tmpline); |
| 1062 | |
| 1063 | s->logs.logwait = 0; |
| 1064 | } |
| 1065 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1066 | /* |
| 1067 | * send a log for the session when we have enough info about it. |
| 1068 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1069 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1070 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1071 | { |
| 1072 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1073 | struct proxy *fe = s->fe; |
| 1074 | struct proxy *be = s->be; |
| 1075 | struct proxy *prx_log; |
| 1076 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1077 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1078 | char *uri, *h; |
| 1079 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1080 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1081 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1082 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1083 | int hdr; |
| 1084 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1085 | /* if we don't want to log normal traffic, return now */ |
| 1086 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1087 | (s->req->cons->conn_retries != be->conn_retries) || |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1088 | txn->status >= 500; |
| 1089 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1090 | return; |
| 1091 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1092 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1093 | return; |
| 1094 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1096 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1097 | return http_sess_clflog(s); |
| 1098 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1099 | if (s->cli_addr.ss_family == AF_INET) |
| 1100 | inet_ntop(AF_INET, |
| 1101 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1102 | pn, sizeof(pn)); |
| 1103 | else |
| 1104 | inet_ntop(AF_INET6, |
| 1105 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1106 | pn, sizeof(pn)); |
| 1107 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1108 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1109 | |
| 1110 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1111 | tolog = fe->to_log; |
| 1112 | |
| 1113 | h = tmpline; |
| 1114 | if (fe->to_log & LW_REQHDR && |
| 1115 | txn->req.cap && |
| 1116 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1117 | *(h++) = ' '; |
| 1118 | *(h++) = '{'; |
| 1119 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1120 | if (hdr) |
| 1121 | *(h++) = '|'; |
| 1122 | if (txn->req.cap[hdr] != NULL) |
| 1123 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1124 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1125 | } |
| 1126 | *(h++) = '}'; |
| 1127 | } |
| 1128 | |
| 1129 | if (fe->to_log & LW_RSPHDR && |
| 1130 | txn->rsp.cap && |
| 1131 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1132 | *(h++) = ' '; |
| 1133 | *(h++) = '{'; |
| 1134 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1135 | if (hdr) |
| 1136 | *(h++) = '|'; |
| 1137 | if (txn->rsp.cap[hdr] != NULL) |
| 1138 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1139 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1140 | } |
| 1141 | *(h++) = '}'; |
| 1142 | } |
| 1143 | |
| 1144 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1145 | *(h++) = ' '; |
| 1146 | *(h++) = '"'; |
| 1147 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1148 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1149 | '#', url_encode_map, uri); |
| 1150 | *(h++) = '"'; |
| 1151 | } |
| 1152 | *h = '\0'; |
| 1153 | |
| 1154 | svid = (tolog & LW_SVID) ? |
| 1155 | (s->data_source != DATA_SRC_STATS) ? |
| 1156 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1157 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1158 | t_request = -1; |
| 1159 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1160 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1161 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1162 | level = LOG_INFO; |
| 1163 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1164 | level = LOG_ERR; |
| 1165 | |
| 1166 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1167 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1168 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1169 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n", |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1170 | pn, |
| 1171 | (s->cli_addr.ss_family == AF_INET) ? |
| 1172 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1173 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1174 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1175 | 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] | 1176 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1177 | t_request, |
| 1178 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1179 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1180 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1181 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1182 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1183 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1184 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1185 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1186 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1187 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1188 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1189 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1190 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 1191 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1192 | (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] | 1193 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1194 | |
| 1195 | s->logs.logwait = 0; |
| 1196 | } |
| 1197 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1198 | |
| 1199 | /* |
| 1200 | * Capture headers from message starting at <som> according to header list |
| 1201 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1202 | */ |
| 1203 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1204 | char **cap, struct cap_hdr *cap_hdr) |
| 1205 | { |
| 1206 | char *eol, *sol, *col, *sov; |
| 1207 | int cur_idx; |
| 1208 | struct cap_hdr *h; |
| 1209 | int len; |
| 1210 | |
| 1211 | sol = som + hdr_idx_first_pos(idx); |
| 1212 | cur_idx = hdr_idx_first_idx(idx); |
| 1213 | |
| 1214 | while (cur_idx) { |
| 1215 | eol = sol + idx->v[cur_idx].len; |
| 1216 | |
| 1217 | col = sol; |
| 1218 | while (col < eol && *col != ':') |
| 1219 | col++; |
| 1220 | |
| 1221 | sov = col + 1; |
| 1222 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1223 | sov++; |
| 1224 | |
| 1225 | for (h = cap_hdr; h; h = h->next) { |
| 1226 | if ((h->namelen == col - sol) && |
| 1227 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1228 | if (cap[h->index] == NULL) |
| 1229 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1230 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1231 | |
| 1232 | if (cap[h->index] == NULL) { |
| 1233 | Alert("HTTP capture : out of memory.\n"); |
| 1234 | continue; |
| 1235 | } |
| 1236 | |
| 1237 | len = eol - sov; |
| 1238 | if (len > h->len) |
| 1239 | len = h->len; |
| 1240 | |
| 1241 | memcpy(cap[h->index], sov, len); |
| 1242 | cap[h->index][len]=0; |
| 1243 | } |
| 1244 | } |
| 1245 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1246 | cur_idx = idx->v[cur_idx].next; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1251 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1252 | */ |
| 1253 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1254 | |
| 1255 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1256 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1257 | */ |
| 1258 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1259 | ptr++; \ |
| 1260 | if (likely(ptr < end)) \ |
| 1261 | goto good; \ |
| 1262 | else { \ |
| 1263 | state = (st); \ |
| 1264 | goto http_msg_ood; \ |
| 1265 | } \ |
| 1266 | } while (0) |
| 1267 | |
| 1268 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1269 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1270 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1271 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1272 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1273 | * will give undefined results. |
| 1274 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1275 | * and that msg->sol points to the beginning of the response. |
| 1276 | * If a complete line is found (which implies that at least one CR or LF is |
| 1277 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1278 | * returned indicating an incomplete line (which does not mean that parts have |
| 1279 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1280 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1281 | * upon next call. |
| 1282 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1283 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1285 | * 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] | 1286 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1287 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1288 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1289 | unsigned int state, const char *ptr, const char *end, |
| 1290 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1291 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1294 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1295 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1296 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1297 | |
| 1298 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1299 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1300 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1301 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1302 | state = HTTP_MSG_ERROR; |
| 1303 | break; |
| 1304 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1305 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1306 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1307 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1308 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | goto http_msg_rpcode; |
| 1310 | } |
| 1311 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1312 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1313 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1314 | state = HTTP_MSG_ERROR; |
| 1315 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1317 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1318 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1319 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1320 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1321 | |
| 1322 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1323 | 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] | 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1325 | } |
| 1326 | |
| 1327 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1328 | 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] | 1329 | http_msg_rsp_reason: |
| 1330 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1331 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | msg->sl.st.r_l = 0; |
| 1333 | goto http_msg_rpline_eol; |
| 1334 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1335 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1336 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1338 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1339 | goto http_msg_rpreason; |
| 1340 | } |
| 1341 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1342 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1343 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1344 | goto http_msg_rsp_reason; |
| 1345 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1347 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1349 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1350 | 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] | 1351 | http_msg_rpline_eol: |
| 1352 | /* We have seen the end of line. Note that we do not |
| 1353 | * necessarily have the \n yet, but at least we know that we |
| 1354 | * have EITHER \r OR \n, otherwise the response would not be |
| 1355 | * complete. We can then record the response length and return |
| 1356 | * to the caller which will be able to register it. |
| 1357 | */ |
| 1358 | msg->sl.st.l = ptr - msg->sol; |
| 1359 | return ptr; |
| 1360 | |
| 1361 | #ifdef DEBUG_FULL |
| 1362 | default: |
| 1363 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1364 | exit(1); |
| 1365 | #endif |
| 1366 | } |
| 1367 | |
| 1368 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1369 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1370 | if (ret_state) |
| 1371 | *ret_state = state; |
| 1372 | if (ret_ptr) |
| 1373 | *ret_ptr = (char *)ptr; |
| 1374 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1375 | } |
| 1376 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | * This function parses a request line between <ptr> and <end>, starting with |
| 1379 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1380 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1381 | * will give undefined results. |
| 1382 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1383 | * and that msg->sol points to the beginning of the request. |
| 1384 | * If a complete line is found (which implies that at least one CR or LF is |
| 1385 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1386 | * returned indicating an incomplete line (which does not mean that parts have |
| 1387 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1388 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1389 | * upon next call. |
| 1390 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1391 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1393 | * 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] | 1394 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1395 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1396 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1397 | unsigned int state, const char *ptr, const char *end, |
| 1398 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1399 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1400 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1402 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1404 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1405 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1406 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1407 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1409 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1410 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1412 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1413 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1415 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1417 | 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] | 1418 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1419 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | msg->sl.rq.v_l = 0; |
| 1421 | goto http_msg_rqline_eol; |
| 1422 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1423 | state = HTTP_MSG_ERROR; |
| 1424 | break; |
| 1425 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1427 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1429 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | goto http_msg_rquri; |
| 1431 | } |
| 1432 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1433 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1434 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1435 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1436 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1438 | http_msg_rquri: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1440 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1441 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1443 | 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] | 1444 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1448 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1451 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1453 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | goto http_msg_rqver; |
| 1455 | } |
| 1456 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1458 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1459 | goto http_msg_req09_ver; |
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_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1462 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1463 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1465 | |
| 1466 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1467 | 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] | 1468 | http_msg_rqline_eol: |
| 1469 | /* We have seen the end of line. Note that we do not |
| 1470 | * necessarily have the \n yet, but at least we know that we |
| 1471 | * have EITHER \r OR \n, otherwise the request would not be |
| 1472 | * complete. We can then record the request length and return |
| 1473 | * to the caller which will be able to register it. |
| 1474 | */ |
| 1475 | msg->sl.rq.l = ptr - msg->sol; |
| 1476 | return ptr; |
| 1477 | } |
| 1478 | |
| 1479 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1480 | state = HTTP_MSG_ERROR; |
| 1481 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | #ifdef DEBUG_FULL |
| 1484 | default: |
| 1485 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1486 | exit(1); |
| 1487 | #endif |
| 1488 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1489 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1491 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | if (ret_state) |
| 1493 | *ret_state = state; |
| 1494 | if (ret_ptr) |
| 1495 | *ret_ptr = (char *)ptr; |
| 1496 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1498 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1499 | /* |
| 1500 | * Returns the data from Authorization header. Function may be called more |
| 1501 | * than once so data is stored in txn->auth_data. When no header is found |
| 1502 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1503 | * searching again for something we are unable to find anyway. |
| 1504 | */ |
| 1505 | |
| 1506 | char get_http_auth_buff[BUFSIZE]; |
| 1507 | |
| 1508 | int |
| 1509 | get_http_auth(struct session *s) |
| 1510 | { |
| 1511 | |
| 1512 | struct http_txn *txn = &s->txn; |
| 1513 | struct chunk auth_method; |
| 1514 | struct hdr_ctx ctx; |
| 1515 | char *h, *p; |
| 1516 | int len; |
| 1517 | |
| 1518 | #ifdef DEBUG_AUTH |
| 1519 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1520 | #endif |
| 1521 | |
| 1522 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1523 | return 0; |
| 1524 | |
| 1525 | if (txn->auth.method) |
| 1526 | return 1; |
| 1527 | |
| 1528 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1529 | |
| 1530 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1531 | |
| 1532 | if (txn->flags & TX_USE_PX_CONN) { |
| 1533 | h = "Proxy-Authorization"; |
| 1534 | len = strlen(h); |
| 1535 | } else { |
| 1536 | h = "Authorization"; |
| 1537 | len = strlen(h); |
| 1538 | } |
| 1539 | |
| 1540 | 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] | 1541 | return 0; |
| 1542 | |
| 1543 | h = ctx.line + ctx.val; |
| 1544 | |
| 1545 | p = memchr(h, ' ', ctx.vlen); |
| 1546 | if (!p || p == h) |
| 1547 | return 0; |
| 1548 | |
| 1549 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1550 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1551 | |
| 1552 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1553 | |
| 1554 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1555 | get_http_auth_buff, BUFSIZE - 1); |
| 1556 | |
| 1557 | if (len < 0) |
| 1558 | return 0; |
| 1559 | |
| 1560 | |
| 1561 | get_http_auth_buff[len] = '\0'; |
| 1562 | |
| 1563 | p = strchr(get_http_auth_buff, ':'); |
| 1564 | |
| 1565 | if (!p) |
| 1566 | return 0; |
| 1567 | |
| 1568 | txn->auth.user = get_http_auth_buff; |
| 1569 | *p = '\0'; |
| 1570 | txn->auth.pass = p+1; |
| 1571 | |
| 1572 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1573 | return 1; |
| 1574 | } |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1580 | /* |
| 1581 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1582 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1583 | * when data are missing and recalled at the exact same location with no |
| 1584 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1585 | * 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] | 1586 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1587 | * the first state, so that none of the msg pointers has to be initialized |
| 1588 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1589 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1591 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1592 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1594 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1595 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | ptr = buf->lr; |
| 1597 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1598 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1599 | if (unlikely(ptr >= end)) |
| 1600 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1601 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1602 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1603 | /* |
| 1604 | * First, states that are specific to the response only. |
| 1605 | * We check them first so that request and headers are |
| 1606 | * closer to each other (accessed more often). |
| 1607 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1608 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1609 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1610 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1611 | /* we have a start of message, but we have to check |
| 1612 | * first if we need to remove some CRLF. We can only |
| 1613 | * do this when send_max=0. |
| 1614 | */ |
| 1615 | char *beg = buf->w + buf->send_max; |
| 1616 | if (beg >= buf->data + buf->size) |
| 1617 | beg -= buf->size; |
| 1618 | if (unlikely(ptr != beg)) { |
| 1619 | if (buf->send_max) |
| 1620 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1621 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1622 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1623 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1624 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1625 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1626 | hdr_idx_init(idx); |
| 1627 | state = HTTP_MSG_RPVER; |
| 1628 | goto http_msg_rpver; |
| 1629 | } |
| 1630 | |
| 1631 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1632 | goto http_msg_invalid; |
| 1633 | |
| 1634 | if (unlikely(*ptr == '\n')) |
| 1635 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1636 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1637 | /* stop here */ |
| 1638 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1639 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1640 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1641 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1642 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1643 | /* stop here */ |
| 1644 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1645 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1646 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1647 | case HTTP_MSG_RPVER_SP: |
| 1648 | case HTTP_MSG_RPCODE: |
| 1649 | case HTTP_MSG_RPCODE_SP: |
| 1650 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1651 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1652 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1653 | if (unlikely(!ptr)) |
| 1654 | return; |
| 1655 | |
| 1656 | /* we have a full response and we know that we have either a CR |
| 1657 | * or an LF at <ptr>. |
| 1658 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1659 | //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] | 1660 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1661 | |
| 1662 | msg->sol = ptr; |
| 1663 | if (likely(*ptr == '\r')) |
| 1664 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1665 | goto http_msg_rpline_end; |
| 1666 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1667 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1668 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1669 | /* msg->sol must point to the first of CR or LF. */ |
| 1670 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1671 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1672 | /* stop here */ |
| 1673 | |
| 1674 | /* |
| 1675 | * Second, states that are specific to the request only |
| 1676 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1678 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1680 | /* we have a start of message, but we have to check |
| 1681 | * first if we need to remove some CRLF. We can only |
| 1682 | * do this when send_max=0. |
| 1683 | */ |
| 1684 | char *beg = buf->w + buf->send_max; |
| 1685 | if (beg >= buf->data + buf->size) |
| 1686 | beg -= buf->size; |
| 1687 | if (likely(ptr != beg)) { |
| 1688 | if (buf->send_max) |
| 1689 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1690 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1691 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1693 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1694 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1695 | /* we will need this when keep-alive will be supported |
| 1696 | hdr_idx_init(idx); |
| 1697 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1698 | state = HTTP_MSG_RQMETH; |
| 1699 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1701 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1703 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1704 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | if (unlikely(*ptr == '\n')) |
| 1706 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1707 | 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] | 1708 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1709 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1711 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1712 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1713 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1714 | /* stop here */ |
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 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1717 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1718 | case HTTP_MSG_RQMETH_SP: |
| 1719 | case HTTP_MSG_RQURI: |
| 1720 | case HTTP_MSG_RQURI_SP: |
| 1721 | case HTTP_MSG_RQVER: |
| 1722 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1723 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | if (unlikely(!ptr)) |
| 1725 | return; |
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 | /* we have a full request and we know that we have either a CR |
| 1728 | * or an LF at <ptr>. |
| 1729 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1730 | //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] | 1731 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1733 | msg->sol = ptr; |
| 1734 | if (likely(*ptr == '\r')) |
| 1735 | 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] | 1736 | goto http_msg_rqline_end; |
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 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1739 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | /* check for HTTP/0.9 request : no version information available. |
| 1741 | * msg->sol must point to the first of CR or LF. |
| 1742 | */ |
| 1743 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1744 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1745 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1747 | 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] | 1748 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1749 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1750 | /* |
| 1751 | * Common states below |
| 1752 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1754 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | msg->sol = ptr; |
| 1756 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1757 | goto http_msg_hdr_name; |
| 1758 | } |
| 1759 | |
| 1760 | if (likely(*ptr == '\r')) |
| 1761 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1762 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1763 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1765 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1766 | /* assumes msg->sol points to the first char */ |
| 1767 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1768 | 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] | 1769 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1770 | if (likely(*ptr == ':')) { |
| 1771 | msg->col = ptr - buf->data; |
| 1772 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1773 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1775 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1776 | goto http_msg_invalid; |
| 1777 | |
| 1778 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1779 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1780 | |
| 1781 | /* and we still accept this non-token character */ |
| 1782 | 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] | 1783 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1785 | http_msg_hdr_l1_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1787 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1788 | 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] | 1789 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | /* header value can be basically anything except CR/LF */ |
| 1791 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1792 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1794 | goto http_msg_hdr_val; |
| 1795 | } |
| 1796 | |
| 1797 | if (likely(*ptr == '\r')) |
| 1798 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1799 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1800 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1802 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1804 | 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] | 1805 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1807 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1809 | /* replace HT,CR,LF with spaces */ |
| 1810 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1811 | buf->data[msg->sov] = ' '; |
| 1812 | goto http_msg_hdr_l1_sp; |
| 1813 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1814 | /* we had a header consisting only in spaces ! */ |
| 1815 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1816 | goto http_msg_complete_header; |
| 1817 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1818 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1819 | http_msg_hdr_val: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1820 | /* assumes msg->sol points to the first char, msg->col to the |
| 1821 | * colon, and msg->sov points to the first character of the |
| 1822 | * value. |
| 1823 | */ |
| 1824 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1825 | 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] | 1826 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | msg->eol = ptr; |
| 1828 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1829 | * real header end in case it ends with lots of LWS, but is this |
| 1830 | * really needed ? |
| 1831 | */ |
| 1832 | if (likely(*ptr == '\r')) |
| 1833 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1834 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1835 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1836 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1837 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1839 | 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] | 1840 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1841 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1842 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1843 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1844 | /* LWS: replace HT,CR,LF with spaces */ |
| 1845 | for (; msg->eol < ptr; msg->eol++) |
| 1846 | *msg->eol = ' '; |
| 1847 | goto http_msg_hdr_val; |
| 1848 | } |
| 1849 | http_msg_complete_header: |
| 1850 | /* |
| 1851 | * It was a new header, so the last one is finished. |
| 1852 | * Assumes msg->sol points to the first char, msg->col to the |
| 1853 | * colon, msg->sov points to the first character of the value |
| 1854 | * and msg->eol to the first CR or LF so we know how the line |
| 1855 | * ends. We insert last header into the index. |
| 1856 | */ |
| 1857 | /* |
| 1858 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1859 | write(2, msg->sol, msg->eol-msg->sol); |
| 1860 | fprintf(stderr,"\n"); |
| 1861 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1862 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1863 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1864 | idx, idx->tail) < 0)) |
| 1865 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1866 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | msg->sol = ptr; |
| 1868 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1869 | goto http_msg_hdr_name; |
| 1870 | } |
| 1871 | |
| 1872 | if (likely(*ptr == '\r')) |
| 1873 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1874 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1875 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1876 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1877 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1878 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1879 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1880 | ptr++; |
| 1881 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1882 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1884 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1885 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1886 | return; |
| 1887 | #ifdef DEBUG_FULL |
| 1888 | default: |
| 1889 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1890 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1891 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1892 | } |
| 1893 | http_msg_ood: |
| 1894 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1895 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | buf->lr = ptr; |
| 1897 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1898 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1899 | http_msg_invalid: |
| 1900 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1901 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1902 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1903 | return; |
| 1904 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1905 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1906 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1907 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1908 | * nothing is done and 1 is returned. |
| 1909 | */ |
| 1910 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1911 | { |
| 1912 | int delta; |
| 1913 | char *cur_end; |
| 1914 | |
| 1915 | if (msg->sl.rq.v_l != 0) |
| 1916 | return 1; |
| 1917 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1918 | cur_end = msg->sol + msg->sl.rq.l; |
| 1919 | delta = 0; |
| 1920 | |
| 1921 | if (msg->sl.rq.u_l == 0) { |
| 1922 | /* if no URI was set, add "/" */ |
| 1923 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1924 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1925 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1926 | } |
| 1927 | /* add HTTP version */ |
| 1928 | 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] | 1929 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1930 | cur_end += delta; |
| 1931 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1932 | HTTP_MSG_RQMETH, |
| 1933 | msg->sol, cur_end + 1, |
| 1934 | NULL, NULL); |
| 1935 | if (unlikely(!cur_end)) |
| 1936 | return 0; |
| 1937 | |
| 1938 | /* we have a full HTTP/1.0 request now and we know that |
| 1939 | * we have either a CR or an LF at <ptr>. |
| 1940 | */ |
| 1941 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1942 | return 1; |
|