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>
|
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
|
|
|
#include "http-parser/http_parser.h"
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
2014-04-03 04:22:11 +02:00
|
|
|
struct Header {
|
|
|
|
Header(std::string name, std::string value, bool no_index=false)
|
|
|
|
: name(std::move(name)),
|
|
|
|
value(std::move(value)),
|
|
|
|
no_index(no_index)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Header()
|
|
|
|
: no_index(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator==(const Header& other) const
|
|
|
|
{
|
|
|
|
return name == other.name && value == other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Header& rhs) const
|
|
|
|
{
|
|
|
|
return name < rhs.name || (name == rhs.name && value < rhs.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
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
|
|
|
|
|
|
|
void capitalize(std::string& s, size_t offset);
|
|
|
|
|
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
|
|
|
void sanitize_header_value(std::string& s, size_t offset);
|
|
|
|
|
|
|
|
// Copies the |field| component value from |u| and |url| to the
|
|
|
|
// |dest|. If |u| does not have |field|, then this function does
|
|
|
|
// nothing.
|
2013-12-21 09:49:31 +01:00
|
|
|
void copy_url_component(std::string& dest, const http_parser_url *u, int field,
|
2013-08-27 19:47:22 +02:00
|
|
|
const char* url);
|
|
|
|
|
|
|
|
// Returns true if the header field |name| with length |namelen| bytes
|
2014-03-30 12:09:21 +02:00
|
|
|
// is valid for HTTP/2.
|
2013-08-27 19:47:22 +02:00
|
|
|
bool check_http2_allowed_header(const uint8_t *name, size_t namelen);
|
|
|
|
|
|
|
|
// Calls check_http2_allowed_header with |name| and strlen(name),
|
|
|
|
// assuming |name| is null-terminated string.
|
|
|
|
bool check_http2_allowed_header(const char *name);
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
// Checks that headers |nva| do not contain disallowed header fields
|
2014-03-30 12:09:21 +02:00
|
|
|
// in HTTP/2 spec. This function returns true if |nva| does not
|
2014-01-16 15:41:13 +01:00
|
|
|
// contains such headers.
|
|
|
|
bool check_http2_headers(const Headers& nva);
|
|
|
|
|
2014-08-08 13:52:32 +02:00
|
|
|
// Calls check_http2_headers()
|
2014-08-08 13:17:03 +02:00
|
|
|
bool check_http2_request_headers(const Headers& nva);
|
2014-07-31 15:52:58 +02:00
|
|
|
|
2014-08-08 13:52:32 +02:00
|
|
|
// Calls check_http2_headers()
|
2014-08-08 13:17:03 +02:00
|
|
|
bool check_http2_response_headers(const Headers& nva);
|
2014-07-31 15:52:58 +02:00
|
|
|
|
2014-08-08 13:52:32 +02:00
|
|
|
// Returns true if |name| is allowed pusedo header for request.
|
|
|
|
bool check_http2_request_pseudo_header(const uint8_t *name, size_t namelen);
|
|
|
|
|
|
|
|
// Returns true if |name| is allowed pusedo header for response.
|
|
|
|
bool check_http2_response_pseudo_header(const uint8_t *name, size_t namelen);
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
bool name_less(const Headers::value_type& lhs, const Headers::value_type& rhs);
|
|
|
|
|
|
|
|
void normalize_headers(Headers& nva);
|
|
|
|
|
|
|
|
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,
|
|
|
|
bool no_index);
|
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
|
|
|
|
// hop.
|
|
|
|
void add_header(Headers& nva,
|
|
|
|
const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen,
|
|
|
|
bool no_index);
|
2013-11-13 15:56:02 +01:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
// Returns the iterator to the entry in |nva| which has name |name|
|
|
|
|
// and the |name| is uinque in the |nva|. If no such entry exist,
|
|
|
|
// returns nullptr.
|
|
|
|
const Headers::value_type* get_unique_header(const Headers& nva,
|
|
|
|
const char *name);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
// Returns the iterator to the entry in |nva| which has name
|
|
|
|
// |name|. If more than one entries which have the name |name|, first
|
|
|
|
// occurrence in |nva| is returned. If no such entry exist, returns
|
|
|
|
// nullptr.
|
|
|
|
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
|
|
|
|
|
|
|
// Returns true if the value of |nv| includes only ' ' (0x20) or '\t'.
|
2014-01-16 15:41:13 +01:00
|
|
|
bool value_lws(const Headers::value_type *nv);
|
2013-08-27 19:47:22 +02:00
|
|
|
|
2013-10-25 14:50:56 +02:00
|
|
|
// Returns true if the value of |nv| is not empty value and not LWS
|
|
|
|
// and not contain illegal characters.
|
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.
|
|
|
|
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|.
|
|
|
|
template<size_t N, size_t M>
|
|
|
|
nghttp2_nv make_nv_ll(const char(&name)[N], const char(&value)[M])
|
|
|
|
{
|
2014-04-30 16:08:34 +02:00
|
|
|
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|.
|
|
|
|
template<size_t N>
|
|
|
|
nghttp2_nv make_nv_lc(const char(&name)[N], const char *value)
|
|
|
|
{
|
2014-04-30 16:08:34 +02:00
|
|
|
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|.
|
|
|
|
template<size_t N>
|
|
|
|
nghttp2_nv make_nv_ls(const char(&name)[N], const std::string& value)
|
|
|
|
{
|
2014-04-30 16:08:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-23 16:18:24 +02:00
|
|
|
// Appends headers in |headers| to |nv|. Certain headers, including
|
2014-03-30 12:09:21 +02:00
|
|
|
// disallowed headers in HTTP/2 spec and headers which require
|
2013-08-27 19:47:22 +02:00
|
|
|
// special handling (i.e. via), are not copied.
|
2013-11-28 13:36:04 +01:00
|
|
|
void copy_norm_headers_to_nva
|
2014-01-16 15:41:13 +01:00
|
|
|
(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
|
|
|
|
// |headers|. Certain headers, which requires special handling
|
2013-11-16 13:15:55 +01:00
|
|
|
// (i.e. via and cookie), are not appended.
|
2013-08-27 19:47:22 +02:00
|
|
|
void build_http1_headers_from_norm_headers
|
2014-01-16 15:41:13 +01:00
|
|
|
(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|.
|
|
|
|
void dump_nv(FILE *out, const Headers& nva);
|
|
|
|
|
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|
|
|
|
|
// stores the result of parsed |uri|. The |request_host| is the host
|
|
|
|
// or :authority header field value in the request. The
|
|
|
|
// |upstream_scheme| is either "https" or "http" in the upstream
|
2013-12-21 10:35:53 +01:00
|
|
|
// interface.
|
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.
|
|
|
|
std::string rewrite_location_uri(const std::string& uri,
|
|
|
|
const http_parser_url& u,
|
|
|
|
const std::string& request_host,
|
|
|
|
const std::string& upstream_scheme,
|
2013-12-21 10:35:53 +01:00
|
|
|
uint16_t upstream_port);
|
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-01-16 18:16:53 +01:00
|
|
|
int check_nv(const uint8_t *name, size_t namelen,
|
|
|
|
const uint8_t *value, size_t valuelen);
|
|
|
|
|
2014-08-08 16:03:12 +02:00
|
|
|
// Returns parsed HTTP status code. Returns -1 on failure.
|
|
|
|
int parse_http_status_code(const std::string& src);
|
|
|
|
|
2013-08-27 19:47:22 +02:00
|
|
|
} // namespace http2
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // HTTP2_H
|