summaryrefslogtreecommitdiff
path: root/src/HttpActionStatic.cc
blob: d1cecafd4a9418a528ec9885908d6f7c413dda3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 &) {
            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;
}