| 334 | */ |
| 335 | |
| 336 | static Dictionary::Ptr FetchData(const String& host, const String& port, const String& password, |
| 337 | const String& endpoint) |
| 338 | { |
| 339 | namespace beast = boost::beast; |
| 340 | namespace http = beast::http; |
| 341 | |
| 342 | Shared<AsioTlsStream>::Ptr tlsStream; |
| 343 | |
| 344 | try { |
| 345 | tlsStream = Connect(host, port); |
| 346 | } catch (const std::exception& ex) { |
| 347 | std::cerr << "Connection error: " << ex.what(); |
| 348 | throw ex; |
| 349 | } |
| 350 | |
| 351 | Url::Ptr url; |
| 352 | |
| 353 | try { |
| 354 | url = new Url(endpoint); |
| 355 | } catch (const std::exception& ex) { |
| 356 | std::cerr << "URL error: " << ex.what(); |
| 357 | throw ex; |
| 358 | } |
| 359 | |
| 360 | url->SetScheme("https"); |
| 361 | url->SetHost(host); |
| 362 | url->SetPort(port); |
| 363 | |
| 364 | // NSClient++ uses `time=1m&time=5m` instead of `time[]=1m&time[]=5m` |
| 365 | url->SetArrayFormatUseBrackets(false); |
| 366 | |
| 367 | http::request<http::string_body> request (http::verb::get, std::string(url->Format(true)), 10); |
| 368 | |
| 369 | request.set(http::field::user_agent, "Icinga/check_nscp_api/" + String(VERSION)); |
| 370 | request.set(http::field::host, host + ":" + port); |
| 371 | |
| 372 | request.set(http::field::accept, "application/json"); |
| 373 | request.set("password", password); |
| 374 | |
| 375 | if (l_Debug) { |
| 376 | std::cout << "Sending request to " << url->Format(false, false) << "'.\n"; |
| 377 | } |
| 378 | |
| 379 | try { |
| 380 | http::write(*tlsStream, request); |
| 381 | tlsStream->flush(); |
| 382 | } catch (const std::exception& ex) { |
| 383 | std::cerr << "Cannot write HTTP request to REST API at URL '" << url->Format(false, false) << "': " << ex.what(); |
| 384 | throw ex; |
| 385 | } |
| 386 | |
| 387 | beast::flat_buffer buffer; |
| 388 | http::parser<false, http::string_body> p; |
| 389 | |
| 390 | try { |
| 391 | HttpResponseReasonInjector<decltype(*tlsStream)> reasonInjector (*tlsStream); |
| 392 | http::read(reasonInjector, buffer, p); |
| 393 | } catch (const std::exception &ex) { |
no test coverage detected