Skip to content

Commit

Permalink
Add static file type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
hugglesfox committed Sep 12, 2020
1 parent 7cef123 commit a931d68
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/static_file.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
#include "static_file.h"

string file_type(string filename) {
string ext = split_at(filename, '.').back();

if (ext == "css") {
return "text/css";
}

if (ext == "csv") {
return "text/csv";
}

if (ext == "html" || ext == "htm") {
return "text/html";
}

if (ext == "ico") {
return "image/vnd.microsoft.icon";
}

if (ext == "js") {
return "text/javascript";
}

if (ext == "json") {
return "application/json";
}

if (ext == "xhtml") {
return "application/xhtml+xml";
}

return "text/plain";
}
11 changes: 10 additions & 1 deletion src/static_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "middleware.h"
#include "route.h"
#include "router.h"
#include "utils.h"

string file_type(string filename);

class StaticFile : public Route {
private:
Expand All @@ -16,9 +19,15 @@ class StaticFile : public Route {
public:
UriArgs uri_args;

StaticFile(string filename, string path, string content_type = "text/plain")
StaticFile(string filename, string path,
string content_type)
: filename(filename), path(path), content_type(content_type) {}

StaticFile(string filename, string path)
: filename(filename), path(path) {
content_type = file_type(filename);
}

Outcome middlewares() {
return MiddleWares<StaticFile>()
.add(new Router<StaticFile>(HTTP_GET_METHOD, {path + filename}))
Expand Down

0 comments on commit a931d68

Please sign in to comment.