diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-12-25 13:29:29 -0800 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-12-25 13:29:29 -0800 |
commit | a4eb01224bace97138be97d41f6691d649032cef (patch) | |
tree | 92041f8977dbb61f089a154614fff07939d537d0 /src | |
parent | 1a5c9e6f03345bd2585928e7a6f51a5eec0c3919 (diff) |
Adding HttpActionStatic.
Diffstat (limited to 'src')
-rw-r--r-- | src/HttpActionStatic.cc | 47 |
1 files changed, 47 insertions, 0 deletions
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<Handle> 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<Input> 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; +} + |