mirror of
https://github.com/matrix-construct/construct
synced 2024-11-27 01:02:46 +01:00
95 lines
2.9 KiB
C++
95 lines
2.9 KiB
C++
/*
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
*
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice is present in all copies.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#include <iterator>
|
|
|
|
using namespace ircd;
|
|
|
|
mapi::header IRCD_MODULE
|
|
{
|
|
"Chat Matrix Protocol"
|
|
};
|
|
|
|
IRCD_INIT_PRIORITY(STD_CONTAINER)
|
|
std::map<std::string, std::string, iless> files
|
|
{
|
|
};
|
|
|
|
__attribute__((constructor))
|
|
void
|
|
init_0()
|
|
{
|
|
for(const auto &file : fs::ls("/home/jason/charybdis/charybdis/modules/static"))
|
|
{
|
|
const auto name(token_last(file, "/"));
|
|
files.emplace(std::string(name), file);
|
|
}
|
|
}
|
|
|
|
resource::response
|
|
get_root(client &client, const resource::request &request)
|
|
{
|
|
auto it(files.find(request.head.path));
|
|
if(it == end(files))
|
|
throw http::error{http::NOT_FOUND};
|
|
|
|
const auto &filename(it->second);
|
|
std::ifstream file(filename);
|
|
std::noskipws(file);
|
|
const std::string content
|
|
{
|
|
std::istream_iterator<char>{file}, std::istream_iterator<char>{}
|
|
};
|
|
|
|
string_view content_type; switch(hash(rsplit(filename, '.').second))
|
|
{
|
|
case hash("css"): content_type = "text/css; charset=utf-8"; break;
|
|
case hash("js"): content_type = "application/javascript; charset=utf-8"; break;
|
|
case hash("html"): content_type = "text/html; charset=utf-8"; break;
|
|
case hash("ico"): content_type = "image/x-icon"; break;
|
|
case hash("svg"): content_type = "image/svg+xml"; break;
|
|
case hash("png"): content_type = "image/png"; break;
|
|
case hash("woff2"): content_type = "application/font-woff2"; break;
|
|
case hash("woff"): content_type = "application/font-woff"; break;
|
|
case hash("eot"): content_type = "application/vnd.ms-fontobject"; break;
|
|
case hash("otf"):
|
|
case hash("ttf"): content_type = "application/font-sfnt"; break;
|
|
default: content_type = "text/plain; charset=utf-8"; break;
|
|
}
|
|
|
|
return resource::response
|
|
{
|
|
client, string_view{content}, content_type
|
|
};
|
|
}
|
|
|
|
resource root_resource
|
|
{
|
|
"", "Root resource",
|
|
{
|
|
root_resource.DIRECTORY
|
|
}
|
|
};
|
|
|
|
resource::method root_get
|
|
{
|
|
root_resource, "GET", get_root
|
|
};
|