/** * * StaticFileRouter.h * An Tao * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license * that can be found in the License file. * * Drogon * */ #pragma once #include "impl_forwards.h" #include "MiddlewaresFunction.h" #include #include #include #include #include #include namespace drogon { class StaticFileRouter { public: static StaticFileRouter &instance() { static StaticFileRouter inst; return inst; } void route(const HttpRequestImplPtr &req, std::function &&callback); void setFileTypes(const std::vector &types); void setStaticFilesCacheTime(int cacheTime) { staticFilesCacheTime_ = cacheTime; } int staticFilesCacheTime() const { return staticFilesCacheTime_; } void setGzipStatic(bool useGzipStatic) { gzipStaticFlag_ = useGzipStatic; } void setBrStatic(bool useBrStatic) { brStaticFlag_ = useBrStatic; } void init(const std::vector &ioLoops); void reset(); void sendStaticFileResponse( const std::string &filePath, const HttpRequestImplPtr &req, std::function &&callback, const std::string_view &defaultContentType); void addALocation(const std::string &uriPrefix, const std::string &defaultContentType, const std::string &alias, bool isCaseSensitive, bool allowAll, bool isRecursive, const std::vector &middlewareNames) { locations_.emplace_back(uriPrefix, defaultContentType, alias, isCaseSensitive, allowAll, isRecursive, middlewareNames); } void setStaticFileHeaders( const std::vector> &headers) { headers_ = headers; } void setImplicitPageEnable(bool useImplicitPage) { implicitPageEnable_ = useImplicitPage; } bool isImplicitPageEnabled() const { return implicitPageEnable_; } void setImplicitPage(const std::string &implicitPageFile) { implicitPage_ = implicitPageFile; } const std::string &getImplicitPage() const { return implicitPage_; } void setDefaultHandler(DefaultHandler &&handler) { defaultHandler_ = std::move(handler); } private: static void defaultHandler( const HttpRequestPtr &req, std::function &&callback); std::set fileTypeSet_{"html", "js", "css", "xml", "xsl", "txt", "svg", "ttf", "otf", "woff2", "woff", "eot", "png", "jpg", "jpeg", "gif", "bmp", "ico", "icns"}; int staticFilesCacheTime_{5}; bool enableLastModify_{true}; bool enableRange_{true}; bool gzipStaticFlag_{true}; bool brStaticFlag_{true}; std::unique_ptr< IOThreadStorage>>> staticFilesCacheMap_; std::unique_ptr< IOThreadStorage>> staticFilesCache_; std::vector> headers_; bool implicitPageEnable_{true}; std::string implicitPage_{"index.html"}; DefaultHandler defaultHandler_ = StaticFileRouter::defaultHandler; struct Location { std::string uriPrefix_; std::string defaultContentType_; std::string alias_; std::string realLocation_; bool isCaseSensitive_; bool allowAll_; bool isRecursive_; std::vector> middlewares_; Location(const std::string &uriPrefix, const std::string &defaultContentType, const std::string &alias, bool isCaseSensitive, bool allowAll, bool isRecursive, const std::vector &middlewares) : uriPrefix_(uriPrefix), alias_(alias), isCaseSensitive_(isCaseSensitive), allowAll_(allowAll), isRecursive_(isRecursive), middlewares_(middlewares_function::createMiddlewares(middlewares)) { if (!defaultContentType.empty()) { defaultContentType_ = std::string{"content-type: "} + defaultContentType + "\r\n"; } } }; std::shared_ptr>> ioLocationsPtr_; std::vector locations_; }; } // namespace drogon