Coverage Report

Created: 2025-11-24 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/haproxy/include/haproxy/http.h
Line
Count
Source
1
/*
2
 * include/haproxy/http.h
3
 *
4
 * Functions for version-agnostic and implementation-agnostic HTTP protocol.
5
 *
6
 * Copyright (C) 2000-2020 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 _HAPROXY_HTTP_H
24
#define _HAPROXY_HTTP_H
25
26
#include <string.h>
27
#include <import/ist.h>
28
#include <haproxy/api.h>
29
#include <haproxy/http-t.h>
30
#include <haproxy/intops.h>
31
32
extern const int http_err_codes[HTTP_ERR_SIZE];
33
extern const char *http_err_msgs[HTTP_ERR_SIZE];
34
extern const struct ist http_known_methods[HTTP_METH_OTHER];
35
extern const uint8_t http_char_classes[256];
36
extern long http_err_status_codes[512 / sizeof(long)];
37
extern long http_fail_status_codes[512 / sizeof(long)];
38
39
enum http_meth_t find_http_meth(const char *str, const int len);
40
int http_get_status_idx(unsigned int status);
41
const char *http_get_reason(unsigned int status);
42
void http_status_add_range(long *array, uint low, uint high);
43
void http_status_del_range(long *array, uint low, uint high);
44
struct ist http_get_host_port(const struct ist host);
45
int http_is_default_port(const struct ist schm, const struct ist port);
46
int http_validate_scheme(const struct ist schm);
47
struct ist http_parse_scheme(struct http_uri_parser *parser);
48
struct ist http_parse_authority(struct http_uri_parser *parser, int no_userinfo);
49
struct ist http_parse_path(struct http_uri_parser *parser);
50
int http_parse_cont_len_header(struct ist *value, unsigned long long *body_len,
51
                               int not_first);
52
int http_header_match2(const char *hdr, const char *end,
53
                       const char *name, int len);
54
char *http_find_hdr_value_end(char *s, const char *e);
55
char *http_find_cookie_value_end(char *s, const char *e);
56
char *http_extract_cookie_value(char *hdr, const char *hdr_end,
57
                                char *cookie_name, size_t cookie_name_l,
58
                                int list, char **value, size_t *value_l);
59
char *http_extract_next_cookie_name(char *hdr_beg, char *hdr_end, int is_req,
60
                                    char **ptr, size_t *len);
61
int http_parse_qvalue(const char *qvalue, const char **end);
62
const char *http_find_url_param_pos(const char **chunks,
63
                                    const char* url_param_name,
64
                                    size_t url_param_name_l, char delim, char insensitive);
65
int http_find_next_url_param(const char **chunks,
66
                             const char* url_param_name, size_t url_param_name_l,
67
                             const char **vstart, const char **vend, char delim, char insensitive);
68
69
int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value);
70
int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3);
71
int http_parse_status_val(const struct ist value, struct ist *status, struct ist *reason);
72
73
int http_compare_etags(struct ist etag1, struct ist etag2);
74
75
struct ist http_trim_leading_spht(struct ist value);
76
struct ist http_trim_trailing_spht(struct ist value);
77
78
/*
79
 * Given a path string and its length, find the position of beginning of the
80
 * query string. Returns NULL if no query string is found in the path.
81
 *
82
 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
83
 *
84
 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
85
 */
