proxygen
proxygen::HTTPConnector Class Reference

#include <HTTPConnector.h>

Inheritance diagram for proxygen::HTTPConnector:
folly::AsyncSocket::ConnectCallback

Classes

class  Callback
 

Public Member Functions

 HTTPConnector (Callback *callback, folly::HHWheelTimer *timeoutSet)
 
 HTTPConnector (Callback *callback, const WheelTimerInstance &timeout)
 
 ~HTTPConnector () override
 
void reset ()
 
void setPlaintextProtocol (const std::string &plaintextProto)
 
void setHTTPVersionOverride (bool enabled)
 
void connect (folly::EventBase *eventBase, const folly::SocketAddress &connectAddr, std::chrono::milliseconds timeoutMs=std::chrono::milliseconds(0), const folly::AsyncSocket::OptionMap &socketOptions=folly::AsyncSocket::emptyOptionMap, const folly::SocketAddress &bindAddr=folly::AsyncSocket::anyAddress())
 
void connectSSL (folly::EventBase *eventBase, const folly::SocketAddress &connectAddr, const std::shared_ptr< folly::SSLContext > &ctx, SSL_SESSION *session=nullptr, std::chrono::milliseconds timeoutMs=std::chrono::milliseconds(0), const folly::AsyncSocket::OptionMap &socketOptions=folly::AsyncSocket::emptyOptionMap, const folly::SocketAddress &bindAddr=folly::AsyncSocket::anyAddress(), const std::string &serverName=empty_string)
 
std::chrono::milliseconds timeElapsed ()
 
bool isBusy () const
 
void setHTTPCodecFactory (std::unique_ptr< DefaultHTTPCodecFactory > factory)
 

Protected Member Functions

void connectSuccess () noexceptoverride
 
void connectErr (const folly::AsyncSocketException &ex) noexceptoverride
 
- Protected Member Functions inherited from folly::AsyncSocket::ConnectCallback
virtual ~ConnectCallback ()=default
 

Protected Attributes

Callbackcb_
 
WheelTimerInstance timeout_
 
folly::AsyncTransportWrapper::UniquePtr socket_
 
wangle::TransportInfo transportInfo_
 
std::string plaintextProtocol_
 
TimePoint connectStart_
 
std::unique_ptr< DefaultHTTPCodecFactoryhttpCodecFactory_
 

Detailed Description

This class establishes new connections to HTTP or HTTPS servers. It can be reused, even to connect to different addresses, but it can only service setting up one connection at a time.

Definition at line 31 of file HTTPConnector.h.

Constructor & Destructor Documentation

proxygen::HTTPConnector::HTTPConnector ( Callback callback,
folly::HHWheelTimer timeoutSet 
)

Construct a HTTPConnector. The constructor arguments are those parameters HTTPConnector needs to keep a copy of through the connection process.

Parameters
callbackThe interface on which to receive the result. Whatever object is passed here MUST outlive this connector and MUST NOT be null.
timeoutSetThe timeout set to be used for the transactions that are opened on the session.

Definition at line 27 of file HTTPConnector.cpp.

Referenced by proxygen::HTTPConnector::Callback::~Callback().

29  : HTTPConnector(callback, WheelTimerInstance(timeoutSet)) {
30 }
HTTPConnector(Callback *callback, folly::HHWheelTimer *timeoutSet)
proxygen::HTTPConnector::HTTPConnector ( Callback callback,
const WheelTimerInstance timeout 
)

Definition at line 32 of file HTTPConnector.cpp.

34  : cb_(CHECK_NOTNULL(callback))
35  , timeout_(timeout)
36  , httpCodecFactory_(std::make_unique<DefaultHTTPCodecFactory>(false)) {}
std::unique_ptr< DefaultHTTPCodecFactory > httpCodecFactory_
WheelTimerInstance timeout_
proxygen::HTTPConnector::~HTTPConnector ( )
override

Clients may delete the connector at any time to cancel it. No callbacks will be received.

Definition at line 38 of file HTTPConnector.cpp.

References reset().

Referenced by proxygen::HTTPConnector::Callback::~Callback().

38  {
39  reset();
40 }

