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"
|
|
|
|
|
2015-05-13 17:17:45 +02:00
|
|
|
#include <cinttypes>
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2014-08-18 17:16:51 +02:00
|
|
|
#include <memory>
|
2014-11-19 17:53:30 +01:00
|
|
|
#include <chrono>
|
2018-01-04 14:05:25 +01:00
|
|
|
#include <algorithm>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <ev.h>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
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"
|
2017-02-16 14:46:22 +01:00
|
|
|
#include "shrpx_log_config.h"
|
2014-01-16 15:41:13 +01:00
|
|
|
#include "http2.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
#include "memchunk.h"
|
2016-03-09 13:15:32 +01:00
|
|
|
#include "allocator.h"
|
2014-01-16 15:41:13 +01:00
|
|
|
|
|
|
|
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;
|
2015-03-11 16:17:05 +01:00
|
|
|
struct BlockedLink;
|
2016-08-04 17:04:47 +02:00
|
|
|
struct DownstreamAddrGroup;
|
|
|
|
struct DownstreamAddr;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
class FieldStore {
|
|
|
|
public:
|
2016-03-09 13:25:11 +01:00
|
|
|
FieldStore(BlockAllocator &balloc, size_t headers_initial_capacity)
|
2016-01-27 13:14:07 +01:00
|
|
|
: content_length(-1),
|
2016-03-09 13:15:32 +01:00
|
|
|
balloc_(balloc),
|
2016-01-27 13:14:07 +01:00
|
|
|
buffer_size_(0),
|
|
|
|
header_key_prev_(false),
|
2016-01-13 14:45:52 +01:00
|
|
|
trailer_key_prev_(false) {
|
|
|
|
headers_.reserve(headers_initial_capacity);
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
const HeaderRefs &headers() const { return headers_; }
|
|
|
|
const HeaderRefs &trailers() const { return trailers_; }
|
2016-01-13 14:45:52 +01:00
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
HeaderRefs &headers() { return headers_; }
|
2016-01-13 14:45:52 +01:00
|
|
|
|
|
|
|
const void add_extra_buffer_size(size_t n) { buffer_size_ += n; }
|
|
|
|
size_t buffer_size() const { return buffer_size_; }
|
|
|
|
|
|
|
|
size_t num_fields() const { return headers_.size() + trailers_.size(); }
|
|
|
|
|
|
|
|
// Returns pointer to the header field with the name |name|. If
|
|
|
|
// multiple header have |name| as name, return last occurrence from
|
|
|
|
// the beginning. If no such header is found, returns nullptr.
|
2016-03-09 13:15:32 +01:00
|
|
|
const HeaderRefs::value_type *header(int32_t token) const;
|
|
|
|
HeaderRefs::value_type *header(int32_t token);
|
2016-01-13 14:45:52 +01:00
|
|
|
// Returns pointer to the header field with the name |name|. If no
|
|
|
|
// such header is found, returns nullptr.
|
2016-03-09 13:15:32 +01:00
|
|
|
const HeaderRefs::value_type *header(const StringRef &name) const;
|
2016-01-13 14:45:52 +01:00
|
|
|
|
2016-02-20 13:41:23 +01:00
|
|
|
void add_header_token(const StringRef &name, const StringRef &value,
|
2016-02-21 08:44:00 +01:00
|
|
|
bool no_index, int32_t token);
|
2016-01-13 14:45:52 +01:00
|
|
|
|
2016-10-01 11:18:50 +02:00
|
|
|
// Adds header field name |name|. First, the copy of header field
|
|
|
|
// name pointed by name.c_str() of length name.size() is made, and
|
|
|
|
// stored.
|
|
|
|
void alloc_add_header_name(const StringRef &name);
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
void append_last_header_key(const char *data, size_t len);
|
|
|
|
void append_last_header_value(const char *data, size_t len);
|
|
|
|
|
|
|
|
bool header_key_prev() const { return header_key_prev_; }
|
|
|
|
|
2016-02-20 13:44:08 +01:00
|
|
|
// Parses content-length, and records it in the field. If there are
|
|
|
|
// multiple Content-Length, returns -1.
|
|
|
|
int parse_content_length();
|
2016-01-13 14:45:52 +01:00
|
|
|
|
|
|
|
// Empties headers.
|
|
|
|
void clear_headers();
|
|
|
|
|
2016-02-20 13:41:23 +01:00
|
|
|
void add_trailer_token(const StringRef &name, const StringRef &value,
|
2016-02-21 08:44:00 +01:00
|
|
|
bool no_index, int32_t token);
|
2016-01-13 14:45:52 +01:00
|
|
|
|
2016-10-01 11:18:50 +02:00
|
|
|
// Adds trailer field name |name|. First, the copy of trailer field
|
|
|
|
// name pointed by name.c_str() of length name.size() is made, and
|
|
|
|
// stored.
|
|
|
|
void alloc_add_trailer_name(const StringRef &name);
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
void append_last_trailer_key(const char *data, size_t len);
|
|
|
|
void append_last_trailer_value(const char *data, size_t len);
|
|
|
|
|
|
|
|
bool trailer_key_prev() const { return trailer_key_prev_; }
|
|
|
|
|
|
|
|
// content-length, -1 if it is unknown.
|
|
|
|
int64_t content_length;
|
|
|
|
|
|
|
|
private:
|
2016-03-09 13:25:11 +01:00
|
|
|
BlockAllocator &balloc_;
|
2016-03-09 13:15:32 +01:00
|
|
|
HeaderRefs headers_;
|
2016-01-13 14:45:52 +01:00
|
|
|
// trailer fields. For HTTP/1.1, trailer fields are only included
|
|
|
|
// with chunked encoding. For HTTP/2, there is no such limit.
|
2016-03-09 13:15:32 +01:00
|
|
|
HeaderRefs trailers_;
|
2016-01-13 14:45:52 +01:00
|
|
|
// Sum of the length of name and value in headers_ and trailers_.
|
|
|
|
// This could also be increased by add_extra_buffer_size() to take
|
|
|
|
// into account for request URI in case of HTTP/1.x request.
|
|
|
|
size_t buffer_size_;
|
|
|
|
bool header_key_prev_;
|
|
|
|
bool trailer_key_prev_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Request {
|
2016-03-09 13:25:11 +01:00
|
|
|
Request(BlockAllocator &balloc)
|
2016-03-09 13:15:32 +01:00
|
|
|
: fs(balloc, 16),
|
2016-01-27 13:14:07 +01:00
|
|
|
recv_body_length(0),
|
|
|
|
unconsumed_body_length(0),
|
|
|
|
method(-1),
|
|
|
|
http_major(1),
|
|
|
|
http_minor(1),
|
|
|
|
upgrade_request(false),
|
|
|
|
http2_upgrade_seen(false),
|
|
|
|
connection_close(false),
|
|
|
|
http2_expect_body(false),
|
|
|
|
no_authority(false) {}
|
2016-01-14 15:36:47 +01:00
|
|
|
|
|
|
|
void consume(size_t len) {
|
|
|
|
assert(unconsumed_body_length >= len);
|
|
|
|
unconsumed_body_length -= len;
|
|
|
|
}
|
2016-01-13 14:45:52 +01:00
|
|
|
|
|
|
|
FieldStore fs;
|
2017-01-11 12:22:55 +01:00
|
|
|
// Timestamp when all request header fields are received.
|
|
|
|
std::shared_ptr<Timestamp> tstamp;
|
2016-01-13 14:45:52 +01:00
|
|
|
// Request scheme. For HTTP/2, this is :scheme header field value.
|
|
|
|
// For HTTP/1.1, this is deduced from URI or connection.
|
2016-03-10 14:42:07 +01:00
|
|
|
StringRef scheme;
|
2016-01-13 14:45:52 +01:00
|
|
|
// Request authority. This is HTTP/2 :authority header field value
|
|
|
|
// or host header field value. We may deduce it from absolute-form
|
|
|
|
// HTTP/1 request. We also store authority-form HTTP/1 request.
|
|
|
|
// This could be empty if request comes from HTTP/1.0 without Host
|
|
|
|
// header field and origin-form.
|
2016-03-10 14:42:07 +01:00
|
|
|
StringRef authority;
|
2016-01-13 14:45:52 +01:00
|
|
|
// Request path, including query component. For HTTP/1.1, this is
|
|
|
|
// request-target. For HTTP/2, this is :path header field value.
|
|
|
|
// For CONNECT request, this is empty.
|
2016-03-10 14:42:07 +01:00
|
|
|
StringRef path;
|
2016-01-14 15:14:58 +01:00
|
|
|
// the length of request body received so far
|
|
|
|
int64_t recv_body_length;
|
2016-01-14 15:36:47 +01:00
|
|
|
// The number of bytes not consumed by the application yet.
|
|
|
|
size_t unconsumed_body_length;
|
2016-01-13 14:45:52 +01:00
|
|
|
int method;
|
|
|
|
// HTTP major and minor version
|
|
|
|
int http_major, http_minor;
|
|
|
|
// Returns true if the request is HTTP upgrade (HTTP Upgrade or
|
|
|
|
// CONNECT method). Upgrade to HTTP/2 is excluded. For HTTP/2
|
|
|
|
// Upgrade, check get_http2_upgrade_request().
|
|
|
|
bool upgrade_request;
|
|
|
|
// true if h2c is seen in Upgrade header field.
|
|
|
|
bool http2_upgrade_seen;
|
|
|
|
bool connection_close;
|
|
|
|
// true if this is HTTP/2, and request body is expected. Note that
|
|
|
|
// we don't take into account HTTP method here.
|
|
|
|
bool http2_expect_body;
|
2016-01-16 13:12:51 +01:00
|
|
|
// true if request does not have any information about authority.
|
|
|
|
// This happens when: For HTTP/2 request, :authority is missing.
|
|
|
|
// For HTTP/1 request, origin or asterisk form is used.
|
|
|
|
bool no_authority;
|
2016-01-13 14:45:52 +01:00
|
|
|
};
|
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
struct Response {
|
2016-03-09 13:25:11 +01:00
|
|
|
Response(BlockAllocator &balloc)
|
2016-03-09 13:15:32 +01:00
|
|
|
: fs(balloc, 32),
|
2016-01-27 13:14:07 +01:00
|
|
|
recv_body_length(0),
|
|
|
|
unconsumed_body_length(0),
|
|
|
|
http_status(0),
|
|
|
|
http_major(1),
|
|
|
|
http_minor(1),
|
2016-04-27 14:19:28 +02:00
|
|
|
connection_close(false),
|
|
|
|
headers_only(false) {}
|
2016-01-14 15:49:21 +01:00
|
|
|
|
|
|
|
void consume(size_t len) {
|
|
|
|
assert(unconsumed_body_length >= len);
|
|
|
|
unconsumed_body_length -= len;
|
|
|
|
}
|
2016-01-13 16:37:45 +01:00
|
|
|
|
2018-01-04 14:05:25 +01:00
|
|
|
// returns true if a resource denoted by scheme, authority, and path
|
|
|
|
// has already been pushed.
|
|
|
|
bool is_resource_pushed(const StringRef &scheme, const StringRef &authority,
|
|
|
|
const StringRef &path) const {
|
|
|
|
if (!pushed_resources) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return std::find(std::begin(*pushed_resources), std::end(*pushed_resources),
|
|
|
|
std::make_tuple(scheme, authority, path)) !=
|
|
|
|
std::end(*pushed_resources);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remember that a resource denoted by scheme, authority, and path
|
|
|
|
// is pushed.
|
|
|
|
void resource_pushed(const StringRef &scheme, const StringRef &authority,
|
|
|
|
const StringRef &path) {
|
|
|
|
if (!pushed_resources) {
|
|
|
|
pushed_resources = make_unique<
|
|
|
|
std::vector<std::tuple<StringRef, StringRef, StringRef>>>();
|
|
|
|
}
|
|
|
|
pushed_resources->emplace_back(scheme, authority, path);
|
|
|
|
}
|
|
|
|
|
2016-01-13 16:37:45 +01:00
|
|
|
FieldStore fs;
|
2018-01-04 14:05:25 +01:00
|
|
|
// array of the tuple of scheme, authority, and path of pushed
|
|
|
|
// resource. This is required because RFC 8297 says that server
|
|
|
|
// typically includes header fields appeared in non-final response
|
|
|
|
// header fields in final response header fields. Without checking
|
|
|
|
// that a particular resource has already been pushed, or not, we
|
|
|
|
// end up pushing the same resource at least twice. It is unknown
|
|
|
|
// that we should use more complex data structure (e.g., std::set)
|
|
|
|
// to find the resources faster.
|
|
|
|
std::unique_ptr<std::vector<std::tuple<StringRef, StringRef, StringRef>>>
|
|
|
|
pushed_resources;
|
2016-01-14 15:20:44 +01:00
|
|
|
// the length of response body received so far
|
|
|
|
int64_t recv_body_length;
|
2016-01-14 15:49:21 +01:00
|
|
|
// The number of bytes not consumed by the application yet. This is
|
|
|
|
// mainly for HTTP/2 backend.
|
|
|
|
size_t unconsumed_body_length;
|
2016-01-13 16:37:45 +01:00
|
|
|
// HTTP status code
|
|
|
|
unsigned int http_status;
|
|
|
|
int http_major, http_minor;
|
|
|
|
bool connection_close;
|
2016-04-27 14:19:28 +02:00
|
|
|
// true if response only consists of HEADERS, and it bears
|
|
|
|
// END_STREAM. This is used to tell Http2Upstream that it can send
|
|
|
|
// response with single HEADERS with END_STREAM flag only.
|
|
|
|
bool headers_only;
|
2016-01-13 16:37:45 +01:00
|
|
|
};
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
class Downstream {
|
|
|
|
public:
|
2016-01-14 16:09:53 +01:00
|
|
|
Downstream(Upstream *upstream, MemchunkPool *mcpool, int32_t stream_id);
|
2012-06-04 16:48:31 +02:00
|
|
|
~Downstream();
|
2013-08-03 11:51:01 +02:00
|
|
|
void reset_upstream(Upstream *upstream);
|
2014-11-27 15:39:04 +01: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;
|
2016-01-20 03:16:49 +01:00
|
|
|
void set_assoc_stream_id(int32_t stream_id);
|
|
|
|
int32_t get_assoc_stream_id() 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-11-27 15:39:04 +01:00
|
|
|
int attach_downstream_connection(std::unique_ptr<DownstreamConnection> dconn);
|
2014-08-18 17:16:51 +02:00
|
|
|
void detach_downstream_connection();
|
2014-11-27 15:39:04 +01: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.
|
2014-12-27 18:59:06 +01:00
|
|
|
bool request_buf_full();
|
2013-07-31 14:48:37 +02:00
|
|
|
// Returns true if upgrade (HTTP Upgrade or CONNECT) is succeeded.
|
2015-05-26 15:26:17 +02:00
|
|
|
// This should not depend on inspect_http1_response().
|
2013-07-31 14:48:37 +02:00
|
|
|
void check_upgrade_fulfilled();
|
2017-10-28 15:25:42 +02:00
|
|
|
// Returns true if the upgrade is succeeded as a result of the call
|
2015-05-25 17:00:11 +02:00
|
|
|
// check_upgrade_fulfilled(). HTTP/2 Upgrade is excluded.
|
2013-07-31 14:48:37 +02:00
|
|
|
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.
|
2016-03-09 13:15:32 +01:00
|
|
|
StringRef get_http2_settings() const;
|
2016-01-13 14:45:52 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
// downstream request API
|
2016-01-13 14:45:52 +01:00
|
|
|
const Request &request() const { return req_; }
|
|
|
|
Request &request() { return req_; }
|
|
|
|
|
2015-11-05 14:48:54 +01:00
|
|
|
// Count number of crumbled cookies
|
|
|
|
size_t count_crumble_request_cookie();
|
|
|
|
// Crumbles (split cookie by ";") in request_headers_ and adds them
|
|
|
|
// in |nva|. Headers::no_index is inherited.
|
|
|
|
void crumble_request_cookie(std::vector<nghttp2_nv> &nva);
|
2016-01-13 16:44:10 +01:00
|
|
|
// Assembles request cookies. The opposite operation against
|
|
|
|
// crumble_request_cookie().
|
2016-03-18 15:50:04 +01:00
|
|
|
StringRef assemble_request_cookie();
|
2016-01-13 14:45:52 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void
|
|
|
|
set_request_start_time(std::chrono::high_resolution_clock::time_point time);
|
|
|
|
const std::chrono::high_resolution_clock::time_point &
|
|
|
|
get_request_start_time() 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
|
|
|
int push_upload_data_chunk(const uint8_t *data, size_t datalen);
|
|
|
|
int end_upload_data();
|
2015-01-17 13:31:28 +01:00
|
|
|
// Validates that received request body length and content-length
|
|
|
|
// matches.
|
2016-01-14 15:14:58 +01:00
|
|
|
bool validate_request_recv_body_length() const;
|
2016-03-10 14:42:07 +01:00
|
|
|
void set_request_downstream_host(const StringRef &host);
|
2014-07-25 16:13:27 +02:00
|
|
|
bool expect_response_body() const;
|
2016-04-27 16:00:36 +02:00
|
|
|
bool expect_response_trailer() 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,
|
2015-01-19 15:44:23 +01:00
|
|
|
MSG_RESET,
|
|
|
|
// header contains invalid header field. We can safely send error
|
|
|
|
// response (502) to a client.
|
|
|
|
MSG_BAD_HEADER,
|
2015-04-29 14:39:46 +02:00
|
|
|
// header fields in HTTP/1 request exceed the configuration limit.
|
|
|
|
// This state is only transitioned from INITIAL state, and solely
|
|
|
|
// used to signal 431 status code to the client.
|
|
|
|
HTTP1_REQUEST_HEADER_TOO_LARGE,
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
void set_request_state(int state);
|
|
|
|
int get_request_state() const;
|
2015-01-20 17:47:43 +01:00
|
|
|
DefaultMemchunks *get_request_buf();
|
2015-02-17 15:15:53 +01:00
|
|
|
void set_request_pending(bool f);
|
|
|
|
bool get_request_pending() const;
|
2016-09-14 17:25:41 +02:00
|
|
|
void set_request_header_sent(bool f);
|
2016-12-25 13:22:34 +01:00
|
|
|
bool get_request_header_sent() const;
|
2015-02-17 15:15:53 +01:00
|
|
|
// Returns true if request is ready to be submitted to downstream.
|
2016-09-14 17:25:41 +02:00
|
|
|
// When sending pending request, get_request_pending() should be
|
|
|
|
// checked too because this function may return true when
|
|
|
|
// get_request_pending() returns false.
|
2015-02-17 15:15:53 +01:00
|
|
|
bool request_submission_ready() const;
|
2016-01-13 16:37:45 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
// downstream response API
|
2016-01-13 16:37:45 +01:00
|
|
|
const Response &response() const { return resp_; }
|
|
|
|
Response &response() { return resp_; }
|
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
// Rewrites the location response header field.
|
2016-03-10 14:42:07 +01:00
|
|
|
void rewrite_location_response_header(const StringRef &upstream_scheme);
|
2016-01-13 16:37:45 +01:00
|
|
|
|
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);
|
2016-01-13 16:37:45 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
void set_response_state(int state);
|
|
|
|
int get_response_state() const;
|
2015-01-20 17:47:43 +01:00
|
|
|
DefaultMemchunks *get_response_buf();
|
2014-12-27 18:59:06 +01:00
|
|
|
bool response_buf_full();
|
2015-01-17 11:33:30 +01:00
|
|
|
// Validates that received response body length and content-length
|
|
|
|
// matches.
|
2016-01-14 15:20:44 +01:00
|
|
|
bool validate_response_recv_body_length() 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();
|
2015-05-26 15:26:17 +02:00
|
|
|
// True if the response is non-final (1xx status code). Note that
|
|
|
|
// if connection was upgraded, 101 status code is treated as final.
|
2014-07-23 16:32:57 +02:00
|
|
|
bool get_non_final_response() const;
|
2017-09-21 14:42:56 +02:00
|
|
|
// True if protocol version used by client supports non final
|
|
|
|
// response. Only HTTP/1.1 and HTTP/2 clients support it.
|
|
|
|
bool supports_non_final_response() const;
|
2014-07-23 16:32:57 +02:00
|
|
|
void set_expect_final_response(bool f);
|
|
|
|
bool get_expect_final_response() 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();
|
|
|
|
|
2015-01-05 16:30:57 +01:00
|
|
|
// Resets upstream read timer. If it is active, timeout value is
|
|
|
|
// reset. If it is not active, timer will be started.
|
2014-08-09 11:47:45 +02:00
|
|
|
void reset_upstream_rtimer();
|
2015-01-05 16:30:57 +01:00
|
|
|
// Resets upstream write timer. If it is active, timeout value is
|
|
|
|
// reset. If it is not active, timer will be started. This
|
|
|
|
// function also resets read timer if it has been started.
|
2014-08-09 11:47:45 +02:00
|
|
|
void reset_upstream_wtimer();
|
2015-01-05 16:30:57 +01:00
|
|
|
// Makes sure that upstream write timer is started. If it has been
|
|
|
|
// started, do nothing. Otherwise, write timer will be started.
|
2014-08-09 11:47:45 +02:00
|
|
|
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 reset_downstream_rtimer();
|
|
|
|
void reset_downstream_wtimer();
|
|
|
|
void ensure_downstream_wtimer();
|
|
|
|
void disable_downstream_rtimer();
|
|
|
|
void disable_downstream_wtimer();
|
2014-11-23 09:24:23 +01:00
|
|
|
|
|
|
|
// Returns true if accesslog can be written for this downstream.
|
|
|
|
bool accesslog_ready() const;
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2015-02-02 17:47:04 +01:00
|
|
|
// Increment retry count
|
|
|
|
void add_retry();
|
|
|
|
// true if retry attempt should not be done.
|
|
|
|
bool no_more_retry() const;
|
|
|
|
|
2015-03-11 16:17:05 +01:00
|
|
|
int get_dispatch_state() const;
|
|
|
|
void set_dispatch_state(int s);
|
|
|
|
|
|
|
|
void attach_blocked_link(BlockedLink *l);
|
2015-07-15 13:44:44 +02:00
|
|
|
BlockedLink *detach_blocked_link();
|
2015-03-11 16:17:05 +01:00
|
|
|
|
2015-07-22 14:41:16 +02:00
|
|
|
// Returns true if downstream_connection can be detached and reused.
|
|
|
|
bool can_detach_downstream_connection() const;
|
|
|
|
|
2015-10-03 04:10:07 +02:00
|
|
|
DefaultMemchunks pop_response_buf();
|
|
|
|
|
2016-03-09 13:25:11 +01:00
|
|
|
BlockAllocator &get_block_allocator();
|
2016-03-09 13:15:32 +01:00
|
|
|
|
2016-03-12 07:05:20 +01:00
|
|
|
void add_rcbuf(nghttp2_rcbuf *rcbuf);
|
|
|
|
|
2016-08-04 17:04:47 +02:00
|
|
|
void
|
|
|
|
set_downstream_addr_group(const std::shared_ptr<DownstreamAddrGroup> &group);
|
|
|
|
void set_addr(const DownstreamAddr *addr);
|
|
|
|
|
|
|
|
const DownstreamAddr *get_addr() const;
|
|
|
|
|
2017-01-11 11:27:08 +01:00
|
|
|
void set_accesslog_written(bool f);
|
|
|
|
|
2017-10-25 17:45:22 +02:00
|
|
|
// Finds affinity cookie from request header fields. The name of
|
|
|
|
// cookie is given in |name|. If an affinity cookie is found, it is
|
|
|
|
// assigned to a member function, and is returned. If it is not
|
|
|
|
// found, or is malformed, returns 0.
|
|
|
|
uint32_t find_affinity_cookie(const StringRef &name);
|
|
|
|
// Set |h| as affinity cookie.
|
|
|
|
void renew_affinity_cookie(uint32_t h);
|
|
|
|
// Returns affinity cookie to send. If it does not need to be sent,
|
|
|
|
// for example, because the value is retrieved from a request header
|
|
|
|
// field, returns 0.
|
|
|
|
uint32_t get_affinity_cookie_to_send() const;
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
enum {
|
|
|
|
EVENT_ERROR = 0x1,
|
|
|
|
EVENT_TIMEOUT = 0x2,
|
|
|
|
};
|
|
|
|
|
2015-03-11 16:17:05 +01:00
|
|
|
enum {
|
|
|
|
DISPATCH_NONE,
|
|
|
|
DISPATCH_PENDING,
|
|
|
|
DISPATCH_BLOCKED,
|
|
|
|
DISPATCH_ACTIVE,
|
|
|
|
DISPATCH_FAILURE,
|
|
|
|
};
|
|
|
|
|
|
|
|
Downstream *dlnext, *dlprev;
|
|
|
|
|
2016-01-14 15:54:28 +01:00
|
|
|
// the length of response body sent to upstream client
|
|
|
|
int64_t response_sent_body_length;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
private:
|
2016-03-09 13:25:11 +01:00
|
|
|
BlockAllocator balloc_;
|
2016-03-09 13:15:32 +01:00
|
|
|
|
2016-03-12 07:05:20 +01:00
|
|
|
std::vector<nghttp2_rcbuf *> rcbufs_;
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
Request req_;
|
2016-01-13 16:37:45 +01:00
|
|
|
Response resp_;
|
2015-03-08 08:29:26 +01:00
|
|
|
|
2015-01-21 14:28:15 +01:00
|
|
|
std::chrono::high_resolution_clock::time_point request_start_time_;
|
|
|
|
|
2015-02-08 11:41:45 +01:00
|
|
|
// host we requested to downstream. This is used to rewrite
|
|
|
|
// location header field to decide the location should be rewritten
|
|
|
|
// or not.
|
2016-03-10 14:42:07 +01:00
|
|
|
StringRef request_downstream_host_;
|
2014-06-15 09:14:00 +02:00
|
|
|
|
2015-01-20 17:47:43 +01:00
|
|
|
DefaultMemchunks request_buf_;
|
|
|
|
DefaultMemchunks response_buf_;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
ev_timer upstream_rtimer_;
|
|
|
|
ev_timer upstream_wtimer_;
|
|
|
|
|
|
|
|
ev_timer downstream_rtimer_;
|
|
|
|
ev_timer downstream_wtimer_;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
Upstream *upstream_;
|
2014-08-18 17:16:51 +02:00
|
|
|
std::unique_ptr<DownstreamConnection> dconn_;
|
2014-08-09 11:47:45 +02:00
|
|
|
|
2017-12-17 05:23:37 +01:00
|
|
|
// only used by HTTP/2 upstream
|
2015-03-11 16:17:05 +01:00
|
|
|
BlockedLink *blocked_link_;
|
2016-08-04 17:04:47 +02:00
|
|
|
// The backend address used to fulfill this request. These are for
|
|
|
|
// logging purpose.
|
|
|
|
std::shared_ptr<DownstreamAddrGroup> group_;
|
|
|
|
const DownstreamAddr *addr_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// How many times we tried in backend connection
|
2015-02-02 17:47:04 +01:00
|
|
|
size_t num_retry_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// The stream ID in frontend connection
|
2012-06-04 16:48:31 +02:00
|
|
|
int32_t stream_id_;
|
2016-01-20 03:16:49 +01:00
|
|
|
// The associated stream ID in frontend connection if this is pushed
|
|
|
|
// stream.
|
|
|
|
int32_t assoc_stream_id_;
|
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_;
|
2017-10-25 17:45:22 +02:00
|
|
|
// An affinity cookie value.
|
|
|
|
uint32_t affinity_cookie_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// request state
|
2013-12-06 15:17:38 +01:00
|
|
|
int request_state_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// response state
|
2013-12-06 15:17:38 +01:00
|
|
|
int response_state_;
|
2017-12-17 05:23:37 +01:00
|
|
|
// only used by HTTP/2 upstream
|
2015-03-11 16:17:05 +01:00
|
|
|
int dispatch_state_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// true if the connection is upgraded (HTTP Upgrade or CONNECT),
|
|
|
|
// excluding upgrade to HTTP/2.
|
2013-07-31 14:48:37 +02:00
|
|
|
bool upgraded_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// true if backend request uses chunked transfer-encoding
|
2012-06-04 16:48:31 +02:00
|
|
|
bool chunked_request_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// true if response to client uses chunked transfer-encoding
|
2012-06-04 16:48:31 +02:00
|
|
|
bool chunked_response_;
|
2016-01-14 16:05:56 +01:00
|
|
|
// true if we have not got final response code
|
2014-07-23 16:32:57 +02:00
|
|
|
bool expect_final_response_;
|
2015-02-17 15:15:53 +01:00
|
|
|
// true if downstream request is pending because backend connection
|
|
|
|
// has not been established or should be checked before use;
|
|
|
|
// currently used only with HTTP/2 connection.
|
|
|
|
bool request_pending_;
|
2016-09-14 17:25:41 +02:00
|
|
|
// true if downstream request header is considered to be sent.
|
|
|
|
bool request_header_sent_;
|
2017-01-11 11:27:08 +01:00
|
|
|
// true if access.log has been written.
|
|
|
|
bool accesslog_written_;
|
2017-10-25 17:45:22 +02:00
|
|
|
// true if affinity cookie is generated for this request.
|
|
|
|
bool new_affinity_cookie_;
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_DOWNSTREAM_H
|