86
static inline char *http_find_param_list(char *path, size_t path_l, char delim)
87
0
{
88
0
  char *p;
89
90
0
  p = memchr(path, delim, path_l);
91
0
  return p ? p + 1 : NULL;
92
0
}
Unexecuted instantiation: flt_http_comp.c:http_find_param_list
Unexecuted instantiation: http.c:http_find_param_list
Unexecuted instantiation: http_ana.c:http_find_param_list
Unexecuted instantiation: http_htx.c:http_find_param_list
Unexecuted instantiation: http_rules.c:http_find_param_list
Unexecuted instantiation: log.c:http_find_param_list
Unexecuted instantiation: sample.c:http_find_param_list
Unexecuted instantiation: session.c:http_find_param_list
Unexecuted instantiation: stats-html.c:http_find_param_list
Unexecuted instantiation: stats.c:http_find_param_list
Unexecuted instantiation: tcpcheck.c:http_find_param_list
Unexecuted instantiation: vars.c:http_find_param_list
Unexecuted instantiation: backend.c:http_find_param_list
Unexecuted instantiation: cache.c:http_find_param_list
Unexecuted instantiation: check.c:http_find_param_list
Unexecuted instantiation: h1.c:http_find_param_list
Unexecuted instantiation: http_fetch.c:http_find_param_list
Unexecuted instantiation: h1_htx.c:http_find_param_list
93
94
static inline int http_is_param_delimiter(char c, char delim)
95
0
{
96
0
  return c == '&' || c == ';' || c == delim;
97
0
}
Unexecuted instantiation: flt_http_comp.c:http_is_param_delimiter
Unexecuted instantiation: http.c:http_is_param_delimiter
Unexecuted instantiation: http_ana.c:http_is_param_delimiter
Unexecuted instantiation: http_htx.c:http_is_param_delimiter
Unexecuted instantiation: http_rules.c:http_is_param_delimiter
Unexecuted instantiation: log.c:http_is_param_delimiter
Unexecuted instantiation: sample.c:http_is_param_delimiter
Unexecuted instantiation: session.c:http_is_param_delimiter
Unexecuted instantiation: stats-html.c:http_is_param_delimiter
Unexecuted instantiation: stats.c:http_is_param_delimiter
Unexecuted instantiation: tcpcheck.c:http_is_param_delimiter
Unexecuted instantiation: vars.c:http_is_param_delimiter
Unexecuted instantiation: backend.c:http_is_param_delimiter
Unexecuted instantiation: cache.c:http_is_param_delimiter
Unexecuted instantiation: check.c:http_is_param_delimiter
Unexecuted instantiation: h1.c:http_is_param_delimiter
Unexecuted instantiation: http_fetch.c:http_is_param_delimiter
Unexecuted instantiation: h1_htx.c:http_is_param_delimiter
98
99
/* Match language range with language tag. RFC2616 14.4:
100
 *
101
 *    A language-range matches a language-tag if it exactly equals
102
 *    the tag, or if it exactly equals a prefix of the tag such
103
 *    that the first tag character following the prefix is "-".
104
 *
105
 * Return 1 if the strings match, else return 0.
106
 */
107
static inline int http_language_range_match(const char *range, int range_len,
108
                                            const char *tag, int tag_len)
