2019-05-04 23:42:03 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
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:
|
2019-05-07 22:56:00 -07:00
|
|
|
Request(Connection *conn);
|
|
|
|
|
|
|
|
|
|
void NewRequest(uint16_t request_id);
|
2019-05-04 23:42:03 -07:00
|
|
|
|
|
|
|
|
uint16_t RequestId();
|
|
|
|
|
|
|
|
|
|
void AddParam(const std::string_view& key, const std::string_view& value);
|
2019-05-09 19:30:36 -07:00
|
|
|
void SetBody(const std::string_view& in);
|
2019-05-04 23:42:03 -07:00
|
|
|
|
2019-05-09 19:49:05 -07:00
|
|
|
const std::string_view& GetParam(const std::string& key);
|
2019-05-09 19:30:36 -07:00
|
|
|
const std::string_view& GetBody();
|
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-04 23:42:03 -07:00
|
|
|
private:
|
|
|
|
|
Header OutputHeader();
|
|
|
|
|
iovec OutputVec();
|
|
|
|
|
|
|
|
|
|
Connection *conn_;
|
2019-05-07 22:56:00 -07:00
|
|
|
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_;
|
2019-05-09 19:30:36 -07:00
|
|
|
std::string_view body_;
|
2019-05-04 23:42:03 -07:00
|
|
|
|
|
|
|
|
firebuf::Buffer out_buf_;
|
2019-05-07 22:56:00 -07:00
|
|
|
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-04 23:42:03 -07:00
|
|
|
} // namespace firecgi
|