mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
113 lines
2.8 KiB
C++
113 lines
2.8 KiB
C++
#pragma once
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include "json.h"
|
|
#include "http_request.h"
|
|
#include "ci_map.h"
|
|
|
|
namespace crow
|
|
{
|
|
template <typename Handler, typename ... Middlewares>
|
|
class Connection;
|
|
struct response
|
|
{
|
|
template <typename Handler, typename ... Middlewares>
|
|
friend class crow::Connection;
|
|
|
|
std::string body;
|
|
json::wvalue json_value;
|
|
int code{200};
|
|
|
|
// `headers' stores HTTP headers.
|
|
ci_map headers;
|
|
|
|
void set_header(std::string key, std::string value)
|
|
{
|
|
headers.erase(key);
|
|
headers.emplace(std::move(key), std::move(value));
|
|
}
|
|
void add_header(std::string key, std::string value)
|
|
{
|
|
headers.emplace(std::move(key), std::move(value));
|
|
}
|
|
|
|
const std::string& get_header_value(const std::string& key)
|
|
{
|
|
return crow::get_header_value(headers, key);
|
|
}
|
|
|
|
|
|
response() {}
|
|
explicit response(int code) : code(code) {}
|
|
response(std::string body) : body(std::move(body)) {}
|
|
response(json::wvalue&& json_value) : json_value(std::move(json_value)) {}
|
|
response(const json::wvalue& json_value) : body(json::dump(json_value)) {}
|
|
response(int code, std::string body) : body(std::move(body)), code(code) {}
|
|
|
|
response(response&& r)
|
|
{
|
|
*this = std::move(r);
|
|
}
|
|
|
|
response& operator = (const response& r) = delete;
|
|
|
|
response& operator = (response&& r) noexcept
|
|
{
|
|
body = std::move(r.body);
|
|
json_value = std::move(r.json_value);
|
|
code = r.code;
|
|
headers = std::move(r.headers);
|
|
completed_ = r.completed_;
|
|
return *this;
|
|
}
|
|
|
|
bool is_completed() const noexcept
|
|
{
|
|
return completed_;
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
body.clear();
|
|
json_value.clear();
|
|
code = 200;
|
|
headers.clear();
|
|
completed_ = false;
|
|
}
|
|
|
|
void write(const std::string& body_part)
|
|
{
|
|
body += body_part;
|
|
}
|
|
|
|
void end()
|
|
{
|
|
if (!completed_)
|
|
{
|
|
completed_ = true;
|
|
|
|
if (complete_request_handler_)
|
|
{
|
|
complete_request_handler_();
|
|
}
|
|
}
|
|
}
|
|
|
|
void end(const std::string& body_part)
|
|
{
|
|
body += body_part;
|
|
end();
|
|
}
|
|
|
|
bool is_alive()
|
|
{
|
|
return is_alive_helper_ && is_alive_helper_();
|
|
}
|
|
|
|
private:
|
|
bool completed_{};
|
|
std::function<void()> complete_request_handler_;
|
|
std::function<bool()> is_alive_helper_;
|
|
};
|
|
}
|