From 4d11399d486e161344720a48c1578bfa17413f07 Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Fri, 22 Dec 2023 19:54:09 +0800 Subject: [PATCH 1/2] add http pipelining support --- src/Http/HttpBody.cpp | 4 ++++ src/Http/HttpBody.h | 9 ++++++++- src/Http/HttpSession.cpp | 12 ++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Http/HttpBody.cpp b/src/Http/HttpBody.cpp index cbada40f..58b7b0b7 100644 --- a/src/Http/HttpBody.cpp +++ b/src/Http/HttpBody.cpp @@ -181,6 +181,10 @@ int HttpFileBody::sendFile(int fd) { #endif } +bool HttpFileBody::isMem(){ + return false; +} + class BufferMmap : public Buffer { public: using Ptr = std::shared_ptr; diff --git a/src/Http/HttpBody.h b/src/Http/HttpBody.h index 3ba60565..76b46edd 100644 --- a/src/Http/HttpBody.h +++ b/src/Http/HttpBody.h @@ -44,6 +44,12 @@ public: */ virtual toolkit::Buffer::Ptr readData(size_t size) { return nullptr;}; + /** + * 判断数据是否在内存里,默认是在内存里 + * @return 默认返回true + */ + virtual bool isMem(){return true;}; + /** * 异步请求读取一定字节数,返回大小可能小于size * @param size 请求大小 @@ -117,10 +123,11 @@ public: * @param max_size 最大读取字节数 */ void setRange(uint64_t offset, uint64_t max_size); - + int64_t remainSize() override; toolkit::Buffer::Ptr readData(size_t size) override; int sendFile(int fd) override; + bool isMem() override; private: int64_t _read_to = 0; diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 38eb5473..a0d98b8a 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -141,12 +141,12 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { _parser.clear(); // 后续是header - setContentLen(0); + //setContentLen(0); return false; }; // 声明后续都是body;Http body在本对象缓冲,不通过HttpRequestSplitter保存 - return -1; + return content_len; } void HttpSession::onRecvContent(const char *data, size_t len) { @@ -687,6 +687,14 @@ void HttpSession::sendResponse(int code, setSocketFlags(); } + if(body->isMem()){ + send(body->readData(body->remainSize())); + if(bClose){ + shutdown(SockException(Err_shutdown, StrPrinter << "close connection after send http body")); + } + return; + } + // 发送http body AsyncSenderData::Ptr data = std::make_shared(static_pointer_cast(shared_from_this()), body, bClose); getSock()->setOnFlush([data]() { return AsyncSender::onSocketFlushed(data); }); From ddc0391d0760525f8dacb020f69b1e63a0e2d948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Sat, 23 Dec 2023 20:51:45 +0800 Subject: [PATCH 2/2] Update HttpSession.cpp --- src/Http/HttpSession.cpp | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index a0d98b8a..d5c536b0 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -124,28 +124,17 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) { } //// body size明确指定且小于最大值的情况 //// - auto body = std::make_shared(); - // 预留一定的内存buffer,防止频繁的内存拷贝 - body->reserve(content_len); - - _on_recv_body = [this, body, content_len, it](const char *data, size_t len) mutable { - body->append(data, len); - if (body->size() < content_len) { - // 未收满数据 - return true; - } - + _on_recv_body = [this, it](const char *data, size_t len) mutable { // 收集body完毕 - _parser.setContent(std::move(*body)); + _parser.setContent(std::string(data, lem)); (this->*(it->second))(); _parser.clear(); - // 后续是header - //setContentLen(0); + // _on_recv_body置空 return false; }; - // 声明后续都是body;Http body在本对象缓冲,不通过HttpRequestSplitter保存 + // 声明body长度;通过HttpRequestSplitter缓存然后一次性回调到_on_recv_body return content_len; }