blob: 0173b7acb479873ea4cc324a23bdbbdc286fed5a [file] [log] [blame]
Willy Tarreau35b51c62018-09-10 15:38:55 +02001/*
2 * include/common/http.h
3 *
4 * Version-agnostic and implementation-agnostic HTTP protocol definitions.
5 *
6 * Copyright (C) 2000-2018 Willy Tarreau - w@1wt.eu
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation, version 2.1
11 * exclusively.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef _COMMON_HTTP_H
24#define _COMMON_HTTP_H
25
Willy Tarreau8dabda72020-05-27 17:22:10 +020026#include <haproxy/buf-t.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020027#include <import/ist.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020028
Christopher Faulet7e266c72018-10-03 14:15:28 +020029/*
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050030 * some macros mainly used when parsing header fields.
Christopher Faulet7e266c72018-10-03 14:15:28 +020031 * from RFC7230:
32 * CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
33 * SEP = one of the 17 defined separators or SP or HT
34 * LWS = CR, LF, SP or HT
35 * SPHT = SP or HT. Use this macro and not a boolean expression for best speed.
36 * CRLF = CR or LF. Use this macro and not a boolean expression for best speed.
37 * token = any CHAR except CTL or SEP. Use this macro and not a boolean expression for best speed.
38 *
39 * added for ease of use:
40 * ver_token = 'H', 'P', 'T', '/', '.', and digits.
41 */
Willy Tarreau35b51c62018-09-10 15:38:55 +020042#define HTTP_FLG_CTL 0x01
43#define HTTP_FLG_SEP 0x02
44#define HTTP_FLG_LWS 0x04
45#define HTTP_FLG_SPHT 0x08
46#define HTTP_FLG_CRLF 0x10
47#define HTTP_FLG_TOK 0x20
48#define HTTP_FLG_VER 0x40
49#define HTTP_FLG_DIG 0x80
50
51#define HTTP_IS_CTL(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_CTL)
52#define HTTP_IS_SEP(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_SEP)
53#define HTTP_IS_LWS(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_LWS)
54#define HTTP_IS_SPHT(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_SPHT)
55#define HTTP_IS_CRLF(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_CRLF)
56#define HTTP_IS_TOKEN(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_TOK)
57#define HTTP_IS_VER_TOKEN(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_VER)
58#define HTTP_IS_DIGIT(x) (http_char_classes[(uint8_t)(x)] & HTTP_FLG_DIG)
59
60/* Known HTTP methods */
61enum http_meth_t {
62 HTTP_METH_OPTIONS,
63 HTTP_METH_GET,
64 HTTP_METH_HEAD,
65 HTTP_METH_POST,
66 HTTP_METH_PUT,
67 HTTP_METH_DELETE,
68 HTTP_METH_TRACE,
69 HTTP_METH_CONNECT,
70 HTTP_METH_OTHER, /* Must be the last entry */
71} __attribute__((packed));
72
73/* Known HTTP authentication schemes */
74enum ht_auth_m {
75 HTTP_AUTH_WRONG = -1, /* missing or unknown */
76 HTTP_AUTH_UNKNOWN = 0,
77 HTTP_AUTH_BASIC,
78 HTTP_AUTH_DIGEST,
79} __attribute__((packed));
80
81/* All implemented HTTP status codes */
82enum {
83 HTTP_ERR_200 = 0,
84 HTTP_ERR_400,
Christopher Faulet612f2ea2020-05-27 09:57:28 +020085 HTTP_ERR_401,
Willy Tarreau35b51c62018-09-10 15:38:55 +020086 HTTP_ERR_403,
Florian Tham9205fea2020-01-08 13:35:30 +010087 HTTP_ERR_404,
Willy Tarreau35b51c62018-09-10 15:38:55 +020088 HTTP_ERR_405,
Christopher Faulet612f2ea2020-05-27 09:57:28 +020089 HTTP_ERR_407,
Willy Tarreau35b51c62018-09-10 15:38:55 +020090 HTTP_ERR_408,
Florian Tham272e29b2020-01-08 10:19:05 +010091 HTTP_ERR_410,
Willy Tarreau35b51c62018-09-10 15:38:55 +020092 HTTP_ERR_421,
93 HTTP_ERR_425,
94 HTTP_ERR_429,
95 HTTP_ERR_500,
96 HTTP_ERR_502,
97 HTTP_ERR_503,
98 HTTP_ERR_504,
99 HTTP_ERR_SIZE
100};
101
102/* Note: the strings below make use of chunks. Chunks may carry an allocated
103 * size in addition to the length. The size counts from the beginning (str)
104 * to the end. If the size is unknown, it MUST be zero, in which case the
105 * sample will automatically be duplicated when a change larger than <len> has
106 * to be performed. Thus it is safe to always set size to zero.
107 */
108struct http_meth {
109 enum http_meth_t meth;
110 struct buffer str;
111};
112
113struct http_auth_data {
114 enum ht_auth_m method; /* one of HTTP_AUTH_* */
115 /* 7 bytes unused here */
116 struct buffer method_data; /* points to the creditial part from 'Authorization:' header */
117 char *user, *pass; /* extracted username & password */
118};
119
120struct http_method_desc {
121 enum http_meth_t meth;
122 const struct ist text;
123};
124
125extern const int http_err_codes[HTTP_ERR_SIZE];
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100126extern const char *http_err_msgs[HTTP_ERR_SIZE];
Willy Tarreaue01d11a2019-03-29 17:52:50 +0100127extern const struct ist http_known_methods[HTTP_METH_OTHER];
Willy Tarreau35b51c62018-09-10 15:38:55 +0200128extern const uint8_t http_char_classes[256];
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200129
Willy Tarreau35b51c62018-09-10 15:38:55 +0200130enum http_meth_t find_http_meth(const char *str, const int len);
Willy Tarreau8de1df92019-04-15 21:27:18 +0200131int http_get_status_idx(unsigned int status);
Willy Tarreau04f1e2d2018-09-10 18:04:24 +0200132const char *http_get_reason(unsigned int status);
Christopher Faulet16fdc552019-10-08 14:56:58 +0200133struct ist http_get_authority(const struct ist uri, int no_userinfo);
Willy Tarreau6b952c82018-09-10 17:45:34 +0200134struct ist http_get_path(const struct ist uri);
Willy Tarreauab813a42018-09-10 18:41:28 +0200135int http_header_match2(const char *hdr, const char *end,
136 const char *name, int len);
137char *http_find_hdr_value_end(char *s, const char *e);
138char *http_find_cookie_value_end(char *s, const char *e);
139char *http_extract_cookie_value(char *hdr, const char *hdr_end,
140 char *cookie_name, size_t cookie_name_l,
141 int list, char **value, size_t *value_l);
142int http_parse_qvalue(const char *qvalue, const char **end);
143const char *http_find_url_param_pos(const char **chunks,
144 const char* url_param_name,
145 size_t url_param_name_l, char delim);
146int http_find_next_url_param(const char **chunks,
147 const char* url_param_name, size_t url_param_name_l,
148 const char **vstart, const char **vend, char delim);
149
Christopher Faulet8277ca72018-10-22 15:12:04 +0200150int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value);
151int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3);
Christopher Faulet341fac12019-09-16 11:37:05 +0200152int http_parse_status_val(const struct ist value, struct ist *status, struct ist *reason);
Christopher Faulet8277ca72018-10-22 15:12:04 +0200153
Willy Tarreauab813a42018-09-10 18:41:28 +0200154/*
155 * Given a path string and its length, find the position of beginning of the
156 * query string. Returns NULL if no query string is found in the path.
157 *
158 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
159 *
160 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
161 */
162static inline char *http_find_param_list(char *path, size_t path_l, char delim)
163{
164 char *p;
165
166 p = memchr(path, delim, path_l);
167 return p ? p + 1 : NULL;
168}
169
170static inline int http_is_param_delimiter(char c, char delim)
171{
172 return c == '&' || c == ';' || c == delim;
173}
174
Willy Tarreau79e57332018-10-02 16:01:16 +0200175/* Match language range with language tag. RFC2616 14.4:
176 *
177 * A language-range matches a language-tag if it exactly equals
178 * the tag, or if it exactly equals a prefix of the tag such
179 * that the first tag character following the prefix is "-".
180 *
181 * Return 1 if the strings match, else return 0.
182 */
183static inline int http_language_range_match(const char *range, int range_len,
184 const char *tag, int tag_len)
185{
186 const char *end = range + range_len;
187 const char *tend = tag + tag_len;
188
189 while (range < end) {
190 if (*range == '-' && tag == tend)
191 return 1;
192 if (*range != *tag || tag == tend)
193 return 0;
194 range++;
195 tag++;
196 }
197 /* Return true only if the last char of the tag is matched. */
198 return tag == tend;
199}
200
Willy Tarreau35b51c62018-09-10 15:38:55 +0200201
202#endif /* _COMMON_HTTP_H */
203
204/*
205 * Local variables:
206 * c-indent-level: 8
207 * c-basic-offset: 8
208 * End:
209 */