-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'http2-beta' of github.com:drogonframework/drogon into h…
…ttp2-beta
- Loading branch information
Showing
25 changed files
with
2,629 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "Http1xTransport.h" | ||
#include "HttpResponseParser.h" | ||
|
||
using namespace drogon; | ||
|
||
Http1xTransport::Http1xTransport(trantor::TcpConnectionPtr connPtr, | ||
Version version, | ||
size_t *bytesSent, | ||
size_t *bytesReceived) | ||
: connPtr(connPtr), | ||
bytesSent_(bytesSent), | ||
bytesReceived_(bytesReceived), | ||
version_(version) | ||
{ | ||
connPtr->setContext(std::make_shared<HttpResponseParser>(connPtr)); | ||
} | ||
|
||
void Http1xTransport::sendRequestInLoop(const HttpRequestPtr &req, | ||
HttpReqCallback &&callback) | ||
{ | ||
sendReq(req); | ||
pipeliningCallbacks_.emplace(std::move(req), std::move(callback)); | ||
} | ||
|
||
void Http1xTransport::onRecvMessage(const trantor::TcpConnectionPtr &conn, | ||
trantor::MsgBuffer *msg) | ||
{ | ||
auto responseParser = connPtr->getContext<HttpResponseParser>(); | ||
assert(responseParser != nullptr); | ||
assert(connPtr.get() == conn.get()); | ||
|
||
// LOG_TRACE << "###:" << msg->readableBytes(); | ||
auto msgSize = msg->readableBytes(); | ||
while (msg->readableBytes() > 0) | ||
{ | ||
if (pipeliningCallbacks_.empty()) | ||
{ | ||
LOG_ERROR << "More responses than expected!"; | ||
connPtr->shutdown(); | ||
return; | ||
} | ||
auto &firstReq = pipeliningCallbacks_.front(); | ||
if (firstReq.first->method() == Head) | ||
{ | ||
responseParser->setForHeadMethod(); | ||
} | ||
if (!responseParser->parseResponse(msg)) | ||
{ | ||
*bytesReceived_ += (msgSize - msg->readableBytes()); | ||
errorCallback(ReqResult::BadResponse); | ||
return; | ||
} | ||
if (responseParser->gotAll()) | ||
{ | ||
auto resp = responseParser->responseImpl(); | ||
responseParser->reset(); | ||
*bytesReceived_ += (msgSize - msg->readableBytes()); | ||
msgSize = msg->readableBytes(); | ||
respCallback(resp, std::move(firstReq), conn); | ||
|
||
pipeliningCallbacks_.pop(); | ||
} | ||
else | ||
{ | ||
*bytesReceived_ += (msgSize - msg->readableBytes()); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
Http1xTransport::~Http1xTransport() | ||
{ | ||
} | ||
|
||
bool Http1xTransport::handleConnectionClose() | ||
{ | ||
auto responseParser = connPtr->getContext<HttpResponseParser>(); | ||
if (responseParser && responseParser->parseResponseOnClose() && | ||
responseParser->gotAll()) | ||
{ | ||
auto &firstReq = pipeliningCallbacks_.front(); | ||
if (firstReq.first->method() == Head) | ||
{ | ||
responseParser->setForHeadMethod(); | ||
} | ||
auto resp = responseParser->responseImpl(); | ||
responseParser->reset(); | ||
respCallback(resp, std::move(firstReq), connPtr); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void Http1xTransport::sendReq(const HttpRequestPtr &req) | ||
{ | ||
trantor::MsgBuffer buffer; | ||
assert(req); | ||
auto implPtr = static_cast<HttpRequestImpl *>(req.get()); | ||
assert(version_ == Version::kHttp10 || version_ == Version::kHttp11); | ||
implPtr->appendToBuffer(&buffer, version_); | ||
LOG_TRACE << "Send request:" | ||
<< std::string_view(buffer.peek(), buffer.readableBytes()); | ||
*bytesSent_ += buffer.readableBytes(); | ||
connPtr->send(std::move(buffer)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include <trantor/net/EventLoop.h> | ||
#include <trantor/net/TcpClient.h> | ||
#include <list> | ||
#include <queue> | ||
#include <vector> | ||
#include "HttpTransport.h" | ||
|
||
namespace drogon | ||
{ | ||
class Http1xTransport : public HttpTransport | ||
{ | ||
private: | ||
std::queue<std::pair<HttpRequestPtr, HttpReqCallback>> pipeliningCallbacks_; | ||
trantor::TcpConnectionPtr connPtr; | ||
size_t *bytesSent_; | ||
size_t *bytesReceived_; | ||
Version version_{Version::kHttp11}; | ||
|
||
void sendReq(const HttpRequestPtr &req); | ||
|
||
public: | ||
Http1xTransport(trantor::TcpConnectionPtr connPtr, | ||
Version version, | ||
size_t *bytesSent, | ||
size_t *bytesReceived); | ||
virtual ~Http1xTransport(); | ||
void sendRequestInLoop(const HttpRequestPtr &req, | ||
HttpReqCallback &&callback) override; | ||
void onRecvMessage(const trantor::TcpConnectionPtr &, | ||
trantor::MsgBuffer *) override; | ||
|
||
size_t requestsInFlight() const override | ||
{ | ||
return pipeliningCallbacks_.size(); | ||
} | ||
|
||
bool handleConnectionClose() override; | ||
|
||
void onError(ReqResult result) override | ||
{ | ||
while (!pipeliningCallbacks_.empty()) | ||
{ | ||
auto &cb = pipeliningCallbacks_.front().second; | ||
cb(result, nullptr); | ||
pipeliningCallbacks_.pop(); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace drogon |
Oops, something went wrong.