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. --- src/HttpActionStatic.cc | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/HttpActionStatic.cc (limited to 'src') 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