Files
firecgi/request.h

74 lines
1.6 KiB
C
Raw Permalink Normal View History

2019-05-04 23:42:03 -07:00
#pragma once
2019-05-09 23:36:31 -07:00
#include <functional>
2019-05-09 20:24:59 -07:00
#include <mutex>
2019-05-04 23:42:03 -07:00
#include <unordered_map>
#include "firebuf/buffer.h"
#include "parse.h"
namespace firecgi {
class Connection;
class Request {
2019-05-18 12:15:11 -07:00
public:
Request(Connection* conn);
~Request();
2019-05-18 12:15:11 -07:00
void NewRequest(uint16_t request_id);
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
uint16_t RequestId() const;
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
void AddParam(const std::string_view& key, const std::string_view& value);
void SetBody(const std::string_view& in);
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
const std::string_view& GetParam(const std::string_view& key) const;
const std::string_view& GetBody() const;
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
void OnClose(const std::function<void()>& callback);
2019-05-11 21:52:49 -07:00
2019-05-18 12:15:11 -07:00
void WriteHeader(const std::string_view& name, const std::string_view& value);
void WriteBody(const std::string_view& body);
[[nodiscard]] bool Flush();
bool End();
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
template <typename... Args>
void WriteBody(const std::string_view& first, Args... more);
2019-05-09 23:04:04 -07:00
2019-05-18 12:15:11 -07:00
template <typename T>
T InTransaction(const std::function<T()>& callback);
2019-05-09 23:36:31 -07:00
2019-05-18 12:15:11 -07:00
private:
Header OutputHeader();
iovec OutputVec();
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
Connection* conn_;
uint16_t request_id_ = 0;
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
std::unordered_map<std::string_view, std::string_view> params_;
std::string_view body_;
2019-05-04 23:42:03 -07:00
2019-05-18 12:15:11 -07:00
std::function<void()> on_close_;
2019-05-11 21:52:49 -07:00
2019-05-18 12:15:11 -07:00
firebuf::Buffer out_buf_;
bool body_written_;
std::recursive_mutex output_mu_;
2019-05-04 23:42:03 -07:00
};
2019-05-18 12:15:11 -07:00
template <typename... Args>
2019-05-09 23:22:45 -07:00
void Request::WriteBody(const std::string_view& first, Args... more) {
2019-05-18 12:15:11 -07:00
std::lock_guard<std::recursive_mutex> l(output_mu_);
WriteBody(first);
WriteBody(more...);
2019-05-09 23:04:04 -07:00
}
2019-05-18 12:15:11 -07:00
template <typename T>
2019-05-09 23:36:31 -07:00
T Request::InTransaction(const std::function<T()>& callback) {
2019-05-18 12:15:11 -07:00
std::lock_guard<std::recursive_mutex> l(output_mu_);
return callback();
2019-05-09 23:36:31 -07:00
}
2019-05-18 12:15:11 -07:00
} // namespace firecgi