MCPcopy Create free account
hub / github.com/bitcoin-sv/bitcoin-sv / http_request_cb

Function http_request_cb

src/httpserver.cpp:137–198  ·  view source on GitHub ↗

HTTP request callback */

Source from the content-addressed store, hash-verified

135
136/** HTTP request callback */
137static 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 }

Callers

nothing calls this directly

Calls 14

RequestMethodStringFunction · 0.85
ClientAllowedFunction · 0.85
make_taskFunction · 0.85
GetRequestMethodMethod · 0.80
GetURIMethod · 0.80
GetPeerMethod · 0.80
WriteReplyMethod · 0.80
GetArgMethod · 0.80
getTaskDepthMethod · 0.80
ToStringMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected