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_CLIENT_HANDLER_H
|
|
|
|
#define SHRPX_CLIENT_HANDLER_H
|
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
2013-09-23 17:02:02 +02:00
|
|
|
#include <memory>
|
2012-06-09 16:14:00 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <ev.h>
|
2014-03-09 06:53:28 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include "shrpx_rate_limit.h"
|
2015-02-04 13:15:58 +01:00
|
|
|
#include "shrpx_connection.h"
|
2015-01-29 14:47:37 +01:00
|
|
|
#include "buffer.h"
|
2015-04-07 15:13:01 +02:00
|
|
|
#include "memchunk.h"
|
2016-09-30 16:09:02 +02:00
|
|
|
#include "allocator.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
class Upstream;
|
2012-06-09 16:14:00 +02:00
|
|
|
class DownstreamConnection;
|
2013-08-03 11:51:01 +02:00
|
|
|
class HttpsUpstream;
|
2014-08-19 16:36:04 +02:00
|
|
|
class ConnectBlocker;
|
2014-10-13 14:09:00 +02:00
|
|
|
class DownstreamConnectionPool;
|
2015-02-11 11:18:41 +01:00
|
|
|
class Worker;
|
2017-02-16 14:46:22 +01:00
|
|
|
class Downstream;
|
2014-06-26 15:55:22 +02:00
|
|
|
struct WorkerStat;
|
2016-04-02 16:11:03 +02:00
|
|
|
struct DownstreamAddrGroup;
|
2016-06-09 15:35:59 +02:00
|
|
|
struct DownstreamAddr;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
class ClientHandler {
|
|
|
|
public:
|
2016-09-30 16:09:02 +02:00
|
|
|
ClientHandler(Worker *worker, int fd, SSL *ssl, const StringRef &ipaddr,
|
|
|
|
const StringRef &port, int family, const UpstreamAddr *faddr);
|
2012-06-04 16:48:31 +02:00
|
|
|
~ClientHandler();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-09-07 15:35:02 +02:00
|
|
|
int noop();
|
2014-12-27 18:59:06 +01:00
|
|
|
// Performs clear text I/O
|
|
|
|
int read_clear();
|
|
|
|
int write_clear();
|
|
|
|
// Performs TLS handshake
|
|
|
|
int tls_handshake();
|
|
|
|
// Performs TLS I/O
|
|
|
|
int read_tls();
|
|
|
|
int write_tls();
|
|
|
|
|
|
|
|
int upstream_noop();
|
|
|
|
int upstream_read();
|
|
|
|
int upstream_http2_connhd_read();
|
|
|
|
int upstream_http1_connhd_read();
|
|
|
|
int upstream_write();
|
|
|
|
|
2015-09-06 11:39:32 +02:00
|
|
|
int proxy_protocol_read();
|
2015-09-06 16:11:07 +02:00
|
|
|
int on_proxy_protocol_finish();
|
2015-09-06 11:39:32 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
// Performs I/O operation. Internally calls on_read()/on_write().
|
|
|
|
int do_read();
|
|
|
|
int do_write();
|
|
|
|
|
|
|
|
// Processes buffers. No underlying I/O operation will be done.
|
2012-06-04 16:48:31 +02:00
|
|
|
int on_read();
|
2014-12-27 18:59:06 +01:00
|
|
|
int on_write();
|
|
|
|
|
|
|
|
struct ev_loop *get_loop() const;
|
|
|
|
void reset_upstream_read_timeout(ev_tstamp t);
|
|
|
|
void reset_upstream_write_timeout(ev_tstamp t);
|
2016-06-23 17:04:39 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
int validate_next_proto();
|
2016-09-30 16:09:02 +02:00
|
|
|
const StringRef &get_ipaddr() const;
|
2012-06-04 16:48:31 +02:00
|
|
|
bool get_should_close_after_write() const;
|
|
|
|
void set_should_close_after_write(bool f);
|
2014-11-27 15:39:04 +01:00
|
|
|
Upstream *get_upstream();
|
2012-06-09 16:14:00 +02:00
|
|
|
|
2014-08-18 17:16:51 +02:00
|
|
|
void pool_downstream_connection(std::unique_ptr<DownstreamConnection> dconn);
|
2012-06-09 16:14:00 +02:00
|
|
|
void remove_downstream_connection(DownstreamConnection *dconn);
|
2017-02-18 10:23:06 +01:00
|
|
|
// Returns DownstreamConnection object based on request path. This
|
|
|
|
// function returns non-null DownstreamConnection, and assigns 0 to
|
|
|
|
// |err| if it succeeds, or returns nullptr, and assigns negative
|
|
|
|
// error code to |err|.
|
2015-07-09 19:52:11 +02:00
|
|
|
std::unique_ptr<DownstreamConnection>
|
2017-02-18 10:23:06 +01:00
|
|
|
get_downstream_connection(int &err, Downstream *downstream);
|
2015-04-07 15:13:01 +02:00
|
|
|
MemchunkPool *get_mcpool();
|
2014-11-27 15:39:04 +01:00
|
|
|
SSL *get_ssl() const;
|
2014-03-30 12:09:21 +02:00
|
|
|
// Call this function when HTTP/2 connection header is received at
|
2013-08-03 11:51:01 +02:00
|
|
|
// the start of the connection.
|
|
|
|
void direct_http2_upgrade();
|
2014-03-30 12:09:21 +02:00
|
|
|
// Performs HTTP/2 Upgrade from the connection managed by
|
2013-08-03 11:51:01 +02:00
|
|
|
// |http|. If this function fails, the connection must be
|
|
|
|
// terminated. This function returns 0 if it succeeds, or -1.
|
|
|
|
int perform_http2_upgrade(HttpsUpstream *http);
|
|
|
|
bool get_http2_upgrade_allowed() const;
|
2013-12-21 09:49:31 +01:00
|
|
|
// Returns upstream scheme, either "http" or "https"
|
2016-03-10 14:42:07 +01:00
|
|
|
StringRef get_upstream_scheme() const;
|
2015-02-04 13:15:58 +01:00
|
|
|
void start_immediate_shutdown();
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
// Writes upstream accesslog using |downstream|. The |downstream|
|
|
|
|
// must not be nullptr.
|
|
|
|
void write_accesslog(Downstream *downstream);
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
Worker *get_worker() const;
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2017-08-09 15:44:14 +02:00
|
|
|
// Initializes forwarded_for_.
|
|
|
|
void init_forwarded_for(int family, const StringRef &ipaddr);
|
|
|
|
|
2017-01-08 08:41:02 +01:00
|
|
|
using ReadBuf = DefaultMemchunkBuffer;
|
2015-01-08 13:28:52 +01:00
|
|
|
|
|
|
|
ReadBuf *get_rb();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
RateLimit *get_rlimit();
|
|
|
|
RateLimit *get_wlimit();
|
|
|
|
|
|
|
|
void signal_write();
|
2015-02-10 16:44:30 +01:00
|
|
|
ev_io *get_wev();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-09-06 11:39:32 +02:00
|
|
|
void setup_upstream_io_callback();
|
|
|
|
|
2016-01-15 15:04:58 +01:00
|
|
|
// Returns string suitable for use in "by" parameter of Forwarded
|
|
|
|
// header field.
|
2016-02-28 15:54:02 +01:00
|
|
|
StringRef get_forwarded_by() const;
|
2016-01-15 15:04:58 +01:00
|
|
|
// Returns string suitable for use in "for" parameter of Forwarded
|
|
|
|
// header field.
|
2016-02-28 15:54:02 +01:00
|
|
|
StringRef get_forwarded_for() const;
|
2016-01-15 15:04:58 +01:00
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
Http2Session *
|
|
|
|
select_http2_session(const std::shared_ptr<DownstreamAddrGroup> &group);
|
2016-04-02 16:11:03 +02:00
|
|
|
|
2016-06-09 15:35:59 +02:00
|
|
|
Http2Session *select_http2_session_with_affinity(
|
|
|
|
const std::shared_ptr<DownstreamAddrGroup> &group, DownstreamAddr *addr);
|
|
|
|
|
2017-10-25 17:45:22 +02:00
|
|
|
// Returns an affinity cookie value for |downstream|. |cookie_name|
|
|
|
|
// is used to inspect cookie header field in request header fields.
|
|
|
|
uint32_t get_affinity_cookie(Downstream *downstream,
|
|
|
|
const StringRef &cookie_name);
|
|
|
|
|
2016-04-27 17:19:30 +02:00
|
|
|
const UpstreamAddr *get_upstream_addr() const;
|
|
|
|
|
2016-06-23 17:04:39 +02:00
|
|
|
void repeat_read_timer();
|
|
|
|
void stop_read_timer();
|
|
|
|
|
2016-09-08 15:49:36 +02:00
|
|
|
Connection *get_connection();
|
|
|
|
|
2016-09-10 15:02:46 +02:00
|
|
|
// Stores |sni| which is TLS SNI extension value client sent in this
|
|
|
|
// connection.
|
|
|
|
void set_tls_sni(const StringRef &sni);
|
|
|
|
// Returns TLS SNI extension value client sent in this connection.
|
|
|
|
StringRef get_tls_sni() const;
|
|
|
|
|
2017-10-29 14:42:30 +01:00
|
|
|
// Returns ALPN negotiated in this connection.
|
|
|
|
StringRef get_alpn() const;
|
|
|
|
|
2016-10-01 17:19:50 +02:00
|
|
|
BlockAllocator &get_block_allocator();
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
private:
|
2016-10-03 15:06:04 +02:00
|
|
|
// Allocator to allocate memory for connection-wide objects. Make
|
|
|
|
// sure that the allocations must be bounded, and not proportional
|
|
|
|
// to the number of requests.
|
2016-09-30 16:09:02 +02:00
|
|
|
BlockAllocator balloc_;
|
2017-01-08 08:41:02 +01:00
|
|
|
DefaultMemchunkBuffer rb_;
|
2015-02-04 13:15:58 +01:00
|
|
|
Connection conn_;
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer reneg_shutdown_timer_;
|
2013-09-23 17:02:02 +02:00
|
|
|
std::unique_ptr<Upstream> upstream_;
|
2016-01-19 15:26:04 +01:00
|
|
|
// IP address of client. If UNIX domain socket is used, this is
|
|
|
|
// "localhost".
|
2016-09-30 16:09:02 +02:00
|
|
|
StringRef ipaddr_;
|
|
|
|
StringRef port_;
|
2014-11-24 07:22:10 +01:00
|
|
|
// The ALPN identifier negotiated for this connection.
|
2016-09-30 16:09:02 +02:00
|
|
|
StringRef alpn_;
|
2016-02-01 15:29:17 +01:00
|
|
|
// The client address used in "for" parameter of Forwarded header
|
|
|
|
// field.
|
2016-09-30 16:09:02 +02:00
|
|
|
StringRef forwarded_for_;
|
2016-09-10 15:02:46 +02:00
|
|
|
// lowercased TLS SNI which client sent.
|
2016-09-30 16:09:02 +02:00
|
|
|
StringRef sni_;
|
2014-12-27 18:59:06 +01:00
|
|
|
std::function<int(ClientHandler &)> read_, write_;
|
|
|
|
std::function<int(ClientHandler &)> on_read_, on_write_;
|
2016-01-31 11:41:56 +01:00
|
|
|
// Address of frontend listening socket
|
2016-02-07 09:51:53 +01:00
|
|
|
const UpstreamAddr *faddr_;
|
2015-02-11 11:18:41 +01:00
|
|
|
Worker *worker_;
|
2014-03-30 12:09:21 +02:00
|
|
|
// The number of bytes of HTTP/2 client connection header to read
|
2013-07-26 14:35:14 +02:00
|
|
|
size_t left_connhd_len_;
|
2016-07-06 15:31:28 +02:00
|
|
|
// hash for session affinity using client IP
|
|
|
|
uint32_t affinity_hash_;
|
2013-12-06 15:17:38 +01:00
|
|
|
bool should_close_after_write_;
|
2016-07-06 15:31:28 +02:00
|
|
|
// true if affinity_hash_ is computed
|
|
|
|
bool affinity_hash_computed_;
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_CLIENT_HANDLER_H
|