MCPcopy Create free account
hub / github.com/LUX-Core/lux / http_request_cb

Function http_request_cb

src/httpserver.cpp:214–273  ·  view source on GitHub ↗

HTTP request callback */

Source from the content-addressed store, hash-verified

212
213/** HTTP request callback */
214static void http_request_cb(struct evhttp_request* req, void* arg)
215{
216 // Disable reading to work around a libevent bug, fixed in 2.2.0.
217 if (event_get_version_number() < 0x02020001) {
218 evhttp_connection* conn = evhttp_request_get_connection(req);
219 if (conn) {
220 bufferevent* bev = evhttp_connection_get_bufferevent(conn);
221 if (bev) {
222 bufferevent_disable(bev, EV_READ);
223 }
224 }
225 }
226 std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
227
228 LogPrint("http", "Received a %s request for %s from %s\n",
229 RequestMethodString(hreq->GetRequestMethod()), hreq->GetURI(), hreq->GetPeer().ToString());
230
231 // Early address-based allow check
232 if (!ClientAllowed(hreq->GetPeer())) {
233 hreq->WriteReply(HTTP_FORBIDDEN);
234 return;
235 }
236
237 // Early reject unknown HTTP methods
238 if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) {
239 hreq->WriteReply(HTTP_BADMETHOD);
240 return;
241 }
242
243 // Find registered handler for prefix
244 std::string strURI = hreq->GetURI();
245 std::string path;
246 std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
247 std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
248 for (; i != iend; ++i) {
249 bool match = false;
250 if (i->exactMatch)
251 match = (strURI == i->prefix);
252 else
253 match = (strURI.substr(0, i->prefix.size()) == i->prefix);
254 if (match) {
255 path = strURI.substr(i->prefix.size());
256 break;
257 }
258 }
259
260 // Dispatch to worker thread
261 if (i != iend) {
262 std::unique_ptr<HTTPWorkItem> item(new HTTPWorkItem(std::move(hreq), path, i->handler));
263 assert(workQueue);
264 if (workQueue->Enqueue(item.get()))
265 item.release(); /* if true, queue took ownership */
266 else {
267 LogPrintf("WARNING: request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting\n");
268 item->req->WriteReply(HTTP_INTERNAL, "Work queue depth exceeded");
269 }
270 } else {
271 hreq->WriteReply(HTTP_NOTFOUND);

Callers

nothing calls this directly

Calls 14

LogPrintFunction · 0.85
RequestMethodStringFunction · 0.85
ClientAllowedFunction · 0.85
GetRequestMethodMethod · 0.80
GetURIMethod · 0.80
GetPeerMethod · 0.80
WriteReplyMethod · 0.80
EnqueueMethod · 0.80
ToStringMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected