2013-08-27 19:47:22 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-08-27 19:47:22 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef HTTP2_H
|
|
|
|
#define HTTP2_H
|
|
|
|
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
2013-11-17 15:52:19 +01:00
|
|
|
#include <cstdio>
|
2013-12-08 14:31:43 +01:00
|
|
|
#include <cstring>
|
2013-08-27 19:47:22 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-02-05 15:06:56 +01:00
|
|
|
#include <array>
|
2013-08-27 19:47:22 +02:00
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
|
|
|
#include "http-parser/http_parser.h"
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2014-04-03 04:22:11 +02:00
|
|
|
struct Header {
|
2015-02-08 06:07:01 +01:00
|
|
|
Header(std::string name, std::string value, bool no_index = false,
|
|
|
|
int16_t token = -1)
|
|
|
|
: name(std::move(name)), value(std::move(value)), token(token),
|
|
|
|
no_index(no_index) {}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
Header() : token(-1), no_index(false) {}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
bool operator==(const Header &other) const {
|
2014-04-03 04:22:11 +02:00
|
|
|
return name == other.name && value == other.value;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool operator<(const Header &rhs) const {
|
2014-04-03 04:22:11 +02:00
|
|
|
return name < rhs.name || (name == rhs.name && value < rhs.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
2015-02-08 06:07:01 +01:00
|
|
|
int16_t token;
|
2014-04-03 04:22:11 +02:00
|
|
|
bool no_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<Header> Headers;
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2013-08-27 19:47:22 +02:00
|
|
|
namespace http2 {
|
|
|
|
|
2013-10-02 16:29:44 +02:00
|
|
|
std::string get_status_string(unsigned int status_code);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void capitalize(std::string &s, size_t offset);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2014-01-16 18:16:53 +01:00
|
|
|
// Returns true if |value| is LWS
|
|
|
|
bool lws(const char *value);
|
2013-09-11 16:24:32 +02:00
|
|
|
|
2013-08-27 19:47:22 +02:00
|
|
|
// Copies the |field| component value from |u| and |url| to the
|
|
|
|
// |dest|. If |u| does not have |field|, then this function does
|
|
|
|
// nothing.
|
2014-11-27 15:39:04 +01:00
|
|
|
void copy_url_component(std::string &dest, const http_parser_url *u, int field,
|
|
|
|
const char *url);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
Headers::value_type to_header(const uint8_t *name, size_t namelen,
|
2014-04-03 04:22:11 +02:00
|
|
|
const uint8_t *value, size_t valuelen,
|
2015-02-08 06:07:01 +01:00
|
|
|
bool no_index, int16_t token);
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-07-12 11:55:08 +02:00
|
|
|
// Add name/value pairs to |nva|. If |no_index| is true, this
|
|
|
|
// name/value pair won't be indexed when it is forwarded to the next
|
2014-12-15 15:04:45 +01:00
|
|
|
// hop. This function strips white spaces around |value|.
|
2014-11-27 15:39:04 +01:00
|
|
|
void add_header(Headers &nva, const uint8_t *name, size_t namelen,
|
2015-02-08 06:07:01 +01:00
|
|
|
const uint8_t *value, size_t valuelen, bool no_index,
|
|
|
|
int16_t token);
|
2013-11-13 15:56:02 +01:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
// Returns pointer to the entry in |nva| which has name |name|. If
|
|
|
|
// more than one entries which have the name |name|, last occurrence
|
|
|
|
// in |nva| is returned. If no such entry exist, returns nullptr.
|
2014-11-27 15:39:04 +01:00
|
|
|
const Headers::value_type *get_header(const Headers &nva, const char *name);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
// Returns nv->second if nv is not nullptr. Otherwise, returns "".
|
|
|
|
std::string value_to_str(const Headers::value_type *nv);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2014-12-15 15:14:07 +01:00
|
|
|
// Returns true if the value of |nv| is not empty.
|
2014-01-16 15:41:13 +01:00
|
|
|
bool non_empty_value(const Headers::value_type *nv);
|
2013-10-25 14:50:56 +02:00
|
|
|
|
2013-11-28 13:36:04 +01:00
|
|
|
// Creates nghttp2_nv using |name| and |value| and returns it. The
|
|
|
|
// returned value only references the data pointer to name.c_str() and
|
2014-04-03 04:22:11 +02:00
|
|
|
// value.c_str(). If |no_index| is true, nghttp2_nv flags member has
|
|
|
|
// NGHTTP2_NV_FLAG_NO_INDEX flag set.
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_nv make_nv(const std::string &name, const std::string &value,
|
2014-04-26 07:56:08 +02:00
|
|
|
bool no_index = false);
|
2013-11-28 13:36:04 +01:00
|
|
|
|
2013-12-08 14:31:43 +01:00
|
|
|
// Create nghttp2_nv from string literal |name| and |value|.
|
2014-11-27 15:39:04 +01:00
|
|
|
template <size_t N, size_t M>
|
|
|
|
nghttp2_nv make_nv_ll(const char (&name)[N], const char (&value)[M]) {
|
|
|
|
return {(uint8_t *)name, (uint8_t *)value, N - 1, M - 1,
|
|
|
|
NGHTTP2_NV_FLAG_NONE};
|
2013-12-08 14:31:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create nghttp2_nv from string literal |name| and c-string |value|.
|
2014-11-27 15:39:04 +01:00
|
|
|
template <size_t N>
|
|
|
|
nghttp2_nv make_nv_lc(const char (&name)[N], const char *value) {
|
|
|
|
return {(uint8_t *)name, (uint8_t *)value, N - 1, strlen(value),
|
|
|
|
NGHTTP2_NV_FLAG_NONE};
|
2013-12-08 14:31:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create nghttp2_nv from string literal |name| and std::string
|
|
|
|
// |value|.
|
2014-11-27 15:39:04 +01:00
|
|
|
template <size_t N>
|
|
|
|
nghttp2_nv make_nv_ls(const char (&name)[N], const std::string &value) {
|
|
|
|
return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
|
|
|
|
NGHTTP2_NV_FLAG_NONE};
|
2013-12-08 14:31:43 +01:00
|
|
|
}
|
|
|
|
|
2015-02-08 06:07:01 +01:00
|
|
|
// Appends headers in |headers| to |nv|. |headers| must be indexed
|
|
|
|
// before this call (its element's token field is assigned). Certain
|
|
|
|
// headers, including disallowed headers in HTTP/2 spec and headers
|
|
|
|
// which require special handling (i.e. via), are not copied.
|
2015-01-04 15:22:39 +01:00
|
|
|
void copy_headers_to_nva(std::vector<nghttp2_nv> &nva, const Headers &headers);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
|
|
|
// Appends HTTP/1.1 style header lines to |hdrs| from headers in
|
2015-02-08 06:07:01 +01:00
|
|
|
// |headers|. |headers| must be indexed before this call (its
|
|
|
|
// element's token field is assigned). Certain headers, which
|
|
|
|
// requires special handling (i.e. via and cookie), are not appended.
|
2015-01-04 15:22:39 +01:00
|
|
|
void build_http1_headers_from_headers(std::string &hdrs,
|
|
|
|
const Headers &headers);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2013-10-29 16:51:01 +01:00
|
|
|
// Return positive window_size_increment if WINDOW_UPDATE should be
|
|
|
|
// sent for the stream |stream_id|. If |stream_id| == 0, this function
|
|
|
|
// determines the necessity of the WINDOW_UPDATE for a connection.
|
|
|
|
//
|
|
|
|
// If the function determines WINDOW_UPDATE is not necessary at the
|
|
|
|
// moment, it returns -1.
|
|
|
|
int32_t determine_window_update_transmission(nghttp2_session *session,
|
|
|
|
int32_t stream_id);
|
|
|
|
|
2013-11-17 15:52:19 +01:00
|
|
|
// Dumps name/value pairs in |nv| to |out|. The |nv| must be
|
|
|
|
// terminated by nullptr.
|
|
|
|
void dump_nv(FILE *out, const char **nv);
|
|
|
|
|
|
|
|
// Dumps name/value pairs in |nva| to |out|.
|
|
|
|
void dump_nv(FILE *out, const nghttp2_nv *nva, size_t nvlen);
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
// Dumps name/value pairs in |nva| to |out|.
|
2014-11-27 15:39:04 +01:00
|
|
|
void dump_nv(FILE *out, const Headers &nva);
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2013-12-21 09:49:31 +01:00
|
|
|
// Rewrites redirection URI which usually appears in location header
|
|
|
|
// field. The |uri| is the URI in the location header field. The |u|
|
2015-02-08 11:41:45 +01:00
|
|
|
// stores the result of parsed |uri|. The |request_authority| is the
|
|
|
|
// host or :authority header field value in the request. The
|
2013-12-21 09:49:31 +01:00
|
|
|
// |upstream_scheme| is either "https" or "http" in the upstream
|
2015-02-08 11:41:45 +01:00
|
|
|
// interface. Rewrite is done only if location header field value
|
|
|
|
// contains |match_host| as host excluding port. The |match_host| and
|
|
|
|
// |request_authority| could be different. If |request_authority| is
|
|
|
|
// empty, strip authority.
|
2013-12-21 09:49:31 +01:00
|
|
|
//
|
|
|
|
// This function returns the new rewritten URI on success. If the
|
|
|
|
// location URI is not subject to the rewrite, this function returns
|
|
|
|
// emtpy string.
|
2014-11-27 15:39:04 +01:00
|
|
|
std::string rewrite_location_uri(const std::string &uri,
|
|
|
|
const http_parser_url &u,
|
2015-02-08 11:41:45 +01:00
|
|
|
const std::string &match_host,
|
|
|
|
const std::string &request_authority,
|
|
|
|
const std::string &upstream_scheme);
|
2013-12-21 09:49:31 +01:00
|
|
|
|
2014-02-01 11:31:50 +01:00
|
|
|
// Checks the header name/value pair using nghttp2_check_header_name()
|
|
|
|
// and nghttp2_check_header_value(). If both function returns nonzero,
|
|
|
|
// this function returns nonzero.
|
2014-11-27 15:39:04 +01:00
|
|
|
int check_nv(const uint8_t *name, size_t namelen, const uint8_t *value,
|
|
|
|
size_t valuelen);
|
2014-01-16 18:16:53 +01:00
|
|
|
|
2014-08-08 16:03:12 +02:00
|
|
|
// Returns parsed HTTP status code. Returns -1 on failure.
|
2014-11-27 15:39:04 +01:00
|
|
|
int parse_http_status_code(const std::string &src);
|
2014-08-08 16:03:12 +02:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
// Header fields to be indexed, except HD_MAXIDX which is convenient
|
|
|
|
// member to get maximum value.
|
2015-01-02 16:12:26 +01:00
|
|
|
enum {
|
2015-01-04 15:22:39 +01:00
|
|
|
HD__AUTHORITY,
|
|
|
|
HD__HOST,
|
|
|
|
HD__METHOD,
|
|
|
|
HD__PATH,
|
|
|
|
HD__SCHEME,
|
|
|
|
HD__STATUS,
|
2015-02-08 06:23:22 +01:00
|
|
|
HD_ACCEPT_ENCODING,
|
|
|
|
HD_ACCEPT_LANGUAGE,
|
2015-01-04 15:22:39 +01:00
|
|
|
HD_ALT_SVC,
|
2015-02-08 06:23:22 +01:00
|
|
|
HD_CACHE_CONTROL,
|
2015-01-02 16:12:26 +01:00
|
|
|
HD_CONNECTION,
|
2015-01-04 15:22:39 +01:00
|
|
|
HD_CONTENT_LENGTH,
|
|
|
|
HD_COOKIE,
|
2015-01-02 16:12:26 +01:00
|
|
|
HD_EXPECT,
|
|
|
|
HD_HOST,
|
2015-01-04 15:22:39 +01:00
|
|
|
HD_HTTP2_SETTINGS,
|
2015-01-02 16:12:26 +01:00
|
|
|
HD_IF_MODIFIED_SINCE,
|
|
|
|
HD_KEEP_ALIVE,
|
2015-02-07 08:09:49 +01:00
|
|
|
HD_LINK,
|
2015-01-04 15:22:39 +01:00
|
|
|
HD_LOCATION,
|
2015-01-02 16:12:26 +01:00
|
|
|
HD_PROXY_CONNECTION,
|
2015-01-04 15:22:39 +01:00
|
|
|
HD_SERVER,
|
2015-01-02 16:12:26 +01:00
|
|
|
HD_TE,
|
2015-01-14 13:33:22 +01:00
|
|
|
HD_TRAILER,
|
2015-01-02 16:12:26 +01:00
|
|
|
HD_TRANSFER_ENCODING,
|
|
|
|
HD_UPGRADE,
|
2015-02-08 06:23:22 +01:00
|
|
|
HD_USER_AGENT,
|
2015-01-04 15:22:39 +01:00
|
|
|
HD_VIA,
|
|
|
|
HD_X_FORWARDED_FOR,
|
|
|
|
HD_X_FORWARDED_PROTO,
|
2015-01-02 16:12:26 +01:00
|
|
|
HD_MAXIDX,
|
|
|
|
};
|
|
|
|
|
2015-02-08 05:05:37 +01:00
|
|
|
using HeaderIndex = std::array<int16_t, HD_MAXIDX>;
|
2015-02-05 15:06:56 +01:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
// Looks up header token for header name |name| of length |namelen|.
|
|
|
|
// Only headers we are interested in are tokenized. If header name
|
|
|
|
// cannot be tokenized, returns -1.
|
|
|
|
int lookup_token(const uint8_t *name, size_t namelen);
|
|
|
|
int lookup_token(const std::string &name);
|
|
|
|
|
2015-01-02 16:12:26 +01:00
|
|
|
// Initializes |hdidx|, header index. The |hdidx| must point to the
|
|
|
|
// array containing at least HD_MAXIDX elements.
|
2015-02-05 15:06:56 +01:00
|
|
|
void init_hdidx(HeaderIndex &hdidx);
|
2015-01-04 15:22:39 +01:00
|
|
|
// Indexes header |token| using index |idx|.
|
2015-02-08 06:07:01 +01:00
|
|
|
void index_header(HeaderIndex &hdidx, int16_t token, size_t idx);
|
2015-01-04 15:22:39 +01:00
|
|
|
|
|
|
|
// Returns true if HTTP/2 request pseudo header |token| is not indexed
|
|
|
|
// yet and not -1.
|
2015-02-08 06:07:01 +01:00
|
|
|
bool check_http2_request_pseudo_header(const HeaderIndex &hdidx, int16_t token);
|
2015-01-04 15:22:39 +01:00
|
|
|
|
|
|
|
// Returns true if HTTP/2 response pseudo header |token| is not
|
|
|
|
// indexed yet and not -1.
|
2015-02-08 06:07:01 +01:00
|
|
|
bool check_http2_response_pseudo_header(const HeaderIndex &hdidx,
|
|
|
|
int16_t token);
|
2015-01-04 15:22:39 +01:00
|
|
|
|
|
|
|
// Returns true if header field denoted by |token| is allowed for
|
|
|
|
// HTTP/2.
|
2015-02-08 06:07:01 +01:00
|
|
|
bool http2_header_allowed(int16_t token);
|
2015-01-04 15:22:39 +01:00
|
|
|
|
|
|
|
// Returns true that |hdidx| contains mandatory HTTP/2 request
|
|
|
|
// headers.
|
2015-02-05 15:06:56 +01:00
|
|
|
bool http2_mandatory_request_headers_presence(const HeaderIndex &hdidx);
|
2015-01-04 15:22:39 +01:00
|
|
|
|
|
|
|
// Returns header denoted by |token| using index |hdidx|.
|
2015-02-08 06:07:01 +01:00
|
|
|
const Headers::value_type *get_header(const HeaderIndex &hdidx, int16_t token,
|
2015-01-02 16:12:26 +01:00
|
|
|
const Headers &nva);
|
|
|
|
|
2015-02-07 08:09:49 +01:00
|
|
|
struct LinkHeader {
|
2015-02-08 09:29:38 +01:00
|
|
|
// The region of URI is [uri.first, uri.second).
|
|
|
|
std::pair<const char *, const char *> uri;
|
2015-02-07 08:09:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Returns next URI-reference in Link header field value |src| of
|
|
|
|
// length |len|. If no URI-reference found after searching all input,
|
|
|
|
// returned uri field is empty. This imply that empty URI-reference
|
|
|
|
// is ignored during parsing.
|
|
|
|
std::vector<LinkHeader> parse_link_header(const char *src, size_t len);
|
|
|
|
|
|
|
|
// Constructs path by combining base path |base_path| of length
|
|
|
|
// |base_pathlen| with another path |rel_path| of length
|
|
|
|
// |rel_pathlen|. The base path and another path can have optional
|
2015-03-03 18:48:20 +01:00
|
|
|
// query component. This function assumes |base_path| is normalized.
|
|
|
|
// In other words, it does not contain ".." or "." path components
|
|
|
|
// and starts with "/" if it is not empty.
|
2015-02-07 08:09:49 +01:00
|
|
|
std::string path_join(const char *base_path, size_t base_pathlen,
|
|
|
|
const char *base_query, size_t base_querylen,
|
|
|
|
const char *rel_path, size_t rel_pathlen,
|
|
|
|
const char *rel_query, size_t rel_querylen);
|
|
|
|
|
2015-03-06 13:36:40 +01:00
|
|
|
// true if response has body, taking into account the request method
|
|
|
|
// and status code.
|
|
|
|
bool expect_response_body(const std::string &method, int status_code);
|
|
|
|
|
|
|
|
// true if response has body, taking into account status code only.
|
|
|
|
bool expect_response_body(int status_code);
|
|
|
|
|
2013-08-27 19:47:22 +02:00
|
|
|
} // namespace http2
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // HTTP2_H
|