MCPcopy Create free account
hub / github.com/ArduPilot/ArduRemoteID / ROMFS_Handler

Class ROMFS_Handler

RemoteIDModule/webinterface.cpp:19–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17 serve files from ROMFS
18 */
19class ROMFS_Handler : public RequestHandler
20{
21 bool canHandle(HTTPMethod method, String uri) {
22 if (uri == "/") {
23 uri = "/index.html";
24 }
25 uri = "web" + uri;
26 if (ROMFS::exists(uri.c_str())) {
27 return true;
28 }
29 return false;
30 }
31
32 bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
33 if (requestUri == "/") {
34 requestUri = "/index.html";
35 }
36 String uri = "web" + requestUri;
37 Serial.printf("handle: '%s'\n", requestUri.c_str());
38
39 // work out content type
40 const char *content_type = "text/html";
41 const struct {
42 const char *extension;
43 const char *content_type;
44 } extensions[] = {
45 { ".js", "text/javascript" },
46 { ".jpg", "image/jpeg" },
47 { ".png", "image/png" },
48 { ".css", "text/css" },
49 };
50 for (const auto &e : extensions) {
51 if (uri.endsWith(e.extension)) {
52 content_type = e.content_type;
53 break;
54 }
55 }
56
57 auto *f = ROMFS::find_stream(uri.c_str());
58 if (f != nullptr) {
59 server.sendHeader("Content-Encoding", "gzip");
60 server.streamFile(*f, content_type);
61 delete f;
62 return true;
63 }
64 return false;
65 }
66
67} ROMFS_Handler;
68
69/*
70 serve files from ROMFS

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected