VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
staticFileController.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2026 MAK Technologies, Inc.
3  * All rights reserved.
4  ******************************************************************************/
5 
6 #pragma once
7 
8 #include <oatpp/web/server/api/ApiController.hpp>
9 #include <oatpp/parser/json/mapping/ObjectMapper.hpp>
10 #include <oatpp/core/macro/codegen.hpp>
11 #include <oatpp/core/macro/component.hpp>
12 
13 #include <fstream>
14 #include <sstream>
15 #include <string>
16 #include <unordered_map>
17 
18 #include OATPP_CODEGEN_BEGIN(ApiController)
19 
20 class DtStaticFileController : public oatpp::web::server::api::ApiController
21 {
22 public:
23  DtStaticFileController(const std::shared_ptr<ObjectMapper>& objectMapper, const std::string& publicDir)
24  : oatpp::web::server::api::ApiController(objectMapper)
25  , myPublicDir(publicDir)
26  {
27  // Ensure trailing separator
28  if (!myPublicDir.empty() && myPublicDir.back() != '/' && myPublicDir.back() != '\\')
29  {
30  myPublicDir += '/';
31  }
32  }
33 
34  // GET / -> index.html (landing page)
35  ENDPOINT("GET", "/", serveRoot, REQUEST(std::shared_ptr<IncomingRequest>, request))
36  {
37  return serveFile("index.html");
38  }
39 
40  // GET /<path> e.g. /testClient.html, /js/app.js, /css/style.css
41  ENDPOINT("GET", "/*", serveAll, REQUEST(std::shared_ptr<IncomingRequest>, request))
42  {
43  const oatpp::String tail = request->getPathTail();
44  const std::string filePath = (tail && !tail->empty()) ? tail->c_str() : "index.html";
45  return serveFile(filePath);
46  }
47 
48 private:
49  std::string myPublicDir;
50 
51  static oatpp::String contentTypeFor(const std::string& filename)
52  {
53  static const std::unordered_map<std::string, std::string> types = {
54  {".html", "text/html"},
55  {".htm", "text/html"},
56  {".css", "text/css"},
57  {".js", "application/javascript"},
58  {".json", "application/json"},
59  {".png", "image/png"},
60  {".jpg", "image/jpeg"},
61  {".jpeg", "image/jpeg"},
62  {".gif", "image/gif"},
63  {".svg", "image/svg+xml"},
64  {".ico", "image/x-icon"},
65  {".txt", "text/plain"},
66  };
67 
68  auto dot = filename.rfind('.');
69  if (dot != std::string::npos)
70  {
71  auto it = types.find(filename.substr(dot));
72  if (it != types.end())
73  {
74  return it->second.c_str();
75  }
76  }
77  return "application/octet-stream";
78  }
79 
80  // Reject paths that try to escape the public directory.
81  // Allows '/' for subdirectory traversal but rejects any ".." segment.
82  // Backslashes are rejected outright: on Windows they act as path separators
83  // and could be used to bypass the '/' segment check (e.g. "sub\..\..").
84  static bool isSafeName(const std::string& name)
85  {
86  if (name.empty() || name.front() == '/' || name.front() == '\\')
87  return false;
88  if (name.find('\\') != std::string::npos)
89  return false;
90  std::istringstream stream(name);
91  std::string segment;
92  while (std::getline(stream, segment, '/'))
93  {
94  if (segment == "..")
95  return false;
96  }
97  return true;
98  }
99 
100  std::shared_ptr<OutgoingResponse> serveFile(const std::string& filename)
101  {
102  if (!isSafeName(filename))
103  {
104  return createResponse(Status::CODE_400, "Bad Request");
105  }
106 
107  std::string path = myPublicDir + filename;
108  std::ifstream file(path, std::ios::binary);
109  if (!file.is_open())
110  {
111  return createResponse(Status::CODE_404, "Not Found");
112  }
113 
114  std::ostringstream ss;
115  ss << file.rdbuf();
116  std::string content = ss.str();
117 
118  auto response = createResponse(Status::CODE_200, content.c_str());
119  response->putHeader("Content-Type", contentTypeFor(filename));
120  return response;
121  }
122 };
123 
124 #include OATPP_CODEGEN_END(ApiController)
std::string myPublicDir
Definition: staticFileController.h:49
std::shared_ptr< OutgoingResponse > serveFile(const std::string &filename)
Definition: staticFileController.h:100
const char * filename
Definition: ioapi.h:38
DtStaticFileController(const std::shared_ptr< ObjectMapper > &objectMapper, const std::string &publicDir)
Definition: staticFileController.h:23
Definition: staticFileController.h:20
static oatpp::String contentTypeFor(const std::string &filename)
Definition: staticFileController.h:51
const std::string filePath
Definition: staticFileController.h:44
static bool isSafeName(const std::string &name)
Definition: staticFileController.h:84
voidpf stream
Definition: ioapi.h:39
return serveFile(filePath)
DT_DLL_vrfutil double dot(double x[3], double y[3])

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)