| 471 | } |
| 472 | |
| 473 | bool InitHTTPServer(Config &config) { |
| 474 | if (!InitHTTPAllowList()) { |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | // Redirect libevent's logging to our own log |
| 479 | event_set_log_callback(&libevent_log_cb); |
| 480 | // Update libevent's log handling. |
| 481 | UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT)); |
| 482 | |
| 483 | #ifdef WIN32 |
| 484 | evthread_use_windows_threads(); |
| 485 | #else |
| 486 | evthread_use_pthreads(); |
| 487 | #endif |
| 488 | |
| 489 | raii_event_base base_ctr = obtain_event_base(); |
| 490 | |
| 491 | /* Create a new evhttp object to handle requests. */ |
| 492 | raii_evhttp http_ctr = obtain_evhttp(base_ctr.get()); |
| 493 | struct evhttp *http = http_ctr.get(); |
| 494 | if (!http) { |
| 495 | LogPrintf("couldn't create evhttp. Exiting.\n"); |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | evhttp_set_timeout(http, gArgs.GetIntArg("-rpcservertimeout", |
| 500 | DEFAULT_HTTP_SERVER_TIMEOUT)); |
| 501 | evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE); |
| 502 | evhttp_set_max_body_size(http, MIN_SUPPORTED_BODY_SIZE + |
| 503 | 2 * config.GetMaxBlockSize()); |
| 504 | evhttp_set_gencb(http, http_request_cb, &config); |
| 505 | |
| 506 | // Only POST and OPTIONS are supported, but we return HTTP 405 for the |
| 507 | // others |
| 508 | evhttp_set_allowed_methods( |
| 509 | http, EVHTTP_REQ_GET | EVHTTP_REQ_POST | EVHTTP_REQ_HEAD | |
| 510 | EVHTTP_REQ_PUT | EVHTTP_REQ_DELETE | EVHTTP_REQ_OPTIONS); |
| 511 | |
| 512 | if (!HTTPBindAddresses(http)) { |
| 513 | LogPrintf("Unable to bind any endpoint for RPC server\n"); |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | LogPrint(BCLog::HTTP, "Initialized HTTP server\n"); |
| 518 | int workQueueDepth = std::max( |
| 519 | (long)gArgs.GetIntArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L); |
| 520 | LogDebug(BCLog::HTTP, "creating work queue of depth %d\n", workQueueDepth); |
| 521 | |
| 522 | workQueue = new WorkQueue<HTTPClosure>(workQueueDepth); |
| 523 | // transfer ownership to eventBase/HTTP via .release() |
| 524 | eventBase = base_ctr.release(); |
| 525 | eventHTTP = http_ctr.release(); |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | void UpdateHTTPServerLogging(bool enable) { |
| 530 | if (enable) { |
no test coverage detected