HTTP request callback */
| 135 | |
| 136 | /** HTTP request callback */ |
| 137 | static void http_request_cb(struct evhttp_request *req, void *arg) { |
| 138 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 139 | Config &config = *reinterpret_cast<Config *>(arg); |
| 140 | |
| 141 | std::shared_ptr<HTTPRequest> hreq { std::make_shared<HTTPRequest>(req) }; |
| 142 | |
| 143 | LogPrint(BCLog::HTTP, "Received a %s request for %s from %s\n", |
| 144 | RequestMethodString(hreq->GetRequestMethod()), hreq->GetURI(), |
| 145 | hreq->GetPeer().ToString()); |
| 146 | |
| 147 | // Early address-based allow check |
| 148 | if (!ClientAllowed(hreq->GetPeer())) { |
| 149 | hreq->WriteReply(HTTP_FORBIDDEN); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // Early reject unknown HTTP methods |
| 154 | if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) { |
| 155 | hreq->WriteReply(HTTP_BADMETHOD); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Find registered handler for prefix |
| 160 | std::string strURI = hreq->GetURI(); |
| 161 | std::string path; |
| 162 | std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin(); |
| 163 | std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end(); |
| 164 | for (; i != iend; ++i) { |
| 165 | bool match = false; |
| 166 | if (i->exactMatch) { |
| 167 | match = (strURI == i->prefix); |
| 168 | } else { |
| 169 | match = (strURI.substr(0, i->prefix.size()) == i->prefix); |
| 170 | } |
| 171 | if (match) { |
| 172 | path = strURI.substr(i->prefix.size()); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Dispatch to worker thread. |
| 178 | if (i != iend) { |
| 179 | size_t workQueueDepth = std::max(static_cast<size_t>(gArgs.GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE)), size_t{1}); |
| 180 | |
| 181 | assert(pWorkQueue); |
| 182 | if(pWorkQueue->getTaskDepth() < workQueueDepth) { |
| 183 | auto handleTask = [&config, hreq = std::move(hreq), path, handler = i->handler]() |
| 184 | { |
| 185 | handler(config, hreq.get(), path); |
| 186 | }; |
| 187 | make_task(*pWorkQueue, std::move(handleTask)); |
| 188 | } |
| 189 | else { |
| 190 | LogPrintf("WARNING: request rejected because http work queue depth " |
| 191 | "exceeded, it can be increased with the -rpcworkqueue= " |
| 192 | "setting\n"); |
| 193 | hreq->WriteReply(HTTP_INTERNAL, "Work queue depth exceeded"); |
| 194 | } |
nothing calls this directly
no test coverage detected