MCPcopy Create free account
hub / github.com/boostorg/beast / handle_request

Function handle_request

example/http/server/async-ssl/http_server_async_ssl.cpp:107–204  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

105// request), is type-erased in message_generator.
106template <class Body, class Allocator>
107http::message_generator
108handle_request(
109 beast::string_view doc_root,
110 http::request<Body, http::basic_fields<Allocator>>&& req)
111{
112 // Returns a bad request response
113 auto const bad_request =
114 [&req](beast::string_view why)
115 {
116 http::response<http::string_body> res{http::status::bad_request, req.version()};
117 res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
118 res.set(http::field::content_type, "text/html");
119 res.keep_alive(req.keep_alive());
120 res.body() = std::string(why);
121 res.prepare_payload();
122 return res;
123 };
124
125 // Returns a not found response
126 auto const not_found =
127 [&req](beast::string_view target)
128 {
129 http::response<http::string_body> res{http::status::not_found, req.version()};
130 res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
131 res.set(http::field::content_type, "text/html");
132 res.keep_alive(req.keep_alive());
133 res.body() = "The resource '" + std::string(target) + "' was not found.";
134 res.prepare_payload();
135 return res;
136 };
137
138 // Returns a server error response
139 auto const server_error =
140 [&req](beast::string_view what)
141 {
142 http::response<http::string_body> res{http::status::internal_server_error, req.version()};
143 res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
144 res.set(http::field::content_type, "text/html");
145 res.keep_alive(req.keep_alive());
146 res.body() = "An error occurred: '" + std::string(what) + "'";
147 res.prepare_payload();
148 return res;
149 };
150
151 // Make sure we can handle the method
152 if( req.method() != http::verb::get &&
153 req.method() != http::verb::head)
154 return bad_request("Unknown HTTP-method");
155
156 // Request path must be absolute and not contain "..".
157 if( req.target().empty() ||
158 req.target()[0] != '/' ||
159 req.target().find("..") != beast::string_view::npos)
160 return bad_request("Illegal request-target");
161
162 // Build the path to the requested file
163 std::string path = path_cat(doc_root, req.target());
164 if(req.target().back() == '/')

Callers 1

on_readMethod · 0.70

Calls 13

moveClass · 0.85
bodyMethod · 0.80
methodMethod · 0.80
targetMethod · 0.80
openMethod · 0.80
c_strMethod · 0.80
messageMethod · 0.80
path_catFunction · 0.70
mime_typeFunction · 0.70
versionMethod · 0.45
keep_aliveMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected