MCPcopy Create free account
hub / github.com/WaykiChain/WaykiChain / http_request_cb

Function http_request_cb

src/rpc/core/httpserver.cpp:227–293  ·  view source on GitHub ↗

HTTP request callback */

Source from the content-addressed store, hash-verified

225
226/** HTTP request callback */
227static void http_request_cb(struct evhttp_request* req, void* arg) {
228 // Disable reading to work around a libevent bug, fixed in 2.2.0.
229 if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) {
230 evhttp_connection* conn = evhttp_request_get_connection(req);
231 if (conn) {
232 bufferevent* bev = evhttp_connection_get_bufferevent(conn);
233 if (bev) {
234 bufferevent_disable(bev, EV_READ);
235 }
236 }
237 }
238 std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
239
240 // Early address-based allow check
241 if (!ClientAllowed(hreq->GetPeer())) {
242 LogPrint(BCLog::RPC, "HTTP request from %s rejected: Client network is not allowed RPC access\n",
243 hreq->GetPeer().ToString());
244 hreq->WriteReply(HTTP_FORBIDDEN);
245 return;
246 }
247
248 // Early reject unknown HTTP methods
249 if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) {
250 LogPrint(BCLog::RPC, "HTTP request from %s rejected: Unknown HTTP request method\n",
251 hreq->GetPeer().ToString());
252 hreq->WriteReply(HTTP_BADMETHOD);
253 return;
254 }
255
256 LogPrint(BCLog::RPC, "Received a %s request for %s from %s\n",
257 RequestMethodString(hreq->GetRequestMethod()),
258 SanitizeString(hreq->GetURI(), SAFE_CHARS_URI).substr(0, 100),
259 hreq->GetPeer().ToString());
260
261 // Find registered handler for prefix
262 std::string strURI = hreq->GetURI();
263 std::string path;
264 std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
265 std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
266 for (; i != iend; ++i) {
267 bool match = false;
268 if (i->exactMatch)
269 match = (strURI == i->prefix);
270 else
271 match = (strURI.substr(0, i->prefix.size()) == i->prefix);
272 if (match) {
273 path = strURI.substr(i->prefix.size());
274 break;
275 }
276 }
277
278 // Dispatch to worker thread
279 if (i != iend) {
280 std::unique_ptr<HTTPWorkItem> item(new HTTPWorkItem(std::move(hreq), path, i->handler));
281 assert(workQueue);
282 if (workQueue->Enqueue(item.get()))
283 item.release(); /* if true, queue took ownership */
284 else {

Callers

nothing calls this directly

Calls 14

ClientAllowedFunction · 0.85
RequestMethodStringFunction · 0.85
GetPeerMethod · 0.80
WriteReplyMethod · 0.80
GetRequestMethodMethod · 0.80
GetURIMethod · 0.80
EnqueueMethod · 0.80
releaseMethod · 0.80
SanitizeStringFunction · 0.70
ToStringMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected