RedirectingHttpWebApp.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_HTTP_WEB_APPS__REDIRECTING_HTTP_WEB_APP
18 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_HTTP_WEB_APPS__REDIRECTING_HTTP_WEB_APP
19 
21 
22 namespace Balau {
23 
24 class BalauLogger;
25 class EnvironmentProperties;
26 
27 namespace Network::Http::HttpWebApps {
28 
50  public: RedirectingHttpWebApp(const EnvironmentProperties & configuration, const BalauLogger & logger);
51 
52  public: void handleGetRequest(HttpSession & session,
53  const StringRequest & request,
54  std::map<std::string, std::string> & variables) override;
55 
56  public: void handleHeadRequest(HttpSession & session,
57  const StringRequest & request,
58  std::map<std::string, std::string> & variables) override;
59 
60  public: void handlePostRequest(HttpSession & session,
61  const StringRequest & request,
62  std::map<std::string, std::string> & variables) override;
63 
65 
66  private: void handle(HttpSession & session,
67  const StringRequest & request,
68  std::map<std::string, std::string> & variables);
69 
70  public: struct PathComponent {
71  bool isVariable;
72  std::string value;
73 
74  // Appends the string or variable value to the stream.
75  void append(std::ostringstream & stream, std::map<std::string, std::string> & variables) const {
76  if (isVariable) {
77  auto iter = variables.find(value);
78 
79  if (iter != variables.end()) {
80  stream << iter->second;
81  }
82  } else {
83  stream << value;
84  }
85  }
86  };
87 
88  public: struct Redirection {
89  std::regex matcher;
90  unsigned int code;
91  std::vector<PathComponent> pathComponents;
92  #ifdef BALAU_DEBUG
93  std::string regexStr;
94  #endif
95 
96  std::string createPath(std::map<std::string, std::string> & variables) const {
97  std::ostringstream stream;
98 
99  for (auto & component : pathComponents) {
100  component.append(stream, variables);
101  }
102 
103  return stream.str();
104  }
105 
106  Redirection() = default;
107  Redirection(const Redirection & ) = default;
108  Redirection & operator = (const Redirection & ) = default;
109 
110  Redirection(const std::regex & matcher_,
111  unsigned int code_,
112  const std::vector<PathComponent> & pathComponents_
113  #ifdef BALAU_DEBUG
114  , const std::string regexStr_
115  #endif
116  )
117  : matcher(matcher_)
118  , code(code_)
119  , pathComponents(pathComponents_)
120  #ifdef BALAU_DEBUG
121  , regexStr(regexStr_)
122  #endif
123  {}
124  };
125 
126  private: static std::vector<Redirection> buildRedirections(const EnvironmentProperties & configuration,
127  const BalauLogger & logger);
128 
129  private: static std::vector<PathComponent> buildPathComponents(const std::string & path);
130 
131  private: const std::vector<Redirection> redirections;
132 };
133 
134 } // namespace Network::Http::HttpWebApps
135 
136 } // namespace Balau
137 
138 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_HTTP_WEB_APPS__REDIRECTING_HTTP_WEB_APP
Manages the handling of HTTP messages and WebSocket upgrade requests in an HTTP connection.
Definition: HttpSession.hpp:46
void handlePostRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override
Handle a POST request.
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
A hierarchical environment properties holder created from a composite property.
Definition: EnvironmentProperties.hpp:59
Abstract base class of HTTP web application handlers.
Definition: HttpWebApp.hpp:39
void handleGetRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override
Handle a GET request.
void handleHeadRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override
Handle a HEAD request.
Abstract base class of HTTP web application handlers.
RedirectingHttpWebApp(const EnvironmentProperties &configuration, const BalauLogger &logger)
Constructor called by the HTTP server during construction.
Request< StringBody > StringRequest
A request with a string body.
Definition: NetworkTypes.hpp:267
An HTTP web application handler that performs 301 or 302 redirection for a range of HTTP paths...
Definition: RedirectingHttpWebApp.hpp:44