MCPcopy Create free account
hub / github.com/FISCO-BCOS/FISCO-BCOS / handleRequest

Method handleRequest

bcos-boostssl/bcos-boostssl/httpserver/HttpSession.cpp:122–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120 }
121}
122void bcos::boostssl::http::HttpSession::handleRequest(const HttpRequest& _httpRequest)
123{
124 HTTP_SESSION(DEBUG) << LOG_BADGE("handleRequest") << LOG_DESC("request")
125 << LOG_KV("method", _httpRequest.method_string())
126 << LOG_KV("target", _httpRequest.target())
127 << LOG_KV("body", _httpRequest.body())
128 << LOG_KV("keep_alive", _httpRequest.keep_alive())
129 << LOG_KV("need_eof", _httpRequest.need_eof());
130
131 auto startT = utcTime();
132 unsigned version = _httpRequest.version();
133
134 // handle options request for CORS preflight
135 if (_httpRequest.method() == boost::beast::http::verb::options)
136 {
137 // The standard response status code for preflight requests is 204(No Content)
138 auto resp = buildHttpResp(boost::beast::http::status::no_content, _httpRequest.keep_alive(),
139 version, {}, m_corsConfig);
140
141 BCOS_LOG(TRACE) << LOG_BADGE("handleRequest") << LOG_DESC("options response")
142 << LOG_KV("body", std::string_view((const char*)resp->body().data(),
143 resp->body().size()))
144 << LOG_KV("keep_alive", resp->keep_alive())
145 << LOG_KV("need_eof", resp->need_eof())
146 << LOG_KV("timecost", (utcTime() - startT));
147
148 queue().enqueue(std::move(resp));
149 return;
150 }
151
152 // handle http request
153 if (m_httpReqHandler)
154 {
155 const std::string& request = _httpRequest.body();
156 m_httpReqHandler(request, [session = shared_from_this(), version, startT,
157 keepAlive = _httpRequest.keep_alive()](bcos::bytes _content) {
158 auto resp = session->buildHttpResp(boost::beast::http::status::ok, keepAlive, version,
159 std::move(_content), session->corsConfig());
160 // put the response into the queue and waiting to be send
161 BCOS_LOG(TRACE) << LOG_BADGE("handleRequest") << LOG_DESC("response")
162 << LOG_KV("body", std::string_view((const char*)resp->body().data(),
163 resp->body().size()))
164 << LOG_KV("keep_alive", resp->keep_alive())
165 << LOG_KV("need_eof", resp->need_eof())
166 << LOG_KV("timecost", (utcTime() - startT));
167 session->queue().enqueue(std::move(resp));
168 });
169 }
170 else
171 {
172 // unsupported http service
173 auto resp = buildHttpResp(boost::beast::http::status::http_version_not_supported,
174 _httpRequest.keep_alive(), version, {}, m_corsConfig);
175 // put the response into the queue and waiting to be send
176 HTTP_SESSION(WARNING) << LOG_BADGE("handleRequest") << LOG_DESC("unsupported http service")
177 << LOG_KV("body", std::string_view((const char*)resp->body().data(),
178 resp->body().size()));
179 queue().enqueue(std::move(resp));

Callers

nothing calls this directly

Calls 8

utcTimeFunction · 0.85
methodMethod · 0.80
buildHttpRespMethod · 0.80
versionMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45
enqueueMethod · 0.45
corsConfigMethod · 0.45

Tested by

no test coverage detected