Renamed BryWebApplication to WebApplication, and moved it and HandlerInstance and Request to a new http folder. Their dependencies with brynet will be removed, they will have subclasses per backend when/if needed.

This commit is contained in:
Relintai 2021-05-27 17:37:23 +02:00
parent a34995e1b7
commit a47cb57da7
13 changed files with 38 additions and 37 deletions

View File

@ -6,6 +6,7 @@ env.core_sources = []
env.add_source_files(env.core_sources, "*.cpp") env.add_source_files(env.core_sources, "*.cpp")
env.add_source_files(env.core_sources, "./html/*.cpp") env.add_source_files(env.core_sources, "./html/*.cpp")
env.add_source_files(env.core_sources, "./http/*.cpp")
env.add_source_files(env.core_sources, "./bry_http/*.cpp") env.add_source_files(env.core_sources, "./bry_http/*.cpp")
env.add_source_files(env.core_sources, "./database/*.cpp") env.add_source_files(env.core_sources, "./database/*.cpp")

View File

@ -1,7 +1,7 @@
#include "http_server.h" #include "http_server.h"
#include "bry_web_application.h" #include "core/http/web_application.h"
#include "request.h" #include "core/http/request.h"
#define LOG_VERBOSE 0 #define LOG_VERBOSE 0

View File

