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