proxygen
HTTPConnector.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
11 
12 #include <wangle/ssl/SSLUtil.h>
20 
21 
22 using namespace folly;
23 using namespace std;
24 
25 namespace proxygen {
26 
27 HTTPConnector::HTTPConnector(Callback* callback,
28  folly::HHWheelTimer* timeoutSet)
29  : HTTPConnector(callback, WheelTimerInstance(timeoutSet)) {
30 }
31 
33  const WheelTimerInstance& timeout)
34  : cb_(CHECK_NOTNULL(callback))
35  , timeout_(timeout)
37 
39  reset();
40 }
41 
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 }
50 
51 void HTTPConnector::setPlaintextProtocol(const std::string& plaintextProto) {
52  plaintextProtocol_ = plaintextProto;
53 }
54 
56  httpCodecFactory_->setForceHTTP1xCodecTo1_1(enabled);
57 }
58 
60  EventBase* eventBase,
61  const folly::SocketAddress& connectAddr,
62  std::chrono::milliseconds timeoutMs,
63  const AsyncSocket::OptionMap& socketOptions,
64  const folly::SocketAddress& bindAddr) {
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 }
75 
77  EventBase* eventBase,
78  const folly::SocketAddress& connectAddr,
79  const shared_ptr<SSLContext>& context,
80  SSL_SESSION* session,
81  std::chrono::milliseconds timeoutMs,
82  const AsyncSocket::OptionMap& socketOptions,
83  const folly::SocketAddress& bindAddr,
84  const std::string& serverName) {
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 }
100 
101 std::chrono::milliseconds HTTPConnector::timeElapsed() {
104  }
105  return std::chrono::milliseconds(0);
106 }
107 
108 // Callback interface
109 
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 
144  timeout_,
145  std::move(socket_), localAddress, peerAddress,
146  std::move(codec), transportInfo_, nullptr);
147 
148  cb_->connectSuccess(session);
149 }
150 
152  socket_.reset();
153  if (cb_) {
154  cb_->connectError(ex);
155  }
156 }
157 
158 }
virtual void connectSuccess(HTTPUpstreamSession *session)=0
context
Definition: CMakeCache.txt:563
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
STL namespace.
std::unique_ptr< DefaultHTTPCodecFactory > httpCodecFactory_
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
SSLResumeEnum sslResume
folly::AsyncTransportWrapper::UniquePtr socket_
std::map< OptionKey, int > OptionMap
Definition: AsyncSocket.h:376
static SSLResumeEnum getResumeState(folly::AsyncSSLSocket *sslSocket)
Definition: SSLUtil.cpp:46
void setHTTPVersionOverride(bool enabled)
WheelTimerInstance timeout_
virtual void connectError(const folly::AsyncSocketException &ex)=0
void setPlaintextProtocol(const std::string &plaintextProto)
std::chrono::milliseconds timeElapsed()
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Definition: Memory.h:259
std::shared_ptr< std::string > sslCipher
std::chrono::milliseconds millisecondsSince(std::chrono::time_point< ClockType > t)
Definition: Time.h:101
const char * string
Definition: Conv.cpp:212
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 connectSuccess() noexceptoverride
std::chrono::time_point< ClockType > getCurrentTime()
Definition: Time.h:41
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::shared_ptr< std::string > appProtocol
void connectErr(const folly::AsyncSocketException &ex) noexceptoverride
std::chrono::milliseconds sslSetupTime
virtual const char * getNegotiatedCipherName() const
bool timePointInitialized(const T &time)
Definition: Time.h:35
HTTPConnector(Callback *callback, folly::HHWheelTimer *timeoutSet)
std::string plaintextProtocol_