12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2012 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#ifndef SHRPX_HTTP_DOWNSTREAM_CONNECTION_H
262c593315Sopenharmony_ci#define SHRPX_HTTP_DOWNSTREAM_CONNECTION_H
272c593315Sopenharmony_ci
282c593315Sopenharmony_ci#include "shrpx.h"
292c593315Sopenharmony_ci
302c593315Sopenharmony_ci#include "llhttp.h"
312c593315Sopenharmony_ci
322c593315Sopenharmony_ci#include "shrpx_downstream_connection.h"
332c593315Sopenharmony_ci#include "shrpx_io_control.h"
342c593315Sopenharmony_ci#include "shrpx_connection.h"
352c593315Sopenharmony_ci
362c593315Sopenharmony_cinamespace shrpx {
372c593315Sopenharmony_ci
382c593315Sopenharmony_ciclass DownstreamConnectionPool;
392c593315Sopenharmony_ciclass Worker;
402c593315Sopenharmony_cistruct DownstreamAddrGroup;
412c593315Sopenharmony_cistruct DownstreamAddr;
422c593315Sopenharmony_cistruct DNSQuery;
432c593315Sopenharmony_ci
442c593315Sopenharmony_ciclass HttpDownstreamConnection : public DownstreamConnection {
452c593315Sopenharmony_cipublic:
462c593315Sopenharmony_ci  HttpDownstreamConnection(const std::shared_ptr<DownstreamAddrGroup> &group,
472c593315Sopenharmony_ci                           DownstreamAddr *addr, struct ev_loop *loop,
482c593315Sopenharmony_ci                           Worker *worker);
492c593315Sopenharmony_ci  virtual ~HttpDownstreamConnection();
502c593315Sopenharmony_ci  virtual int attach_downstream(Downstream *downstream);
512c593315Sopenharmony_ci  virtual void detach_downstream(Downstream *downstream);
522c593315Sopenharmony_ci
532c593315Sopenharmony_ci  virtual int push_request_headers();
542c593315Sopenharmony_ci  virtual int push_upload_data_chunk(const uint8_t *data, size_t datalen);
552c593315Sopenharmony_ci  virtual int end_upload_data();
562c593315Sopenharmony_ci  void end_upload_data_chunk();
572c593315Sopenharmony_ci
582c593315Sopenharmony_ci  virtual void pause_read(IOCtrlReason reason);
592c593315Sopenharmony_ci  virtual int resume_read(IOCtrlReason reason, size_t consumed);
602c593315Sopenharmony_ci  virtual void force_resume_read();
612c593315Sopenharmony_ci
622c593315Sopenharmony_ci  virtual int on_read();
632c593315Sopenharmony_ci  virtual int on_write();
642c593315Sopenharmony_ci
652c593315Sopenharmony_ci  virtual void on_upstream_change(Upstream *upstream);
662c593315Sopenharmony_ci
672c593315Sopenharmony_ci  virtual bool poolable() const;
682c593315Sopenharmony_ci
692c593315Sopenharmony_ci  virtual const std::shared_ptr<DownstreamAddrGroup> &
702c593315Sopenharmony_ci  get_downstream_addr_group() const;
712c593315Sopenharmony_ci  virtual DownstreamAddr *get_addr() const;
722c593315Sopenharmony_ci
732c593315Sopenharmony_ci  int initiate_connection();
742c593315Sopenharmony_ci
752c593315Sopenharmony_ci  int write_first();
762c593315Sopenharmony_ci  int read_clear();
772c593315Sopenharmony_ci  int write_clear();
782c593315Sopenharmony_ci  int read_tls();
792c593315Sopenharmony_ci  int write_tls();
802c593315Sopenharmony_ci
812c593315Sopenharmony_ci  int process_input(const uint8_t *data, size_t datalen);
822c593315Sopenharmony_ci  int tls_handshake();
832c593315Sopenharmony_ci
842c593315Sopenharmony_ci  int connected();
852c593315Sopenharmony_ci  void signal_write();
862c593315Sopenharmony_ci  int actual_signal_write();
872c593315Sopenharmony_ci
882c593315Sopenharmony_ci  // Returns address used to connect to backend.  Could be nullptr.
892c593315Sopenharmony_ci  const Address *get_raddr() const;
902c593315Sopenharmony_ci
912c593315Sopenharmony_ci  int noop();
922c593315Sopenharmony_ci
932c593315Sopenharmony_ci  int process_blocked_request_buf();
942c593315Sopenharmony_ci
952c593315Sopenharmony_ciprivate:
962c593315Sopenharmony_ci  Connection conn_;
972c593315Sopenharmony_ci  std::function<int(HttpDownstreamConnection &)> on_read_, on_write_,
982c593315Sopenharmony_ci      signal_write_;
992c593315Sopenharmony_ci  Worker *worker_;
1002c593315Sopenharmony_ci  // nullptr if TLS is not used.
1012c593315Sopenharmony_ci  SSL_CTX *ssl_ctx_;
1022c593315Sopenharmony_ci  std::shared_ptr<DownstreamAddrGroup> group_;
1032c593315Sopenharmony_ci  // Address of remote endpoint
1042c593315Sopenharmony_ci  DownstreamAddr *addr_;
1052c593315Sopenharmony_ci  // Actual remote address used to contact backend.  This is initially
1062c593315Sopenharmony_ci  // nullptr, and may point to either &addr_->addr, or
1072c593315Sopenharmony_ci  // resolved_addr_.get().
1082c593315Sopenharmony_ci  const Address *raddr_;
1092c593315Sopenharmony_ci  // Resolved IP address if dns parameter is used
1102c593315Sopenharmony_ci  std::unique_ptr<Address> resolved_addr_;
1112c593315Sopenharmony_ci  std::unique_ptr<DNSQuery> dns_query_;
1122c593315Sopenharmony_ci  IOControl ioctrl_;
1132c593315Sopenharmony_ci  llhttp_t response_htp_;
1142c593315Sopenharmony_ci  // true if first write succeeded.
1152c593315Sopenharmony_ci  bool first_write_done_;
1162c593315Sopenharmony_ci  // true if this object can be reused
1172c593315Sopenharmony_ci  bool reusable_;
1182c593315Sopenharmony_ci  // true if request header is written to request buffer.
1192c593315Sopenharmony_ci  bool request_header_written_;
1202c593315Sopenharmony_ci};
1212c593315Sopenharmony_ci
1222c593315Sopenharmony_ci} // namespace shrpx
1232c593315Sopenharmony_ci
1242c593315Sopenharmony_ci#endif // SHRPX_HTTP_DOWNSTREAM_CONNECTION_H
125