blob: a5e6e161e28252ad2c8b9493e1a7996c01be545e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreau1c47f852006-07-09 08:22:27 +020071/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010072const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020073 "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 Tarreau0f772532006-12-23 20:51:41 +010080const struct chunk http_200_chunk = {
81 .str = (char *)&HTTP_200,
82 .len = sizeof(HTTP_200)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "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 */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
106const 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 Tarreau791d66d2006-07-08 16:53:38 +0200110 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 "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 Tarreau844a7e72010-01-31 21:46:18 +0100115const 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 Tarreau0f772532006-12-23 20:51:41 +0100124
125const 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 Tarreau80587432006-12-24 17:47:20 +0100135static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100137 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 "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 Tarreau80587432006-12-24 17:47:20 +0100194/* We must put the messages here since GCC cannot initialize consts depending
195 * on strlen().
196 */
197struct chunk http_err_chunks[HTTP_ERR_SIZE];
198
Willy Tarreau42250582007-04-01 01:30:43 +0200199#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 */
208fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209fd_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 Tarreau80587432006-12-24 17:47:20 +0100215void init_proto_http()
216{
Willy Tarreau42250582007-04-01 01:30:43 +0200217 int i;
218 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100219 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200220
Willy Tarreau80587432006-12-24 17:47:20 +0100221 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 Tarreau42250582007-04-01 01:30:43 +0200230
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 Tarreau332f8bf2007-05-13 21:36:56 +0200258
259 /* memory allocations */
260 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200261 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau53b6c742006-12-17 13:37:46 +0100264/*
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 */
268struct http_method_desc {
269 http_meth_t meth;
270 int len;
271 const char text[8];
272};
273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100274const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275 ['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 Tarreau8d5d7f22007-01-21 19:16:41 +0100299/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301 * RFC2616 for those chars.
302 */
303
304const char http_is_spht[256] = {
305 [' '] = 1, ['\t'] = 1,
306};
307
308const char http_is_crlf[256] = {
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_lws[256] = {
313 [' '] = 1, ['\t'] = 1,
314 ['\r'] = 1, ['\n'] = 1,
315};
316
317const 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
325const 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 */
335const 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 Tarreau4b89ad42007-03-04 18:13:58 +0100363/*
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 */
367const 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 Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100376 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
377 */
378#if defined(DEBUG_FSM)
379static 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 Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * 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 */
408int 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 Tarreaufa355d42009-11-29 18:12:29 +0100417 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418 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 */
429int 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 Tarreaufa355d42009-11-29 18:12:29 +0100437 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
439}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100442 * 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 */
448int 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 Tarreau68085d82010-01-18 14:54:04 +0100467/* 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 Tarreau33a7e692007-06-10 19:45:56 +0200470 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100471char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200472{
473 int quoted, qdpair;
474
475 quoted = qdpair = 0;
476 for (; s < e; s++) {
477 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200478 else if (quoted) {
479 if (*s == '\\') qdpair = 1;
480 else if (*s == '"') quoted = 0;
481 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482 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 Tarreau68085d82010-01-18 14:54:04 +0100493 * 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 Tarreau33a7e692007-06-10 19:45:56 +0200497 */
498int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200500 struct hdr_ctx *ctx)
501{
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 char *eol, *sov;
503 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 cur_idx = ctx->idx;
506 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 /* We have previously returned a value, let's search
508 * another one on the same line.
509 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 ctx->del = ctx->val + ctx->vlen;
512 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513 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 Tarreau68085d82010-01-18 14:54:04 +0100519 /* values remaining for this header, skip the comma but save it
520 * for later use (eg: for header deletion).
521 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 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 Tarreau68085d82010-01-18 14:54:04 +0100531 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200533 while (cur_idx) {
534 eol = sol + idx->v[cur_idx].len;
535
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200536 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 Tarreau33a7e692007-06-10 19:45:56 +0200544 if ((len < eol - sol) &&
545 (sol[len] == ':') &&
546 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100547 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200548 sov = sol + len + 1;
549 while (sov < eol && http_is_lws[(unsigned char)*sov])
550 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100553 ctx->prev = old_idx;
554 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200555 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 Tarreau68085d82010-01-18 14:54:04 +0100564 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 cur_idx = idx->v[cur_idx].next;
566 }
567 return 0;
568}
569
570int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 struct hdr_ctx *ctx)
573{
574 return http_find_header2(name, strlen(name), sol, idx, ctx);
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* 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 */
585int 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 Tarreau2d3d94c2008-11-30 20:20:08 +0100627/* 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 Tarreau6f0aa472009-03-08 20:33:29 +0100631 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100633static void http_server_error(struct session *t, struct stream_interface *si,
634 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100636 buffer_auto_read(si->ob);
637 buffer_abort(si->ob);
638 buffer_auto_close(si->ob);
639 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200640 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100641 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100642 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100643 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 }
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 Tarreau80587432006-12-24 17:47:20 +0100652/* This function returns the appropriate error location for the given session
653 * and message.
654 */
655
656struct chunk *error_message(struct session *s, int msgnum)
657{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200658 if (s->be->errmsg[msgnum].str)
659 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100660 else if (s->fe->errmsg[msgnum].str)
661 return &s->fe->errmsg[msgnum];
662 else
663 return &http_err_chunks[msgnum];
664}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665
Willy Tarreau53b6c742006-12-17 13:37:46 +0100666/*
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 */
670static http_meth_t find_http_meth(const char *str, const int len)
671{
672 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100673 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100674
675 m = ((unsigned)*str - 'A');
676
677 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 for (h = http_methods[m]; h->len > 0; h++) {
679 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100681 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683 };
684 return HTTP_METH_OTHER;
685 }
686 return HTTP_METH_NONE;
687
688}
689
Willy Tarreau21d2af32008-02-14 20:25:24 +0100690/* 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 */
694static char *
695http_get_path(struct http_txn *txn)
696{
697 char *ptr, *end;
698
Willy Tarreau962c3f42010-01-10 00:15:35 +0100699 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100700 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 Tarreauefb453c2008-10-26 20:49:47 +0100738/* 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 Tarreau55a8d0e2008-11-30 18:47:21 +0100742void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100743{
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 Tarreau59e0b0f2010-01-09 21:29:23 +0100752 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753 memcpy(rdr.str, HTTP_302, rdr.len);
754
755 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200756 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 return;
758
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100759 /* 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 Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 3: add the request URI */
766 txn = &s->txn;
767 path = http_get_path(txn);
768 if (!path)
769 return;
770
Willy Tarreau962c3f42010-01-10 00:15:35 +0100771 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200772 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
775 memcpy(rdr.str + rdr.len, path, len);
776 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777
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 Tarreauefb453c2008-10-26 20:49:47 +0100785
786 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->shutr(si);
788 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 si->err_type = SI_ET_NONE;
790 si->err_loc = NULL;
791 si->state = SI_ST_CLO;
792
793 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100794 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100795
796 /* FIXME: we should increase a counter of redirects per server and per backend. */
797 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100798 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799}
800
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100801/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 * 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 Tarreau0cac36f2008-11-30 20:44:17 +0100810void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
831 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
834 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835}
836
Willy Tarreau42250582007-04-01 01:30:43 +0200837extern const char sess_term_cond[8];
838extern const char sess_fin_state[8];
839extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200840const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
841const 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 Tarreau332f8bf2007-05-13 21:36:56 +0200844struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200845struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846
Emeric Brun3a058f32009-06-30 18:26:00 +0200847void 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 Tarreauee28de02010-06-01 09:51:00 +0200865 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200866 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 Tarreauee28de02010-06-01 09:51:00 +0200981 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200982 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é7f2c5392010-03-13 15:15:07 +01001024 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 Brun3a058f32009-06-30 18:26:00 +02001034 }
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é7f2c5392010-03-13 15:15:07 +01001041 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 Brun3a058f32009-06-30 18:26:00 +02001051 }
1052 }
1053
1054trunc:
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 Tarreau42250582007-04-01 01:30:43 +02001066/*
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 Tarreau8d5d7f22007-01-21 19:16:41 +01001069 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001070void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001071{
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 Tarreauc9bd0cc2009-05-10 11:57:02 +02001077 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001078 char *uri, *h;
1079 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001080 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001081 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001082 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001083 int hdr;
1084
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001085 /* if we don't want to log normal traffic, return now */
1086 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001087 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001088 txn->status >= 500;
1089 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1090 return;
1091
Willy Tarreau42250582007-04-01 01:30:43 +02001092 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1093 return;
1094 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095
Emeric Brun3a058f32009-06-30 18:26:00 +02001096 if (prx_log->options2 & PR_O2_CLFLOG)
1097 return http_sess_clflog(s);
1098
Willy Tarreau42250582007-04-01 01:30:43 +02001099 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 Tarreaub7f694f2008-06-22 17:18:02 +02001108 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001109
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 Tarreau70089872008-06-13 21:12:51 +02001158 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 Tarreauc9bd0cc2009-05-10 11:57:02 +02001162 level = LOG_INFO;
1163 if (err && (fe->options2 & PR_O2_LOGERRORS))
1164 level = LOG_ERR;
1165
1166 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001167 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001168 " %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 Tarreau42250582007-04-01 01:30:43 +02001170 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 Tarreaufe944602007-10-25 10:34:16 +02001174 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001175 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001176 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001177 t_request,
1178 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001179 (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 Tarreau8b3977f2008-01-18 11:16:32 +01001183 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001184 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 Oledzki25b501a2008-01-06 16:36:16 +01001191 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001192 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001193 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1194
1195 s->logs.logwait = 0;
1196}
1197
Willy Tarreau117f59e2007-03-04 18:17:17 +01001198
1199/*
1200 * Capture headers from message starting at <som> according to header list
1201 * <cap_hdr>, and fill the <idx> structure appropriately.
1202 */
1203void 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 Tarreaucf7f3202007-05-13 22:46:04 +02001230 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001231
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 Tarreau42250582007-04-01 01:30:43 +02001251/* 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 Tarreaubaaee002006-06-26 02:48:02 +02001269/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001270 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 * 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 Tarreau9cdde232007-05-02 20:58:19 +02001283 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 * 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 Tarreau9cdde232007-05-02 20:58:19 +02001286 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001288const 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 Tarreau8973c702007-01-21 23:58:29 +01001291{
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001294 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001295 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1297
1298 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001299 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1301 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001302 state = HTTP_MSG_ERROR;
1303 break;
1304
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001306 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001308 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 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 Tarreau7552c032009-03-01 11:10:40 +01001314 state = HTTP_MSG_ERROR;
1315 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001316
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 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 Tarreau962c3f42010-01-10 00:15:35 +01001323 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 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 Tarreau962c3f42010-01-10 00:15:35 +01001328 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 http_msg_rsp_reason:
1330 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001331 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 msg->sl.st.r_l = 0;
1333 goto http_msg_rpline_eol;
1334
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001336 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001338 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 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 Tarreau8973c702007-01-21 23:58:29 +01001346 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (likely(!HTTP_IS_CRLF(*ptr)))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001350 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 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 Tarreau7552c032009-03-01 11:10:40 +01001369 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001370 if (ret_state)
1371 *ret_state = state;
1372 if (ret_ptr)
1373 *ret_ptr = (char *)ptr;
1374 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375}
1376
Willy Tarreau8973c702007-01-21 23:58:29 +01001377/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 * 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 Tarreau9cdde232007-05-02 20:58:19 +02001391 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 * 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 Tarreau9cdde232007-05-02 20:58:19 +02001394 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001396const 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 Tarreaubaaee002006-06-26 02:48:02 +02001399{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 if (likely(HTTP_IS_TOKEN(*ptr)))
1404 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001407 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 if (likely(HTTP_IS_CRLF(*ptr))) {
1412 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001413 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001415 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001417 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001419 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 msg->sl.rq.v_l = 0;
1421 goto http_msg_rqline_eol;
1422 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001423 state = HTTP_MSG_ERROR;
1424 break;
1425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001427 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001429 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 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 Tarreau58f10d72006-12-04 02:26:12 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(!HTTP_IS_LWS(*ptr)))
1440 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001443 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1448 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001451 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001453 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 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 Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001462 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001463 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001465
1466 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001467 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001468 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 Tarreau7552c032009-03-01 11:10:40 +01001480 state = HTTP_MSG_ERROR;
1481 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483#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 Tarreau58f10d72006-12-04 02:26:12 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001491 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 if (ret_state)
1493 *ret_state = state;
1494 if (ret_ptr)
1495 *ret_ptr = (char *)ptr;
1496 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001498
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001499/*
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
1506char get_http_auth_buff[BUFSIZE];
1507
1508int
1509get_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 Tarreau844a7e72010-01-31 21:46:18 +01001531
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 Oledzkif9423ae2010-01-29 19:26:18 +01001541 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 Tarreau58f10d72006-12-04 02:26:12 +01001579
Willy Tarreau8973c702007-01-21 23:58:29 +01001580/*
1581 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001583 * 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 Tarreau9cdde232007-05-02 20:58:19 +02001585 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001586 * 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 Tarreau8973c702007-01-21 23:58:29 +01001589 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1591{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001592 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001595 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 ptr = buf->lr;
1597 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 if (unlikely(ptr >= end))
1600 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001603 /*
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 Tarreau8973c702007-01-21 23:58:29 +01001608 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001609 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001610 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001611 /* 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 Tarreau1d3bcce2009-12-27 15:50:06 +01001621 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001622 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001623 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001624 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001625 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001626 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 Tarreau8973c702007-01-21 23:58:29 +01001639 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001640 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001641 EXPECT_LF_HERE(ptr, http_msg_invalid);
1642 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1643 /* stop here */
1644
Willy Tarreau8973c702007-01-21 23:58:29 +01001645 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001646 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001647 case HTTP_MSG_RPVER_SP:
1648 case HTTP_MSG_RPCODE:
1649 case HTTP_MSG_RPCODE_SP:
1650 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001651 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001652 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 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 Tarreaub326fcc2007-03-03 13:54:32 +01001659 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001660 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 Tarreau8973c702007-01-21 23:58:29 +01001667 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001668 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001669 /* 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 Tarreau8d5d7f22007-01-21 19:16:41 +01001677 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001678 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001680 /* 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 Tarreau1d3bcce2009-12-27 15:50:06 +01001690 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001691 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001693 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001694 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001695 /* we will need this when keep-alive will be supported
1696 hdr_idx_init(idx);
1697 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 state = HTTP_MSG_RQMETH;
1699 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1703 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001704
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 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 Tarreau8973c702007-01-21 23:58:29 +01001708 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001711 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 EXPECT_LF_HERE(ptr, http_msg_invalid);
1713 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001714 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001717 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 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 Tarreaub326fcc2007-03-03 13:54:32 +01001723 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (unlikely(!ptr))
1725 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001726
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 /* we have a full request and we know that we have either a CR
1728 * or an LF at <ptr>.
1729 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001730 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 msg->sol = ptr;
1734 if (likely(*ptr == '\r'))
1735 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001739 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 /* 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 Tarreau976f1ee2006-12-17 10:06:03 +01001745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 EXPECT_LF_HERE(ptr, http_msg_invalid);
1747 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001748 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001749
Willy Tarreau8973c702007-01-21 23:58:29 +01001750 /*
1751 * Common states below
1752 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001754 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 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 Tarreau976f1ee2006-12-17 10:06:03 +01001763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001765 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 /* 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 Tarreau58f10d72006-12-04 02:26:12 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 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 Tarreau58f10d72006-12-04 02:26:12 +01001774
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001775 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 Tarreau230fd0b2006-12-17 12:05:00 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001785 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 /* 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 Tarreau230fd0b2006-12-17 12:05:00 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* header value can be basically anything except CR/LF */
1791 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 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 Tarreau976f1ee2006-12-17 10:06:03 +01001800
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001802 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 EXPECT_LF_HERE(ptr, http_msg_invalid);
1804 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001807 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 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 Tarreauaa9dce32007-03-18 23:50:16 +01001814 /* we had a header consisting only in spaces ! */
1815 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001816 goto http_msg_complete_header;
1817
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001818 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001819 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 /* 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 Tarreau976f1ee2006-12-17 10:06:03 +01001826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 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 Tarreau976f1ee2006-12-17 10:06:03 +01001835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001837 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 EXPECT_LF_HERE(ptr, http_msg_invalid);
1839 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001840
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001842 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 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 Tarreau976f1ee2006-12-17 10:06:03 +01001862
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1864 idx, idx->tail) < 0))
1865 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 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 Tarreau230fd0b2006-12-17 12:05:00 +01001875
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001877 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 /* 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 Tarreaufa355d42009-11-29 18:12:29 +01001882 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001884 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001885 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 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 Tarreau230fd0b2006-12-17 12:05:00 +01001891#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 }
1893 http_msg_ood:
1894 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001895 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 buf->lr = ptr;
1897 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001898
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 http_msg_invalid:
1900 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001901 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001902 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 return;
1904}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001905
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001906/* 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 */
1910static 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 Tarreau2492d5b2009-07-11 00:06:00 +02001918 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 Tarreaufa355d42009-11-29 18:12:29 +01001925 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001926 }
1927 /* add HTTP version */
1928 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001929 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930 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;