1 /* 2 * nghttp3 3 * 4 * Copyright (c) 2019 nghttp3 contributors 5 * Copyright (c) 2015 nghttp2 contributors 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be 16 * included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 #ifndef NGHTTP3_HTTP_H 27 #define NGHTTP3_HTTP_H 28 29 #ifdef HAVE_CONFIG_H 30 # include <config.h> 31 #endif /* HAVE_CONFIG_H */ 32 33 #include <nghttp3/nghttp3.h> 34 35 typedef struct nghttp3_stream nghttp3_stream; 36 37 typedef struct nghttp3_http_state nghttp3_http_state; 38 39 /* HTTP related flags to enforce HTTP semantics */ 40 41 /* NGHTTP3_HTTP_FLAG_NONE indicates that no flag is set. */ 42 #define NGHTTP3_HTTP_FLAG_NONE 0x00u 43 /* header field seen so far */ 44 #define NGHTTP3_HTTP_FLAG__AUTHORITY 0x01u 45 #define NGHTTP3_HTTP_FLAG__PATH 0x02u 46 #define NGHTTP3_HTTP_FLAG__METHOD 0x04u 47 #define NGHTTP3_HTTP_FLAG__SCHEME 0x08u 48 /* host is not pseudo header, but we require either host or 49 :authority */ 50 #define NGHTTP3_HTTP_FLAG_HOST 0x10u 51 #define NGHTTP3_HTTP_FLAG__STATUS 0x20u 52 /* required header fields for HTTP request except for CONNECT 53 method. */ 54 #define NGHTTP3_HTTP_FLAG_REQ_HEADERS \ 55 (NGHTTP3_HTTP_FLAG__METHOD | NGHTTP3_HTTP_FLAG__PATH | \ 56 NGHTTP3_HTTP_FLAG__SCHEME) 57 #define NGHTTP3_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED 0x40u 58 /* HTTP method flags */ 59 #define NGHTTP3_HTTP_FLAG_METH_CONNECT 0x80u 60 #define NGHTTP3_HTTP_FLAG_METH_HEAD 0x0100u 61 #define NGHTTP3_HTTP_FLAG_METH_OPTIONS 0x0200u 62 #define NGHTTP3_HTTP_FLAG_METH_ALL \ 63 (NGHTTP3_HTTP_FLAG_METH_CONNECT | NGHTTP3_HTTP_FLAG_METH_HEAD | \ 64 NGHTTP3_HTTP_FLAG_METH_OPTIONS) 65 /* :path category */ 66 /* path starts with "/" */ 67 #define NGHTTP3_HTTP_FLAG_PATH_REGULAR 0x0400u 68 /* path "*" */ 69 #define NGHTTP3_HTTP_FLAG_PATH_ASTERISK 0x0800u 70 /* scheme */ 71 /* "http" or "https" scheme */ 72 #define NGHTTP3_HTTP_FLAG_SCHEME_HTTP 0x1000u 73 /* set if final response is expected */ 74 #define NGHTTP3_HTTP_FLAG_EXPECT_FINAL_RESPONSE 0x2000u 75 /* NGHTTP3_HTTP_FLAG__PROTOCOL is set when :protocol pseudo header 76 field is seen. */ 77 #define NGHTTP3_HTTP_FLAG__PROTOCOL 0x4000u 78 /* NGHTTP3_HTTP_FLAG_PRIORITY is set when priority header field is 79 processed. */ 80 #define NGHTTP3_HTTP_FLAG_PRIORITY 0x8000u 81 /* NGHTTP3_HTTP_FLAG_BAD_PRIORITY is set when an error is encountered 82 while parsing priority header field. */ 83 #define NGHTTP3_HTTP_FLAG_BAD_PRIORITY 0x010000u 84 85 /* 86 * This function is called when HTTP header field |nv| received for 87 * |http|. This function will validate |nv| against the current state 88 * of stream. Pass nonzero if this is request headers. Pass nonzero 89 * to |trailers| if |nv| is included in trailers. |connect_protocol| 90 * is nonzero if Extended CONNECT Method is enabled. 91 * 92 * This function returns 0 if it succeeds, or one of the following 93 * negative error codes: 94 * 95 * NGHTTP3_ERR_MALFORMED_HTTP_HEADER 96 * Invalid HTTP header field was received. 97 * NGHTTP3_ERR_REMOVE_HTTP_HEADER 98 * Invalid HTTP header field was received but it can be treated as 99 * if it was not received because of compatibility reasons. 100 */ 101 int nghttp3_http_on_header(nghttp3_http_state *http, nghttp3_qpack_nv *nv, 102 int request, int trailers, int connect_protocol); 103 104 /* 105 * This function is called when request header is received. This 106 * function performs validation and returns 0 if it succeeds, or one 107 * of the following negative error codes: 108 * 109 * NGHTTP3_ERR_MALFORMED_HTTP_HEADER 110 * Required HTTP header field was not received; or an invalid 111 * header field was received. 112 */ 113 int nghttp3_http_on_request_headers(nghttp3_http_state *http); 114 115 /* 116 * This function is called when response header is received. This 117 * function performs validation and returns 0 if it succeeds, or one 118 * of the following negative error codes: 119 * 120 * NGHTTP3_ERR_MALFORMED_HTTP_HEADER 121 * Required HTTP header field was not received; or an invalid 122 * header field was received. 123 */ 124 int nghttp3_http_on_response_headers(nghttp3_http_state *http); 125 126 /* 127 * This function is called when read side stream is closed. This 128 * function performs validation and returns 0 if it succeeds, or one 129 * of the following negative error codes: 130 * 131 * NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING 132 * HTTP messaging is violated. 133 */ 134 int nghttp3_http_on_remote_end_stream(nghttp3_stream *stream); 135 136 /* 137 * This function is called when chunk of data is received. This 138 * function performs validation and returns 0 if it succeeds, or one 139 * of the following negative error codes: 140 * 141 * NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING 142 * HTTP messaging is violated. 143 */ 144 int nghttp3_http_on_data_chunk(nghttp3_stream *stream, size_t n); 145 146 /* 147 * This function inspects header fields in |nva| of length |nvlen| and 148 * records its method in stream->http_flags. 149 */ 150 void nghttp3_http_record_request_method(nghttp3_stream *stream, 151 const nghttp3_nv *nva, size_t nvlen); 152 153 /* 154 * RFC 8941 Structured Field Values. 155 */ 156 typedef enum nghttp3_sf_value_type { 157 NGHTTP3_SF_VALUE_TYPE_BOOLEAN, 158 NGHTTP3_SF_VALUE_TYPE_INTEGER, 159 NGHTTP3_SF_VALUE_TYPE_DECIMAL, 160 NGHTTP3_SF_VALUE_TYPE_STRING, 161 NGHTTP3_SF_VALUE_TYPE_TOKEN, 162 NGHTTP3_SF_VALUE_TYPE_BYTESEQ, 163 NGHTTP3_SF_VALUE_TYPE_INNER_LIST, 164 } nghttp3_sf_value_type; 165 166 /* 167 * nghttp3_sf_value stores Structured Field Values item. For Inner 168 * List, only type is set to NGHTTP3_SF_VALUE_TYPE_INNER_LIST. 169 */ 170 typedef struct nghttp3_sf_value { 171 uint8_t type; 172 union { 173 int b; 174 int64_t i; 175 double d; 176 struct { 177 const uint8_t *base; 178 size_t len; 179 } s; 180 }; 181 } nghttp3_sf_value; 182 183 /* 184 * nghttp3_sf_parse_item parses the input sequence [|begin|, |end|) 185 * and stores the parsed an Item in |dest|. It returns the number of 186 * bytes consumed if it succeeds, or -1. This function is declared 187 * here for unit tests. 188 */ 189 nghttp3_ssize nghttp3_sf_parse_item(nghttp3_sf_value *dest, 190 const uint8_t *begin, const uint8_t *end); 191 192 /* 193 * nghttp3_sf_parse_inner_list parses the input sequence [|begin|, |end|) 194 * and stores the parsed an Inner List in |dest|. It returns the number of 195 * bytes consumed if it succeeds, or -1. This function is declared 196 * here for unit tests. 197 */ 198 nghttp3_ssize nghttp3_sf_parse_inner_list(nghttp3_sf_value *dest, 199 const uint8_t *begin, 200 const uint8_t *end); 201 202 #endif /* NGHTTP3_HTTP_H */ 203