NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/ext/http/server/file_http_server.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <fstream>
7 #include <iostream>
8 #include <string>
9 #include <unordered_map>
10 #include <vector>
11 
12 #include "opentelemetry/ext/http/server/http_server.h"
13 
14 namespace HTTP_SERVER_NS
15 {
16 
18 {
19 protected:
25  FileHttpServer(const std::string &host = "127.0.0.1", int port = 3333) : HttpServer()
26  {
27  std::ostringstream os;
28  os << host << ":" << port;
29  setServerName(os.str());
30  addListeningPort(port);
31  };
32 
39  void InitializeFileEndpoint(FileHttpServer &server) { server[root_endpt_] = ServeFile; }
40 
41 private:
52  bool FileGetSuccess(const std::string &filename, std::vector<char> &result)
53  {
54 #ifdef _WIN32
55  std::replace(filename.begin(), filename.end(), '/', '\\');
56 #endif
57  std::streampos size;
58  std::ifstream file(filename, std::ios::in | std::ios::binary | std::ios::ate);
59  if (file.is_open())
60  {
61  size = file.tellg();
62  if (size)
63  {
64  result.resize(size);
65  file.seekg(0, std::ios::beg);
66  file.read(result.data(), size);
67  }
68  file.close();
69  return true;
70  }
71  return false;
72  };
73 
79  std::string GetMimeContentType(const std::string &filename)
80  {
81  std::string file_ext = filename.substr(filename.find_last_of(".") + 1);
82  auto file_type = mime_types_.find(file_ext);
83  return (file_type != mime_types_.end()) ? file_type->second : HTTP_SERVER_NS::CONTENT_TYPE_TEXT;
84  };
85 
91  std::string GetFileName(std::string name)
92  {
93  if (name.back() == '/')
94  {
95  auto temp = name.substr(0, name.size() - 1);
96  name = temp;
97  }
98  // If filename appears to be a directory, serve the hypothetical index.html
99  // file there
100  if (name.find(".") == std::string::npos)
101  name += "/index.html";
102 
103  return name;
104  }
105 
116  LOG_INFO("File: %s\n", req.uri.c_str());
117  auto f = GetFileName(req.uri);
118  auto filename = f.c_str() + 1;
119 
120  std::vector<char> content;
121  if (FileGetSuccess(filename, content))
122  {
123  resp.headers[HTTP_SERVER_NS::CONTENT_TYPE] = GetMimeContentType(filename);
124  resp.body = std::string(content.data(), content.size());
125  resp.code = 200;
127  return resp.code;
128  }
129  // Two additional 'special' return codes possible here:
130  // 0 - proceed to next handler
131  // -1 - immediately terminate and close connection
133  resp.code = 404;
135  resp.body = resp.message;
136  return 404;
137  }};
138 
139  // Maps file extensions to their HTTP-compatible mime file type
140  const std::unordered_map<std::string, std::string> mime_types_ = {
141  {"css", "text/css"}, {"png", "image/png"}, {"js", "text/javascript"},
142  {"htm", "text/html"}, {"html", "text/html"}, {"json", "application/json"},
143  {"txt", "text/plain"}, {"jpg", "image/jpeg"}, {"jpeg", "image/jpeg"},
144  };
145  const std::string root_endpt_ = "/";
146 };
147 
148 } // namespace HTTP_SERVER_NS
HTTP_SERVER_NS::HttpRequest::uri
std::string uri
Definition: includes/opentelemetry/ext/http/server/http_server.h:34
HTTP_SERVER_NS::HttpServer::setServerName
void setServerName(std::string const &name)
Definition: includes/opentelemetry/ext/http/server/http_server.h:193
HTTP_SERVER_NS::HttpRequest
Definition: includes/opentelemetry/ext/http/server/http_server.h:30
HTTP_SERVER_NS::CONTENT_TYPE
constexpr const char * CONTENT_TYPE
Definition: includes/opentelemetry/ext/http/server/http_server.h:26
HTTP_SERVER_NS::HttpRequestCallback
Definition: includes/opentelemetry/ext/http/server/http_server.h:50
half_float::detail::binary
HALF_CONSTEXPR_CONST binary_t binary
Tag for binary construction.
Definition: includes/half.hpp:553
HTTP_SERVER_NS::HttpResponse
Definition: includes/opentelemetry/ext/http/server/http_server.h:40
HTTP_SERVER_NS::HttpServer
Definition: includes/opentelemetry/ext/http/server/http_server.h:90
HTTP_SERVER_NS
Definition: includes/opentelemetry/ext/http/server/file_http_server.h:14
pydeepstreamdoc::tensor::TensorDoc::size
constexpr const char * size
Definition: service-maker/sources/python/src/bindings/docstrings/tensordoc.h:46
HTTP_SERVER_NS::HttpServer::addListeningPort
int addListeningPort(int port)
Definition: includes/opentelemetry/ext/http/server/http_server.h:195
HTTP_SERVER_NS::FileHttpServer
Definition: includes/opentelemetry/ext/http/server/file_http_server.h:17
HTTP_SERVER_NS::FileHttpServer::InitializeFileEndpoint
void InitializeFileEndpoint(FileHttpServer &server)
Set the HTTP server to serve static files from the root of host:port.
Definition: includes/opentelemetry/ext/http/server/file_http_server.h:39
HTTP_SERVER_NS::HttpServer::getDefaultResponseMessage
static const char * getDefaultResponseMessage(int code)
Definition: includes/opentelemetry/ext/http/server/http_server.h:777
HTTP_SERVER_NS::CONTENT_TYPE_TEXT
constexpr const char * CONTENT_TYPE_TEXT
Definition: includes/opentelemetry/ext/http/server/http_server.h:27
HTTP_SERVER_NS::FileHttpServer::FileHttpServer
FileHttpServer(const std::string &host="127.0.0.1", int port=3333)
Construct the server by initializing the endpoint for serving static files, which show up on the web ...
Definition: includes/opentelemetry/ext/http/server/file_http_server.h:25
LOG_INFO
#define LOG_INFO(fmt,...)
Definition: includes/ds3d/common/defines.h:66