Member Function Documentation

void proxygen::HTTPConnector::connect ( folly::EventBase eventBase,
const folly::SocketAddress connectAddr,
std::chrono::milliseconds  timeoutMs = std::chrono::milliseconds(0),
const folly::AsyncSocket::OptionMap socketOptions = folly::AsyncSocket::emptyOptionMap,
const folly::SocketAddress bindAddr = folly::AsyncSocket::anyAddress() 
)

Begin the process of getting a plaintext connection to the server specified by 'connectAddr'. This function immediately starts async work and may invoke functions on Callback immediately.

Parameters
eventBaseThe event base to put events on.
connectAddrThe address to connect to.
timeoutMsOptional. If this value is greater than zero, then a connect error will be given if no connection is established within this amount of time.
socketOptionsOptional socket options to set on the connection.
bindAddrOptional address to bind to locally.

Definition at line 59 of file HTTPConnector.cpp.

References connectStart_, proxygen::getCurrentTime(), isBusy(), wangle::TransportInfo::secure, socket_, and transportInfo_.

Referenced by ScopedServerTest::connectPlainText(), ProxyService::ProxyHandler::onRequest(), TEST(), and proxygen::HTTPConnector::Callback::~Callback().

64  {
65 
66  DCHECK(!isBusy());
68  transportInfo_.secure = false;
69  auto sock = new AsyncSocket(eventBase);
70  socket_.reset(sock);
72  sock->connect(this, connectAddr, timeoutMs.count(),
73  socketOptions, bindAddr);
74 }
wangle::TransportInfo transportInfo_
folly::AsyncTransportWrapper::UniquePtr socket_
std::chrono::time_point< ClockType > getCurrentTime()
Definition: Time.h:41
void proxygen::HTTPConnector::connectErr ( const folly::AsyncSocketException ex)
overrideprotectedvirtualnoexcept

connectErr() will be invoked if the connection attempt fails.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 151 of file HTTPConnector.cpp.

References cb_, proxygen::HTTPConnector::Callback::connectError(), and socket_.

Referenced by setHTTPCodecFactory().

151  {
152  socket_.reset();
153  if (cb_) {
154  cb_->connectError(ex);
155  }
156 }
folly::AsyncTransportWrapper::UniquePtr socket_
virtual void connectError(const folly::AsyncSocketException &ex)=0
void proxygen::HTTPConnector::connectSSL ( folly::EventBase eventBase,
const folly::SocketAddress connectAddr,
const std::shared_ptr< folly::SSLContext > &  ctx,
SSL_SESSION *  session = nullptr,
std::chrono::milliseconds  timeoutMs = std::chrono::milliseconds(0),
const folly::AsyncSocket::OptionMap socketOptions = folly::AsyncSocket::emptyOptionMap,
const folly::SocketAddress bindAddr = folly::AsyncSocket::anyAddress(),
const std::string serverName = empty_string 
)

Begin the process of getting a secure connection to the server specified by 'connectAddr'. This function immediately starts async work and may invoke functions on Callback immediately.

Parameters
eventBaseThe event base to put events on.
connectAddrThe address to connect to.
ctxSSL context to use. Must not be null.
sessionOptional ssl session to use.
timeoutMsOptional. If this value is greater than zero, then a connect error will be given if no connection is established within this amount of time.
socketOptionsOptional socket options to set on the connection.
bindAddrOptional address to bind to locally.

Definition at line 76 of file HTTPConnector.cpp.

References connectStart_, proxygen::getCurrentTime(), isBusy(), wangle::TransportInfo::secure, socket_, and transportInfo_.

Referenced by ScopedServerTest::connectSSL(), and proxygen::HTTPConnector::Callback::~Callback().

84  {
85 
86  DCHECK(!isBusy());
88  transportInfo_.secure = true;
89  auto sslSock = new AsyncSSLSocket(context, eventBase);
90  if (session) {
91  sslSock->setSSLSession(session, true /* take ownership */);
92  }
93  sslSock->setServerName(serverName);
94  sslSock->forceCacheAddrOnFailure(true);
95  socket_.reset(sslSock);
97  sslSock->connect(this, connectAddr, timeoutMs.count(),
98  socketOptions, bindAddr);
99 }
context
Definition: CMakeCache.txt:563
wangle::TransportInfo transportInfo_
folly::AsyncTransportWrapper::UniquePtr socket_
std::chrono::time_point< ClockType > getCurrentTime()
Definition: Time.h:41
void proxygen::HTTPConnector::connectSuccess ( )
overrideprotectedvirtualnoexcept

connectSuccess() will be invoked when the connection has been successfully established.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 110 of file HTTPConnector.cpp.

References wangle::TransportInfo::acceptTime, wangle::TransportInfo::appProtocol, cb_, codec, connectStart_, proxygen::HTTPConnector::Callback::connectSuccess(), proxygen::getCurrentTime(), folly::AsyncSSLSocket::getNegotiatedCipherName(), wangle::SSLUtil::getResumeState(), folly::AsyncSSLSocket::getSSLVersion(), httpCodecFactory_, proxygen::millisecondsSince(), folly::gen::move, plaintextProtocol_, wangle::TransportInfo::secure, socket_, wangle::TransportInfo::sslCipher, wangle::TransportInfo::sslResume, wangle::TransportInfo::sslSetupTime, wangle::TransportInfo::sslVersion, timeout_, transportInfo_, and proxygen::UPSTREAM.

110  {
111  if (!cb_) {
112  return;
113  }
114 
115  folly::SocketAddress localAddress;
116  folly::SocketAddress peerAddress;
117  socket_->getLocalAddress(&localAddress);
118  socket_->getPeerAddress(&peerAddress);
119 
120  std::unique_ptr<HTTPCodec> codec;
121 
123  if (transportInfo_.secure) {
124  AsyncSSLSocket* sslSocket = socket_->getUnderlyingTransport<AsyncSSLSocket>();
125 
126  if (sslSocket) {
128  std::make_shared<std::string>(socket_->getApplicationProtocol());
131  std::make_shared<std::string>(sslSocket->getNegotiatedCipherName()) :
132  nullptr;
133  transportInfo_.sslVersion = sslSocket->getSSLVersion();
135  }
136  codec = httpCodecFactory_->getCodec(socket_->getApplicationProtocol(),
138  } else {
139  codec = httpCodecFactory_->getCodec(plaintextProtocol_,
141  }
142 
143  HTTPUpstreamSession* session = new HTTPUpstreamSession(
144  timeout_,
145  std::move(socket_), localAddress, peerAddress,
146  std::move(codec), transportInfo_, nullptr);
147 
148  cb_->connectSuccess(session);
149 }
virtual void connectSuccess(HTTPUpstreamSession *session)=0
wangle::TransportInfo transportInfo_
std::chrono::steady_clock::time_point acceptTime
Definition: TransportInfo.h:65
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CodecFactory codec
std::unique_ptr< DefaultHTTPCodecFactory > httpCodecFactory_
SSLResumeEnum sslResume
folly::AsyncTransportWrapper::UniquePtr socket_
static SSLResumeEnum getResumeState(folly::AsyncSSLSocket *sslSocket)
Definition: SSLUtil.cpp:46
WheelTimerInstance timeout_
std::shared_ptr< std::string > sslCipher
std::chrono::milliseconds millisecondsSince(std::chrono::time_point< ClockType > t)
Definition: Time.h:101
std::chrono::time_point< ClockType > getCurrentTime()
Definition: Time.h:41
std::shared_ptr< std::string > appProtocol
std::chrono::milliseconds sslSetupTime
virtual const char * getNegotiatedCipherName() const
std::string plaintextProtocol_
bool proxygen::HTTPConnector::isBusy ( ) const
inline
Returns
true iff this connector is busy setting up a connection. If this is false, it is safe to call connect() or connectSSL() on it again.

Definition at line 145 of file HTTPConnector.h.

References socket_.

Referenced by connect(), and connectSSL().

145 { return socket_.get(); }
folly::AsyncTransportWrapper::UniquePtr socket_
void proxygen::HTTPConnector::reset ( )

Reset the object so that it can begin a new connection. No callbacks will be invoked as a result of executing this function. After this function returns, isBusy() will return false.

Definition at line 42 of file HTTPConnector.cpp.

References cb_, and socket_.

Referenced by proxygen::HTTPConnector::Callback::~Callback(), and ~HTTPConnector().

42  {
43  if (socket_) {
44  auto cb = cb_;
45  cb_ = nullptr;
46  socket_.reset(); // This invokes connectError() but will be ignored
47  cb_ = cb;
48  }
49 }
folly::AsyncTransportWrapper::UniquePtr socket_
void proxygen::HTTPConnector::setHTTPCodecFactory ( std::unique_ptr< DefaultHTTPCodecFactory factory)
inline

Definition at line 147 of file HTTPConnector.h.

References connectErr(), proxygen::HTTPConnector::Callback::connectSuccess(), httpCodecFactory_, folly::gen::move, and folly::pushmi::__adl::noexcept().

147  {
148  httpCodecFactory_ = std::move(factory);
149  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< DefaultHTTPCodecFactory > httpCodecFactory_
void proxygen::HTTPConnector::setHTTPVersionOverride ( bool  enabled)

Overrides the HTTP version to always use the latest and greatest version we support.

Definition at line 55 of file HTTPConnector.cpp.

References httpCodecFactory_.

Referenced by proxygen::HTTPConnector::Callback::~Callback().

55  {
56  httpCodecFactory_->setForceHTTP1xCodecTo1_1(enabled);
57 }
std::unique_ptr< DefaultHTTPCodecFactory > httpCodecFactory_
void proxygen::HTTPConnector::setPlaintextProtocol ( const std::string plaintextProto)

Sets the plain text protocol to use after the connection is established.

Definition at line 51 of file HTTPConnector.cpp.

References plaintextProtocol_.

Referenced by main(), and proxygen::HTTPConnector::Callback::~Callback().

51  {
52  plaintextProtocol_ = plaintextProto;
53 }
std::string plaintextProtocol_
std::chrono::milliseconds proxygen::HTTPConnector::timeElapsed ( )
Returns
the number of milliseconds since connecting began, or zero if connecting hasn't started yet.

Definition at line 101 of file HTTPConnector.cpp.

References connectStart_, proxygen::millisecondsSince(), and proxygen::timePointInitialized().

Referenced by proxygen::HTTPConnector::Callback::~Callback().

101  {
104  }
105  return std::chrono::milliseconds(0);
106 }
std::chrono::milliseconds millisecondsSince(std::chrono::time_point< ClockType > t)
Definition: Time.h:101
bool timePointInitialized(const T &time)
Definition: Time.h:35

Member Data Documentation

Callback* proxygen::HTTPConnector::cb_
protected

Definition at line 157 of file HTTPConnector.h.

Referenced by connectErr(), connectSuccess(), and reset().

TimePoint proxygen::HTTPConnector::connectStart_
protected

Definition at line 162 of file HTTPConnector.h.

Referenced by connect(), connectSSL(), connectSuccess(), and timeElapsed().

std::unique_ptr<DefaultHTTPCodecFactory> proxygen::HTTPConnector::httpCodecFactory_
protected

Definition at line 163 of file HTTPConnector.h.

Referenced by connectSuccess(), setHTTPCodecFactory(), and setHTTPVersionOverride().

std::string proxygen::HTTPConnector::plaintextProtocol_
protected

Definition at line 161 of file HTTPConnector.h.

Referenced by connectSuccess(), and setPlaintextProtocol().

folly::AsyncTransportWrapper::UniquePtr proxygen::HTTPConnector::socket_
protected

Definition at line 159 of file HTTPConnector.h.

Referenced by connect(), connectErr(), connectSSL(), connectSuccess(), isBusy(), and reset().

WheelTimerInstance proxygen::HTTPConnector::timeout_
protected

Definition at line 158 of file HTTPConnector.h.

Referenced by connectSuccess().

wangle::TransportInfo proxygen::HTTPConnector::transportInfo_
protected

Definition at line 160 of file HTTPConnector.h.

Referenced by connect(), connectSSL(), and connectSuccess().


The documentation for this class was generated from the following files: