Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HTTP1xCodec.cpp #532

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion proxygen/lib/http/codec/HTTP1xCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,10 @@ int HTTP1xCodec::onHeadersComplete(size_t len) {
ignoreBody = false;
} else {
is1xxResponse_ = msg_->is1xxResponse();
if (expectNoResponseBody_) {
std::string contentLengthHeader = msg_->getHeaders().getSingleOrEmpty(HTTP_HEADER_CONTENT_LENGTH);
if (connectRequest_ && (msg_->is4xxResponse() || msg_->is5xxResponse()) && !contentLengthHeader.empty() && std::stol(contentLengthHeader) != 0) {
ignoreBody = false;
} else if (expectNoResponseBody_) {
ignoreBody = true;
} else {
ignoreBody = RFC2616::responseBodyMustBeEmpty(msg_->getStatusCode());
Expand Down
73 changes: 73 additions & 0 deletions proxygen/lib/http/codec/test/HTTP1xCodecTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,79 @@ TEST(HTTP1xCodecTest, AbsoluteURLNoPath) {
EXPECT_EQ(callbacks.msg_->getPathAsStringPiece(), string("/"));
}

TEST(HTTP1xCodecTest, ConnectRequestSuccess) {
HTTP1xCodec codec(TransportDirection::UPSTREAM);
HTTP1xCodecCallback callbacks;
HTTPMessage req;
auto id = codec.createStream();
req.setHTTPVersion(1, 1);
req.setMethod(HTTPMethod::CONNECT);
req.setURL("facebook.com:443");
codec.setCallback(&callbacks);
folly::IOBufQueue buf;
codec.generateHeader(buf, id, req, true);
auto buffer = folly::IOBuf::copyBuffer(
string("HTTP/1.1 200 Connection established\r\n\r\n"));
codec.onIngress(*buffer);
EXPECT_EQ(callbacks.headersComplete, 1);
EXPECT_EQ(callbacks.bodyLen, 0);
EXPECT_EQ(callbacks.errors, 0);
EXPECT_EQ(callbacks.messageComplete, 1);
}

TEST(HTTP1xCodecTest, ConnectRequestError) {
HTTP1xCodec codec(TransportDirection::UPSTREAM);
HTTP1xCodecCallback callbacks;
HTTPMessage req;
auto id = codec.createStream();
req.setHTTPVersion(1, 1);
req.setMethod(HTTPMethod::CONNECT);
req.setURL("facebook.com:443");
codec.setCallback(&callbacks);
folly::IOBufQueue buf;
codec.generateHeader(buf, id, req, true);
auto buffer = folly::IOBuf::copyBuffer(string("HTTP/1.1 407 Proxy Authentication Required\r\n"
"Content-Type: text/html;charset=utf-8\r\n"
"Content-Length: 165\r\n"
"Proxy-Authenticate: Basic\r\n"
"Proxy-Authenticate: NTLM\r\n"
"Connection: keep-alive\r\n"
"\r\n"
"<!DOCTYPE html><html><head><title>ERROR: "
"Cache Access Denied</title></head><body id"
"=ERR_CACHE_ACCESS_DENIED><h1>ERROR</h1><h2>"
"Cache Access Denied.</h2></body></html>"));
codec.onIngress(*buffer);
EXPECT_EQ(callbacks.headersComplete, 1);
EXPECT_EQ(callbacks.bodyLen, 165);
EXPECT_EQ(callbacks.errors, 0);
EXPECT_EQ(callbacks.messageComplete, 1);
}

TEST(HTTP1xCodecTest, ConnectRequestErrorWithoutBody) {
HTTP1xCodec codec(TransportDirection::UPSTREAM);
HTTP1xCodecCallback callbacks;
HTTPMessage req;
auto id = codec.createStream();
req.setHTTPVersion(1, 1);
req.setMethod(HTTPMethod::CONNECT);
req.setURL("facebook.com:443");
codec.setCallback(&callbacks);
folly::IOBufQueue buf;
codec.generateHeader(buf, id, req, true);
auto buffer = folly::IOBuf::copyBuffer(string("HTTP/1.1 407 Proxy Authentication Required\r\n"
"Content-Type: text/html;charset=utf-8\r\n"
"Proxy-Authenticate: Basic\r\n"
"Proxy-Authenticate: NTLM\r\n"
"Connection: keep-alive\r\n"
"\r\n"));
codec.onIngress(*buffer);
EXPECT_EQ(callbacks.headersComplete, 1);
EXPECT_EQ(callbacks.bodyLen, 0);
EXPECT_EQ(callbacks.errors, 0);
EXPECT_EQ(callbacks.messageComplete, 1);
}

class ConnectionHeaderTest
: public TestWithParam<std::pair<std::list<string>, string>> {
public:
Expand Down
Loading