Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
HttpFileserver Class Reference

HTTP-level controller for serving static files and directory listings. More...

#include <HttpFileserver.h>

+ Collaboration diagram for HttpFileserver:

Public Member Functions

 HttpFileserver ()
 Construct a new HttpFileserver object.
 
void handle_static_request (HttpRequest &req, HttpResponse &res, const RouteMatch &match)
 Handle requests for static file content.
 
void handle_list_directory (HttpRequest &req, HttpResponse &res, const RouteMatch &match)
 Handle requests to list directory contents.
 
std::string getMimeType (const std::string &filePath)
 Get the MIME type based on the file extension.
 

Private Attributes

FileHandler fileHandler
 

Detailed Description

Definition at line 67 of file HttpFileserver.h.

Constructor & Destructor Documentation

◆ HttpFileserver()

HttpFileserver::HttpFileserver ( )

Construct a new HttpFileserver object.

Definition at line 152 of file HttpFileserver.cpp.

153{
154 // fileHandler.init(); // switched to lazy init to avoid startup issues
155}

Member Function Documentation

◆ getMimeType()

std::string HttpFileserver::getMimeType ( const std::string &  filePath)

Get the MIME type based on the file extension.

Parameters
filePathFull file path.
Returns
MIME type string.
Parameters
filePathFull file path.
Returns
MIME type string.

Definition at line 222 of file HttpFileserver.cpp.

223{
224 TRACE("Getting MIME type for file: %s\n", filePath.c_str());
225
226 static const std::unordered_map<std::string, std::string> mimeTypes = {
227 {".html", "text/html"},
228 {".css", "text/css"},
229 {".js", "application/javascript"},
230 {".json", "application/json"},
231 {".jpg", "image/jpeg"},
232 {".jpeg", "image/jpeg"},
233 {".png", "image/png"},
234 {".gif", "image/gif"},
235 {".txt", "text/plain"},
236 {".xml", "application/xml"},
237 {".pdf", "application/pdf"},
238 {".zip", "application/zip"},
239 {".gz", "application/x-gzip-compressed"},
240 {".tar", "application/x-tar"},
241 {".mp4", "video/mp4"},
242 {".webm", "video/webm"},
243 {".ogg", "audio/ogg"},
244 {".flac", "audio/flac"},
245 {".aac", "audio/aac"},
246 {".mp4", "video/mp4"},
247 {".mp3", "audio/mpeg"},
248 {".wav", "audio/wav"},
249 {".csv", "text/csv"}};
250
251 size_t extPos = filePath.find_last_of(".");
252 if (extPos != std::string::npos)
253 {
254 std::string ext = filePath.substr(extPos);
255 TRACE("Extracted extension: %s\n", ext.c_str());
256
257 auto it = mimeTypes.find(ext);
258 if (it != mimeTypes.end())
259 {
260 return it->second;
261 }
262 }
263
264 return "application/octet-stream";
265}
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187

References TRACE.

◆ handle_list_directory()

void HttpFileserver::handle_list_directory ( HttpRequest req,
HttpResponse res,
const RouteMatch match 
)

Handle requests to list directory contents.

Parameters
reqThe HTTP request object.
resThe HTTP response object.
paramsRoute parameters (unused).
Parameters
reqThe HTTP request object.
resThe HTTP response object.
paramsRoute parameters (unused).

Definition at line 158 of file HttpFileserver.cpp.

159{
160 std::string directory_path = req.getPath();
161 int pos = directory_path.find("/api/v1/ls");
162 if (pos != std::string::npos)
163 {
164 directory_path = directory_path.substr(pos + strlen("/api/v1/ls"));
165 }
166
167 if (directory_path.empty())
168 {
169 directory_path = "/"; // Default to root directory if no path is specified
170 }
171
172 std::vector<FileInfo> entries;
173 if (!fileHandler.listDirectory(directory_path, entries))
174 {
175 res.sendError(404, "not_found", "Directory not found or inaccessible");
176 return;
177 }
178
179 nlohmann::json fileArray = nlohmann::json::array();
180 for (const auto &entry : entries)
181 {
182 fileArray.push_back({{"name", entry.name},
183 {"size", entry.size},
184 {"type", entry.isDirectory ? "directory" : "file"}});
185 }
186
187 nlohmann::json result = {
188 {"path", directory_path},
189 {"files", fileArray}};
190
191 res.sendSuccess(result, "Directory listed successfully.");
192}
bool listDirectory(const std::string &path, std::vector< FileInfo > &out)
Return a list of a given directory.
FileHandler fileHandler
const std::string & getPath() const
Get the parsed request path (without query string).
HttpResponse & sendSuccess(const nlohmann::json &data={}, const std::string &message="")
HttpResponse & sendError(int statusCode, const std::string &code, const std::string &message)

References fileHandler, HttpRequest::getPath(), FileHandler::listDirectory(), HttpResponse::sendError(), and HttpResponse::sendSuccess().

Referenced by Router::listDirectory().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_static_request()

void HttpFileserver::handle_static_request ( HttpRequest req,
HttpResponse res,
const RouteMatch match 
)

Handle requests for static file content.

Parameters
reqThe HTTP request object.
resThe HTTP response object.
paramsRoute parameters (unused).
Parameters
reqThe HTTP request object.
resThe HTTP response object.
paramsRoute parameters (unused).

Definition at line 204 of file HttpFileserver.cpp.

205{
206 const std::string &uri = req.getPath();
207 printf("Serving static request for URI: %s\n", uri.c_str());
208
209 std::string filePath = urlDecode(uri);
210 printf("Decoded URI: %s\n", filePath.c_str());
211
212 if (uri.empty() || uri == "/")
213 {
214 filePath = "/index.html";
215 }
216
217
218 fileHandler.serveFile(res, filePath.c_str());
219}
bool serveFile(HttpResponse &res, const char *uri)
Serve a file to the client via the HttpResponse object.
std::string urlDecode(const std::string &src)
Definition url_utils.cpp:28

References fileHandler, HttpRequest::getPath(), FileHandler::serveFile(), and urlDecode().

Referenced by Router::serveStatic().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ fileHandler

FileHandler HttpFileserver::fileHandler
private

Definition at line 99 of file HttpFileserver.h.

Referenced by handle_list_directory(), and handle_static_request().


The documentation for this class was generated from the following files: