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

Function handle_request

example/http/server/coro/http_server_coro.cpp:104–201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

do_sessionFunction · 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