Skip to content

Commit 381900c

Browse files
committed
Support CONNECT headers in getHTTPSessionViaProxy
Allow callers to pass optional `CONNECT` request headers through `HTTPClient::getHTTPSessionViaProxy`. Forward them to `HTTPCoroConnector::proxyConnect` so `HTTPConnectStream` can include headers such as `Proxy-Authorization` when establishing the tunnel.
1 parent 530fef8 commit 381900c

7 files changed

Lines changed: 39 additions & 9 deletions

File tree

proxygen/lib/http/coro/client/HTTPClient.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ folly::coro::Task<HTTPCoroSession*> HTTPClient::getHTTPSessionViaProxy(
292292
std::chrono::milliseconds connectTimeout,
293293
std::chrono::milliseconds readTimeout,
294294
std::string clientCertPath,
295-
std::string clientKeyPath) {
295+
std::string clientKeyPath,
296+
HTTPCoroConnector::ConnectHeaderMap connectHeaders) {
296297
HTTPCoroConnector::ConnectionParams connParams;
297298
auto tlsParams =
298299
makeTLSParams(clientCertPath, clientKeyPath, kDefaultNextProtocols);
@@ -308,7 +309,8 @@ folly::coro::Task<HTTPCoroSession*> HTTPClient::getHTTPSessionViaProxy(
308309
connectUnique,
309310
connectTimeout,
310311
connParams,
311-
getSessionParams(readTimeout)));
312+
getSessionParams(readTimeout),
313+
std::move(connectHeaders)));
312314
co_return res;
313315
}
314316

proxygen/lib/http/coro/client/HTTPClient.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ class HTTPClient {
265265
std::chrono::milliseconds connectTimeout,
266266
std::chrono::milliseconds readTimeout,
267267
std::string clientCertPath = "",
268-
std::string clientKeyPath = "");
268+
std::string clientKeyPath = "",
269+
HTTPCoroConnector::ConnectHeaderMap connectHeaders =
270+
HTTPCoroConnector::ConnectHeaderMap());
269271

270272
private:
271273
static std::vector<std::string>& defaultCAPaths();

proxygen/lib/http/coro/client/HTTPCoroConnector.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,17 +654,26 @@ folly::coro::Task<HTTPCoroSession*> HTTPCoroConnector::proxyConnect(
654654
bool connectUnique,
655655
std::chrono::milliseconds timeout,
656656
const ConnectionParams& connParams,
657-
const SessionParams& sessionParams) {
657+
const SessionParams& sessionParams,
658+
ConnectHeaderMap connectHeaders) {
658659

659660
// egress bufer option?
660661
XLOG(DBG2) << "Sending CONNECT to " << authority;
661662
std::unique_ptr<HTTPConnectStream> connectStream;
662663
if (connectUnique) {
663664
connectStream = co_await co_nothrow(HTTPConnectStream::connectUnique(
664-
proxySession, std::move(reservation), authority, timeout));
665+
proxySession,
666+
std::move(reservation),
667+
authority,
668+
timeout,
669+
std::move(connectHeaders)));
665670
} else {
666671
connectStream = co_await co_nothrow(HTTPConnectStream::connect(
667-
proxySession, std::move(reservation), authority, timeout));
672+
proxySession,
673+
std::move(reservation),
674+
authority,
675+
timeout,
676+
std::move(connectHeaders)));
668677
}
669678
auto peerAddr = connectStream->peerAddr_;
670679
co_return co_await co_nothrow(connectImpl(proxySession->getEventBase(),

proxygen/lib/http/coro/client/HTTPCoroConnector.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string>
2525

2626
#include "proxygen/lib/http/coro/HTTPCoroSession.h"
27+
#include "proxygen/lib/http/coro/transport/HTTPConnectStream.h"
2728
#include <proxygen/lib/http/codec/HTTPSettings.h>
2829
#include <proxygen/lib/http/codec/compress/HeaderCodec.h>
2930
#include <proxygen/lib/sampling/Sampling.h>
@@ -177,15 +178,19 @@ class HTTPCoroConnector {
177178
const SessionParams& sessionParams = defaultSessionParams(),
178179
std::chrono::milliseconds happyEyeballsTimeout = kHappyEyeballsDelay);
179180

180-
// For HTTP connections over HTTP CONNECT
181+
// For HTTP connections over HTTP CONNECT. `connectHeaders` is forwarded
182+
// to `HTTPConnectStream::connect[Unique]` so callers can inject
183+
// `Proxy-Authorization` and similar.
184+
using ConnectHeaderMap = HTTPConnectStream::RequestHeaderMap;
181185
static folly::coro::Task<HTTPCoroSession*> proxyConnect(
182186
HTTPCoroSession* proxySession,
183187
HTTPCoroSession::RequestReservation reservation,
184188
std::string authority,
185189
bool connectUnique,
186190
std::chrono::milliseconds timeout,
187191
const ConnectionParams& connParams = defaultConnectionParams(),
188-
const SessionParams& sessionParams = defaultSessionParams());
192+
const SessionParams& sessionParams = defaultSessionParams(),
193+
ConnectHeaderMap connectHeaders = ConnectHeaderMap());
189194

190195
static folly::coro::Task<HTTPCoroSession*> connect(
191196
folly::EventBase* evb,

proxygen/lib/http/coro/client/test/HTTPClientTests.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,14 +669,21 @@ CO_TEST_P_X(HTTPClientTests, Connect) {
669669
timeout));
670670
EXPECT_FALSE(sess.hasException());
671671

672+
HTTPCoroConnector::ConnectHeaderMap connectHeaders = {
673+
{"Proxy-Authorization", "Basic dGVzdA=="},
674+
{"X-Proxy-Trace", "trace-id"}};
675+
testHandler_->expectedConnectHeaders_ = connectHeaders;
672676
auto sessViaProxy = co_await co_awaitTry(
673677
HTTPClient::getHTTPSessionViaProxy(*sess,
674678
"example.com",
675679
443,
676680
true,
677681
transportImpl(TransportType::TCP),
678682
timeout,
679-
timeout));
683+
timeout,
684+
/*clientCertPath=*/"",
685+
/*clientKeyPath=*/"",
686+
std::move(connectHeaders)));
680687
EXPECT_FALSE(sessViaProxy.hasException());
681688

682689
(*sessViaProxy)->initiateDrain();

proxygen/lib/http/coro/client/test/HTTPClientTestsCommon.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ folly::coro::Task<HTTPSourceHolder> TestHandler::handleRequest(
9494
EXPECT_EQ(request->isSecure(), ctx->getSetupTransportInfo().secure);
9595

9696
if (request->getMethod() == HTTPMethod::CONNECT) {
97+
for (const auto& expectedHeader : expectedConnectHeaders_) {
98+
EXPECT_EQ(request->getHeaders().getSingleOrEmpty(expectedHeader.first),
99+
expectedHeader.second);
100+
}
97101
// Hack to silence the expect in connectHandler
98102
request->getHeaders().add("Foo", "Bar");
99103
auto hybridSource = new HTTPHybridSource(std::move(headerEvent.headers),

proxygen/lib/http/coro/client/test/HTTPClientTestsCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TestHandler : public HTTPHandler {
5555
HTTPSourceHolder requestSource) override;
5656

5757
ConnectHandler connectHandler_;
58+
HTTPCoroConnector::ConnectHeaderMap expectedConnectHeaders_;
5859
};
5960

6061
class HTTPClientTests : public TestWithParam<TransportType> {

0 commit comments

Comments
 (0)