| 354 | } |
| 355 | |
| 356 | bool InitHTTPServer() |
| 357 | { |
| 358 | if (!InitHTTPAllowList()) |
| 359 | return false; |
| 360 | |
| 361 | // Redirect libevent's logging to our own log |
| 362 | event_set_log_callback(&libevent_log_cb); |
| 363 | // Update libevent's log handling. Returns false if our version of |
| 364 | // libevent doesn't support debug logging, in which case we should |
| 365 | // clear the BCLog::LIBEVENT flag. |
| 366 | if (!UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT))) { |
| 367 | LogInstance().DisableCategory(BCLog::LIBEVENT); |
| 368 | } |
| 369 | |
| 370 | #ifdef WIN32 |
| 371 | evthread_use_windows_threads(); |
| 372 | #else |
| 373 | evthread_use_pthreads(); |
| 374 | #endif |
| 375 | |
| 376 | raii_event_base base_ctr = obtain_event_base(); |
| 377 | |
| 378 | /* Create a new evhttp object to handle requests. */ |
| 379 | raii_evhttp http_ctr = obtain_evhttp(base_ctr.get()); |
| 380 | struct evhttp* http = http_ctr.get(); |
| 381 | if (!http) { |
| 382 | LogPrintf("couldn't create evhttp. Exiting.\n"); |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | evhttp_set_timeout(http, gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT)); |
| 387 | evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE); |
| 388 | evhttp_set_max_body_size(http, MAX_SIZE); |
| 389 | evhttp_set_gencb(http, http_request_cb, nullptr); |
| 390 | |
| 391 | if (!HTTPBindAddresses(http)) { |
| 392 | LogPrintf("Unable to bind any endpoint for RPC server\n"); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | LogPrint(BCLog::HTTP, "Initialized HTTP server\n"); |
| 397 | int workQueueDepth = std::max((long)gArgs.GetIntArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L); |
| 398 | LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth); |
| 399 | |
| 400 | g_work_queue = std::make_unique<WorkQueue<HTTPClosure>>(workQueueDepth); |
| 401 | // transfer ownership to eventBase/HTTP via .release() |
| 402 | eventBase = base_ctr.release(); |
| 403 | eventHTTP = http_ctr.release(); |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | bool UpdateHTTPServerLogging(bool enable) { |
| 408 | #if LIBEVENT_VERSION_NUMBER >= 0x02010100 |
no test coverage detected