@ -15,7 +15,7 @@
#include <brynet/net/wrapper/ServiceBuilder.hpp> #include <brynet/net/wrapper/ServiceBuilder.hpp>
class Request; class Request;
class BryWebApplication; class WebApplication;
class HTTPServer { class HTTPServer {
public: public:
@ -40,7 +40,7 @@ public:
virtual ~HTTPServer(); virtual ~HTTPServer();
//move this to a sublcass //move this to a sublcass
BryWebApplication *application; WebApplication *application;
protected: protected:
std::map<HttpSession *, Request *> _request_map; std::map<HttpSession *, Request *> _request_map;

View File

@ -1,6 +1,6 @@
#include "request.h" #include "request.h"
#include "bry_web_application.h" #include "web_application.h"
void Request::compile_body() { void Request::compile_body() {
compiled_body.reserve(body.size() + head.size() + 13 + 14 + 15); compiled_body.reserve(body.size() + head.size() + 13 + 14 + 15);

View File

@ -9,14 +9,14 @@
#include "handler_instance.h" #include "handler_instance.h"
class BryWebApplication; class WebApplication;
class Request { class Request {
public: public:
HTTPParser::Ptr http_parser; HTTPParser::Ptr http_parser;
HttpSession::Ptr session; HttpSession::Ptr session;
HttpResponse *response; HttpResponse *response;
BryWebApplication *application; WebApplication *application;
uint32_t current_middleware_index; uint32_t current_middleware_index;
HandlerInstance handler_instance; HandlerInstance handler_instance;

View File

@ -1,4 +1,4 @@
#include "bry_web_application.h" #include "web_application.h"
#include <functional> #include <functional>
#include <string> #include <string>
@ -12,20 +12,20 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void BryWebApplication::load_settings() { void WebApplication::load_settings() {
} }
void BryWebApplication::setup_routes() { void WebApplication::setup_routes() {
default_error_handler_func = BryWebApplication::default_fallback_error_handler; default_error_handler_func = WebApplication::default_fallback_error_handler;
error_handler_map[404] = BryWebApplication::default_404_error_handler; error_handler_map[404] = WebApplication::default_404_error_handler;
} }
void BryWebApplication::setup_middleware() { void WebApplication::setup_middleware() {
middlewares.push_back(HandlerInstance([this](Object *instance, Request *request){ this->default_routing_middleware(instance, request); })); middlewares.push_back(HandlerInstance([this](Object *instance, Request *request){ this->default_routing_middleware(instance, request); }));
} }
void BryWebApplication::default_routing_middleware(Object *instance, Request *request) { void WebApplication::default_routing_middleware(Object *instance, Request *request) {
std::string path = request->http_parser->getPath(); std::string path = request->http_parser->getPath();
if (FileCache::get_singleton()->wwwroot_has_file(path)) { if (FileCache::get_singleton()->wwwroot_has_file(path)) {
@ -60,24 +60,24 @@ void BryWebApplication::default_routing_middleware(Object *instance, Request *re
request->next_stage(); request->next_stage();
} }
void BryWebApplication::default_fallback_error_handler(int error_code, Request *request) { void WebApplication::default_fallback_error_handler(int error_code, Request *request) {
request->response->setBody(default_generic_error_body); request->response->setBody(default_generic_error_body);
request->send(); request->send();
} }
void BryWebApplication::default_404_error_handler(int error_code, Request *request) { void WebApplication::default_404_error_handler(int error_code, Request *request) {
request->response->setBody(default_error_404_body); request->response->setBody(default_error_404_body);
request->send(); request->send();
} }
void BryWebApplication::handle_request(Request *request) { void WebApplication::handle_request(Request *request) {
request->middleware_stack = &middlewares; request->middleware_stack = &middlewares;
//note that middlewares handle the routing -> BryWebApplication::default_routing_middleware by default //note that middlewares handle the routing -> WebApplication::default_routing_middleware by default
request->next_stage(); request->next_stage();
} }
void BryWebApplication::send_error(int error_code, Request *request) { void WebApplication::send_error(int error_code, Request *request) {
std::function<void(int, Request *)> func = error_handler_map[error_code]; std::function<void(int, Request *)> func = error_handler_map[error_code];
if (!func) { if (!func) {
@ -88,21 +88,21 @@ void BryWebApplication::send_error(int error_code, Request *request) {
func(error_code, request); func(error_code, request);
} }
void BryWebApplication::send_file(const std::string &path, Request *request) { void WebApplication::send_file(const std::string &path, Request *request) {
std::string fp = FileCache::get_singleton()->wwwroot + path; std::string fp = FileCache::get_singleton()->wwwroot + path;
request->send_file(fp); request->send_file(fp);
} }
void BryWebApplication::migrate() { void WebApplication::migrate() {
} }
void BryWebApplication::register_request_update(Request *request) { void WebApplication::register_request_update(Request *request) {
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex); std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
_update_registered_requests.push_back(request); _update_registered_requests.push_back(request);
} }
void BryWebApplication::unregister_request_update(Request *request) { void WebApplication::unregister_request_update(Request *request) {
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex); std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
std::size_t s = _update_registered_requests.size(); std::size_t s = _update_registered_requests.size();
@ -119,7 +119,7 @@ void BryWebApplication::unregister_request_update(Request *request) {
} }
} }
void BryWebApplication::update() { void WebApplication::update() {
for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) { for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) {
Request *r = _update_registered_requests[i]; Request *r = _update_registered_requests[i];
@ -127,14 +127,14 @@ void BryWebApplication::update() {
} }
} }
BryWebApplication::BryWebApplication() { WebApplication::WebApplication() {
} }
BryWebApplication::~BryWebApplication() { WebApplication::~WebApplication() {
main_route_map.clear(); main_route_map.clear();
error_handler_map.clear(); error_handler_map.clear();
middlewares.clear(); middlewares.clear();
} }
std::string BryWebApplication::default_error_404_body = "<html><body>404 :(</body></html>"; std::string WebApplication::default_error_404_body = "<html><body>404 :(</body></html>";
std::string BryWebApplication::default_generic_error_body = "<html><body>Internal server error! :(</body></html>"; std::string WebApplication::default_generic_error_body = "<html><body>Internal server error! :(</body></html>";

View File

@ -1,5 +1,5 @@
#ifndef BRY_WEB_APPLICATION_H #ifndef WEB_APPLICATION_H
#define BRY_WEB_APPLICATION_H #define WEB_APPLICATION_H
#include "core/object.h" #include "core/object.h"
#include <functional> #include <functional>
@ -13,7 +13,7 @@
class Request; class Request;
class BryWebApplication { class WebApplication {
public: public:
static std::string default_error_404_body; static std::string default_error_404_body;
static std::string default_generic_error_body; static std::string default_generic_error_body;
@ -37,8 +37,8 @@ public:
void unregister_request_update(Request *request); void unregister_request_update(Request *request);
void update(); void update();
BryWebApplication(); WebApplication();
virtual ~BryWebApplication(); virtual ~WebApplication();
public: public:
HandlerInstance index_func; HandlerInstance index_func;

View File

@ -6,7 +6,7 @@
#include "core/object.h" #include "core/object.h"
#include "core/bry_http/request.h" #include "core/http/request.h"
class ListPage : public Object { class ListPage : public Object {

View File

@ -6,7 +6,7 @@
#include "core/object.h" #include "core/object.h"
#include "core/bry_http/request.h" #include "core/http/request.h"
class MessagePage : public Object { class MessagePage : public Object {

View File

@ -8,7 +8,7 @@
#include <tinydir/tinydir.h> #include <tinydir/tinydir.h>
#include <iostream> #include <iostream>
#include "core/bry_http/bry_web_application.h" #include "core/http/web_application.h"
void PagedArticle::index(Request *request) { void PagedArticle::index(Request *request) {
const std::string r = request->get_current_path_segment(); const std::string r = request->get_current_path_segment();

View File

@ -8,7 +8,7 @@
#include "core/file_cache.h" #include "core/file_cache.h"
#include "core/object.h" #include "core/object.h"
#include "core/bry_http/request.h" #include "core/http/request.h"
struct Article { struct Article {
std::string url; std::string url;