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

Function handle_request

example/advanced/server/advanced_server.cpp:110–207  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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