Files
firecgi/request.h

69 lines
1.5 KiB
C
Raw 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 {
public:
Request(Connection *conn);
void NewRequest(uint16_t request_id);
2019-05-04 23:42:03 -07:00
uint16_t RequestId() const;
2019-05-04 23:42:03 -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
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
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-09 23:04:04 -07:00
template<typename...Args>
2019-05-09 23:22:45 -07:00
void WriteBody(const std::string_view& first, Args... more);
2019-05-09 23:04:04 -07:00
2019-05-09 23:36:31 -07:00
template<typename T>
T InTransaction(const std::function<T()>& callback);
2019-05-04 23:42:03 -07:00
private:
Header OutputHeader();
iovec OutputVec();
Connection *conn_;
uint16_t request_id_ = 0;
2019-05-04 23:42:03 -07:00
2019-05-09 19:49:05 -07:00
std::unordered_map<std::string_view, std::string_view> params_;
std::string_view body_;
2019-05-04 23:42:03 -07:00
firebuf::Buffer out_buf_;
bool body_written_;
2019-05-09 23:22:45 -07:00
std::recursive_mutex output_mu_;
2019-05-04 23:42:03 -07:00
};
2019-05-09 23:04:04 -07:00
template<typename...Args>
2019-05-09 23:22:45 -07:00
void Request::WriteBody(const std::string_view& first, Args... more) {
std::lock_guard<std::recursive_mutex> l(output_mu_);
WriteBody(first);
WriteBody(more...);
2019-05-09 23:04:04 -07:00
}
2019-05-09 23:36:31 -07:00
template<typename T>
T Request::InTransaction(const std::function<T()>& callback) {
std::lock_guard<std::recursive_mutex> l(output_mu_);
return callback();
}
2019-05-04 23:42:03 -07:00
} // namespace firecgi