| /* |
| * HTTP protocol analyzer |
| * |
| * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
| * |
| * This program is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU General Public License |
| * as published by the Free Software Foundation; either version |
| * 2 of the License, or (at your option) any later version. |
| * |
| */ |
| |
| #include <ctype.h> |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <syslog.h> |
| #include <time.h> |
| |
| #include <sys/socket.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| |
| #include <netinet/tcp.h> |
| |
| #include <common/appsession.h> |
| #include <common/base64.h> |
| #include <common/chunk.h> |
| #include <common/compat.h> |
| #include <common/config.h> |
| #include <common/debug.h> |
| #include <common/memory.h> |
| #include <common/mini-clist.h> |
| #include <common/standard.h> |
| #include <common/ticks.h> |
| #include <common/time.h> |
| #include <common/uri_auth.h> |
| #include <common/version.h> |
| |
| #include <types/capture.h> |
| #include <types/global.h> |
| |
| #include <proto/acl.h> |
| #include <proto/arg.h> |
| #include <proto/auth.h> |
| #include <proto/backend.h> |
| #include <proto/channel.h> |
| #include <proto/checks.h> |
| #include <proto/compression.h> |
| #include <proto/dumpstats.h> |
| #include <proto/fd.h> |
| #include <proto/frontend.h> |
| #include <proto/log.h> |
| #include <proto/hdr_idx.h> |
| #include <proto/pattern.h> |
| #include <proto/proto_tcp.h> |
| #include <proto/proto_http.h> |
| #include <proto/proxy.h> |
| #include <proto/queue.h> |
| #include <proto/sample.h> |
| #include <proto/server.h> |
| #include <proto/session.h> |
| #include <proto/stream_interface.h> |
| #include <proto/task.h> |
| |
| const char HTTP_100[] = |
| "HTTP/1.1 100 Continue\r\n\r\n"; |
| |
| const struct chunk http_100_chunk = { |
| .str = (char *)&HTTP_100, |
| .len = sizeof(HTTP_100)-1 |
| }; |
| |
| /* Warning: no "connection" header is provided with the 3xx messages below */ |
| const char *HTTP_301 = |
| "HTTP/1.1 301 Moved Permanently\r\n" |
| "Content-length: 0\r\n" |
| "Location: "; /* not terminated since it will be concatenated with the URL */ |
| |
| const char *HTTP_302 = |
| "HTTP/1.1 302 Found\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Content-length: 0\r\n" |
| "Location: "; /* not terminated since it will be concatenated with the URL */ |
| |
| /* same as 302 except that the browser MUST retry with the GET method */ |
| const char *HTTP_303 = |
| "HTTP/1.1 303 See Other\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Content-length: 0\r\n" |
| "Location: "; /* not terminated since it will be concatenated with the URL */ |
| |
| |
| /* same as 302 except that the browser MUST retry with the same method */ |
| const char *HTTP_307 = |
| "HTTP/1.1 307 Temporary Redirect\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Content-length: 0\r\n" |
| "Location: "; /* not terminated since it will be concatenated with the URL */ |
| |
| /* same as 301 except that the browser MUST retry with the same method */ |
| const char *HTTP_308 = |
| "HTTP/1.1 308 Permanent Redirect\r\n" |
| "Content-length: 0\r\n" |
| "Location: "; /* not terminated since it will be concatenated with the URL */ |
| |
| /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| const char *HTTP_401_fmt = |
| "HTTP/1.0 401 Unauthorized\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| "\r\n" |
| "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| |
| const char *HTTP_407_fmt = |
| "HTTP/1.0 407 Unauthorized\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| "\r\n" |
| "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| |
| |
| const int http_err_codes[HTTP_ERR_SIZE] = { |
| [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
| [HTTP_ERR_400] = 400, |
| [HTTP_ERR_403] = 403, |
| [HTTP_ERR_408] = 408, |
| [HTTP_ERR_500] = 500, |
| [HTTP_ERR_502] = 502, |
| [HTTP_ERR_503] = 503, |
| [HTTP_ERR_504] = 504, |
| }; |
| |
| static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
| [HTTP_ERR_200] = |
| "HTTP/1.0 200 OK\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| |
| [HTTP_ERR_400] = |
| "HTTP/1.0 400 Bad request\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| |
| [HTTP_ERR_403] = |
| "HTTP/1.0 403 Forbidden\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| |
| [HTTP_ERR_408] = |
| "HTTP/1.0 408 Request Time-out\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| |
| [HTTP_ERR_500] = |
| "HTTP/1.0 500 Server Error\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| |
| [HTTP_ERR_502] = |
| "HTTP/1.0 502 Bad Gateway\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| |
| [HTTP_ERR_503] = |
| "HTTP/1.0 503 Service Unavailable\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| |
| [HTTP_ERR_504] = |
| "HTTP/1.0 504 Gateway Time-out\r\n" |
| "Cache-Control: no-cache\r\n" |
| "Connection: close\r\n" |
| "Content-Type: text/html\r\n" |
| "\r\n" |
| "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| |
| }; |
| |
| /* status codes available for the stats admin page (strictly 4 chars length) */ |
| const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| [STAT_STATUS_DENY] = "DENY", |
| [STAT_STATUS_DONE] = "DONE", |
| [STAT_STATUS_ERRP] = "ERRP", |
| [STAT_STATUS_EXCD] = "EXCD", |
| [STAT_STATUS_NONE] = "NONE", |
| [STAT_STATUS_PART] = "PART", |
| [STAT_STATUS_UNKN] = "UNKN", |
| }; |
| |
| |
| /* We must put the messages here since GCC cannot initialize consts depending |
| * on strlen(). |
| */ |
| struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| |
| /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| static struct hdr_ctx static_hdr_ctx; |
| |
| #define FD_SETS_ARE_BITFIELDS |
| #ifdef FD_SETS_ARE_BITFIELDS |
| /* |
| * This map is used with all the FD_* macros to check whether a particular bit |
| * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| * exclusively to the macros. |
| */ |
| fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| |
| #else |
| #error "Check if your OS uses bitfields for fd_sets" |
| #endif |
| |
| void init_proto_http() |
| { |
| int i; |
| char *tmp; |
| int msg; |
| |
| for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| if (!http_err_msgs[msg]) { |
| Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| abort(); |
| } |
| |
| http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| } |
| |
| /* initialize the log header encoding map : '{|}"#' should be encoded with |
| * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| * URL encoding only requires '"', '#' to be encoded as well as non- |
| * printable characters above. |
| */ |
| memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| memset(url_encode_map, 0, sizeof(url_encode_map)); |
| for (i = 0; i < 32; i++) { |
| FD_SET(i, hdr_encode_map); |
| FD_SET(i, url_encode_map); |
| } |
| for (i = 127; i < 256; i++) { |
| FD_SET(i, hdr_encode_map); |
| FD_SET(i, url_encode_map); |
| } |
| |
| tmp = "\"#{|}"; |
| while (*tmp) { |
| FD_SET(*tmp, hdr_encode_map); |
| tmp++; |
| } |
| |
| tmp = "\"#"; |
| while (*tmp) { |
| FD_SET(*tmp, url_encode_map); |
| tmp++; |
| } |
| |
| /* memory allocations */ |
| pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
| pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
| } |
| |
| /* |
| * We have 26 list of methods (1 per first letter), each of which can have |
| * up to 3 entries (2 valid, 1 null). |
| */ |
| struct http_method_desc { |
| enum http_meth_t meth; |
| int len; |
| const char text[8]; |
| }; |
| |
| const struct http_method_desc http_methods[26][3] = { |
| ['C' - 'A'] = { |
| [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| }, |
| ['D' - 'A'] = { |
| [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| }, |
| ['G' - 'A'] = { |
| [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| }, |
| ['H' - 'A'] = { |
| [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| }, |
| ['P' - 'A'] = { |
| [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| }, |
| ['T' - 'A'] = { |
| [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| }, |
| /* rest is empty like this : |
| * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| */ |
| }; |
| |
| /* It is about twice as fast on recent architectures to lookup a byte in a |
| * table than to perform a boolean AND or OR between two tests. Refer to |
| * RFC2616 for those chars. |
| */ |
| |
| const char http_is_spht[256] = { |
| [' '] = 1, ['\t'] = 1, |
| }; |
| |
| const char http_is_crlf[256] = { |
| ['\r'] = 1, ['\n'] = 1, |
| }; |
| |
| const char http_is_lws[256] = { |
| [' '] = 1, ['\t'] = 1, |
| ['\r'] = 1, ['\n'] = 1, |
| }; |
| |
| const char http_is_sep[256] = { |
| ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| }; |
| |
| const char http_is_ctl[256] = { |
| [0 ... 31] = 1, |
| [127] = 1, |
| }; |
| |
| /* |
| * A token is any ASCII char that is neither a separator nor a CTL char. |
| * Do not overwrite values in assignment since gcc-2.95 will not handle |
| * them correctly. Instead, define every non-CTL char's status. |
| */ |
| const char http_is_token[256] = { |
| [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| }; |
| |
| |
| /* |
| * An http ver_token is any ASCII which can be found in an HTTP version, |
| * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| */ |
| const char http_is_ver_token[256] = { |
| ['.'] = 1, ['/'] = 1, |
| ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| }; |
| |
| |
| /* |
| * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| */ |
| #if defined(DEBUG_FSM) |
| static void http_silent_debug(int line, struct session *s) |
| { |
| chunk_printf(&trash, |
| "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n", |
| line, |
| s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags); |
| write(-1, trash.str, trash.len); |
| |
| chunk_printf(&trash, |
| " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n", |
| line, |
| s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward); |
| write(-1, trash.str, trash.len); |
| } |
| #else |
| #define http_silent_debug(l,s) do { } while (0) |
| #endif |
| |
| /* |
| * Adds a header and its CRLF at the tail of the message's buffer, just before |
| * the last CRLF. Text length is measured first, so it cannot be NULL. |
| * The header is also automatically added to the index <hdr_idx>, and the end |
| * of headers is automatically adjusted. The number of bytes added is returned |
| * on success, otherwise <0 is returned indicating an error. |
| */ |
| int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text) |
| { |
| int bytes, len; |
| |
| len = strlen(text); |
| bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
| if (!bytes) |
| return -1; |
| http_msg_move_end(msg, bytes); |
| return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| } |
| |
| /* |
| * Adds a header and its CRLF at the tail of the message's buffer, just before |
| * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| * the buffer is only opened and the space reserved, but nothing is copied. |
| * The header is also automatically added to the index <hdr_idx>, and the end |
| * of headers is automatically adjusted. The number of bytes added is returned |
| * on success, otherwise <0 is returned indicating an error. |
| */ |
| int http_header_add_tail2(struct http_msg *msg, |
| struct hdr_idx *hdr_idx, const char *text, int len) |
| { |
| int bytes; |
| |
| bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len); |
| if (!bytes) |
| return -1; |
| http_msg_move_end(msg, bytes); |
| return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| } |
| |
| /* |
| * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| * If so, returns the position of the first non-space character relative to |
| * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| * to return a pointer to the place after the first space. Returns 0 if the |
| * header name does not match. Checks are case-insensitive. |
| */ |
| int http_header_match2(const char *hdr, const char *end, |
| const char *name, int len) |
| { |
| const char *val; |
| |
| if (hdr + len >= end) |
| return 0; |
| if (hdr[len] != ':') |
| return 0; |
| if (strncasecmp(hdr, name, len) != 0) |
| return 0; |
| val = hdr + len + 1; |
| while (val < end && HTTP_IS_SPHT(*val)) |
| val++; |
| if ((val >= end) && (len + 2 <= end - hdr)) |
| return len + 2; /* we may replace starting from second space */ |
| return val - hdr; |
| } |
| |
| /* Find the first or next occurrence of header <name> in message buffer <sol> |
| * using headers index <idx>, and return it in the <ctx> structure. This |
| * structure holds everything necessary to use the header and find next |
| * occurrence. If its <idx> member is 0, the header is searched from the |
| * beginning. Otherwise, the next occurrence is returned. The function returns |
| * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| * http_find_header2() except that it is designed to work with full-line headers |
| * whose comma is not a delimiter but is part of the syntax. As a special case, |
| * if ctx->val is NULL when searching for a new values of a header, the current |
| * header is rescanned. This allows rescanning after a header deletion. |
| */ |
| int http_find_full_header2(const char *name, int len, |
| char *sol, struct hdr_idx *idx, |
| struct hdr_ctx *ctx) |
| { |
| char *eol, *sov; |
| int cur_idx, old_idx; |
| |
| cur_idx = ctx->idx; |
| if (cur_idx) { |
| /* We have previously returned a header, let's search another one */ |
| sol = ctx->line; |
| eol = sol + idx->v[cur_idx].len; |
| goto next_hdr; |
| } |
| |
| /* first request for this header */ |
| sol += hdr_idx_first_pos(idx); |
| old_idx = 0; |
| cur_idx = hdr_idx_first_idx(idx); |
| while (cur_idx) { |
| eol = sol + idx->v[cur_idx].len; |
| |
| if (len == 0) { |
| /* No argument was passed, we want any header. |
| * To achieve this, we simply build a fake request. */ |
| while (sol + len < eol && sol[len] != ':') |
| len++; |
| name = sol; |
| } |
| |
| if ((len < eol - sol) && |
| (sol[len] == ':') && |
| (strncasecmp(sol, name, len) == 0)) { |
| ctx->del = len; |
| sov = sol + len + 1; |
| while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| sov++; |
| |
| ctx->line = sol; |
| ctx->prev = old_idx; |
| ctx->idx = cur_idx; |
| ctx->val = sov - sol; |
| ctx->tws = 0; |
| while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| eol--; |
| ctx->tws++; |
| } |
| ctx->vlen = eol - sov; |
| return 1; |
| } |
| next_hdr: |
| sol = eol + idx->v[cur_idx].cr + 1; |
| old_idx = cur_idx; |
| cur_idx = idx->v[cur_idx].next; |
| } |
| return 0; |
| } |
| |
| /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| * par 2.2 for more information. Note that it requires a valid header to return |
| * a valid result. This works for headers defined as comma-separated lists. |
| */ |
| char *find_hdr_value_end(char *s, const char *e) |
| { |
| int quoted, qdpair; |
| |
| quoted = qdpair = 0; |
| for (; s < e; s++) { |
| if (qdpair) qdpair = 0; |
| else if (quoted) { |
| if (*s == '\\') qdpair = 1; |
| else if (*s == '"') quoted = 0; |
| } |
| else if (*s == '"') quoted = 1; |
| else if (*s == ',') return s; |
| } |
| return s; |
| } |
| |
| /* Find the first or next occurrence of header <name> in message buffer <sol> |
| * using headers index <idx>, and return it in the <ctx> structure. This |
| * structure holds everything necessary to use the header and find next |
| * occurrence. If its <idx> member is 0, the header is searched from the |
| * beginning. Otherwise, the next occurrence is returned. The function returns |
| * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| * with headers defined as comma-separated lists. As a special case, if ctx->val |
| * is NULL when searching for a new values of a header, the current header is |
| * rescanned. This allows rescanning after a header deletion. |
| */ |
| int http_find_header2(const char *name, int len, |
| char *sol, struct hdr_idx *idx, |
| struct hdr_ctx *ctx) |
| { |
| char *eol, *sov; |
| int cur_idx, old_idx; |
| |
| cur_idx = ctx->idx; |
| if (cur_idx) { |
| /* We have previously returned a value, let's search |
| * another one on the same line. |
| */ |
| sol = ctx->line; |
| ctx->del = ctx->val + ctx->vlen + ctx->tws; |
| sov = sol + ctx->del; |
| eol = sol + idx->v[cur_idx].len; |
| |
| if (sov >= eol) |
| /* no more values in this header */ |
| goto next_hdr; |
| |
| /* values remaining for this header, skip the comma but save it |
| * for later use (eg: for header deletion). |
| */ |
| sov++; |
| while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| sov++; |
| |
| goto return_hdr; |
| } |
| |
| /* first request for this header */ |
| sol += hdr_idx_first_pos(idx); |
| old_idx = 0; |
| cur_idx = hdr_idx_first_idx(idx); |
| while (cur_idx) { |
| eol = sol + idx->v[cur_idx].len; |
| |
| if (len == 0) { |
| /* No argument was passed, we want any header. |
| * To achieve this, we simply build a fake request. */ |
| while (sol + len < eol && sol[len] != ':') |
| len++; |
| name = sol; |
| } |
| |
| if ((len < eol - sol) && |
| (sol[len] == ':') && |
| (strncasecmp(sol, name, len) == 0)) { |
| ctx->del = len; |
| sov = sol + len + 1; |
| while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| sov++; |
| |
| ctx->line = sol; |
| ctx->prev = old_idx; |
| return_hdr: |
| ctx->idx = cur_idx; |
| ctx->val = sov - sol; |
| |
| eol = find_hdr_value_end(sov, eol); |
| ctx->tws = 0; |
| while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) { |
| eol--; |
| ctx->tws++; |
| } |
| ctx->vlen = eol - sov; |
| return 1; |
| } |
| next_hdr: |
| sol = eol + idx->v[cur_idx].cr + 1; |
| old_idx = cur_idx; |
| cur_idx = idx->v[cur_idx].next; |
| } |
| return 0; |
| } |
| |
| int http_find_header(const char *name, |
| char *sol, struct hdr_idx *idx, |
| struct hdr_ctx *ctx) |
| { |
| return http_find_header2(name, strlen(name), sol, idx, ctx); |
| } |
| |
| /* Remove one value of a header. This only works on a <ctx> returned by one of |
| * the http_find_header functions. The value is removed, as well as surrounding |
| * commas if any. If the removed value was alone, the whole header is removed. |
| * The ctx is always updated accordingly, as well as the buffer and HTTP |
| * message <msg>. The new index is returned. If it is zero, it means there is |
| * no more header, so any processing may stop. The ctx is always left in a form |
| * that can be handled by http_find_header2() to find next occurrence. |
| */ |
| int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| { |
| int cur_idx = ctx->idx; |
| char *sol = ctx->line; |
| struct hdr_idx_elem *hdr; |
| int delta, skip_comma; |
| |
| if (!cur_idx) |
| return 0; |
| |
| hdr = &idx->v[cur_idx]; |
| if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
| /* This was the only value of the header, we must now remove it entirely. */ |
| delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| http_msg_move_end(msg, delta); |
| idx->used--; |
| hdr->len = 0; /* unused entry */ |
| idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
| if (idx->tail == ctx->idx) |
| idx->tail = ctx->prev; |
| ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| ctx->tws = ctx->vlen = 0; |
| return ctx->idx; |
| } |
| |
| /* This was not the only value of this header. We have to remove between |
| * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| * last entry of the list, we remove the last separator. |
| */ |
| |
| skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
| delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma, |
| sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
| NULL, 0); |
| hdr->len += delta; |
| http_msg_move_end(msg, delta); |
| ctx->val = ctx->del; |
| ctx->tws = ctx->vlen = 0; |
| return ctx->idx; |
| } |
| |
| /* This function handles a server error at the stream interface level. The |
| * stream interface is assumed to be already in a closed state. An optional |
| * message is copied into the input buffer, and an HTTP status code stored. |
| * The error flags are set to the values in arguments. Any pending request |
| * in this buffer will be lost. |
| */ |
| static void http_server_error(struct session *t, struct stream_interface *si, |
| int err, int finst, int status, const struct chunk *msg) |
| { |
| channel_auto_read(si->ob); |
| channel_abort(si->ob); |
| channel_auto_close(si->ob); |
| channel_erase(si->ob); |
| channel_auto_close(si->ib); |
| channel_auto_read(si->ib); |
| if (status > 0 && msg) { |
| t->txn.status = status; |
| bo_inject(si->ib, msg->str, msg->len); |
| } |
| if (!(t->flags & SN_ERR_MASK)) |
| t->flags |= err; |
| if (!(t->flags & SN_FINST_MASK)) |
| t->flags |= finst; |
| } |
| |
| /* This function returns the appropriate error location for the given session |
| * and message. |
| */ |
| |
| struct chunk *http_error_message(struct session *s, int msgnum) |
| { |
| if (s->be->errmsg[msgnum].str) |
| return &s->be->errmsg[msgnum]; |
| else if (s->fe->errmsg[msgnum].str) |
| return &s->fe->errmsg[msgnum]; |
| else |
| return &http_err_chunks[msgnum]; |
| } |
| |
| /* |
| * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| */ |
| static enum http_meth_t find_http_meth(const char *str, const int len) |
| { |
| unsigned char m; |
| const struct http_method_desc *h; |
| |
| m = ((unsigned)*str - 'A'); |
| |
| if (m < 26) { |
| for (h = http_methods[m]; h->len > 0; h++) { |
| if (unlikely(h->len != len)) |
| continue; |
| if (likely(memcmp(str, h->text, h->len) == 0)) |
| return h->meth; |
| }; |
| return HTTP_METH_OTHER; |
| } |
| return HTTP_METH_NONE; |
| |
| } |
| |
| /* Parse the URI from the given transaction (which is assumed to be in request |
| * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| * It is returned otherwise. |
| */ |
| static char * |
| http_get_path(struct http_txn *txn) |
| { |
| char *ptr, *end; |
| |
| ptr = txn->req.chn->buf->p + txn->req.sl.rq.u; |
| end = ptr + txn->req.sl.rq.u_l; |
| |
| if (ptr >= end) |
| return NULL; |
| |
| /* RFC2616, par. 5.1.2 : |
| * Request-URI = "*" | absuri | abspath | authority |
| */ |
| |
| if (*ptr == '*') |
| return NULL; |
| |
| if (isalpha((unsigned char)*ptr)) { |
| /* this is a scheme as described by RFC3986, par. 3.1 */ |
| ptr++; |
| while (ptr < end && |
| (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| ptr++; |
| /* skip '://' */ |
| if (ptr == end || *ptr++ != ':') |
| return NULL; |
| if (ptr == end || *ptr++ != '/') |
| return NULL; |
| if (ptr == end || *ptr++ != '/') |
| return NULL; |
| } |
| /* skip [user[:passwd]@]host[:[port]] */ |
| |
| while (ptr < end && *ptr != '/') |
| ptr++; |
| |
| if (ptr == end) |
| return NULL; |
| |
| /* OK, we got the '/' ! */ |
| return ptr; |
| } |
| |
| /* Parse the URI from the given string and look for the "/" beginning the PATH. |
| * If not found, return NULL. It is returned otherwise. |
| */ |
| static char * |
| http_get_path_from_string(char *str) |
| { |
| char *ptr = str; |
| |
| /* RFC2616, par. 5.1.2 : |
| * Request-URI = "*" | absuri | abspath | authority |
| */ |
| |
| if (*ptr == '*') |
| return NULL; |
| |
| if (isalpha((unsigned char)*ptr)) { |
| /* this is a scheme as described by RFC3986, par. 3.1 */ |
| ptr++; |
| while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.') |
| ptr++; |
| /* skip '://' */ |
| if (*ptr == '\0' || *ptr++ != ':') |
| return NULL; |
| if (*ptr == '\0' || *ptr++ != '/') |
| return NULL; |
| if (*ptr == '\0' || *ptr++ != '/') |
| return NULL; |
| } |
| /* skip [user[:passwd]@]host[:[port]] */ |
| |
| while (*ptr != '\0' && *ptr != ' ' && *ptr != '/') |
| ptr++; |
| |
| if (*ptr == '\0' || *ptr == ' ') |
| return NULL; |
| |
| /* OK, we got the '/' ! */ |
| return ptr; |
| } |
| |
| /* Returns a 302 for a redirectable request that reaches a server working in |
| * in redirect mode. This may only be called just after the stream interface |
| * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| * follow normal proxy processing. NOTE: this function is designed to support |
| * being called once data are scheduled for forwarding. |
| */ |
| void http_perform_server_redirect(struct session *s, struct stream_interface *si) |
| { |
| struct http_txn *txn; |
| struct server *srv; |
| char *path; |
| int len, rewind; |
| |
| /* 1: create the response header */ |
| trash.len = strlen(HTTP_302); |
| memcpy(trash.str, HTTP_302, trash.len); |
| |
| srv = objt_server(s->target); |
| |
| /* 2: add the server's prefix */ |
| if (trash.len + srv->rdr_len > trash.size) |
| return; |
| |
| /* special prefix "/" means don't change URL */ |
| if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len); |
| trash.len += srv->rdr_len; |
| } |
| |
| /* 3: add the request URI. Since it was already forwarded, we need |
| * to temporarily rewind the buffer. |
| */ |
| txn = &s->txn; |
| b_rew(s->req->buf, rewind = s->req->buf->o); |
| |
| path = http_get_path(txn); |
| len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
| |
| b_adv(s->req->buf, rewind); |
| |
| if (!path) |
| return; |
| |
| if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
| return; |
| |
| memcpy(trash.str + trash.len, path, len); |
| trash.len += len; |
| |
| if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| trash.len += 29; |
| } else { |
| memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| trash.len += 23; |
| } |
| |
| /* prepare to return without error. */ |
| si_shutr(si); |
| si_shutw(si); |
| si->err_type = SI_ET_NONE; |
| si->state = SI_ST_CLO; |
| |
| /* send the message */ |
| http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash); |
| |
| /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| srv_inc_sess_ctr(srv); |
| srv_set_sess_last(srv); |
| } |
| |
| /* Return the error message corresponding to si->err_type. It is assumed |
| * that the server side is closed. Note that err_type is actually a |
| * bitmask, where almost only aborts may be cumulated with other |
| * values. We consider that aborted operations are more important |
| * than timeouts or errors due to the fact that nobody else in the |
| * logs might explain incomplete retries. All others should avoid |
| * being cumulated. It should normally not be possible to have multiple |
| * aborts at once, but just in case, the first one in sequence is reported. |
| * Note that connection errors appearing on the second request of a keep-alive |
| * connection are not reported since this allows the client to retry. |
| */ |
| void http_return_srv_error(struct session *s, struct stream_interface *si) |
| { |
| int err_type = si->err_type; |
| |
| if (err_type & SI_ET_QUEUE_ABRT) |
| http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 503, http_error_message(s, HTTP_ERR_503)); |
| else if (err_type & SI_ET_CONN_ABRT) |
| http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| http_error_message(s, HTTP_ERR_503)); |
| else if (err_type & SI_ET_QUEUE_TO) |
| http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 503, http_error_message(s, HTTP_ERR_503)); |
| else if (err_type & SI_ET_QUEUE_ERR) |
| http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 503, http_error_message(s, HTTP_ERR_503)); |
| else if (err_type & SI_ET_CONN_TO) |
| http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| http_error_message(s, HTTP_ERR_503)); |
| else if (err_type & SI_ET_CONN_ERR) |
| http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 503, (s->flags & SN_SRV_REUSED) ? NULL : |
| http_error_message(s, HTTP_ERR_503)); |
| else if (err_type & SI_ET_CONN_RES) |
| http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C, |
| 503, (s->txn.flags & TX_NOT_FIRST) ? NULL : |
| http_error_message(s, HTTP_ERR_503)); |
| else /* SI_ET_CONN_OTHER and others */ |
| http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 500, http_error_message(s, HTTP_ERR_500)); |
| } |
| |
| extern const char sess_term_cond[8]; |
| extern const char sess_fin_state[8]; |
| extern const char *monthname[12]; |
| struct pool_head *pool2_requri; |
| struct pool_head *pool2_capture = NULL; |
| struct pool_head *pool2_uniqueid; |
| |
| /* |
| * Capture headers from message starting at <som> according to header list |
| * <cap_hdr>, and fill the <idx> structure appropriately. |
| */ |
| void capture_headers(char *som, struct hdr_idx *idx, |
| char **cap, struct cap_hdr *cap_hdr) |
| { |
| char *eol, *sol, *col, *sov; |
| int cur_idx; |
| struct cap_hdr *h; |
| int len; |
| |
| sol = som + hdr_idx_first_pos(idx); |
| cur_idx = hdr_idx_first_idx(idx); |
| |
| while (cur_idx) { |
| eol = sol + idx->v[cur_idx].len; |
| |
| col = sol; |
| while (col < eol && *col != ':') |
| col++; |
| |
| sov = col + 1; |
| while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| sov++; |
| |
| for (h = cap_hdr; h; h = h->next) { |
| if ((h->namelen == col - sol) && |
| (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| if (cap[h->index] == NULL) |
| cap[h->index] = |
| pool_alloc2(h->pool); |
| |
| if (cap[h->index] == NULL) { |
| Alert("HTTP capture : out of memory.\n"); |
| continue; |
| } |
| |
| len = eol - sov; |
| if (len > h->len) |
| len = h->len; |
| |
| memcpy(cap[h->index], sov, len); |
| cap[h->index][len]=0; |
| } |
| } |
| sol = eol + idx->v[cur_idx].cr + 1; |
| cur_idx = idx->v[cur_idx].next; |
| } |
| } |
| |
| |
| /* either we find an LF at <ptr> or we jump to <bad>. |
| */ |
| #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| |
| /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| * otherwise to <http_msg_ood> with <state> set to <st>. |
| */ |
| #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| ptr++; \ |
| if (likely(ptr < end)) \ |
| goto good; \ |
| else { \ |
| state = (st); \ |
| goto http_msg_ood; \ |
| } \ |
| } while (0) |
| |
| |
| /* |
| * This function parses a status line between <ptr> and <end>, starting with |
| * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| * will give undefined results. |
| * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| * and that msg->sol points to the beginning of the response. |
| * If a complete line is found (which implies that at least one CR or LF is |
| * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| * returned indicating an incomplete line (which does not mean that parts have |
| * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| * upon next call. |
| * |
| * This function was intentionally designed to be called from |
| * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| * within its state machine and use the same macros, hence the need for same |
| * labels and variable names. Note that msg->sol is left unchanged. |
| */ |
| const char *http_parse_stsline(struct http_msg *msg, |
| enum ht_state state, const char *ptr, const char *end, |
| unsigned int *ret_ptr, enum ht_state *ret_state) |
| { |
| const char *msg_start = msg->chn->buf->p; |
| |
| switch (state) { |
| case HTTP_MSG_RPVER: |
| http_msg_rpver: |
| if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| |
| if (likely(HTTP_IS_SPHT(*ptr))) { |
| msg->sl.st.v_l = ptr - msg_start; |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| } |
| state = HTTP_MSG_ERROR; |
| break; |
| |
| case HTTP_MSG_RPVER_SP: |
| http_msg_rpver_sp: |
| if (likely(!HTTP_IS_LWS(*ptr))) { |
| msg->sl.st.c = ptr - msg_start; |
| goto http_msg_rpcode; |
| } |
| if (likely(HTTP_IS_SPHT(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| /* so it's a CR/LF, this is invalid */ |
| state = HTTP_MSG_ERROR; |
| break; |
| |
| case HTTP_MSG_RPCODE: |
| http_msg_rpcode: |
| if (likely(!HTTP_IS_LWS(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| |
| if (likely(HTTP_IS_SPHT(*ptr))) { |
| msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| } |
| |
| /* so it's a CR/LF, so there is no reason phrase */ |
| msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
| http_msg_rsp_reason: |
| /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| msg->sl.st.r = ptr - msg_start; |
| msg->sl.st.r_l = 0; |
| goto http_msg_rpline_eol; |
| |
| case HTTP_MSG_RPCODE_SP: |
| http_msg_rpcode_sp: |
| if (likely(!HTTP_IS_LWS(*ptr))) { |
| msg->sl.st.r = ptr - msg_start; |
| goto http_msg_rpreason; |
| } |
| if (likely(HTTP_IS_SPHT(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| /* so it's a CR/LF, so there is no reason phrase */ |
| goto http_msg_rsp_reason; |
| |
| case HTTP_MSG_RPREASON: |
| http_msg_rpreason: |
| if (likely(!HTTP_IS_CRLF(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
| http_msg_rpline_eol: |
| /* We have seen the end of line. Note that we do not |
| * necessarily have the \n yet, but at least we know that we |
| * have EITHER \r OR \n, otherwise the response would not be |
| * complete. We can then record the response length and return |
| * to the caller which will be able to register it. |
| */ |
| msg->sl.st.l = ptr - msg_start - msg->sol; |
| return ptr; |
| |
| default: |
| #ifdef DEBUG_FULL |
| fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| exit(1); |
| #endif |
| ; |
| } |
| |
| http_msg_ood: |
| /* out of valid data */ |
| if (ret_state) |
| *ret_state = state; |
| if (ret_ptr) |
| *ret_ptr = ptr - msg_start; |
| return NULL; |
| } |
| |
| /* |
| * This function parses a request line between <ptr> and <end>, starting with |
| * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| * will give undefined results. |
| * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| * and that msg->sol points to the beginning of the request. |
| * If a complete line is found (which implies that at least one CR or LF is |
| * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| * returned indicating an incomplete line (which does not mean that parts have |
| * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| * upon next call. |
| * |
| * This function was intentionally designed to be called from |
| * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| * within its state machine and use the same macros, hence the need for same |
| * labels and variable names. Note that msg->sol is left unchanged. |
| */ |
| const char *http_parse_reqline(struct http_msg *msg, |
| enum ht_state state, const char *ptr, const char *end, |
| unsigned int *ret_ptr, enum ht_state *ret_state) |
| { |
| const char *msg_start = msg->chn->buf->p; |
| |
| switch (state) { |
| case HTTP_MSG_RQMETH: |
| http_msg_rqmeth: |
| if (likely(HTTP_IS_TOKEN(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
| |
| if (likely(HTTP_IS_SPHT(*ptr))) { |
| msg->sl.rq.m_l = ptr - msg_start; |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| } |
| |
| if (likely(HTTP_IS_CRLF(*ptr))) { |
| /* HTTP 0.9 request */ |
| msg->sl.rq.m_l = ptr - msg_start; |
| http_msg_req09_uri: |
| msg->sl.rq.u = ptr - msg_start; |
| http_msg_req09_uri_e: |
| msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
| http_msg_req09_ver: |
| msg->sl.rq.v = ptr - msg_start; |
| msg->sl.rq.v_l = 0; |
| goto http_msg_rqline_eol; |
| } |
| state = HTTP_MSG_ERROR; |
| break; |
| |
| case HTTP_MSG_RQMETH_SP: |
| http_msg_rqmeth_sp: |
| if (likely(!HTTP_IS_LWS(*ptr))) { |
| msg->sl.rq.u = ptr - msg_start; |
| goto http_msg_rquri; |
| } |
| if (likely(HTTP_IS_SPHT(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| goto http_msg_req09_uri; |
| |
| case HTTP_MSG_RQURI: |
| http_msg_rquri: |
| if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
| EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| |
| if (likely(HTTP_IS_SPHT(*ptr))) { |
| msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
| EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| } |
| |
| if (likely((unsigned char)*ptr >= 128)) { |
| /* non-ASCII chars are forbidden unless option |
| * accept-invalid-http-request is enabled in the frontend. |
| * In any case, we capture the faulty char. |
| */ |
| if (msg->err_pos < -1) |
| goto invalid_char; |
| if (msg->err_pos == -1) |
| msg->err_pos = ptr - msg_start; |
| EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| } |
| |
| if (likely(HTTP_IS_CRLF(*ptr))) { |
| /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| goto http_msg_req09_uri_e; |
| } |
| |
| /* OK forbidden chars, 0..31 or 127 */ |
| invalid_char: |
| msg->err_pos = ptr - msg_start; |
| state = HTTP_MSG_ERROR; |
| break; |
| |
| case HTTP_MSG_RQURI_SP: |
| http_msg_rquri_sp: |
| if (likely(!HTTP_IS_LWS(*ptr))) { |
| msg->sl.rq.v = ptr - msg_start; |
| goto http_msg_rqver; |
| } |
| if (likely(HTTP_IS_SPHT(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| goto http_msg_req09_ver; |
| |
| case HTTP_MSG_RQVER: |
| http_msg_rqver: |
| if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
| |
| if (likely(HTTP_IS_CRLF(*ptr))) { |
| msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
| http_msg_rqline_eol: |
| /* We have seen the end of line. Note that we do not |
| * necessarily have the \n yet, but at least we know that we |
| * have EITHER \r OR \n, otherwise the request would not be |
| * complete. We can then record the request length and return |
| * to the caller which will be able to register it. |
| */ |
| msg->sl.rq.l = ptr - msg_start - msg->sol; |
| return ptr; |
| } |
| |
| /* neither an HTTP_VER token nor a CRLF */ |
| state = HTTP_MSG_ERROR; |
| break; |
| |
| default: |
| #ifdef DEBUG_FULL |
| fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| exit(1); |
| #endif |
| ; |
| } |
| |
| http_msg_ood: |
| /* out of valid data */ |
| if (ret_state) |
| *ret_state = state; |
| if (ret_ptr) |
| *ret_ptr = ptr - msg_start; |
| return NULL; |
| } |
| |
| /* |
| * Returns the data from Authorization header. Function may be called more |
| * than once so data is stored in txn->auth_data. When no header is found |
| * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| * searching again for something we are unable to find anyway. However, if |
| * the result if valid, the cache is not reused because we would risk to |
| * have the credentials overwritten by another session in parallel. |
| */ |
| |
| char *get_http_auth_buff; |
| |
| int |
| get_http_auth(struct session *s) |
| { |
| |
| struct http_txn *txn = &s->txn; |
| struct chunk auth_method; |
| struct hdr_ctx ctx; |
| char *h, *p; |
| int len; |
| |
| #ifdef DEBUG_AUTH |
| printf("Auth for session %p: %d\n", s, txn->auth.method); |
| #endif |
| |
| if (txn->auth.method == HTTP_AUTH_WRONG) |
| return 0; |
| |
| txn->auth.method = HTTP_AUTH_WRONG; |
| |
| ctx.idx = 0; |
| |
| if (txn->flags & TX_USE_PX_CONN) { |
| h = "Proxy-Authorization"; |
| len = strlen(h); |
| } else { |
| h = "Authorization"; |
| len = strlen(h); |
| } |
| |
| if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx)) |
| return 0; |
| |
| h = ctx.line + ctx.val; |
| |
| p = memchr(h, ' ', ctx.vlen); |
| if (!p || p == h) |
| return 0; |
| |
| chunk_initlen(&auth_method, h, 0, p-h); |
| chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| |
| if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| |
| len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| get_http_auth_buff, global.tune.bufsize - 1); |
| |
| if (len < 0) |
| return 0; |
| |
| |
| get_http_auth_buff[len] = '\0'; |
| |
| p = strchr(get_http_auth_buff, ':'); |
| |
| if (!p) |
| return 0; |
| |
| txn->auth.user = get_http_auth_buff; |
| *p = '\0'; |
| txn->auth.pass = p+1; |
| |
| txn->auth.method = HTTP_AUTH_BASIC; |
| return 1; |
| } |
| |
| return 0; |
| } |
| |
| |
| /* |
| * This function parses an HTTP message, either a request or a response, |
| * depending on the initial msg->msg_state. The caller is responsible for |
| * ensuring that the message does not wrap. The function can be preempted |
| * everywhere when data are missing and recalled at the exact same location |
| * with no information loss. The message may even be realigned between two |
| * calls. The header index is re-initialized when switching from |
| * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| * fields. Note that msg->sol will be initialized after completing the first |
| * state, so that none of the msg pointers has to be initialized prior to the |
| * first call. |
| */ |
| void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
| { |
| enum ht_state state; /* updated only when leaving the FSM */ |
| register char *ptr, *end; /* request pointers, to avoid dereferences */ |
| struct buffer *buf; |
| |
| state = msg->msg_state; |
| buf = msg->chn->buf; |
| ptr = buf->p + msg->next; |
| end = buf->p + buf->i; |
| |
| if (unlikely(ptr >= end)) |
| goto http_msg_ood; |
| |
| switch (state) { |
| /* |
| * First, states that are specific to the response only. |
| * We check them first so that request and headers are |
| * closer to each other (accessed more often). |
| */ |
| case HTTP_MSG_RPBEFORE: |
| http_msg_rpbefore: |
| if (likely(HTTP_IS_TOKEN(*ptr))) { |
| /* we have a start of message, but we have to check |
| * first if we need to remove some CRLF. We can only |
| * do this when o=0. |
| */ |
| if (unlikely(ptr != buf->p)) { |
| if (buf->o) |
| goto http_msg_ood; |
| /* Remove empty leading lines, as recommended by RFC2616. */ |
| bi_fast_delete(buf, ptr - buf->p); |
| } |
| msg->sol = 0; |
| msg->sl.st.l = 0; /* used in debug mode */ |
| hdr_idx_init(idx); |
| state = HTTP_MSG_RPVER; |
| goto http_msg_rpver; |
| } |
| |
| if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| goto http_msg_invalid; |
| |
| if (unlikely(*ptr == '\n')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| /* stop here */ |
| |
| case HTTP_MSG_RPBEFORE_CR: |
| http_msg_rpbefore_cr: |
| EXPECT_LF_HERE(ptr, http_msg_invalid); |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| /* stop here */ |
| |
| case HTTP_MSG_RPVER: |
| http_msg_rpver: |
| case HTTP_MSG_RPVER_SP: |
| case HTTP_MSG_RPCODE: |
| case HTTP_MSG_RPCODE_SP: |
| case HTTP_MSG_RPREASON: |
| ptr = (char *)http_parse_stsline(msg, |
| state, ptr, end, |
| &msg->next, &msg->msg_state); |
| if (unlikely(!ptr)) |
| return; |
| |
| /* we have a full response and we know that we have either a CR |
| * or an LF at <ptr>. |
| */ |
| hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| |
| msg->sol = ptr - buf->p; |
| if (likely(*ptr == '\r')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| goto http_msg_rpline_end; |
| |
| case HTTP_MSG_RPLINE_END: |
| http_msg_rpline_end: |
| /* msg->sol must point to the first of CR or LF. */ |
| EXPECT_LF_HERE(ptr, http_msg_invalid); |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| /* stop here */ |
| |
| /* |
| * Second, states that are specific to the request only |
| */ |
| case HTTP_MSG_RQBEFORE: |
| http_msg_rqbefore: |
| if (likely(HTTP_IS_TOKEN(*ptr))) { |
| /* we have a start of message, but we have to check |
| * first if we need to remove some CRLF. We can only |
| * do this when o=0. |
| */ |
| if (likely(ptr != buf->p)) { |
| if (buf->o) |
| goto http_msg_ood; |
| /* Remove empty leading lines, as recommended by RFC2616. */ |
| bi_fast_delete(buf, ptr - buf->p); |
| } |
| msg->sol = 0; |
| msg->sl.rq.l = 0; /* used in debug mode */ |
| state = HTTP_MSG_RQMETH; |
| goto http_msg_rqmeth; |
| } |
| |
| if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| goto http_msg_invalid; |
| |
| if (unlikely(*ptr == '\n')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR); |
| /* stop here */ |
| |
| case HTTP_MSG_RQBEFORE_CR: |
| http_msg_rqbefore_cr: |
| EXPECT_LF_HERE(ptr, http_msg_invalid); |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| /* stop here */ |
| |
| case HTTP_MSG_RQMETH: |
| http_msg_rqmeth: |
| case HTTP_MSG_RQMETH_SP: |
| case HTTP_MSG_RQURI: |
| case HTTP_MSG_RQURI_SP: |
| case HTTP_MSG_RQVER: |
| ptr = (char *)http_parse_reqline(msg, |
| state, ptr, end, |
| &msg->next, &msg->msg_state); |
| if (unlikely(!ptr)) |
| return; |
| |
| /* we have a full request and we know that we have either a CR |
| * or an LF at <ptr>. |
| */ |
| hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
| |
| msg->sol = ptr - buf->p; |
| if (likely(*ptr == '\r')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END); |
| goto http_msg_rqline_end; |
| |
| case HTTP_MSG_RQLINE_END: |
| http_msg_rqline_end: |
| /* check for HTTP/0.9 request : no version information available. |
| * msg->sol must point to the first of CR or LF. |
| */ |
| if (unlikely(msg->sl.rq.v_l == 0)) |
| goto http_msg_last_lf; |
| |
| EXPECT_LF_HERE(ptr, http_msg_invalid); |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| /* stop here */ |
| |
| /* |
| * Common states below |
| */ |
| case HTTP_MSG_HDR_FIRST: |
| http_msg_hdr_first: |
| msg->sol = ptr - buf->p; |
| if (likely(!HTTP_IS_CRLF(*ptr))) { |
| goto http_msg_hdr_name; |
| } |
| |
| if (likely(*ptr == '\r')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| goto http_msg_last_lf; |
| |
| case HTTP_MSG_HDR_NAME: |
| http_msg_hdr_name: |
| /* assumes msg->sol points to the first char */ |
| if (likely(HTTP_IS_TOKEN(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
| |
| if (likely(*ptr == ':')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| |
| if (likely(msg->err_pos < -1) || *ptr == '\n') |
| goto http_msg_invalid; |
| |
| if (msg->err_pos == -1) /* capture error pointer */ |
| msg->err_pos = ptr - buf->p; /* >= 0 now */ |
| |
| /* and we still accept this non-token character */ |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME); |
| |
| case HTTP_MSG_HDR_L1_SP: |
| http_msg_hdr_l1_sp: |
| /* assumes msg->sol points to the first char */ |
| if (likely(HTTP_IS_SPHT(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| |
| /* header value can be basically anything except CR/LF */ |
| msg->sov = ptr - buf->p; |
| |
| if (likely(!HTTP_IS_CRLF(*ptr))) { |
| goto http_msg_hdr_val; |
| } |
| |
| if (likely(*ptr == '\r')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| goto http_msg_hdr_l1_lf; |
| |
| case HTTP_MSG_HDR_L1_LF: |
| http_msg_hdr_l1_lf: |
| EXPECT_LF_HERE(ptr, http_msg_invalid); |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS); |
| |
| case HTTP_MSG_HDR_L1_LWS: |
| http_msg_hdr_l1_lws: |
| if (likely(HTTP_IS_SPHT(*ptr))) { |
| /* replace HT,CR,LF with spaces */ |
| for (; buf->p + msg->sov < ptr; msg->sov++) |
| buf->p[msg->sov] = ' '; |
| goto http_msg_hdr_l1_sp; |
| } |
| /* we had a header consisting only in spaces ! */ |
| msg->eol = msg->sov; |
| goto http_msg_complete_header; |
| |
| case HTTP_MSG_HDR_VAL: |
| http_msg_hdr_val: |
| /* assumes msg->sol points to the first char, and msg->sov |
| * points to the first character of the value. |
| */ |
| if (likely(!HTTP_IS_CRLF(*ptr))) |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL); |
| |
| msg->eol = ptr - buf->p; |
| /* Note: we could also copy eol into ->eoh so that we have the |
| * real header end in case it ends with lots of LWS, but is this |
| * really needed ? |
| */ |
| if (likely(*ptr == '\r')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| goto http_msg_hdr_l2_lf; |
| |
| case HTTP_MSG_HDR_L2_LF: |
| http_msg_hdr_l2_lf: |
| EXPECT_LF_HERE(ptr, http_msg_invalid); |
| EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS); |
| |
| case HTTP_MSG_HDR_L2_LWS: |
| http_msg_hdr_l2_lws: |
| if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| /* LWS: replace HT,CR,LF with spaces */ |
| for (; buf->p + msg->eol < ptr; msg->eol++) |
| buf->p[msg->eol] = ' '; |
| goto http_msg_hdr_val; |
| } |
| http_msg_complete_header: |
| /* |
| * It was a new header, so the last one is finished. |
| * Assumes msg->sol points to the first char, msg->sov points |
| * to the first character of the value and msg->eol to the |
| * first CR or LF so we know how the line ends. We insert last |
| * header into the index. |
| */ |
| if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
| idx, idx->tail) < 0)) |
| goto http_msg_invalid; |
| |
| msg->sol = ptr - buf->p; |
| if (likely(!HTTP_IS_CRLF(*ptr))) { |
| goto http_msg_hdr_name; |
| } |
| |
| if (likely(*ptr == '\r')) |
| EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| goto http_msg_last_lf; |
| |
| case HTTP_MSG_LAST_LF: |
| http_msg_last_lf: |
| /* Assumes msg->sol points to the first of either CR or LF */ |
| EXPECT_LF_HERE(ptr, http_msg_invalid); |
| ptr++; |
| msg->sov = msg->next = ptr - buf->p; |
| msg->eoh = msg->sol; |
| msg->sol = 0; |
| msg->msg_state = HTTP_MSG_BODY; |
| return; |
| |
| case HTTP_MSG_ERROR: |
| /* this may only happen if we call http_msg_analyser() twice with an error */ |
| break; |
| |
| default: |
| #ifdef DEBUG_FULL |
| fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| exit(1); |
| #endif |
| ; |
| } |
| http_msg_ood: |
| /* out of data */ |
| msg->msg_state = state; |
| msg->next = ptr - buf->p; |
| return; |
| |
| http_msg_invalid: |
| /* invalid message */ |
| msg->msg_state = HTTP_MSG_ERROR; |
| msg->next = ptr - buf->p; |
| return; |
| } |
| |
| /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| * nothing is done and 1 is returned. |
| */ |
| static int http_upgrade_v09_to_v10(struct http_txn *txn) |
| { |
| int delta; |
| char *cur_end; |
| struct http_msg *msg = &txn->req; |
| |
| if (msg->sl.rq.v_l != 0) |
| return 1; |
| |
| cur_end = msg->chn->buf->p + msg->sl.rq.l; |
| delta = 0; |
| |
| if (msg->sl.rq.u_l == 0) { |
| /* if no URI was set, add "/" */ |
| delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2); |
| cur_end += delta; |
| http_msg_move_end(msg, delta); |
| } |
| /* add HTTP version */ |
| delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| http_msg_move_end(msg, delta); |
| cur_end += delta; |
| cur_end = (char *)http_parse_reqline(msg, |
| HTTP_MSG_RQMETH, |
| msg->chn->buf->p, cur_end + 1, |
| NULL, NULL); |
| if (unlikely(!cur_end)) |
| return 0; |
| |
| /* we have a full HTTP/1.0 request now and we know that |
| * we have either a CR or an LF at <ptr>. |
| */ |
| hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| return 1; |
| } |
| |
| /* Parse the Connection: header of an HTTP request, looking for both "close" |
| * and "keep-alive" values. If we already know that some headers may safely |
| * be removed, we remove them now. The <to_del> flags are used for that : |
| * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| * Presence of the "Upgrade" token is also checked and reported. |
| * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| * harmless combinations may be removed. Do not call that after changes have |
| * been processed. |
| */ |
| void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
| { |
| struct hdr_ctx ctx; |
| const char *hdr_val = "Connection"; |
| int hdr_len = 10; |
| |
| if (txn->flags & TX_HDR_CONN_PRS) |
| return; |
| |
| if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| hdr_val = "Proxy-Connection"; |
| hdr_len = 16; |
| } |
| |
| ctx.idx = 0; |
| txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
| while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
| if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| txn->flags |= TX_HDR_CONN_KAL; |
| if (to_del & 2) |
| http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| else |
| txn->flags |= TX_CON_KAL_SET; |
| } |
| else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| txn->flags |= TX_HDR_CONN_CLO; |
| if (to_del & 1) |
| http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| else |
| txn->flags |= TX_CON_CLO_SET; |
| } |
| else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| txn->flags |= TX_HDR_CONN_UPG; |
| } |
| } |
| |
| txn->flags |= TX_HDR_CONN_PRS; |
| return; |
| } |
| |
| /* Apply desired changes on the Connection: header. Values may be removed and/or |
| * added depending on the <wanted> flags, which are exclusively composed of |
| * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| */ |
| void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
| { |
| struct hdr_ctx ctx; |
| const char *hdr_val = "Connection"; |
| int hdr_len = 10; |
| |
| ctx.idx = 0; |
| |
| |
| if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| hdr_val = "Proxy-Connection"; |
| hdr_len = 16; |
| } |
| |
| txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
| while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) { |
| if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| if (wanted & TX_CON_KAL_SET) |
| txn->flags |= TX_CON_KAL_SET; |
| else |
| http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| } |
| else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| if (wanted & TX_CON_CLO_SET) |
| txn->flags |= TX_CON_CLO_SET; |
| else |
| http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| } |
| } |
| |
| if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| return; |
| |
| if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| txn->flags |= TX_CON_CLO_SET; |
| hdr_val = "Connection: close"; |
| hdr_len = 17; |
| if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| hdr_val = "Proxy-Connection: close"; |
| hdr_len = 23; |
| } |
| http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
| } |
| |
| if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| txn->flags |= TX_CON_KAL_SET; |
| hdr_val = "Connection: keep-alive"; |
| hdr_len = 22; |
| if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| hdr_val = "Proxy-Connection: keep-alive"; |
| hdr_len = 28; |
| } |
| http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
| } |
| return; |
| } |
| |
| /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the |
| * first byte of body, and increments msg->sov by the number of bytes parsed, |
| * so that we know we can forward between ->sol and ->sov. |
| * Return >0 on success, 0 when some data is missing, <0 on error. |
| * Note: this function is designed to parse wrapped CRLF at the end of the buffer. |
| */ |
| static inline int http_parse_chunk_size(struct http_msg *msg) |
| { |
| const struct buffer *buf = msg->chn->buf; |
| const char *ptr = b_ptr(buf, msg->next); |
| const char *ptr_old = ptr; |
| const char *end = buf->data + buf->size; |
| const char *stop = bi_end(buf); |
| unsigned int chunk = 0; |
| |
| /* The chunk size is in the following form, though we are only |
| * interested in the size and CRLF : |
| * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| */ |
| while (1) { |
| int c; |
| if (ptr == stop) |
| return 0; |
| c = hex2i(*ptr); |
| if (c < 0) /* not a hex digit anymore */ |
| break; |
| if (unlikely(++ptr >= end)) |
| ptr = buf->data; |
| if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
| goto error; |
| chunk = (chunk << 4) + c; |
| } |
| |
| /* empty size not allowed */ |
| if (unlikely(ptr == ptr_old)) |
| goto error; |
| |
| while (http_is_spht[(unsigned char)*ptr]) { |
| if (++ptr >= end) |
| ptr = buf->data; |
| if (unlikely(ptr == stop)) |
| return 0; |
| } |
| |
| /* Up to there, we know that at least one byte is present at *ptr. Check |
| * for the end of chunk size. |
| */ |
| while (1) { |
| if (likely(HTTP_IS_CRLF(*ptr))) { |
| /* we now have a CR or an LF at ptr */ |
| if (likely(*ptr == '\r')) { |
| if (++ptr >= end) |
| ptr = buf->data; |
| if (ptr == stop) |
| return 0; |
| } |
| |
| if (*ptr != '\n') |
| goto error; |
| if (++ptr >= end) |
| ptr = buf->data; |
| /* done */ |
| break; |
| } |
| else if (*ptr == ';') { |
| /* chunk extension, ends at next CRLF */ |
| if (++ptr >= end) |
| ptr = buf->data; |
| if (ptr == stop) |
| return 0; |
| |
| while (!HTTP_IS_CRLF(*ptr)) { |
| if (++ptr >= end) |
| ptr = buf->data; |
| if (ptr == stop) |
| return 0; |
| } |
| /* we have a CRLF now, loop above */ |
| continue; |
| } |
| else |
| goto error; |
| } |
| |
| /* OK we found our CRLF and now <ptr> points to the next byte, |
| * which may or may not be present. We save that into ->next and |
| * ->sov. |
| */ |
| if (unlikely(ptr < ptr_old)) |
| msg->sov += buf->size; |
| msg->sov += ptr - ptr_old; |
| msg->next = buffer_count(buf, buf->p, ptr); |
| msg->chunk_len = chunk; |
| msg->body_len += chunk; |
| msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
| return 1; |
| error: |
| msg->err_pos = buffer_count(buf, buf->p, ptr); |
| return -1; |
| } |
| |
| /* This function skips trailers in the buffer associated with HTTP |
| * message <msg>. The first visited position is msg->next. If the end of |
| * the trailers is found, it is automatically scheduled to be forwarded, |
| * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| * If not enough data are available, the function does not change anything |
| * except maybe msg->next and msg->sov if it could parse some lines, and returns |
| * zero. If a parse error is encountered, the function returns < 0 and does not |
| * change anything except maybe msg->next and msg->sov. Note that the message |
| * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| * which implies that all non-trailers data have already been scheduled for |
| * forwarding, and that the difference between msg->sol and msg->sov exactly |
| * matches the length of trailers already parsed and not forwarded. It is also |
| * important to note that this function is designed to be able to parse wrapped |
| * headers at end of buffer. |
| */ |
| static int http_forward_trailers(struct http_msg *msg) |
| { |
| const struct buffer *buf = msg->chn->buf; |
| |
| /* we have msg->next which points to next line. Look for CRLF. */ |
| while (1) { |
| const char *p1 = NULL, *p2 = NULL; |
| const char *ptr = b_ptr(buf, msg->next); |
| const char *stop = bi_end(buf); |
| int bytes; |
| |
| /* scan current line and stop at LF or CRLF */ |
| while (1) { |
| if (ptr == stop) |
| return 0; |
| |
| if (*ptr == '\n') { |
| if (!p1) |
| p1 = ptr; |
| p2 = ptr; |
| break; |
| } |
| |
| if (*ptr == '\r') { |
| if (p1) { |
| msg->err_pos = buffer_count(buf, buf->p, ptr); |
| return -1; |
| } |
| p1 = ptr; |
| } |
| |
| ptr++; |
| if (ptr >= buf->data + buf->size) |
| ptr = buf->data; |
| } |
| |
| /* after LF; point to beginning of next line */ |
| p2++; |
| if (p2 >= buf->data + buf->size) |
| p2 = buf->data; |
| |
| bytes = p2 - b_ptr(buf, msg->next); |
| if (bytes < 0) |
| bytes += buf->size; |
| |
| /* schedule this line for forwarding */ |
| msg->sov += bytes; |
| if (msg->sov >= buf->size) |
| msg->sov -= buf->size; |
| |
| if (p1 == b_ptr(buf, msg->next)) { |
| /* LF/CRLF at beginning of line => end of trailers at p2. |
| * Everything was scheduled for forwarding, there's nothing |
| * left from this message. |
| */ |
| msg->next = buffer_count(buf, buf->p, p2); |
| msg->msg_state = HTTP_MSG_DONE; |
| return 1; |
| } |
| /* OK, next line then */ |
| msg->next = buffer_count(buf, buf->p, p2); |
| } |
| } |
| |
| /* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or |
| * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| * ->sol, ->next in order to include this part into the next forwarding phase. |
| * Note that the caller must ensure that ->p points to the first byte to parse. |
| * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| * not enough data are available, the function does not change anything and |
| * returns zero. If a parse error is encountered, the function returns < 0 and |
| * does not change anything. Note: this function is designed to parse wrapped |
| * CRLF at the end of the buffer. |
| */ |
| static inline int http_skip_chunk_crlf(struct http_msg *msg) |
| { |
| const struct buffer *buf = msg->chn->buf; |
| const char *ptr; |
| int bytes; |
| |
| /* NB: we'll check data availabilty at the end. It's not a |
| * problem because whatever we match first will be checked |
| * against the correct length. |
| */ |
| bytes = 1; |
| ptr = buf->p; |
| if (*ptr == '\r') { |
| bytes++; |
| ptr++; |
| if (ptr >= buf->data + buf->size) |
| ptr = buf->data; |
| } |
| |
| if (bytes > buf->i) |
| return 0; |
| |
| if (*ptr != '\n') { |
| msg->err_pos = buffer_count(buf, buf->p, ptr); |
| return -1; |
| } |
| |
| ptr++; |
| if (unlikely(ptr >= buf->data + buf->size)) |
| ptr = buf->data; |
| /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| msg->sol = 0; |
| msg->sov = msg->next = bytes; |
| msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| return 1; |
| } |
| |
| |
| /* |
| * Selects a compression algorithm depending on the client request. |
| */ |
| int select_compression_request_header(struct session *s, struct buffer *req) |
| { |
| struct http_txn *txn = &s->txn; |
| struct http_msg *msg = &txn->req; |
| struct hdr_ctx ctx; |
| struct comp_algo *comp_algo = NULL; |
| struct comp_algo *comp_algo_back = NULL; |
| |
| /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later). |
| * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| */ |
| ctx.idx = 0; |
| if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| ctx.vlen >= 9 && |
| memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| (ctx.vlen < 31 || |
| memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| ctx.line[ctx.val + 30] < '6' || |
| (ctx.line[ctx.val + 30] == '6' && |
| (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| s->comp_algo = NULL; |
| return 0; |
| } |
| |
| /* search for the algo in the backend in priority or the frontend */ |
| if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| ctx.idx = 0; |
| while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) { |
| s->comp_algo = comp_algo; |
| |
| /* remove all occurrences of the header when "compression offload" is set */ |
| |
| if ((s->be->comp && s->be->comp->offload) || |
| (s->fe->comp && s->fe->comp->offload)) { |
| http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| ctx.idx = 0; |
| while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| } |
| } |
| return 1; |
| } |
| } |
| } |
| } |
| |
| /* identity is implicit does not require headers */ |
| if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) { |
| if (comp_algo->add_data == identity_add_data) { |
| s->comp_algo = comp_algo; |
| return 1; |
| } |
| } |
| } |
| |
| s->comp_algo = NULL; |
| return 0; |
| } |
| |
| /* |
| * Selects a comression algorithm depending of the server response. |
| */ |
| int select_compression_response_header(struct session *s, struct buffer *res) |
| { |
| struct http_txn *txn = &s->txn; |
| struct http_msg *msg = &txn->rsp; |
| struct hdr_ctx ctx; |
| struct comp_type *comp_type; |
| |
| /* no common compression algorithm was found in request header */ |
| if (s->comp_algo == NULL) |
| goto fail; |
| |
| /* HTTP < 1.1 should not be compressed */ |
| if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11)) |
| goto fail; |
| |
| /* 200 only */ |
| if (txn->status != 200) |
| goto fail; |
| |
| /* Content-Length is null */ |
| if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| goto fail; |
| |
| /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */ |
| if (msg->flags & HTTP_MSGF_TE_CHNK) |
| goto fail; |
| |
| /* content is already compressed */ |
| ctx.idx = 0; |
| if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| goto fail; |
| |
| /* no compression when Cache-Control: no-transform is present in the message */ |
| ctx.idx = 0; |
| while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) { |
| if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) |
| goto fail; |
| } |
| |
| comp_type = NULL; |
| |
| /* we don't want to compress multipart content-types, nor content-types that are |
| * not listed in the "compression type" directive if any. If no content-type was |
| * found but configuration requires one, we don't compress either. Backend has |
| * the priority. |
| */ |
| ctx.idx = 0; |
| if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| goto fail; |
| |
| if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| (s->fe->comp && (comp_type = s->fe->comp->types))) { |
| for (; comp_type; comp_type = comp_type->next) { |
| if (ctx.vlen >= comp_type->name_len && |
| strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) |
| /* this Content-Type should be compressed */ |
| break; |
| } |
| /* this Content-Type should not be compressed */ |
| if (comp_type == NULL) |
| goto fail; |
| } |
| } |
| else { /* no content-type header */ |
| if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| goto fail; /* a content-type was required */ |
| } |
| |
| /* limit compression rate */ |
| if (global.comp_rate_lim > 0) |
| if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| goto fail; |
| |
| /* limit cpu usage */ |
| if (idle_pct < compress_min_idle) |
| goto fail; |
| |
| /* initialize compression */ |
| if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0) |
| goto fail; |
| |
| s->flags |= SN_COMP_READY; |
| |
| /* remove Content-Length header */ |
| ctx.idx = 0; |
| if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| |
| /* add Transfer-Encoding header */ |
| if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| |
| /* |
| * Add Content-Encoding header when it's not identity encoding. |
| * RFC 2616 : Identity encoding: This content-coding is used only in the |
| * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| * header. |
| */ |
| if (s->comp_algo->add_data != identity_add_data) { |
| trash.len = 18; |
| memcpy(trash.str, "Content-Encoding: ", trash.len); |
| memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| trash.len += s->comp_algo->name_len; |
| trash.str[trash.len] = '\0'; |
| http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len); |
| } |
| return 1; |
| |
| fail: |
| s->comp_algo = NULL; |
| return 0; |
| } |
| |
| |
| /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| * processing can continue on next analysers, or zero if it either needs more |
| * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| * when it has nothing left to do, and may remove any analyser when it wants to |
| * abort. |
| */ |
| int http_wait_for_request(struct session *s, struct channel *req, int an_bit) |
| { |
| /* |
| * We will parse the partial (or complete) lines. |
| * We will check the request syntax, and also join multi-line |
| * headers. An index of all the lines will be elaborated while |
| * parsing. |
| * |
| * For the parsing, we use a 28 states FSM. |
| * |
| * Here is the information we currently have : |
| * req->buf->p = beginning of request |
| * req->buf->p + msg->eoh = end of processed headers / start of current one |
| * req->buf->p + req->buf->i = end of input data |
| * msg->eol = end of current header or line (LF or CRLF) |
| * msg->next = first non-visited byte |
| * |
| * At end of parsing, we may perform a capture of the error (if any), and |
| * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| * finally headers capture. |
| */ |
| |
| int cur_idx; |
| int use_close_only; |
| struct http_txn *txn = &s->txn; |
| struct http_msg *msg = &txn->req; |
| struct hdr_ctx ctx; |
| |
| DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
| now_ms, __FUNCTION__, |
| s, |
| req, |
| req->rex, req->wex, |
| req->flags, |
| req->buf->i, |
| req->analysers); |
| |
| /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| s->srv_error = http_return_srv_error; |
| |
| /* There's a protected area at the end of the buffer for rewriting |
| * purposes. We don't want to start to parse the request if the |
| * protected area is affected, because we may have to move processed |
| * data later, which is much more complicated. |
| */ |
| if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
| if (txn->flags & TX_NOT_FIRST) { |
| if (unlikely(!channel_reserved(req))) { |
| if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| goto failed_keep_alive; |
| /* some data has still not left the buffer, wake us once that's done */ |
| channel_dont_connect(req); |
| req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| req->flags |= CF_WAKE_WRITE; |
| return 0; |
| } |
| if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) |
| buffer_slow_realign(req->buf); |
| } |
| |
| /* Note that we have the same problem with the response ; we |
| * may want to send a redirect, error or anything which requires |
| * some spare space. So we'll ensure that we have at least |
| * maxrewrite bytes available in the response buffer before |
| * processing that one. This will only affect pipelined |
| * keep-alive requests. |
| */ |
| if ((txn->flags & TX_NOT_FIRST) && |
| unlikely(!channel_reserved(s->rep) || |
| bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| if (s->rep->buf->o) { |
| if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| goto failed_keep_alive; |
| /* don't let a connection request be initiated */ |
| channel_dont_connect(req); |
| s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| s->rep->flags |= CF_WAKE_WRITE; |
| s->rep->analysers |= an_bit; /* wake us up once it changes */ |
| return 0; |
| } |
| } |
| |
| if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
| http_msg_analyzer(msg, &txn->hdr_idx); |
| } |
| |
| /* 1: we might have to print this header in debug mode */ |
| if (unlikely((global.mode & MODE_DEBUG) && |
| (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| char *eol, *sol; |
| |
| sol = req->buf->p; |
| /* this is a bit complex : in case of error on the request line, |
| * we know that rq.l is still zero, so we display only the part |
| * up to the end of the line (truncated by debug_hdr). |
| */ |
| eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i); |
| debug_hdr("clireq", s, sol, eol); |
| |
| sol += hdr_idx_first_pos(&txn->hdr_idx); |
| cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| |
| while (cur_idx) { |
| eol = sol + txn->hdr_idx.v[cur_idx].len; |
| debug_hdr("clihdr", s, sol, eol); |
| sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| cur_idx = txn->hdr_idx.v[cur_idx].next; |
| } |
| } |
| |
| |
| /* |
| * Now we quickly check if we have found a full valid request. |
| * If not so, we check the FD and buffer states before leaving. |
| * A full request is indicated by the fact that we have seen |
| * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| * requests are checked first. When waiting for a second request |
| * on a keep-alive session, if we encounter and error, close, t/o, |
| * we note the error in the session flags but don't set any state. |
| * Since the error will be noted there, it will not be counted by |
| * process_session() as a frontend error. |
| * Last, we may increase some tracked counters' http request errors on |
| * the cases that are deliberately the client's fault. For instance, |
| * a timeout or connection reset is not counted as an error. However |
| * a bad request is. |
| */ |
| |
| if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| /* |
| * First, let's catch bad requests. |
| */ |
| if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| session_inc_http_req_ctr(s); |
| session_inc_http_err_ctr(s); |
| proxy_inc_fe_req_ctr(s->fe); |
| goto return_bad_req; |
| } |
| |
| /* 1: Since we are in header mode, if there's no space |
| * left for headers, we won't be able to free more |
| * later, so the session will never terminate. We |
| * must terminate it now. |
| */ |
| if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
| /* FIXME: check if URI is set and return Status |
| * 414 Request URI too long instead. |
| */ |
| session_inc_http_req_ctr(s); |
| session_inc_http_err_ctr(s); |
| proxy_inc_fe_req_ctr(s->fe); |
| if (msg->err_pos < 0) |
| msg->err_pos = req->buf->i; |
| goto return_bad_req; |
| } |
| |
| /* 2: have we encountered a read error ? */ |
| else if (req->flags & CF_READ_ERROR) { |
| if (!(s->flags & SN_ERR_MASK)) |
| s->flags |= SN_ERR_CLICL; |
| |
| if (txn->flags & TX_WAIT_NEXT_RQ) |
| goto failed_keep_alive; |
| |
| /* we cannot return any message on error */ |
| if (msg->err_pos >= 0) { |
| http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
| session_inc_http_err_ctr(s); |
| } |
| |
| txn->status = 400; |
| stream_int_retnclose(req->prod, NULL); |
| msg->msg_state = HTTP_MSG_ERROR; |
| req->analysers = 0; |
| |
| session_inc_http_req_ctr(s); |
| proxy_inc_fe_req_ctr(s->fe); |
| s->fe->fe_counters.failed_req++; |
| if (s->listener->counters) |
| s->listener->counters->failed_req++; |
| |
| if (!(s->flags & SN_FINST_MASK)) |
| s->flags |= SN_FINST_R; |
| return 0; |
| } |
| |
| /* 3: has the read timeout expired ? */ |
| else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
| if (!(s->flags & SN_ERR_MASK)) |
| s->flags |= SN_ERR_CLITO; |
| |
| if (txn->flags & TX_WAIT_NEXT_RQ) |
| goto failed_keep_alive; |
| |
| /* read timeout : give up with an error message. */ |
| if (msg->err_pos >= 0) { |
| http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
| session_inc_http_err_ctr(s); |
| } |
| txn->status = 408; |
| stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
| msg->msg_state = HTTP_MSG_ERROR; |
| req->analysers = 0; |
| |
| session_inc_http_req_ctr(s); |
| proxy_inc_fe_req_ctr(s->fe); |
| s->fe->fe_counters.failed_req++; |
| if (s->listener->counters) |
| s->listener->counters->failed_req++; |
| |
| if (!(s->flags & SN_FINST_MASK)) |
| s->flags |= SN_FINST_R; |
| return 0; |
| } |
| |
| /* 4: have we encountered a close ? */ |
| else if (req->flags & CF_SHUTR) { |
| if (!(s->flags & SN_ERR_MASK)) |
| s->flags |= SN_ERR_CLICL; |
| |
| if (txn->flags & TX_WAIT_NEXT_RQ) |
| goto failed_keep_alive; |
| |
| if (msg->err_pos >= 0) |
| http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
| txn->status = 400; |
| stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
| msg->msg_state = HTTP_MSG_ERROR; |
| req->analysers = 0; |
| |
| session_inc_http_err_ctr(s); |
| session_inc_http_req_ctr(s); |
| proxy_inc_fe_req_ctr(s->fe); |
| s->fe->fe_counters.failed_req++; |
| if (s->listener->counters) |
| s->listener->counters->failed_req++; |
| |
| if (!(s->flags & SN_FINST_MASK)) |
| s->flags |= SN_FINST_R; |
| return 0; |
| } |
| |
| channel_dont_connect(req); |
| req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| #ifdef TCP_QUICKACK |
| if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) { |
| /* We need more data, we have to re-enable quick-ack in case we |
| * previously disabled it, otherwise we might cause the client |
| * to delay next data. |
| */ |
| setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| } |
| #endif |
| |
| if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| /* If the client starts to talk, let's fall back to |
| * request timeout processing. |
| */ |
| txn->flags &= ~TX_WAIT_NEXT_RQ; |
| req->analyse_exp = TICK_ETERNITY; |
| } |
| |
| /* just set the request timeout once at the beginning of the request */ |
| if (!tick_isset(req->analyse_exp)) { |
| if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| (txn->flags & TX_WAIT_NEXT_RQ) && |
| tick_isset(s->be->timeout.httpka)) |
| req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| else |
| req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| } |
| |
| /* we're not ready yet */ |
| return 0; |
| |
| failed_keep_alive: |
| /* Here we process low-level errors for keep-alive requests. In |
| * short, if the request is not the first one and it experiences |
| * a timeout, read error or shutdown, we just silently close so |
| * that the client can try again. |
| */ |
| txn->status = 0; |
| msg->msg_state = HTTP_MSG_RQBEFORE; |
| req->analysers = 0; |
| s->logs.logwait = 0; |
| s->logs.level = 0; |
| s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| stream_int_retnclose(req->prod, NULL); |
| return 0; |
| } |
| |
| /* OK now we have a complete HTTP request with indexed headers. Let's |
| * complete the request parsing by setting a few fields we will need |
| * later. At this point, we have the last CRLF at req->buf->data + msg->eoh. |
| * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| * points to the CRLF of the request line. msg->next points to the first |
| * byte after the last LF. msg->sov points to the first byte of data. |
| * msg->eol cannot be trusted because it may have been left uninitialized |
| * (for instance in the absence of headers). |
| */ |
| |
| session_inc_http_req_ctr(s); |
| proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| |
| if (txn->flags & TX_WAIT_NEXT_RQ) { |
| /* kill the pending keep-alive timeout */ |
| txn->flags &= ~TX_WAIT_NEXT_RQ; |
| req->analyse_exp = TICK_ETERNITY; |
| } |
| |
| |
| /* Maybe we found in invalid header name while we were configured not |
| * to block on that, so we have to capture it now. |
| */ |
| if (unlikely(msg->err_pos >= 0)) |
| http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
| |
| /* |
| * 1: identify the method |
| */ |
| txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l); |
| |
| /* we can make use of server redirect on GET and HEAD */ |
| if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| s->flags |= SN_REDIRECTABLE; |
| |
| /* |
| * 2: check if the URI matches the monitor_uri. |
| * We have to do this for every request which gets in, because |
| * the monitor-uri is defined by the frontend. |
| */ |
| if (unlikely((s->fe->monitor_uri_len != 0) && |
| (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| !memcmp(req->buf->p + msg->sl.rq.u, |
| s->fe->monitor_uri, |
| s->fe->monitor_uri_len))) { |
| /* |
| * We have found the monitor URI |
| */ |
| struct acl_cond *cond; |
| |
| s->flags |= SN_MONITOR; |
| s->fe->fe_counters.intercepted_req++; |
| |
| /* Check if we want to fail this monitor request or not */ |
| list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| |
| ret = acl_pass(ret); |
| if (cond->pol == ACL_COND_UNLESS) |
| ret = !ret; |
| |
| if (ret) { |
| /* we fail this request, let's return 503 service unavail */ |
| txn->status = 503; |
| stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
| if (!(s->flags & SN_ERR_MASK)) |
| s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
| goto return_prx_cond; |
| } |
| } |
| |
| /* nothing to fail, let's reply normaly */ |
| txn->status = 200; |
| stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
| if (!(s->flags & SN_ERR_MASK)) |
| s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */ |
| goto return_prx_cond; |
| } |
| |
| /* |
| * 3: Maybe we have to copy the original REQURI for the logs ? |
| * Note: we cannot log anymore if the request has been |
| * classified as invalid. |
| */ |
| if (unlikely(s->logs.logwait & LW_REQ)) { |
| /* we have a complete HTTP request that we must log */ |
| if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| int urilen = msg->sl.rq.l; |
| |
| if (urilen >= REQURI_LEN) |
| urilen = REQURI_LEN - 1; |
| memcpy(txn->uri, req->buf->p, urilen); |
| txn->uri[urilen] = 0; |
| |
| if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| s->do_log(s); |
| } else { |
| Alert("HTTP logging : out of memory.\n"); |
| } |
| } |
| |
| /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| goto return_bad_req; |
| |
| /* ... and check if the request is HTTP/1.1 or above */ |
| if ((msg->sl.rq.v_l == 8) && |
| ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
| msg->flags |= HTTP_MSGF_VER_11; |
| |
| /* "connection" has not been parsed yet */ |
| txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG); |
| |
| /* if the frontend has "option http-use-proxy-header", we'll check if |
| * we have what looks like a proxied connection instead of a connection, |
| * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| * Note that this is *not* RFC-compliant, however browsers and proxies |
| * happen to do that despite being non-standard :-( |
| * We consider that a request not beginning with either '/' or '*' is |
| * a proxied connection, which covers both "scheme://location" and |
| * CONNECT ip:port. |
| */ |
| if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*') |
| txn->flags |= TX_USE_PX_CONN; |
| |
| /* transfer length unknown*/ |
| msg->flags &= ~HTTP_MSGF_XFER_LEN; |
| |
| /* 5: we may need to capture headers */ |
| if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
| capture_headers(req->buf->p, &txn->hdr_idx, |
| txn->req.cap, s->fe->req_cap); |
| |
| /* 6: determine the transfer-length. |
| * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| * the presence of a message-body in a REQUEST and its transfer length |
| * must be determined that way (in order of precedence) : |
| * 1. The presence of a message-body in a request is signaled by the |
| * inclusion of a Content-Length or Transfer-Encoding header field |
| * in the request's header fields. When a request message contains |
| * both a message-body of non-zero length and a method that does |
| * not define any semantics for that request message-body, then an |
| * origin server SHOULD either ignore the message-body or respond |
| * with an appropriate error message (e.g., 413). A proxy or |
| * gateway, when presented the same request, SHOULD either forward |
| * the request inbound with the message- body or ignore the |
| * message-body when determining a response. |
| * |
| * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| * and the "chunked" transfer-coding (Section 6.2) is used, the |
| * transfer-length is defined by the use of this transfer-coding. |
| * If a Transfer-Encoding header field is present and the "chunked" |
| * transfer-coding is not present, the transfer-length is defined |
| * by the sender closing the connection. |
| * |
| * 3. If a Content-Length header field is present, its decimal value in |
| * OCTETs represents both the entity-length and the transfer-length. |
| * If a message is received with both a Transfer-Encoding header |
| * field and a Content-Length header field, the latter MUST be ignored. |
| * |
| * 4. By the server closing the connection. (Closing the connection |
| * cannot be used to indicate the end of a request body, since that |
| * would leave no possibility for the server to send back a response.) |
| * |
| * Whenever a transfer-coding is applied to a message-body, the set of |
| * transfer-codings MUST include "chunked", unless the message indicates |
| * it is terminated by closing the connection. When the "chunked" |
| * transfer-coding is used, it MUST be the last transfer-coding applied |
| * to the message-body. |
| */ |
| |
| use_close_only = 0; |
| ctx.idx = 0; |
| /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
| while ((msg->flags & HTTP_MSGF_VER_11) && |
| http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) { |
| if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
| /* bad transfer-encoding (chunked followed by something else) */ |
| use_close_only = 1; |
| msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| break; |
| } |
| } |
| |
| ctx.idx = 0; |
| while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
| http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) { |
| signed long long cl; |
| |
| if (!ctx.vlen) { |
| msg->err_pos = ctx.line + ctx.val - req->buf->p; |
| goto return_bad_req; |
| } |
| |
| if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| msg->err_pos = ctx.line + ctx.val - req->buf->p; |
| goto return_bad_req; /* parse failure */ |
| } |
| |
| if (cl < 0) { |
| msg->err_pos = ctx.line + ctx.val - req->buf->p; |
| goto return_bad_req; |
| } |
| |
| if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
| msg->err_pos = ctx.line + ctx.val - req->buf->p; |
| goto return_bad_req; /* already specified, was different */ |
| } |
| |
| msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
| msg->body_len = msg->chunk_len = cl; |
| } |
| |
| /* bodyless requests have a known length */ |
| if (!use_close_only) |
| msg->flags |= HTTP_MSGF_XFER_LEN; |
| |
| /* end of job, return OK */ |
| req->analysers &= ~an_bit; |
| req->analyse_exp = TICK_ETERNITY; |
| return 1; |
| |
| return_bad_req: |
| /* We centralize bad requests processing here */ |
| if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| /* we detected a parsing error. We want to archive this request |
| * in the dedicated proxy area for later troubleshooting. |
| */ |
| http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
| } |
| |
| txn->req.msg_state = HTTP_MSG_ERROR; |
| txn->status = 400; |
| stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
| |
| s->fe->fe_counters.failed_req++; |
| if (s->listener->counters) |
| s->listener->counters->failed_req++; |
| |
| return_prx_cond: |
| if (!(s->flags & SN_ERR_MASK)) |
| s->flags |= SN_ERR_PRXCOND; |
| if (!(s->flags & SN_FINST_MASK)) |
| s->flags |= SN_FINST_R; |
| |
| req->analysers = 0; |
| req->analyse_exp = TICK_ETERNITY; |
| return 0; |
| } |
| |
| |
| /* This function prepares an applet to handle the stats. It can deal with the |
| * "100-continue" expectation, check that admin rules are met for POST requests, |
| * and program a response message if something was unexpected. It cannot fail |
| * and always relies on the stats applet to complete the job. It does not touch |
| * analysers nor counters, which are left to the caller. It does not touch |
| * s->target which is supposed to already point to the stats applet. The caller |
| * is expected to have already assigned an appctx to the session. |
| */ |
| int http_handle_stats(struct session *s, struct channel *req) |
| { |
| struct stats_admin_rule *stats_admin_rule; |
| struct stream_interface *si = s->rep->prod; |
| struct http_txn *txn = &s->txn; |
| struct http_msg *msg = &txn->req; |
|