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