109
0
{
110
0
  const char *end = range + range_len;
111
0
  const char *tend = tag + tag_len;
112
0
113
0
  while (range < end) {
114
0
    if (*range == '-' && tag == tend)
115
0
      return 1;
116
0
    if (*range != *tag || tag == tend)
117
0
      return 0;
118
0
    range++;
119
0
    tag++;
120
0
  }
121
0
  /* Return true only if the last char of the tag is matched. */
122
0
  return tag == tend;
123
0
}
Unexecuted instantiation: flt_http_comp.c:http_language_range_match
Unexecuted instantiation: http.c:http_language_range_match
Unexecuted instantiation: http_ana.c:http_language_range_match
Unexecuted instantiation: http_htx.c:http_language_range_match
Unexecuted instantiation: http_rules.c:http_language_range_match
Unexecuted instantiation: log.c:http_language_range_match
Unexecuted instantiation: sample.c:http_language_range_match
Unexecuted instantiation: session.c:http_language_range_match
Unexecuted instantiation: stats-html.c:http_language_range_match
Unexecuted instantiation: stats.c:http_language_range_match
Unexecuted instantiation: tcpcheck.c:http_language_range_match
Unexecuted instantiation: vars.c:http_language_range_match
Unexecuted instantiation: backend.c:http_language_range_match
Unexecuted instantiation: cache.c:http_language_range_match
Unexecuted instantiation: check.c:http_language_range_match
Unexecuted instantiation: h1.c:http_language_range_match
Unexecuted instantiation: http_fetch.c:http_language_range_match
Unexecuted instantiation: h1_htx.c:http_language_range_match
124
125
static inline enum http_etag_type http_get_etag_type(const struct ist etag)
126
0
{
127
  /* An ETag must be at least 2 characters. */
128
0
  if (etag.len < 2)
129
0
    return ETAG_INVALID;
130
131
  /* The last character must be a `"`. */
132
0
  if (etag.ptr[etag.len - 1] != '"')
133
0
    return ETAG_INVALID;
134
135
  /* If the ETag starts with a `"` then it is a strong ETag. */
136
0
  if (etag.ptr[0] == '"')
137
0
    return ETAG_STRONG;
138
139
  /* If the ETag starts with `W/"` then it is a weak ETag. */
140
0
  if (istnmatch(etag, ist("W/\""), 3))
141
0
    return ETAG_WEAK;
142
143
0
  return ETAG_INVALID;
144
0
}
Unexecuted instantiation: flt_http_comp.c:http_get_etag_type
Unexecuted instantiation: http.c:http_get_etag_type
Unexecuted instantiation: http_ana.c:http_get_etag_type
Unexecuted instantiation: http_htx.c:http_get_etag_type
Unexecuted instantiation: http_rules.c:http_get_etag_type
Unexecuted instantiation: log.c:http_get_etag_type
Unexecuted instantiation: sample.c:http_get_etag_type
Unexecuted instantiation: session.c:http_get_etag_type
Unexecuted instantiation: stats-html.c:http_get_etag_type
Unexecuted instantiation: stats.c:http_get_etag_type
Unexecuted instantiation: tcpcheck.c:http_get_etag_type
Unexecuted instantiation: vars.c:http_get_etag_type
Unexecuted instantiation: backend.c:http_get_etag_type
Unexecuted instantiation: cache.c:http_get_etag_type
Unexecuted instantiation: check.c:http_get_etag_type
Unexecuted instantiation: h1.c:http_get_etag_type
Unexecuted instantiation: http_fetch.c:http_get_etag_type
Unexecuted instantiation: h1_htx.c:http_get_etag_type
145
146
/* Initialize a HTTP URI parser to use it with http URI parsing functions. The
147
 * URI format is detected according to its first character.
148
 */
149
static inline struct http_uri_parser http_uri_parser_init(const struct ist uri)
150
0
{
151
0
  struct http_uri_parser parser = {
152
0
    .uri    = uri,
153
0
    .state  = URI_PARSER_STATE_BEFORE,
154
0
  };
155
156
  /* RFC7230, par. 2.7 :
157
   * Request-URI = "*" | absuri | abspath | authority
158
   */
159
160
0
  if (!istlen(parser.uri)) {
161
0
    parser.format = URI_PARSER_FORMAT_EMPTY;
162
0
  }
163
0
  else {
164
    /* detect the format according to the first URI character */
165
0
    switch (*istptr(parser.uri)) {
166
0
    case '*':
167
0
      parser.format = URI_PARSER_FORMAT_ASTERISK;
168
0
      break;
169
170
0
    case '/':
171
0
      parser.format = URI_PARSER_FORMAT_ABSPATH;
172
0
      break;
173
174
0
    default:
175
0
      parser.format = URI_PARSER_FORMAT_ABSURI_OR_AUTHORITY;
176
0
      break;
177
0
    }
178
0
  }
179
180
0
  return parser;
181
0
}
Unexecuted instantiation: flt_http_comp.c:http_uri_parser_init
Unexecuted instantiation: http.c:http_uri_parser_init
Unexecuted instantiation: http_ana.c:http_uri_parser_init
Unexecuted instantiation: http_htx.c:http_uri_parser_init
Unexecuted instantiation: http_rules.c:http_uri_parser_init
Unexecuted instantiation: log.c:http_uri_parser_init
Unexecuted instantiation: sample.c:http_uri_parser_init
Unexecuted instantiation: session.c:http_uri_parser_init
Unexecuted instantiation: stats-html.c:http_uri_parser_init
Unexecuted instantiation: stats.c:http_uri_parser_init
Unexecuted instantiation: tcpcheck.c:http_uri_parser_init
Unexecuted instantiation: vars.c:http_uri_parser_init
Unexecuted instantiation: backend.c:http_uri_parser_init
Unexecuted instantiation: cache.c:http_uri_parser_init
Unexecuted instantiation: check.c:http_uri_parser_init
Unexecuted instantiation: h1.c:http_uri_parser_init
Unexecuted instantiation: http_fetch.c:http_uri_parser_init
Unexecuted instantiation: h1_htx.c:http_uri_parser_init
182
183
/* Looks into <ist> for forbidden characters for header values (0x00, 0x0A,
184
 * 0x0D), starting at pointer <start> which must be within <ist>. Returns
185
 * non-zero if such a character is found, 0 otherwise. When run on unlikely
186
 * header match, it's recommended to first check for the presence of control
187
 * chars using ist_find_ctl().
188
 */
