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

Function handle_request

example/advanced/server-flex/advanced_server_flex.cpp:114–211  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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