From 6c1cc0b2c854dd56dcb6238816a6ce2cb493c71c Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 10 May 2020 18:18:37 +0200 Subject: Separated out lib --- plugins/static-files/Makefile | 11 ++++++---- plugins/static-files/static-files.cpp | 40 +++-------------------------------- 2 files changed, 10 insertions(+), 41 deletions(-) (limited to 'plugins/static-files') diff --git a/plugins/static-files/Makefile b/plugins/static-files/Makefile index fdc0896..e96257d 100644 --- a/plugins/static-files/Makefile +++ b/plugins/static-files/Makefile @@ -19,7 +19,7 @@ endif # -fprofile-instr-generate -fcoverage-mapping # gcc:--coverage -CXXFLAGS+= -Wall -I. +CXXFLAGS+= -Wall -I. -I../.. CXXFLAGS+= -pthread -fvisibility=hidden -fPIC ifeq ($(CXX),clang++-10) @@ -40,7 +40,8 @@ LIBS=\ -lboost_regex \ -lpthread \ -lssl -lcrypto \ --ldl +-ldl \ +-lcommon ifeq ($(CXX),clang++-10) LIBS+= \ @@ -56,6 +57,8 @@ LIBS+= \ -lstdc++fs endif +LDFLAGS=-L../../libcommon + PROGSRC=\ static-files.cpp @@ -73,8 +76,8 @@ all: $(PROJECTNAME).so test-$(PROJECTNAME): $(TESTSRC:.cpp=.o) $(CXX) $(CXXFLAGS) $^ $(LIBS) -o $@ -$(PROJECTNAME).so: $(SRC:.cpp=.o) - $(CXX) -shared $(CXXFLAGS) $^ $(LIBS) -o $@ +$(PROJECTNAME).so: ../../libcommon/libcommon.a $(SRC:.cpp=.o) + $(CXX) -shared $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@ dep: $(TESTSRC:.cpp=.d) diff --git a/plugins/static-files/static-files.cpp b/plugins/static-files/static-files.cpp index 011e37f..3f1c63a 100644 --- a/plugins/static-files/static-files.cpp +++ b/plugins/static-files/static-files.cpp @@ -1,5 +1,7 @@ #include "static-files.h" +#include "libcommon/mime.h" + #include #include @@ -12,42 +14,6 @@ namespace fs = std::filesystem; namespace { -// Return a reasonable mime type based on the extension of a file. -std::string -mime_type(fs::path path) -{ - using boost::algorithm::iequals; - auto const ext = [&path] - { - size_t pos = path.string().rfind("."); - if (pos == std::string::npos) - return std::string{}; - return path.string().substr(pos); - }(); - if(iequals(ext, ".htm")) return "text/html"; // TODO: unordered_map - if(iequals(ext, ".html")) return "text/html"; - if(iequals(ext, ".php")) return "text/html"; - if(iequals(ext, ".css")) return "text/css"; - if(iequals(ext, ".txt")) return "text/plain"; - if(iequals(ext, ".js")) return "application/javascript"; - if(iequals(ext, ".json")) return "application/json"; - if(iequals(ext, ".xml")) return "application/xml"; - if(iequals(ext, ".swf")) return "application/x-shockwave-flash"; - if(iequals(ext, ".flv")) return "video/x-flv"; - if(iequals(ext, ".png")) return "image/png"; - if(iequals(ext, ".jpe")) return "image/jpeg"; - if(iequals(ext, ".jpeg")) return "image/jpeg"; - if(iequals(ext, ".jpg")) return "image/jpeg"; - if(iequals(ext, ".gif")) return "image/gif"; - if(iequals(ext, ".bmp")) return "image/bmp"; - if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon"; - if(iequals(ext, ".tiff")) return "image/tiff"; - if(iequals(ext, ".tif")) return "image/tiff"; - if(iequals(ext, ".svg")) return "image/svg+xml"; - if(iequals(ext, ".svgz")) return "image/svg+xml"; - return "application/text"; -} - std::string getFile(const fs::path& filename) { std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate); @@ -126,7 +92,7 @@ std::string static_files_plugin::generate_page( return HttpStatus("301", "Correcting directory path", SetResponseHeader); } path = {extend_index_html(path)}; - SetResponseHeader("content_type", mime_type(path)); + SetResponseHeader("content_type", mime_type(path.string())); try { return getFile(path); -- cgit v1.2.3