69 lines
1.6 KiB
C++
69 lines
1.6 KiB
C++
|
#pragma once
|
||
|
|
||
|
// This header needs to be included before beast headers
|
||
|
// because of a bit of a bug. I hope 1.84 solves this
|
||
|
//clang-format off
|
||
|
#include <cstdint>
|
||
|
//clang-format on
|
||
|
|
||
|
#include <boost/beast/http/message.hpp>
|
||
|
#include <boost/beast/http/string_body.hpp>
|
||
|
#include <boost/url/url_view.hpp>
|
||
|
#include <http/r3_helpers.hpp>
|
||
|
#include <r3.hpp>
|
||
|
#include <base/assert.hpp>
|
||
|
#include <base/types.hpp>
|
||
|
#include <unordered_map>
|
||
|
|
||
|
namespace base::http {
|
||
|
|
||
|
struct Request {
|
||
|
using BeastRequest = beast::http::request<beast::http::string_body>;
|
||
|
|
||
|
Request(BeastRequest& request, burl::url_view url, r3::MatchEntry& me) : req(request), url(url) {
|
||
|
BASE_ASSERT(me.get() != nullptr, "Request constructor requires a valid match entry..");
|
||
|
|
||
|
// convert data from r3 into something with brain cells
|
||
|
auto& data = me.get()->vars;
|
||
|
for(u32 i = 0; i < data.tokens.size; ++i) {
|
||
|
auto& siov = data.slugs.entries[i];
|
||
|
auto& eiov = data.tokens.entries[i];
|
||
|
|
||
|
matches[R3IovecString(siov)] = R3IovecString(eiov);
|
||
|
}
|
||
|
|
||
|
// convert boost.URL params
|
||
|
for(auto kv : url.params()) {
|
||
|
if(kv.key.empty())
|
||
|
continue;
|
||
|
|
||
|
if(kv.has_value) {
|
||
|
query[kv.key] = kv.value;
|
||
|
} else {
|
||
|
query[kv.key] = "";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
BeastRequest& Native() { return req; }
|
||
|
|
||
|
const auto& Matches() const { return matches; }
|
||
|
|
||
|
const std::string& Get(const std::string& key) const { return matches.at(key); }
|
||
|
|
||
|
asio::ip::address src;
|
||
|
|
||
|
std::unordered_map<std::string, std::string> matches;
|
||
|
|
||
|
// Query string parameters
|
||
|
std::unordered_map<std::string, std::string> query;
|
||
|
|
||
|
private:
|
||
|
friend struct Server;
|
||
|
|
||
|
BeastRequest req;
|
||
|
burl::url_view url;
|
||
|
};
|
||
|
|
||
|
} // namespace base::http
|