From c59248435068032a360c21dead6731cf53886672 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 6 Feb 2022 09:21:01 +0100 Subject: [PATCH] Added a new FolderServeNode. --- web_modules/folder_serve_nodes/SCsub | 12 ++++ web_modules/folder_serve_nodes/detect.py | 27 +++++++++ .../folder_serve_nodes/folder_serve_node.cpp | 56 +++++++++++++++++++ .../folder_serve_nodes/folder_serve_node.h | 27 +++++++++ 4 files changed, 122 insertions(+) create mode 100644 web_modules/folder_serve_nodes/SCsub create mode 100644 web_modules/folder_serve_nodes/detect.py create mode 100644 web_modules/folder_serve_nodes/folder_serve_node.cpp create mode 100644 web_modules/folder_serve_nodes/folder_serve_node.h diff --git a/web_modules/folder_serve_nodes/SCsub b/web_modules/folder_serve_nodes/SCsub new file mode 100644 index 0000000..e184b7a --- /dev/null +++ b/web_modules/folder_serve_nodes/SCsub @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +Import("env_mod") +Import("env") + +env_mod.core_sources = [] + +env_mod.add_source_files(env_mod.core_sources, "*.cpp") + +# Build it all as a library +lib = env_mod.add_library("paged_list", env_mod.core_sources) +env.Prepend(LIBS=[lib]) diff --git a/web_modules/folder_serve_nodes/detect.py b/web_modules/folder_serve_nodes/detect.py new file mode 100644 index 0000000..3132192 --- /dev/null +++ b/web_modules/folder_serve_nodes/detect.py @@ -0,0 +1,27 @@ +import os +import platform +import sys + + +def is_active(): + return True + + +def get_name(): + return "paged_list" + + +def can_build(): + return True + + +def get_opts(): + return [] + +def get_flags(): + + return [] + + +def configure(env): + pass diff --git a/web_modules/folder_serve_nodes/folder_serve_node.cpp b/web_modules/folder_serve_nodes/folder_serve_node.cpp new file mode 100644 index 0000000..be956a6 --- /dev/null +++ b/web_modules/folder_serve_nodes/folder_serve_node.cpp @@ -0,0 +1,56 @@ +#include "folder_serve_node.h" + +#include "web/file_cache.h" +#include "web/html/html_builder.h" +#include "web/http/request.h" + +void FolderServeNode::handle_request_main(Request *request) { + const String &rp = request->get_current_path_segment(); + + if (rp == "") { + request->send_error(HTTP_STATUS_CODE_404_NOT_FOUND); + return; + } + + String file_name = "/" + request->get_path(); + + if (file_cache->wwwroot_has_file(file_name)) { + String fp = file_cache->wwwroot; + fp.append_path(file_name); + + request->send_file(fp); + return; + } + + if (!try_route_request_to_children(request)) { + _handle_request_main(request); + } +} + +void FolderServeNode::load() { + file_cache->clear(); + + if (serve_folder == "") { + return; + } + + file_cache->wwwroot = serve_folder; + file_cache->wwwroot_refresh_cache(); +} + +void FolderServeNode::_notification(const int what) { + switch (what) { + case NOTIFICATION_ENTER_TREE: + load(); + break; + default: + break; + } +} + +FolderServeNode::FolderServeNode() : + WebNode() { +} + +FolderServeNode::~FolderServeNode() { +} diff --git a/web_modules/folder_serve_nodes/folder_serve_node.h b/web_modules/folder_serve_nodes/folder_serve_node.h new file mode 100644 index 0000000..b742710 --- /dev/null +++ b/web_modules/folder_serve_nodes/folder_serve_node.h @@ -0,0 +1,27 @@ +#ifndef FOLDER_SERVE_NODE_H +#define FOLDER_SERVE_NODE_H + +#include "core/string.h" + +#include "web/http/web_node.h" + +class FolderServeNode : public WebNode { + RCPP_OBJECT(FolderServeNode, WebNode); + +public: + void handle_request_main(Request *request); + + virtual void load(); + + void _notification(const int what); + + String serve_folder; + + FolderServeNode(); + ~FolderServeNode(); + +protected: + FileCache *file_cache; +}; + +#endif \ No newline at end of file