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

4
.gitignore vendored
View file

@ -1,7 +1,9 @@
build/ build/
debug-build/ debug-build/
build-debug/
release-build/ release-build/
build-release/
cmake-build-debug/ cmake-build-debug/
.idea/ .idea/
example/build example/build

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.23) cmake_minimum_required(VERSION 3.21)
project(VWeb) project(VWeb)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
@ -26,4 +26,4 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(mode debug) set(mode debug)
endif () endif ()
set(target_file ${CMAKE_SOURCE_DIR}/dist/libVWeb.${mode}.a) set(target_file ${CMAKE_SOURCE_DIR}/dist/libVWeb.${mode}.a)
add_custom_command(TARGET VWeb POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:VWeb> ${target_file}) add_custom_command(TARGET VWeb POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:VWeb> ${target_file})

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::SetStatus(HttpStatusCode statusCode) { Status = statusCode; }
void Response::SetMethod(HttpMethod method) { Method = method; } 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::string Response::TransformHeaders(const std::string &content) {
std::ostringstream stream; 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::HEAD);
m_AllowedMethods.push_back(HttpMethod::OPTIONS); m_AllowedMethods.push_back(HttpMethod::OPTIONS);
} }
bool Route::Execute(const Request &request, Response &response) { bool Route::Execute(Request &request, Response &response) {
switch (request.Method) { switch (request.Method) {
case HttpMethod::GET: case HttpMethod::GET:
case HttpMethod::HEAD: return Get(request, response); case HttpMethod::HEAD: return Get(request, response);
@ -35,22 +35,22 @@ bool Route::Execute(const Request &request, Response &response) {
default: return Fallback(request, response); default: return Fallback(request, response);
} }
} }
bool Route::Get(const Request &request, Response &response) { bool Route::Get(Request &request, Response &response) {
return true; return true;
} }
bool Route::Post(const Request &request, Response &response) { bool Route::Post(Request &request, Response &response) {
return true; return true;
} }
bool Route::Put(const Request &request, Response &response) { bool Route::Put(Request &request, Response &response) {
return true; return true;
} }
bool Route::Patch(const Request &request, Response &response) { bool Route::Patch(Request &request, Response &response) {
return true; return true;
} }
bool Route::Delete(const Request &request, Response &response) { bool Route::Delete(Request &request, Response &response) {
return true; return true;
} }
bool Route::Options(const Request &request, Response &response) { bool Route::Options(Request &request, Response &response) {
std::stringstream str{}; std::stringstream str{};
bool isFirst = true; bool isFirst = true;
if (m_AllowAll) { if (m_AllowAll) {
@ -71,11 +71,11 @@ bool Route::Options(const Request &request, Response &response) {
response.SetHeader("Allow", str.str()); response.SetHeader("Allow", str.str());
return true; return true;
} }
bool Route::Fallback(const Request &request, Response &response) { bool Route::Fallback(Request &request, Response &response) {
return true; return true;
} }
bool Route::IsAllowed(const Request &request) { return true; } bool Route::IsAllowed(Request &request) { return true; }
bool Route::SupportsMethod(const Request &request) { bool Route::SupportsMethod(Request &request) {
return m_AllowAll || return m_AllowAll ||
std::find(m_AllowedMethods.begin(), m_AllowedMethods.end(), std::find(m_AllowedMethods.begin(), m_AllowedMethods.end(),
request.Method) != 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 { class ErrorRoute : public Route {
public: public:
bool Execute(const Request &request, Response &response) override { bool Execute(Request &request, Response &response) override {
response.Reset(); response.Reset();
response << "Unhandled Error: Status " response << "Unhandled Error: Status "
<< std::to_string(to_underlying(response.Status)); << std::to_string(to_underlying(response.Status));
@ -23,19 +23,19 @@ public:
class InstanceHandleRoute : public Route { class InstanceHandleRoute : public Route {
public: public:
bool Get(const Request &request, Response &response) override { bool Get(Request &request, Response &response) override {
return GetFunc && GetFunc(request, response); 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); 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); 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); 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); return DeleteFunc && DeleteFunc(request, response);
} }

38
VWeb.h
View file

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

BIN
dist/libVWeb.debug.a vendored

Binary file not shown.

BIN
dist/libVWeb.release.a vendored

Binary file not shown.