2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-04 16:48:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 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 SHRPX_DOWNSTREAM_H
|
|
|
|
#define SHRPX_DOWNSTREAM_H
|
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2014-08-18 17:16:51 +02:00
|
|
|
#include <memory>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
#include <event.h>
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
|
2013-07-26 12:33:25 +02:00
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
2012-06-04 20:11:43 +02:00
|
|
|
#include "shrpx_io_control.h"
|
2014-01-16 15:41:13 +01:00
|
|
|
#include "http2.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
2012-06-04 20:11:43 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
class Upstream;
|
2012-06-09 16:14:00 +02:00
|
|
|
class DownstreamConnection;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
class Downstream {
|
|
|
|
public:
|
2014-08-18 15:59:31 +02:00
|
|
|
Downstream(Upstream *upstream, int32_t stream_id, int32_t priority);
|
2012-06-04 16:48:31 +02:00
|
|
|
~Downstream();
|
2013-08-03 11:51:01 +02:00
|
|
|
void reset_upstream(Upstream *upstream);
|
2012-06-04 16:48:31 +02:00
|
|
|
Upstream* get_upstream() const;
|
2013-08-03 11:51:01 +02:00
|
|
|
void set_stream_id(int32_t stream_id);
|
2012-06-04 16:48:31 +02:00
|
|
|
int32_t get_stream_id() const;
|
2013-11-16 07:41:24 +01:00
|
|
|
void set_priority(int32_t pri);
|
2014-02-19 14:53:33 +01:00
|
|
|
int32_t get_priority() const;
|
2012-06-04 20:11:43 +02:00
|
|
|
void pause_read(IOCtrlReason reason);
|
2014-08-21 14:22:16 +02:00
|
|
|
int resume_read(IOCtrlReason reason, size_t consumed);
|
2012-06-06 14:39:55 +02:00
|
|
|
void force_resume_read();
|
2013-11-04 10:22:29 +01:00
|
|
|
// Set stream ID for downstream HTTP2 connection.
|
2012-11-18 13:23:13 +01:00
|
|
|
void set_downstream_stream_id(int32_t stream_id);
|
|
|
|
int32_t get_downstream_stream_id() const;
|
|
|
|
|
2014-08-18 17:16:51 +02:00
|
|
|
int attach_downstream_connection
|
|
|
|
(std::unique_ptr<DownstreamConnection> dconn);
|
|
|
|
void detach_downstream_connection();
|
|
|
|
// Releases dconn_, without freeing it.
|
|
|
|
void release_downstream_connection();
|
2012-06-09 16:14:00 +02:00
|
|
|
DownstreamConnection* get_downstream_connection();
|
2014-08-18 17:16:51 +02:00
|
|
|
// Returns dconn_ and nullifies dconn_.
|
|
|
|
std::unique_ptr<DownstreamConnection> pop_downstream_connection();
|
|
|
|
|
2012-06-09 17:49:33 +02:00
|
|
|
// Returns true if output buffer is full. If underlying dconn_ is
|
|
|
|
// NULL, this function always returns false.
|
|
|
|
bool get_output_buffer_full();
|
2013-07-31 14:48:37 +02:00
|
|
|
// Returns true if upgrade (HTTP Upgrade or CONNECT) is succeeded.
|
|
|
|
void check_upgrade_fulfilled();
|
|
|
|
// Returns true if the request is upgrade.
|
|
|
|
bool get_upgrade_request() const;
|
|
|
|
// Returns true if the upgrade is succeded as a result of the call
|
|
|
|
// check_upgrade_fulfilled().
|
|
|
|
bool get_upgraded() const;
|
2014-06-15 09:14:00 +02:00
|
|
|
// Inspects HTTP/2 request.
|
|
|
|
void inspect_http2_request();
|
|
|
|
// Inspects HTTP/1 request. This checks whether the request is
|
|
|
|
// upgrade request and tranfer-encoding etc.
|
|
|
|
void inspect_http1_request();
|
2014-03-30 12:09:21 +02:00
|
|
|
// Returns true if the request is HTTP Upgrade for HTTP/2
|
2014-06-15 09:14:00 +02:00
|
|
|
bool get_http2_upgrade_request() const;
|
|
|
|
// Returns the value of HTTP2-Settings request header field.
|
|
|
|
const std::string& get_http2_settings() const;
|
2012-06-04 16:48:31 +02:00
|
|
|
// downstream request API
|
|
|
|
const Headers& get_request_headers() const;
|
2013-11-16 13:15:55 +01:00
|
|
|
void crumble_request_cookie();
|
|
|
|
void assemble_request_cookie();
|
|
|
|
const std::string& get_assembled_request_cookie() const;
|
2013-08-27 17:09:46 +02:00
|
|
|
// Makes key lowercase and sort headers by name using <
|
|
|
|
void normalize_request_headers();
|
|
|
|
// Returns iterator pointing to the request header with the name
|
|
|
|
// |name|. If multiple header have |name| as name, return first
|
|
|
|
// occurrence from the beginning. If no such header is found,
|
|
|
|
// returns std::end(get_request_headers()). This function must be
|
|
|
|
// called after calling normalize_request_headers().
|
|
|
|
Headers::const_iterator get_norm_request_header
|
|
|
|
(const std::string& name) const;
|
2013-09-24 16:53:55 +02:00
|
|
|
void add_request_header(std::string name, std::string value);
|
|
|
|
void set_last_request_header_value(std::string value);
|
2012-07-11 11:32:04 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
void split_add_request_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
|
|
|
|
2012-07-11 11:32:04 +02:00
|
|
|
bool get_request_header_key_prev() const;
|
|
|
|
void append_last_request_header_key(const char *data, size_t len);
|
|
|
|
void append_last_request_header_value(const char *data, size_t len);
|
2014-06-15 09:14:00 +02:00
|
|
|
// Empties request headers.
|
|
|
|
void clear_request_headers();
|
2012-07-11 11:32:04 +02:00
|
|
|
|
2014-01-27 17:17:54 +01:00
|
|
|
size_t get_request_headers_sum() const;
|
|
|
|
|
2013-09-24 16:53:55 +02:00
|
|
|
void set_request_method(std::string method);
|
2012-07-11 09:20:16 +02:00
|
|
|
const std::string& get_request_method() const;
|
2013-09-24 16:53:55 +02:00
|
|
|
void set_request_path(std::string path);
|
2012-07-11 11:32:04 +02:00
|
|
|
void append_request_path(const char *data, size_t len);
|
2013-10-25 14:50:56 +02:00
|
|
|
// Returns request path. For HTTP/1.1, this is request-target. For
|
|
|
|
// HTTP/2, this is :path header field value.
|
2012-07-11 09:20:16 +02:00
|
|
|
const std::string& get_request_path() const;
|
2013-10-25 14:50:56 +02:00
|
|
|
// Returns HTTP/2 :scheme header field value.
|
|
|
|
const std::string& get_request_http2_scheme() const;
|
|
|
|
void set_request_http2_scheme(std::string scheme);
|
|
|
|
// Returns HTTP/2 :authority header field value.
|
|
|
|
const std::string& get_request_http2_authority() const;
|
|
|
|
void set_request_http2_authority(std::string authority);
|
2012-06-06 17:43:18 +02:00
|
|
|
void set_request_major(int major);
|
|
|
|
void set_request_minor(int minor);
|
2012-06-07 15:40:42 +02:00
|
|
|
int get_request_major() const;
|
|
|
|
int get_request_minor() const;
|
2012-06-04 16:48:31 +02:00
|
|
|
int push_request_headers();
|
|
|
|
bool get_chunked_request() const;
|
2014-07-03 12:59:10 +02:00
|
|
|
void set_chunked_request(bool f);
|
2012-06-04 16:48:31 +02:00
|
|
|
bool get_request_connection_close() const;
|
|
|
|
void set_request_connection_close(bool f);
|
2014-07-05 11:22:40 +02:00
|
|
|
void set_request_user_agent(std::string user_agent);
|
|
|
|
const std::string& get_request_user_agent() const;
|
2014-07-03 12:59:10 +02:00
|
|
|
bool get_request_http2_expect_body() const;
|
|
|
|
void set_request_http2_expect_body(bool f);
|
2012-06-04 16:48:31 +02:00
|
|
|
int push_upload_data_chunk(const uint8_t *data, size_t datalen);
|
|
|
|
int end_upload_data();
|
2014-07-25 14:26:03 +02:00
|
|
|
size_t get_request_datalen() const;
|
2014-08-21 14:22:16 +02:00
|
|
|
void dec_request_datalen(size_t len);
|
2014-07-25 14:26:03 +02:00
|
|
|
void reset_request_datalen();
|
2014-08-08 13:52:32 +02:00
|
|
|
bool request_pseudo_header_allowed() const;
|
2014-07-25 16:13:27 +02:00
|
|
|
bool expect_response_body() const;
|
2012-06-04 16:48:31 +02:00
|
|
|
enum {
|
|
|
|
INITIAL,
|
|
|
|
HEADER_COMPLETE,
|
|
|
|
MSG_COMPLETE,
|
2012-06-05 19:23:07 +02:00
|
|
|
STREAM_CLOSED,
|
2012-06-07 16:42:11 +02:00
|
|
|
CONNECT_FAIL,
|
2012-11-18 13:23:13 +01:00
|
|
|
IDLE,
|
|
|
|
MSG_RESET
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
void set_request_state(int state);
|
|
|
|
int get_request_state() const;
|
|
|
|
// downstream response API
|
|
|
|
const Headers& get_response_headers() const;
|
2013-08-27 17:09:46 +02:00
|
|
|
// Makes key lowercase and sort headers by name using <
|
|
|
|
void normalize_response_headers();
|
|
|
|
// Returns iterator pointing to the response header with the name
|
|
|
|
// |name|. If multiple header have |name| as name, return first
|
|
|
|
// occurrence from the beginning. If no such header is found,
|
|
|
|
// returns std::end(get_response_headers()). This function must be
|
|
|
|
// called after calling normalize_response_headers().
|
|
|
|
Headers::const_iterator get_norm_response_header
|
|
|
|
(const std::string& name) const;
|
2013-12-21 09:49:31 +01:00
|
|
|
// Rewrites the location response header field. This function must
|
|
|
|
// be called after calling normalize_response_headers() and
|
|
|
|
// normalize_request_headers().
|
|
|
|
void rewrite_norm_location_response_header
|
|
|
|
(const std::string& upstream_scheme,
|
2013-12-21 10:35:53 +01:00
|
|
|
uint16_t upstream_port);
|
2013-09-24 16:53:55 +02:00
|
|
|
void add_response_header(std::string name, std::string value);
|
|
|
|
void set_last_response_header_value(std::string value);
|
2012-07-11 11:32:04 +02:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
void split_add_response_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
|
|
|
|
2012-07-11 11:32:04 +02:00
|
|
|
bool get_response_header_key_prev() const;
|
|
|
|
void append_last_response_header_key(const char *data, size_t len);
|
|
|
|
void append_last_response_header_value(const char *data, size_t len);
|
2014-06-15 09:14:00 +02:00
|
|
|
// Empties response headers.
|
|
|
|
void clear_response_headers();
|
2012-07-11 11:32:04 +02:00
|
|
|
|
2014-01-27 17:17:54 +01:00
|
|
|
size_t get_response_headers_sum() const;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
unsigned int get_response_http_status() const;
|
|
|
|
void set_response_http_status(unsigned int status);
|
2012-06-06 17:43:18 +02:00
|
|
|
void set_response_major(int major);
|
|
|
|
void set_response_minor(int minor);
|
|
|
|
int get_response_major() const;
|
|
|
|
int get_response_minor() const;
|
2012-07-11 09:20:16 +02:00
|
|
|
int get_response_version() const;
|
2012-06-04 16:48:31 +02:00
|
|
|
bool get_chunked_response() const;
|
2012-11-18 13:23:13 +01:00
|
|
|
void set_chunked_response(bool f);
|
2012-06-07 16:42:11 +02:00
|
|
|
bool get_response_connection_close() const;
|
2012-07-11 09:20:16 +02:00
|
|
|
void set_response_connection_close(bool f);
|
2012-06-04 16:48:31 +02:00
|
|
|
void set_response_state(int state);
|
|
|
|
int get_response_state() const;
|
|
|
|
int init_response_body_buf();
|
|
|
|
evbuffer* get_response_body_buf();
|
2014-07-05 11:22:40 +02:00
|
|
|
void add_response_bodylen(size_t amount);
|
|
|
|
int64_t get_response_bodylen() const;
|
2014-08-23 10:34:56 +02:00
|
|
|
uint32_t get_response_rst_stream_error_code() const;
|
|
|
|
void set_response_rst_stream_error_code(uint32_t error_code);
|
2014-06-15 09:14:00 +02:00
|
|
|
// Inspects HTTP/1 response. This checks tranfer-encoding etc.
|
|
|
|
void inspect_http1_response();
|
2014-07-23 16:32:57 +02:00
|
|
|
// Clears some of member variables for response.
|
|
|
|
void reset_response();
|
|
|
|
bool get_non_final_response() const;
|
|
|
|
void set_expect_final_response(bool f);
|
|
|
|
bool get_expect_final_response() const;
|
2014-07-25 14:26:03 +02:00
|
|
|
void add_response_datalen(size_t len);
|
2014-08-21 14:22:16 +02:00
|
|
|
void dec_response_datalen(size_t len);
|
2014-07-25 14:26:03 +02:00
|
|
|
size_t get_response_datalen() const;
|
|
|
|
void reset_response_datalen();
|
2014-08-08 13:52:32 +02:00
|
|
|
bool response_pseudo_header_allowed() const;
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2012-11-20 17:29:39 +01:00
|
|
|
// Call this method when there is incoming data in downstream
|
|
|
|
// connection.
|
|
|
|
int on_read();
|
|
|
|
|
2014-01-18 08:12:03 +01:00
|
|
|
// Change the priority of downstream
|
|
|
|
int change_priority(int32_t pri);
|
2014-01-26 16:44:08 +01:00
|
|
|
|
2014-01-27 17:17:54 +01:00
|
|
|
// Maximum buffer size for header name/value pairs.
|
|
|
|
static const size_t MAX_HEADERS_SUM = 32768;
|
2014-07-02 16:56:26 +02:00
|
|
|
|
|
|
|
bool get_rst_stream_after_end_stream() const;
|
|
|
|
void set_rst_stream_after_end_stream(bool f);
|
2014-08-09 11:47:45 +02:00
|
|
|
|
|
|
|
// Initializes upstream timers, but they are not pending.
|
|
|
|
void init_upstream_timer();
|
|
|
|
// Makes upstream read timer pending. If it is already pending,
|
|
|
|
// timeout value is reset. This function also resets write timer if
|
|
|
|
// it is already pending.
|
|
|
|
void reset_upstream_rtimer();
|
|
|
|
// Makes upstream write timer pending. If it is already pending,
|
|
|
|
// timeout value is reset. This function also resets read timer if
|
|
|
|
// it is already pending.
|
|
|
|
void reset_upstream_wtimer();
|
|
|
|
// Makes upstream write timer pending. If it is already pending, do
|
|
|
|
// nothing.
|
|
|
|
void ensure_upstream_wtimer();
|
|
|
|
// Disables upstream read timer.
|
|
|
|
void disable_upstream_rtimer();
|
|
|
|
// Disables upstream write timer.
|
|
|
|
void disable_upstream_wtimer();
|
|
|
|
|
|
|
|
// Downstream timer functions. They works in a similar way just
|
|
|
|
// like the upstream timer function.
|
|
|
|
void init_downstream_timer();
|
|
|
|
void reset_downstream_rtimer();
|
|
|
|
void reset_downstream_wtimer();
|
|
|
|
void ensure_downstream_wtimer();
|
|
|
|
void disable_downstream_rtimer();
|
|
|
|
void disable_downstream_wtimer();
|
2012-06-04 16:48:31 +02:00
|
|
|
private:
|
2013-12-06 15:17:38 +01:00
|
|
|
Headers request_headers_;
|
|
|
|
Headers response_headers_;
|
|
|
|
|
|
|
|
std::string request_method_;
|
|
|
|
std::string request_path_;
|
2014-07-05 11:22:40 +02:00
|
|
|
std::string request_user_agent_;
|
2013-12-06 15:17:38 +01:00
|
|
|
std::string request_http2_scheme_;
|
|
|
|
std::string request_http2_authority_;
|
|
|
|
std::string assembled_request_cookie_;
|
2014-06-15 09:14:00 +02:00
|
|
|
std::string http2_settings_;
|
|
|
|
|
2013-12-06 15:17:38 +01:00
|
|
|
// the length of request body
|
|
|
|
int64_t request_bodylen_;
|
2014-07-05 11:22:40 +02:00
|
|
|
// the length of response body
|
|
|
|
int64_t response_bodylen_;
|
2013-12-06 15:17:38 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
Upstream *upstream_;
|
2014-08-18 17:16:51 +02:00
|
|
|
std::unique_ptr<DownstreamConnection> dconn_;
|
2013-12-06 15:17:38 +01:00
|
|
|
// This buffer is used to temporarily store downstream response
|
|
|
|
// body. nghttp2 library reads data from this in the callback.
|
|
|
|
evbuffer *response_body_buf_;
|
|
|
|
|
2014-08-09 11:47:45 +02:00
|
|
|
event *upstream_rtimerev_;
|
|
|
|
event *upstream_wtimerev_;
|
|
|
|
|
|
|
|
event *downstream_rtimerev_;
|
|
|
|
event *downstream_wtimerev_;
|
|
|
|
|
2014-01-27 17:17:54 +01:00
|
|
|
size_t request_headers_sum_;
|
|
|
|
size_t response_headers_sum_;
|
|
|
|
|
2014-07-25 14:26:03 +02:00
|
|
|
// The number of bytes not consumed by the application yet.
|
|
|
|
size_t request_datalen_;
|
|
|
|
size_t response_datalen_;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
int32_t stream_id_;
|
2013-11-16 07:41:24 +01:00
|
|
|
int32_t priority_;
|
2012-11-18 13:23:13 +01:00
|
|
|
// stream ID in backend connection
|
|
|
|
int32_t downstream_stream_id_;
|
2013-12-06 15:17:38 +01:00
|
|
|
|
|
|
|
// RST_STREAM error_code from downstream HTTP2 connection
|
2014-08-23 10:34:56 +02:00
|
|
|
uint32_t response_rst_stream_error_code_;
|
2013-12-06 15:17:38 +01:00
|
|
|
|
|
|
|
int request_state_;
|
|
|
|
int request_major_;
|
|
|
|
int request_minor_;
|
|
|
|
|
|
|
|
int response_state_;
|
|
|
|
unsigned int response_http_status_;
|
|
|
|
int response_major_;
|
|
|
|
int response_minor_;
|
|
|
|
|
2013-07-31 14:48:37 +02:00
|
|
|
// true if the request contains upgrade token (HTTP Upgrade or
|
|
|
|
// CONNECT)
|
|
|
|
bool upgrade_request_;
|
|
|
|
// true if the connection is upgraded (HTTP Upgrade or CONNECT)
|
|
|
|
bool upgraded_;
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2014-06-15 09:14:00 +02:00
|
|
|
bool http2_upgrade_seen_;
|
|
|
|
bool http2_settings_seen_;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
bool chunked_request_;
|
|
|
|
bool request_connection_close_;
|
2012-07-11 11:32:04 +02:00
|
|
|
bool request_header_key_prev_;
|
2014-07-03 12:59:10 +02:00
|
|
|
bool request_http2_expect_body_;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
bool chunked_response_;
|
2012-06-07 16:42:11 +02:00
|
|
|
bool response_connection_close_;
|
2012-07-11 11:32:04 +02:00
|
|
|
bool response_header_key_prev_;
|
2014-07-23 16:32:57 +02:00
|
|
|
bool expect_final_response_;
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_DOWNSTREAM_H
|