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

Function handle_request

example/http/server/sync/http_server_sync.cpp:102–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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