libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
httpfilehandler.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_HTTP
13#include <promeki/namespace.h>
14#include <promeki/string.h>
15#include <promeki/dir.h>
16#include <promeki/hashmap.h>
17#include <promeki/httphandler.h>
18
19PROMEKI_NAMESPACE_BEGIN
20
67class HttpFileHandler : public HttpHandler {
68 PROMEKI_SHARED_DERIVED(HttpFileHandler)
69 public:
71 static const String DefaultIndexFile;
72
74 static constexpr int64_t StreamChunkBytes = 64 * 1024;
75
83 explicit HttpFileHandler(const Dir &root);
84
86 explicit HttpFileHandler(const String &rootPath);
87
89 const Dir &root() const { return _root; }
90
100 void setPathParamName(const String &name) { _pathParam = name; }
101
109 void setIndexFile(const String &name) { _indexFile = name; }
110
119 void addMimeType(const String &ext, const String &mime);
120
129 void setListDirectories(bool enable) { _listDirs = enable; }
130
140 String resolveRequestPath(const HttpRequest &request) const;
141
143 String mimeType(const String &filePath) const;
144
146 void serve(const HttpRequest &request, HttpResponse &response) override;
147
148 private:
149 Dir _root;
150 String _pathParam = "path";
151 String _indexFile = DefaultIndexFile;
152 bool _listDirs = false;
153 HashMap<String, String> _extraMime;
154
155 static String etagFor(int64_t size, int64_t mtimeEpoch);
156 static String httpDateFor(int64_t mtimeEpoch);
157 static bool isPathSafe(const String &relPath);
158
159 bool serveRange(const HttpRequest &request, HttpResponse &response, const String &fullPath,
160 int64_t fileSize, const String &mime);
161};
162
163PROMEKI_NAMESPACE_END
164
165#endif // PROMEKI_ENABLE_HTTP