add http pipelining support

This commit is contained in:
xiongguangjie 2023-12-22 19:54:09 +08:00
parent a17b950b21
commit 4d11399d48
3 changed files with 22 additions and 3 deletions

View File

@ -181,6 +181,10 @@ int HttpFileBody::sendFile(int fd) {
#endif #endif
} }
bool HttpFileBody::isMem(){
return false;
}
class BufferMmap : public Buffer { class BufferMmap : public Buffer {
public: public:
using Ptr = std::shared_ptr<BufferMmap>; using Ptr = std::shared_ptr<BufferMmap>;

View File

@ -44,6 +44,12 @@ public:
*/ */
virtual toolkit::Buffer::Ptr readData(size_t size) { return nullptr;}; virtual toolkit::Buffer::Ptr readData(size_t size) { return nullptr;};
/**
*
* @return true
*/
virtual bool isMem(){return true;};
/** /**
* size * size
* @param size * @param size
@ -117,10 +123,11 @@ public:
* @param max_size * @param max_size
*/ */
void setRange(uint64_t offset, uint64_t max_size); void setRange(uint64_t offset, uint64_t max_size);
int64_t remainSize() override; int64_t remainSize() override;
toolkit::Buffer::Ptr readData(size_t size) override; toolkit::Buffer::Ptr readData(size_t size) override;
int sendFile(int fd) override; int sendFile(int fd) override;
bool isMem() override;
private: private:
int64_t _read_to = 0; int64_t _read_to = 0;

View File

@ -141,12 +141,12 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) {
_parser.clear(); _parser.clear();
// 后续是header // 后续是header
setContentLen(0); //setContentLen(0);
return false; return false;
}; };
// 声明后续都是bodyHttp body在本对象缓冲不通过HttpRequestSplitter保存 // 声明后续都是bodyHttp body在本对象缓冲不通过HttpRequestSplitter保存
return -1; return content_len;
} }
void HttpSession::onRecvContent(const char *data, size_t len) { void HttpSession::onRecvContent(const char *data, size_t len) {
@ -687,6 +687,14 @@ void HttpSession::sendResponse(int code,
setSocketFlags(); 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 // 发送http body
AsyncSenderData::Ptr data = std::make_shared<AsyncSenderData>(static_pointer_cast<HttpSession>(shared_from_this()), body, bClose); AsyncSenderData::Ptr data = std::make_shared<AsyncSenderData>(static_pointer_cast<HttpSession>(shared_from_this()), body, bClose);
getSock()->setOnFlush([data]() { return AsyncSender::onSocketFlushed(data); }); getSock()->setOnFlush([data]() { return AsyncSender::onSocketFlushed(data); });