Skip to content

Commit 4b7f933

Browse files
committed
Add setting chunk callback to http request
1 parent e155df9 commit 4b7f933

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

examples/client_example/main.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ int nth_resp = 0;
1414

1515
int main()
1616
{
17+
#if 1
1718
trantor::Logger::setLogLevel(trantor::Logger::kTrace);
1819
{
1920
auto client = HttpClient::newHttpClient("http://www.baidu.com");
@@ -75,6 +76,28 @@ int main()
7576
std::cout << "requestsBufferSize:" << client->requestsBufferSize()
7677
<< std::endl;
7778
}
79+
#else
80+
{
81+
auto client = HttpClient::newHttpClient("http://127.0.0.1:8848/stream");
7882
83+
auto req = HttpRequest::newHttpRequest();
84+
req->setMethod(drogon::Get);
85+
req->setPath("/stream");
86+
req->setChunkCallback([](std::string data) {
87+
std::cout << "recv chunk:" << data << std::endl;
88+
});
89+
client->sendRequest(
90+
req, [](ReqResult result, const HttpResponsePtr &response) {
91+
if (result != ReqResult::Ok)
92+
{
93+
std::cout
94+
<< "error while sending request to server! result: "
95+
<< result << std::endl;
96+
return;
97+
}
98+
std::cout << "recv chunk done" << std::endl;
99+
});
100+
}
101+
#endif
79102
app().run();
80103
}

lib/inc/drogon/HttpRequest.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <trantor/net/InetAddress.h>
2626
#include <trantor/net/Certificate.h>
2727
#include <trantor/utils/Date.h>
28+
#include <functional>
2829
#include <memory>
2930
#include <string>
3031
#include <unordered_map>
@@ -510,6 +511,12 @@ class DROGON_EXPORT HttpRequest
510511
virtual const std::weak_ptr<trantor::TcpConnection> &getConnectionPtr()
511512
const noexcept = 0;
512513

514+
virtual void setChunkCallback(
515+
std::function<void(std::string)>) noexcept = 0;
516+
517+
virtual const std::function<void(std::string)> &getChunkCallback()
518+
const noexcept = 0;
519+
513520
virtual ~HttpRequest()
514521
{
515522
}

lib/src/HttpClientImpl.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ void HttpClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr,
618618
{
619619
responseParser->setForHeadMethod();
620620
}
621-
if (!responseParser->parseResponse(msg))
621+
if (!responseParser->parseResponse(msg,
622+
firstReq.first->getChunkCallback()))
622623
{
623624
onError(ReqResult::BadResponse);
624625
bytesReceived_ += (msgSize - msg->readableBytes());

lib/src/HttpRequestImpl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ class HttpRequestImpl : public HttpRequest
579579
return connPtr_;
580580
}
581581

582+
void setChunkCallback(std::function<void(std::string)> cb) noexcept override
583+
{
584+
chunkCb_ = std::move(cb);
585+
}
586+
587+
const std::function<void(std::string)> &getChunkCallback()
588+
const noexcept override
589+
{
590+
return chunkCb_;
591+
}
592+
582593
bool isOnSecureConnection() const noexcept override
583594
{
584595
return isOnSecureConnection_;
@@ -731,6 +742,7 @@ class HttpRequestImpl : public HttpRequest
731742
std::exception_ptr streamExceptionPtr_;
732743
bool startProcessing_{false};
733744
std::weak_ptr<trantor::TcpConnection> connPtr_;
745+
std::function<void(std::string)> chunkCb_;
734746

735747
protected:
736748
std::string content_;

lib/src/HttpResponseParser.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <trantor/utils/Logger.h>
1818
#include <trantor/utils/MsgBuffer.h>
1919
#include <algorithm>
20+
#include <string>
2021

2122
using namespace trantor;
2223
using namespace drogon;
@@ -84,7 +85,9 @@ bool HttpResponseParser::parseResponseOnClose()
8485
}
8586

8687
// return false if any error
87-
bool HttpResponseParser::parseResponse(MsgBuffer *buf)
88+
bool HttpResponseParser::parseResponse(
89+
MsgBuffer *buf,
90+
const std::function<void(std::string)> &chunkCallback)
8891
{
8992
bool ok = true;
9093
bool hasMore = true;
@@ -277,6 +280,11 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf)
277280
responsePtr_->bodyPtr_ =
278281
std::make_shared<HttpMessageStringBody>();
279282
}
283+
if (chunkCallback)
284+
{
285+
chunkCallback(
286+
std::string(buf->peek(), currentChunkLength_));
287+
}
280288
responsePtr_->bodyPtr_->append(buf->peek(),
281289
currentChunkLength_);
282290
buf->retrieve(currentChunkLength_ + 2);

lib/src/HttpResponseParser.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <trantor/utils/NonCopyable.h>
1919
#include <trantor/net/TcpConnection.h>
2020
#include <trantor/utils/MsgBuffer.h>
21+
#include <functional>
2122
#include <list>
2223
#include <mutex>
2324

@@ -43,7 +44,10 @@ class HttpResponseParser : public trantor::NonCopyable
4344
// default copy-ctor, dtor and assignment are fine
4445

4546
// return false if any error
46-
bool parseResponse(trantor::MsgBuffer *buf);
47+
bool parseResponse(
48+
trantor::MsgBuffer *buf,
49+
const std::function<void(std::string)> &chunkCallback = nullptr);
50+
4751
bool parseResponseOnClose();
4852

4953
bool gotAll() const

0 commit comments

Comments
 (0)