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.
|
|
|
|
*/
|
2013-07-26 13:12:55 +02:00
|
|
|
#ifndef SHRPX_HTTP2_UPSTREAM_H
|
|
|
|
#define SHRPX_HTTP2_UPSTREAM_H
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
2013-09-26 14:46:35 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <ev.h>
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
#include <nghttp2/nghttp2.h>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
#include "shrpx_upstream.h"
|
|
|
|
#include "shrpx_downstream_queue.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
#include "memchunk.h"
|
2015-10-02 15:42:46 +02:00
|
|
|
#include "buffer.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
class ClientHandler;
|
2013-08-03 11:51:01 +02:00
|
|
|
class HttpsUpstream;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2013-07-26 12:38:54 +02:00
|
|
|
class Http2Upstream : public Upstream {
|
2012-06-04 16:48:31 +02:00
|
|
|
public:
|
2013-07-26 12:38:54 +02:00
|
|
|
Http2Upstream(ClientHandler *handler);
|
|
|
|
virtual ~Http2Upstream();
|
2012-06-04 16:48:31 +02:00
|
|
|
virtual int on_read();
|
2012-06-04 20:11:43 +02:00
|
|
|
virtual int on_write();
|
2014-08-09 11:47:45 +02:00
|
|
|
virtual int on_timeout(Downstream *downstream);
|
2014-06-27 15:34:54 +02:00
|
|
|
virtual int on_downstream_abort_request(Downstream *downstream,
|
|
|
|
unsigned int status_code);
|
2017-02-18 10:23:06 +01:00
|
|
|
virtual int
|
|
|
|
on_downstream_abort_request_with_https_redirect(Downstream *downstream);
|
2014-11-27 15:39:04 +01:00
|
|
|
virtual ClientHandler *get_client_handler() const;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
virtual int downstream_read(DownstreamConnection *dconn);
|
|
|
|
virtual int downstream_write(DownstreamConnection *dconn);
|
|
|
|
virtual int downstream_eof(DownstreamConnection *dconn);
|
|
|
|
virtual int downstream_error(DownstreamConnection *dconn, int events);
|
|
|
|
|
2014-08-18 15:59:31 +02:00
|
|
|
void add_pending_downstream(std::unique_ptr<Downstream> downstream);
|
2012-06-04 16:48:31 +02:00
|
|
|
void remove_downstream(Downstream *downstream);
|
2012-06-07 17:36:19 +02:00
|
|
|
|
2014-08-23 10:34:56 +02:00
|
|
|
int rst_stream(Downstream *downstream, uint32_t error_code);
|
|
|
|
int terminate_session(uint32_t error_code);
|
2013-09-08 07:29:18 +02:00
|
|
|
int error_reply(Downstream *downstream, unsigned int status_code);
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
virtual void pause_read(IOCtrlReason reason);
|
2014-08-21 14:22:16 +02:00
|
|
|
virtual int resume_read(IOCtrlReason reason, Downstream *downstream,
|
|
|
|
size_t consumed);
|
2012-11-18 13:23:13 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
virtual int on_downstream_header_complete(Downstream *downstream);
|
2014-11-27 15:39:04 +01:00
|
|
|
virtual int on_downstream_body(Downstream *downstream, const uint8_t *data,
|
|
|
|
size_t len, bool flush);
|
2012-06-04 16:48:31 +02:00
|
|
|
virtual int on_downstream_body_complete(Downstream *downstream);
|
2012-06-09 18:36:30 +02:00
|
|
|
|
2014-11-18 17:59:09 +01:00
|
|
|
virtual void on_handler_delete();
|
2016-09-14 15:16:07 +02:00
|
|
|
virtual int on_downstream_reset(Downstream *downstream, bool no_retry);
|
2015-09-03 15:29:16 +02:00
|
|
|
virtual int send_reply(Downstream *downstream, const uint8_t *body,
|
|
|
|
size_t bodylen);
|
2016-03-03 16:38:43 +01:00
|
|
|
virtual int initiate_push(Downstream *downstream, const StringRef &uri);
|
2015-10-02 15:42:46 +02:00
|
|
|
virtual int response_riovec(struct iovec *iov, int iovcnt) const;
|
|
|
|
virtual void response_drain(size_t n);
|
|
|
|
virtual bool response_empty() const;
|
|
|
|
|
2015-11-15 16:12:54 +01:00
|
|
|
virtual Downstream *on_downstream_push_promise(Downstream *downstream,
|
|
|
|
int32_t promised_stream_id);
|
|
|
|
virtual int
|
|
|
|
on_downstream_push_promise_complete(Downstream *downstream,
|
|
|
|
Downstream *promised_downstream);
|
|
|
|
virtual bool push_enabled() const;
|
|
|
|
virtual void cancel_premature_downstream(Downstream *promised_downstream);
|
|
|
|
|
2012-06-09 18:36:30 +02:00
|
|
|
bool get_flow_control() const;
|
2014-03-30 12:09:21 +02:00
|
|
|
// Perform HTTP/2 upgrade from |upstream|. On success, this object
|
2013-08-03 11:51:01 +02:00
|
|
|
// takes ownership of the |upstream|. This function returns 0 if it
|
|
|
|
// succeeds, or -1.
|
|
|
|
int upgrade_upstream(HttpsUpstream *upstream);
|
2014-12-27 18:59:06 +01:00
|
|
|
void start_settings_timer();
|
2013-10-30 16:44:23 +01:00
|
|
|
void stop_settings_timer();
|
2014-07-25 14:26:03 +02:00
|
|
|
int consume(int32_t stream_id, size_t len);
|
2014-07-25 17:40:06 +02:00
|
|
|
void log_response_headers(Downstream *downstream,
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::vector<nghttp2_nv> &nva) const;
|
2014-12-04 17:07:00 +01:00
|
|
|
void start_downstream(Downstream *downstream);
|
2015-03-11 16:17:05 +01:00
|
|
|
void initiate_downstream(Downstream *downstream);
|
2014-11-05 16:56:07 +01:00
|
|
|
|
2015-01-21 17:43:56 +01:00
|
|
|
void submit_goaway();
|
|
|
|
void check_shutdown();
|
2017-02-20 15:36:50 +01:00
|
|
|
// Starts graceful shutdown period.
|
|
|
|
void start_graceful_shutdown();
|
2015-01-21 17:43:56 +01:00
|
|
|
|
2015-02-07 08:09:49 +01:00
|
|
|
int prepare_push_promise(Downstream *downstream);
|
2016-03-10 14:42:07 +01:00
|
|
|
int submit_push_promise(const StringRef &scheme, const StringRef &authority,
|
|
|
|
const StringRef &path, Downstream *downstream);
|
2015-02-07 08:09:49 +01:00
|
|
|
|
2017-02-20 15:36:50 +01:00
|
|
|
// Called when new request has started.
|
|
|
|
void on_start_request(const nghttp2_frame *frame);
|
2015-03-11 16:17:05 +01:00
|
|
|
int on_request_headers(Downstream *downstream, const nghttp2_frame *frame);
|
|
|
|
|
2016-01-26 15:04:53 +01:00
|
|
|
DefaultMemchunks *get_response_buf();
|
2015-10-03 04:10:07 +02:00
|
|
|
|
2016-09-08 15:49:36 +02:00
|
|
|
size_t get_max_buffer_size() const;
|
|
|
|
|
2017-02-18 10:23:06 +01:00
|
|
|
int redirect_to_https(Downstream *downstream);
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
private:
|
2016-01-26 15:04:53 +01:00
|
|
|
DefaultMemchunks wb_;
|
2013-09-26 14:46:35 +02:00
|
|
|
std::unique_ptr<HttpsUpstream> pre_upstream_;
|
2014-12-27 18:59:06 +01:00
|
|
|
DownstreamQueue downstream_queue_;
|
|
|
|
ev_timer settings_timer_;
|
2015-01-21 17:43:56 +01:00
|
|
|
ev_timer shutdown_timer_;
|
|
|
|
ev_prepare prep_;
|
2013-12-06 15:17:38 +01:00
|
|
|
ClientHandler *handler_;
|
|
|
|
nghttp2_session *session_;
|
2016-09-08 15:49:36 +02:00
|
|
|
size_t max_buffer_size_;
|
2017-02-20 15:36:50 +01:00
|
|
|
// The number of requests seen so far.
|
|
|
|
size_t num_requests_;
|
2013-12-06 15:17:38 +01:00
|
|
|
bool flow_control_;
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
|
2015-02-24 07:21:10 +01:00
|
|
|
nghttp2_session_callbacks *create_http2_upstream_callbacks();
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|
|
|
|
|
2013-07-26 13:12:55 +02:00
|
|
|
#endif // SHRPX_HTTP2_UPSTREAM_H
|