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

Function handle_request

example/http/server/flex/http_server_flex.cpp:106–203  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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