Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
FileSystemRequestHandler.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Framework_WebServer_FileSystemRequestHandler_h_
5#define _Stroika_Framework_WebServer_FileSystemRequestHandler_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
9#include <filesystem>
10#include <optional>
11
12#include "Stroika/Foundation/IO/Network/HTTP/CacheControl.h"
13
14#include "Stroika/Frameworks/WebServer/Router.h"
15
16/*
17 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
18 *
19 * TODO:
20 * @todo Add optional logger feature, to log requests (possibly in standard https://www.w3.org/TR/WD-logfile)
21 *
22 */
23
25
26 using namespace Stroika::Foundation;
27
32
33 class Router;
34
35 /**
36 * @todo unclear if this should BE a Router or RequestHandler, but I think Handler, and so rename!
37 */
39 public:
40 struct Options;
41 static const Options kDefaultOptions;
42
43 public:
44 /**
45 * Any route to apply the handler, must match ALL argument constraints.
46 */
47 FileSystemRequestHandler (const filesystem::path& filesystemRoot, const Options& options = kDefaultOptions);
48 };
49
50 struct FileSystemRequestHandler::Options {
51 /**
52 * \brief typically starts with '/' since HTTP GET argument must be root relative
53 *
54 * but if you have a fURLPrefix2Strip = "/Files/" that means
55 * GET /Files/phred
56 *
57 * will look 'program EXE relative' for the file named 'phred' (convert /Files/phred, then strip leading "/Files/"
58 *
59 * Defaults to "/"
60 */
61 optional<String> fURLPrefix2Strip;
62
63 /**
64 * When a Lookup is requested of a top-level directory, the argument filenames will also match
65 * that directory lookup. This is - for example, so that home.asp, or home.html can be matched by
66 * a lookup of '/'.
67 */
68 optional<Sequence<filesystem::path>> fDefaultIndexFileNames;
69
70 /**
71 * NYI - see http://stroika-bugs.sophists.com/browse/STK-732
72 */
73 enum ETagStrategy {
74 eDont,
75 eDateStamp,
76 eDigest,
77 };
78
79 /**
80 * ReportETags
81 * @todo NYI
82 * NYI - see http://stroika-bugs.sophists.com/browse/STK-732
83 */
84 optional<ETagStrategy> fETagStrategy;
85
86 /**
87 */
88 static constexpr ETagStrategy kDefault_ETagStrategy{eDigest};
89
90 // @todo
91 // ETagCacheSize (ether digest or date stamp - whether we keep in RAM idea of current value
92 // to respond without read (note date check is still a form of read); but this cache is less
93 // useful IF using datestamp etag strategy
94 // @see http://stroika-bugs.sophists.com/browse/STK-732
95
96 /**
97 * fCacheControlSettings provides a sequence of RegExp: CacheControl pairs. These are automatically applied
98 * to the host-relative pathname for the resource (foo/bar/blah.gif forward slash even in windows) - and
99 * the first matching regexp - if any - we default to using its associated Cache-Control settings.
100 *
101 * This makes it easy to setup default/automatic rules for applying cache control settings.
102 *
103 * \par Example Usage:
104 * \code
105 * Sequence<pair<RegularExpression, CacheControl>> kFSCacheControlSettings_ {
106 * // webpack generates js/css files with a hex/hash prefix, so those are immutable
107 * pair<RegularExpression, CacheControl>{RegularExpression{".*[0-9a-f]+\\.(js|css|js\\.map)", eCaseInsensitive}, CacheControl::kImmutable},
108 * // treat everything else as valid to be cached for a day (very arbitrary)
109 * pair<RegularExpression, CacheControl>{RegularExpression::kAny, CacheControl{.fMaxAge = Duration{5min}.As<int32_t> ()}},
110 * };
111 * \endcode
112 */
113 optional<Sequence<pair<RegularExpression, CacheControl>>> fCacheControlSettings;
114
115 /**
116 * This is not needed in general. If the FileSystemRequestHandler doesn't find a match, it will just return
117 * 'not handled' - and the next route can match and handle it. But for some situations, its helpful to just
118 * have return a special magical page as if it matched. For example, this is helpful with
119 * https://router.vuejs.org/guide/essentials/history-mode.html HTML5Mode.
120 */
121 optional<filesystem::path> fFallbackFile;
122 };
123 inline const FileSystemRequestHandler::Options FileSystemRequestHandler::kDefaultOptions;
124
125}
126
127/*
128 ********************************************************************************
129 ***************************** Implementation Details ***************************
130 ********************************************************************************
131 */
132#include "FileSystemRequestHandler.inl"
133
134#endif /*_Stroika_Framework_WebServer_FileSystemRequestHandler_h_*/
RegularExpression is a compiled regular expression which can be used to match on a String class.
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.