From a4eb01224bace97138be97d41f6691d649032cef Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Wed, 25 Dec 2013 13:29:29 -0800 Subject: Adding HttpActionStatic. --- Makefile | 1 + includes/HttpActionStatic.h | 16 +++++++++++++++ src/HttpActionStatic.cc | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 includes/HttpActionStatic.h create mode 100644 src/HttpActionStatic.cc diff --git a/Makefile b/Makefile index 3b10442..5a59dc1 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,7 @@ HelperTasks.cc \ \ Http.cc \ HttpServer.cc \ +HttpActionStatic.cc \ SimpleMustache.cc \ BWebSocket.cc \ \ diff --git a/includes/HttpActionStatic.h b/includes/HttpActionStatic.h new file mode 100644 index 0000000..76be287 --- /dev/null +++ b/includes/HttpActionStatic.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace Balau { + +class HttpActionStatic : public HttpServer::Action { + public: + // the regex needs a capture on the file name. + HttpActionStatic(const String & base, Regex & url) : Action(url), m_base(base) { } + private: + virtual bool Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException); + String m_base; +}; + +}; diff --git a/src/HttpActionStatic.cc b/src/HttpActionStatic.cc new file mode 100644 index 0000000..d340bfa --- /dev/null +++ b/src/HttpActionStatic.cc @@ -0,0 +1,47 @@ +#include "HttpActionStatic.h" +#include "Input.h" +#include "TaskMan.h" +#include "HelperTasks.h" + +bool Balau::HttpActionStatic::Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException) { + HttpServer::Response response(server, req, out); + String & fname = match.uri[1]; + String extension; + + ssize_t dot = fname.strrchr('.'); + + if (dot > 0) + extension = fname.extract(dot + 1); + + bool error = false; + + if (fname.strstr("/../") > 0) + error = true; + + IO file(new Input(m_base + fname)); + + if (!error) { + try { + file->open(); + } + catch (ENoEnt & e) { + error = true; + } + } + + if (error) { + response.get()->writeString("Static file not found."); + response.SetResponseCode(404); + response.SetContentType("text/plain"); + } + else { + Events::TaskEvent evt; + Task * copy = TaskMan::registerTask(new CopyTask(file, response.get()), &evt); + Task::operationYield(&evt); + file->close(); + response.SetContentType(Http::getContentType(extension)); + } + response.Flush(); + return true; +} + -- cgit v1.2.3