HTTP request callback */
| 216 | |
| 217 | /** HTTP request callback */ |
| 218 | static void http_request_cb(struct evhttp_request* req, void* arg) |
| 219 | { |
| 220 | std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req)); |
| 221 | |
| 222 | LogPrint("http", "Received a %s request for %s from %s\n", |
| 223 | RequestMethodString(hreq->GetRequestMethod()), hreq->GetURI(), hreq->GetPeer().ToString()); |
| 224 | |
| 225 | // Early address-based allow check |
| 226 | if (!ClientAllowed(hreq->GetPeer())) { |
| 227 | hreq->WriteReply(HTTP_FORBIDDEN); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | // Early reject unknown HTTP methods |
| 232 | if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) { |
| 233 | hreq->WriteReply(HTTP_BADMETHOD); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // Find registered handler for prefix |
| 238 | std::string strURI = hreq->GetURI(); |
| 239 | std::string path; |
| 240 | std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin(); |
| 241 | std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end(); |
| 242 | for (; i != iend; ++i) { |
| 243 | bool match = false; |
| 244 | if (i->exactMatch) |
| 245 | match = (strURI == i->prefix); |
| 246 | else |
| 247 | match = (strURI.substr(0, i->prefix.size()) == i->prefix); |
| 248 | if (match) { |
| 249 | path = strURI.substr(i->prefix.size()); |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Dispatch to worker thread |
| 255 | if (i != iend) { |
| 256 | std::auto_ptr<HTTPWorkItem> item(new HTTPWorkItem(hreq.release(), path, i->handler)); |
| 257 | assert(workQueue); |
| 258 | if (workQueue->Enqueue(item.get())) |
| 259 | item.release(); /* if true, queue took ownership */ |
| 260 | else |
| 261 | item->req->WriteReply(HTTP_INTERNAL, "Work queue depth exceeded"); |
| 262 | } else { |
| 263 | hreq->WriteReply(HTTP_NOTFOUND); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** Event dispatcher thread */ |
| 268 | static void ThreadHTTP(struct event_base* base, struct evhttp* http) |
nothing calls this directly
no test coverage detected