189
static inline int http_header_has_forbidden_char(const struct ist ist, const char *start)
190
0
{
191
0
  do {
192
0
    if ((uint8_t)*start <= 0x0d &&
193
0
        (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0)))
194
0
      return 1;
195
0
    start++;
196
0
  } while (start < istend(ist));
197
0
  return 0;
198
0
}
Unexecuted instantiation: flt_http_comp.c:http_header_has_forbidden_char
Unexecuted instantiation: http.c:http_header_has_forbidden_char
Unexecuted instantiation: http_ana.c:http_header_has_forbidden_char
Unexecuted instantiation: http_htx.c:http_header_has_forbidden_char
Unexecuted instantiation: http_rules.c:http_header_has_forbidden_char
Unexecuted instantiation: log.c:http_header_has_forbidden_char
Unexecuted instantiation: sample.c:http_header_has_forbidden_char
Unexecuted instantiation: session.c:http_header_has_forbidden_char
Unexecuted instantiation: stats-html.c:http_header_has_forbidden_char
Unexecuted instantiation: stats.c:http_header_has_forbidden_char
Unexecuted instantiation: tcpcheck.c:http_header_has_forbidden_char
Unexecuted instantiation: vars.c:http_header_has_forbidden_char
Unexecuted instantiation: backend.c:http_header_has_forbidden_char
Unexecuted instantiation: cache.c:http_header_has_forbidden_char
Unexecuted instantiation: check.c:http_header_has_forbidden_char
Unexecuted instantiation: h1.c:http_header_has_forbidden_char
Unexecuted instantiation: http_fetch.c:http_header_has_forbidden_char
Unexecuted instantiation: h1_htx.c:http_header_has_forbidden_char
199
200
/* Check that method only contains token as required.
201
 * See RFC 9110 9. Methods
202
 */
203
static inline int http_method_has_forbidden_char(const struct ist ist)
204
0
{
205
0
  const char *start = istptr(ist);
206
0
207
0
  do {
208
0
    if (!HTTP_IS_TOKEN(*start))
209
0
      return 1;
210
0
    start++;
211
0
  } while (start < istend(ist));
212
0
  return 0;
213
0
}
Unexecuted instantiation: flt_http_comp.c:http_method_has_forbidden_char
Unexecuted instantiation: http.c:http_method_has_forbidden_char
Unexecuted instantiation: http_ana.c:http_method_has_forbidden_char
Unexecuted instantiation: http_htx.c:http_method_has_forbidden_char
Unexecuted instantiation: http_rules.c:http_method_has_forbidden_char
Unexecuted instantiation: log.c:http_method_has_forbidden_char
Unexecuted instantiation: sample.c:http_method_has_forbidden_char
Unexecuted instantiation: session.c:http_method_has_forbidden_char
Unexecuted instantiation: stats-html.c:http_method_has_forbidden_char
Unexecuted instantiation: stats.c:http_method_has_forbidden_char
Unexecuted instantiation: tcpcheck.c:http_method_has_forbidden_char
Unexecuted instantiation: vars.c:http_method_has_forbidden_char
Unexecuted instantiation: backend.c:http_method_has_forbidden_char
Unexecuted instantiation: cache.c:http_method_has_forbidden_char
Unexecuted instantiation: check.c:http_method_has_forbidden_char
Unexecuted instantiation: h1.c:http_method_has_forbidden_char
Unexecuted instantiation: http_fetch.c:http_method_has_forbidden_char
Unexecuted instantiation: h1_htx.c:http_method_has_forbidden_char
214
215
/* Looks into <ist> for forbidden characters for :path values (0x00..0x1F,
216
 * 0x20, 0x23), starting at pointer <start> which must be within <ist>.
217
 * Returns non-zero if such a character is found, 0 otherwise. When run on
218
 * unlikely header match, it's recommended to first check for the presence
219
 * of control chars using ist_find_ctl().
220
 */
221
static inline int http_path_has_forbidden_char(const struct ist ist, const char *start)
222
0
{
223
0
  do {
224
0
    if ((uint8_t)*start <= 0x23) {
225
0
      if ((uint8_t)*start < 0x20)
226
0
        return 1;
227
0
      if ((1U << ((uint8_t)*start & 0x1F)) & ((1<<3) | (1<<0)))
228
0
        return 1;
229
0
    }
230
0
    start++;
231
0
  } while (start < istend(ist));
232
0
  return 0;
233
0
}
Unexecuted instantiation: flt_http_comp.c:http_path_has_forbidden_char
Unexecuted instantiation: http.c:http_path_has_forbidden_char
Unexecuted instantiation: http_ana.c:http_path_has_forbidden_char
Unexecuted instantiation: http_htx.c:http_path_has_forbidden_char
Unexecuted instantiation: http_rules.c:http_path_has_forbidden_char
Unexecuted instantiation: log.c:http_path_has_forbidden_char
Unexecuted instantiation: sample.c:http_path_has_forbidden_char
Unexecuted instantiation: session.c:http_path_has_forbidden_char
Unexecuted instantiation: stats-html.c:http_path_has_forbidden_char
Unexecuted instantiation: stats.c:http_path_has_forbidden_char
Unexecuted instantiation: tcpcheck.c:http_path_has_forbidden_char
Unexecuted instantiation: vars.c:http_path_has_forbidden_char
Unexecuted instantiation: backend.c:http_path_has_forbidden_char
Unexecuted instantiation: cache.c:http_path_has_forbidden_char
Unexecuted instantiation: check.c:http_path_has_forbidden_char
Unexecuted instantiation: h1.c:http_path_has_forbidden_char
Unexecuted instantiation: http_fetch.c:http_path_has_forbidden_char
Unexecuted instantiation: h1_htx.c:http_path_has_forbidden_char
234
235
/* Checks whether the :authority pseudo header contains dangerous chars that
236
 * might affect its reassembly. We want to catch anything below 0x21, above
237
 * 0x7e, as well as '@', '[', ']', '/','?', '#', '\', CR, LF, NUL. Then we
238
 * fall back to the slow path and decide. Brackets are used for IP-literal and
239
 * deserve special case, that is better handled in the slow path. The function
240
 * returns 0 if no forbidden char is presnet, non-zero otherwise.
241
 */
242
static inline int http_authority_has_forbidden_char(const struct ist ist)
243
0
{
244
0
  size_t ofs, len = istlen(ist);
245
0
  const char *p = istptr(ist);
246
0
  int brackets = 0;
247
0
  uchar c;
248
249
  /* Many attempts with various methods have shown that moderately recent
250
   * compilers (gcc >= 9, clang >= 13) will arrange the code below as an
251
   * evaluation tree that remains efficient at -O2 and above (~1.2ns per
252
   * char). The immediate next efficient one is the bitmap from 64-bit
253
   * registers but it's extremely sensitive to code arrangements and
254
   * optimization.
255
   */
256
0
  for (ofs = 0; ofs < len; ofs++) {
257
0
    c = p[ofs];
258
259
0
    if (unlikely(c < 0x21 || c > 0x7e ||
260
0
           c == '#' || c == '/' || c == '?' || c == '@' ||
261
0
           c == '[' || c == '\\' || c == ']')) {
262
      /* all of them must be rejected, except '[' which may
263
       * only appear at the beginning, and ']' which may
264
       * only appear at the end or before a colon.
265
       */
266
0
      if ((c == '[' && ofs == 0) ||
267
0
          (c == ']' && (ofs == len - 1 || p[ofs + 1] == ':'))) {
268
        /* that's an IP-literal (see RFC3986#3.2), it's
269
         * OK for now.
270
         */
271
0
        brackets ^= 1;
272
0
      } else {
273
0
        return 1;
274
0
      }
275
0
    }
276
0
  }
277
  /* there must be no opening bracket left nor lone closing one */
278
0
  return brackets;
279
0
}
Unexecuted instantiation: flt_http_comp.c:http_authority_has_forbidden_char
Unexecuted instantiation: http.c:http_authority_has_forbidden_char
Unexecuted instantiation: http_ana.c:http_authority_has_forbidden_char
Unexecuted instantiation: http_htx.c:http_authority_has_forbidden_char
Unexecuted instantiation: http_rules.c:http_authority_has_forbidden_char
Unexecuted instantiation: log.c:http_authority_has_forbidden_char
Unexecuted instantiation: sample.c:http_authority_has_forbidden_char
Unexecuted instantiation: session.c:http_authority_has_forbidden_char
Unexecuted instantiation: stats-html.c:http_authority_has_forbidden_char
Unexecuted instantiation: stats.c:http_authority_has_forbidden_char
Unexecuted instantiation: tcpcheck.c:http_authority_has_forbidden_char
Unexecuted instantiation: vars.c:http_authority_has_forbidden_char
Unexecuted instantiation: backend.c:http_authority_has_forbidden_char
Unexecuted instantiation: cache.c:http_authority_has_forbidden_char
Unexecuted instantiation: check.c:http_authority_has_forbidden_char
Unexecuted instantiation: h1.c:http_authority_has_forbidden_char
Unexecuted instantiation: http_fetch.c:http_authority_has_forbidden_char
Unexecuted instantiation: h1_htx.c:http_authority_has_forbidden_char
280
281
/* Checks status code array <array> for the presence of status code <status>.
282
 * Returns non-zero if the code is present, zero otherwise. Any status code is
283
 * permitted.
284
 */
285
static inline int http_status_matches(const long *array, uint status)
286
0
{
287
0
  if (status < 100 || status > 599)
288
0
    return 0;
289
290
0
  return ha_bit_test(status - 100, array);
291
0
}
Unexecuted instantiation: flt_http_comp.c:http_status_matches
Unexecuted instantiation: http.c:http_status_matches
Unexecuted instantiation: http_ana.c:http_status_matches
Unexecuted instantiation: http_htx.c:http_status_matches
Unexecuted instantiation: http_rules.c:http_status_matches
Unexecuted instantiation: log.c:http_status_matches
Unexecuted instantiation: sample.c:http_status_matches
Unexecuted instantiation: session.c:http_status_matches
Unexecuted instantiation: stats-html.c:http_status_matches
Unexecuted instantiation: stats.c:http_status_matches
Unexecuted instantiation: tcpcheck.c:http_status_matches
Unexecuted instantiation: vars.c:http_status_matches
Unexecuted instantiation: backend.c:http_status_matches
Unexecuted instantiation: cache.c:http_status_matches
Unexecuted instantiation: check.c:http_status_matches
Unexecuted instantiation: h1.c:http_status_matches
Unexecuted instantiation: http_fetch.c:http_status_matches
Unexecuted instantiation: h1_htx.c:http_status_matches
292
293
#endif /* _HAPROXY_HTTP_H */
294
295
/*
296
 * Local variables:
297
 *  c-indent-level: 8
298
 *  c-basic-offset: 8
299
 * End:
300
 */