Add close handling
This commit is contained in:
15
request.cc
15
request.cc
@@ -20,10 +20,21 @@ Request::Request(Connection* conn)
|
||||
: conn_(conn),
|
||||
out_buf_(64*1024) {}
|
||||
|
||||
Request::~Request() {
|
||||
if (on_close_) {
|
||||
on_close_();
|
||||
}
|
||||
}
|
||||
|
||||
void Request::NewRequest(uint16_t request_id) {
|
||||
if (on_close_) {
|
||||
on_close_();
|
||||
}
|
||||
|
||||
request_id_ = request_id;
|
||||
params_.clear();
|
||||
body_ = {};
|
||||
on_close_ = nullptr;
|
||||
out_buf_.Reset();
|
||||
body_written_ = false;
|
||||
}
|
||||
@@ -53,6 +64,10 @@ const std::string_view& Request::GetBody() const {
|
||||
return body_;
|
||||
}
|
||||
|
||||
void Request::OnClose(const std::function<void()>& on_close) {
|
||||
on_close_ = on_close;
|
||||
}
|
||||
|
||||
void Request::WriteHeader(const std::string_view& name, const std::string_view& value) {
|
||||
std::lock_guard<std::recursive_mutex> l(output_mu_);
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ class Connection;
|
||||
class Request {
|
||||
public:
|
||||
Request(Connection *conn);
|
||||
~Request();
|
||||
|
||||
void NewRequest(uint16_t request_id);
|
||||
|
||||
@@ -26,6 +27,8 @@ class Request {
|
||||
const std::string_view& GetParam(const std::string_view& key) const;
|
||||
const std::string_view& GetBody() const;
|
||||
|
||||
void OnClose(const std::function<void()>& callback);
|
||||
|
||||
void WriteHeader(const std::string_view& name, const std::string_view& value);
|
||||
void WriteBody(const std::string_view& body);
|
||||
[[nodiscard]] bool Flush();
|
||||
@@ -47,6 +50,8 @@ class Request {
|
||||
std::unordered_map<std::string_view, std::string_view> params_;
|
||||
std::string_view body_;
|
||||
|
||||
std::function<void()> on_close_;
|
||||
|
||||
firebuf::Buffer out_buf_;
|
||||
bool body_written_;
|
||||
std::recursive_mutex output_mu_;
|
||||
|
||||
Reference in New Issue
Block a user