Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <import/ist.h> |
Willy Tarreau | d62af6a | 2020-06-05 15:37:34 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 29 | #include <haproxy/http-t.h> |
| 30 | |
| 31 | extern const int http_err_codes[HTTP_ERR_SIZE]; |
| 32 | extern const char *http_err_msgs[HTTP_ERR_SIZE]; |
| 33 | extern const struct ist http_known_methods[HTTP_METH_OTHER]; |
| 34 | extern const uint8_t http_char_classes[256]; |
| 35 | |
| 36 | enum http_meth_t find_http_meth(const char *str, const int len); |
| 37 | int http_get_status_idx(unsigned int status); |
| 38 | const char *http_get_reason(unsigned int status); |
| 39 | struct ist http_get_authority(const struct ist uri, int no_userinfo); |
| 40 | struct ist http_get_path(const struct ist uri); |
| 41 | int http_header_match2(const char *hdr, const char *end, |
| 42 | const char *name, int len); |
| 43 | char *http_find_hdr_value_end(char *s, const char *e); |
| 44 | char *http_find_cookie_value_end(char *s, const char *e); |
| 45 | char *http_extract_cookie_value(char *hdr, const char *hdr_end, |
| 46 | char *cookie_name, size_t cookie_name_l, |
| 47 | int list, char **value, size_t *value_l); |
| 48 | int http_parse_qvalue(const char *qvalue, const char **end); |
| 49 | const char *http_find_url_param_pos(const char **chunks, |
| 50 | const char* url_param_name, |
| 51 | size_t url_param_name_l, char delim); |
| 52 | int http_find_next_url_param(const char **chunks, |
| 53 | const char* url_param_name, size_t url_param_name_l, |
| 54 | const char **vstart, const char **vend, char delim); |
| 55 | |
| 56 | int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value); |
| 57 | int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3); |
| 58 | int http_parse_status_val(const struct ist value, struct ist *status, struct ist *reason); |
| 59 | |
Remi Tricot-Le Breton | bcced09 | 2020-10-22 10:40:03 +0200 | [diff] [blame] | 60 | int http_compare_etags(struct ist etag1, struct ist etag2); |
| 61 | |
Remi Tricot-Le Breton | 56e46cb | 2020-12-23 18:13:48 +0100 | [diff] [blame] | 62 | struct ist http_trim_leading_spht(struct ist value); |
| 63 | struct ist http_trim_trailing_spht(struct ist value); |
| 64 | |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 65 | /* |
| 66 | * Given a path string and its length, find the position of beginning of the |
| 67 | * query string. Returns NULL if no query string is found in the path. |
| 68 | * |
| 69 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 70 | * |
| 71 | * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string. |
| 72 | */ |
| 73 | static inline char *http_find_param_list(char *path, size_t path_l, char delim) |
| 74 | { |
| 75 | char *p; |
| 76 | |
| 77 | p = memchr(path, delim, path_l); |
| 78 | return p ? p + 1 : NULL; |
| 79 | } |
| 80 | |
| 81 | static inline int http_is_param_delimiter(char c, char delim) |
| 82 | { |
| 83 | return c == '&' || c == ';' || c == delim; |
| 84 | } |
| 85 | |
| 86 | /* Match language range with language tag. RFC2616 14.4: |
| 87 | * |
| 88 | * A language-range matches a language-tag if it exactly equals |
| 89 | * the tag, or if it exactly equals a prefix of the tag such |
| 90 | * that the first tag character following the prefix is "-". |
| 91 | * |
| 92 | * Return 1 if the strings match, else return 0. |
| 93 | */ |
| 94 | static inline int http_language_range_match(const char *range, int range_len, |
| 95 | const char *tag, int tag_len) |
| 96 | { |
| 97 | const char *end = range + range_len; |
| 98 | const char *tend = tag + tag_len; |
| 99 | |
| 100 | while (range < end) { |
| 101 | if (*range == '-' && tag == tend) |
| 102 | return 1; |
| 103 | if (*range != *tag || tag == tend) |
| 104 | return 0; |
| 105 | range++; |
| 106 | tag++; |
| 107 | } |
| 108 | /* Return true only if the last char of the tag is matched. */ |
| 109 | return tag == tend; |
| 110 | } |
| 111 | |
Tim Duesterhus | 2493ee8 | 2020-10-22 10:36:24 +0200 | [diff] [blame] | 112 | static inline enum http_etag_type http_get_etag_type(const struct ist etag) |
| 113 | { |
| 114 | /* An ETag must be at least 2 characters. */ |
| 115 | if (etag.len < 2) |
| 116 | return ETAG_INVALID; |
| 117 | |
| 118 | /* The last character must be a `"`. */ |
| 119 | if (etag.ptr[etag.len - 1] != '"') |
| 120 | return ETAG_INVALID; |
| 121 | |
| 122 | /* If the ETag starts with a `"` then it is a strong ETag. */ |
| 123 | if (etag.ptr[0] == '"') |
| 124 | return ETAG_STRONG; |
| 125 | |
| 126 | /* If the ETag starts with `W/"` then it is a weak ETag. */ |
| 127 | if (istnmatch(etag, ist("W/\""), 3)) |
| 128 | return ETAG_WEAK; |
| 129 | |
| 130 | return ETAG_INVALID; |
| 131 | } |
| 132 | |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 133 | |
| 134 | #endif /* _HAPROXY_HTTP_H */ |
| 135 | |
| 136 | /* |
| 137 | * Local variables: |
| 138 | * c-indent-level: 8 |
| 139 | * c-basic-offset: 8 |
| 140 | * End: |
| 141 | */ |