Fix Bugs... make Request non Const

This commit is contained in:
Maurice 2022-09-10 15:09:18 +02:00
parent 638276d8e0
commit afd8f4d999
8 changed files with 40 additions and 42 deletions

2
.gitignore vendored
View file

@ -1,6 +1,8 @@
build/
debug-build/
build-debug/
release-build/
build-release/
cmake-build-debug/
.idea/

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.23)
cmake_minimum_required(VERSION 3.21)
project(VWeb)
set(CMAKE_CXX_STANDARD 20)

View file

@ -100,7 +100,9 @@ void Response::AddContent(const std::string &data) { m_Content << data; }
void Response::SetStatus(HttpStatusCode statusCode) { Status = statusCode; }
void Response::SetMethod(HttpMethod method) { Method = method; }
void Response::Reset() { m_Content.clear(); }
void Response::Reset() {
m_Content = {};
}
std::string Response::TransformHeaders(const std::string &content) {
std::ostringstream stream;

View file

@ -23,7 +23,7 @@ Route::Route(std::initializer_list<HttpMethod> methods) {
m_AllowedMethods.push_back(HttpMethod::HEAD);
m_AllowedMethods.push_back(HttpMethod::OPTIONS);
}
bool Route::Execute(const Request &request, Response &response) {
bool Route::Execute(Request &request, Response &response) {
switch (request.Method) {
case HttpMethod::GET:
case HttpMethod::HEAD: return Get(request, response);
@ -35,22 +35,22 @@ bool Route::Execute(const Request &request, Response &response) {
default: return Fallback(request, response);
}
}
bool Route::Get(const Request &request, Response &response) {
bool Route::Get(Request &request, Response &response) {
return true;
}
bool Route::Post(const Request &request, Response &response) {
bool Route::Post(Request &request, Response &response) {
return true;
}
bool Route::Put(const Request &request, Response &response) {
bool Route::Put(Request &request, Response &response) {
return true;
}
bool Route::Patch(const Request &request, Response &response) {
bool Route::Patch(Request &request, Response &response) {
return true;
}
bool Route::Delete(const Request &request, Response &response) {
bool Route::Delete(Request &request, Response &response) {
return true;
}
bool Route::Options(const Request &request, Response &response) {
bool Route::Options(Request &request, Response &response) {
std::stringstream str{};
bool isFirst = true;
if (m_AllowAll) {
@ -71,11 +71,11 @@ bool Route::Options(const Request &request, Response &response) {
response.SetHeader("Allow", str.str());
return true;
}
bool Route::Fallback(const Request &request, Response &response) {
bool Route::Fallback(Request &request, Response &response) {
return true;
}
bool Route::IsAllowed(const Request &request) { return true; }
bool Route::SupportsMethod(const Request &request) {
bool Route::IsAllowed(Request &request) { return true; }
bool Route::SupportsMethod(Request &request) {
return m_AllowAll ||
std::find(m_AllowedMethods.begin(), m_AllowedMethods.end(),
request.Method) != m_AllowedMethods.end();

View file

@ -12,7 +12,7 @@ template <typename E> constexpr auto to_underlying(E e) noexcept {
class ErrorRoute : public Route {
public:
bool Execute(const Request &request, Response &response) override {
bool Execute(Request &request, Response &response) override {
response.Reset();
response << "Unhandled Error: Status "
<< std::to_string(to_underlying(response.Status));
@ -23,19 +23,19 @@ public:
class InstanceHandleRoute : public Route {
public:
bool Get(const Request &request, Response &response) override {
bool Get(Request &request, Response &response) override {
return GetFunc && GetFunc(request, response);
}
bool Post(const Request &request, Response &response) override {
bool Post(Request &request, Response &response) override {
return PostFunc && PostFunc(request, response);
}
bool Put(const Request &request, Response &response) override {
bool Put(Request &request, Response &response) override {
return PutFunc && PutFunc(request, response);
}
bool Patch(const Request &request, Response &response) override {
bool Patch(Request &request, Response &response) override {
return PatchFunc && PatchFunc(request, response);
}
bool Delete(const Request &request, Response &response) override {
bool Delete(Request &request, Response &response) override {
return DeleteFunc && DeleteFunc(request, response);
}

36
VWeb.h
View file

@ -442,16 +442,10 @@ public:
Ref<Cookies> CookieData;
Cookie &GetCookie(const std::string &key) { return CookieData->Get(key); };
ParameterValue &Parameter(const std::string &key) { return Parameters[key]; }
bool HasParameter(const std::string &key) const {
return Parameters.contains(key);
}
bool HasHeader(const std::string &key) const {
return Headers.contains(key);
}
std::string &FirstOf(const std::string &key) {
return Parameters[key].GetFirst();
}
ParameterValue &Header(const std::string &key) { return Parameters[key]; }
bool HasParameter(const std::string &key) const { return Parameters.contains(key); }
bool HasHeader(const std::string &key) const { return Headers.contains(key); }
std::string &FirstOf(const std::string &key) { return Parameters[key].GetFirst(); }
ParameterValue &Header(const std::string &key) { return Headers[key]; }
std::unordered_map<std::string, ParameterValue> Parameters;
std::unordered_map<std::string, ParameterValue> Headers;
Vector<std::string> URLParameters;
@ -509,22 +503,22 @@ protected:
friend Server;
};
typedef std::function<bool(const Request&,Response&)> RouteFunction;
typedef std::function<bool(Request&,Response&)> RouteFunction;
class Route {
public:
Route() = default;
virtual ~Route() = default;
Route(std::initializer_list<HttpMethod>);
virtual bool Execute(const Request &request, Response &response);
virtual bool Get(const Request &request, Response &response);
virtual bool Post(const Request &request, Response &response);
virtual bool Put(const Request &request, Response &response);
virtual bool Patch(const Request &request, Response &response);
virtual bool Delete(const Request &request, Response &response);
bool Options(const Request &request, Response &response);
virtual bool Fallback(const Request &request, Response &response);
bool SupportsMethod(const Request &request);
virtual bool IsAllowed(const Request &request);
virtual bool Execute(Request &request, Response &response);
virtual bool Get(Request &request, Response &response);
virtual bool Post(Request &request, Response &response);
virtual bool Put(Request &request, Response &response);
virtual bool Patch(Request &request, Response &response);
virtual bool Delete(Request &request, Response &response);
bool Options(Request &request, Response &response);
virtual bool Fallback(Request &request, Response &response);
bool SupportsMethod(Request &request);
virtual bool IsAllowed(Request &request);
void AllowMethod(HttpMethod method);

BIN
dist/libVWeb.debug.a vendored

Binary file not shown.

BIN
dist/libVWeb.release.a vendored

Binary file not shown.