summaryrefslogtreecommitdiffhomepage
path: root/Starserver/NetFileServlet.cpp
blob: 11cb0a63ad33018bc500d2fb3db3ee61c374ecfd (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    HTTP Servlet for File Transfer
*/

#include "NetFileServlet.h"

#include <DataLoader.h>
#include <Text.h>
#include <Types.h>


NetFileServlet::NetFileServlet() {}
NetFileServlet::~NetFileServlet() {}


bool
NetFileServlet::DoGet(HttpRequest& request, HttpResponse& response)
{
    if (!CheckUser(request, response))
    return true;

    Text content;
    Text path = request.GetParam("path");
    Text name = request.GetParam("name");

    if (name.length()) {
        BYTE*       buffer = 0;
        DataLoader* loader = DataLoader::GetLoader();

        if (loader) {
            loader->SetDataPath(path);
            int len = loader->LoadBuffer(name, buffer);

            if (len) {
                content = Text((const char*) buffer, len);
            }

            loader->ReleaseBuffer(buffer);
            loader->SetDataPath(0);
        }
    }

    response.SetStatus(HttpResponse::SC_OK);
    response.AddHeader("MIME-Version",  "1.0");
    response.AddHeader("Cache-Control", "no-cache");
    response.AddHeader("Expires",       "-1");
    response.AddHeader("Content-Type",  "text/plain");
    response.SetContent(content);

    return true;
}


NetWebServlet::NetWebServlet() {}
NetWebServlet::~NetWebServlet() {}


bool
NetWebServlet::DoGet(HttpRequest& request, HttpResponse& response)
{
    Text content;
    Text name = request.URI();
    bool found = false;

    if (name.length() > 4) {
        char filename[256];
        strcpy_s(filename, name.data() + 1);  // skip leading '/'

        FILE* f;
        ::fopen_s(&f, filename, "rb");

        if (f) {
            ::fseek(f, 0, SEEK_END);
            int len = ftell(f);
            ::fseek(f, 0, SEEK_SET);

            BYTE* buf = new BYTE[len];

            ::fread(buf, len, 1, f);
            ::fclose(f);

            content = Text((const char*) buf, len);
            delete [] buf;

            found = true;
            ::Print("weblog: 200 OK %s %d bytes\n", name.data(), len);
        }
        else {
            ::Print("weblog: 404 Not Found %s\n", name.data());
        }
    }

    if (found) {
        Text content_type = "text/plain";

        if (name.contains(".gif"))
        content_type = "image/gif";
        else if (name.contains(".jpg"))
        content_type = "image/jpeg";
        else if (name.contains(".htm"))
        content_type = "text/html";

        response.SetStatus(HttpResponse::SC_OK);
        response.AddHeader("MIME-Version",  "1.0");
        response.AddHeader("Content-Type",  content_type);
        response.SetContent(content);
    }
    else {
        response.SetStatus(HttpResponse::SC_NOT_FOUND);
    }

    return true;
}