mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 22:34:58 +01:00
fix(editor): the closing doesn't wait for a geoportal service that doesn't answer (#432)
* fix(editor): the closing doesn't wait for a geoportal service that doesn't answer When geoportal.gov.pl took the connection and never answered, the editor's closing stood still for tens of seconds and was killed: the WinHTTP client was synchronous and looked at the stop only between the reads of a body, so a request waiting for the answer held the closing for the timeouts (up to 20 s for the answer, a few tries a square), and the map of the place-picker downloaded with no stop at all, its std::async future waited for to the end. On Windows the client now uses WinHTTP asynchronously: each step is waited for while the stop is looked at every 50 ms, and a stop closes the request at once. The timeouts, the user agent, the size limit, the errors and the body of a response with an error status stay as they were. The map of the place-picker is given up when it goes. libcurl looked at the stop during the whole transfer already. The unit tests run the client against a local server that never answers (given up within 0.4 s of the stop), one with a body of 100 kB and one with an error status. * fix(editor): the capabilities of a WMS service being added don't hold the closing either The reading of the layers of a WMS service the user adds ran on a std::async future of the editor with no stop: closing the editor while such a service didn't answer waited for its three tries. The reading is given up when the editor goes, as the other downloads now are, and editor_http_get, left with no callers, goes. The unit test's silent server no longer waits for the client to close its connection, and the requests to the local server are skipped when the environment names a proxy, which couldn't reach it. * fix(editor): the stop of the WMS reading outlives the future waiting for it The flag was declared after the future of the reading, so it was destroyed first while the future's destructor still waited for the task reading it. --------- Co-authored-by: maj00r <maj00r@host>
This commit is contained in:
@@ -618,6 +618,8 @@ void editor_mode::ground_sources(editor_orthophoto &Layer)
|
|||||||
editor_mode::~editor_mode()
|
editor_mode::~editor_mode()
|
||||||
{
|
{
|
||||||
editor_track::observe(nullptr);
|
editor_track::observe(nullptr);
|
||||||
|
// the capabilities of a WMS service being read are given up rather than waited for to their timeouts
|
||||||
|
m_wms_adding.closing = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<double> editor_mode::ground_heights(std::vector<glm::dvec3> const &Points, bool const Fresh, std::vector<char> *Found) const
|
std::vector<double> editor_mode::ground_heights(std::vector<glm::dvec3> const &Points, bool const Fresh, std::vector<char> *Found) const
|
||||||
@@ -4101,7 +4103,7 @@ void editor_mode::render_wms_adding()
|
|||||||
adding.info.reset();
|
adding.info.reset();
|
||||||
adding.ticked.clear();
|
adding.ticked.clear();
|
||||||
adding.service = url;
|
adding.service = url;
|
||||||
adding.reading = std::async(std::launch::async, [url]() { return editor_orthophoto::describe(url); });
|
adding.reading = std::async(std::launch::async, [url, closing = &adding.closing]() { return editor_orthophoto::describe(url, [closing]() { return closing->load(); }); });
|
||||||
}
|
}
|
||||||
if (adding.reading.valid())
|
if (adding.reading.valid())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <atomic>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -915,6 +916,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri
|
|||||||
{
|
{
|
||||||
std::array<char, 512> url{};
|
std::array<char, 512> url{};
|
||||||
std::array<char, 64> filter{};
|
std::array<char, 64> filter{};
|
||||||
|
// the reading is given up: the editor goes. before the reading, so it outlives the future waiting for the reading to end
|
||||||
|
std::atomic<bool> closing{false};
|
||||||
std::future<editor_orthophoto::service_info> reading;
|
std::future<editor_orthophoto::service_info> reading;
|
||||||
std::optional<editor_orthophoto::service_info> info;
|
std::optional<editor_orthophoto::service_info> info;
|
||||||
std::string service; // the address the capabilities were read of
|
std::string service; // the address the capabilities were read of
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "editor/editorGeoMap.hpp"
|
#include "editor/editorGeoMap.hpp"
|
||||||
#include "editor/editorFormat.hpp"
|
#include "editor/editorFormat.hpp"
|
||||||
|
#include "editor/editorHttp.hpp"
|
||||||
#include "editor/editorOrthophoto.hpp"
|
#include "editor/editorOrthophoto.hpp"
|
||||||
#include "utilities/translation.h"
|
#include "utilities/translation.h"
|
||||||
|
|
||||||
@@ -164,6 +165,8 @@ bool parse_place(std::string const &Text, glm::dvec2 &Point)
|
|||||||
|
|
||||||
editor_map::~editor_map()
|
editor_map::~editor_map()
|
||||||
{
|
{
|
||||||
|
// the download running is broken off rather than waited for to its end or its timeout
|
||||||
|
m_closing = true;
|
||||||
if (m_request.valid())
|
if (m_request.valid())
|
||||||
m_request.wait();
|
m_request.wait();
|
||||||
release();
|
release();
|
||||||
@@ -328,12 +331,13 @@ void editor_map::request(glm::vec2 const &Size)
|
|||||||
char bbox[160];
|
char bbox[160];
|
||||||
std::snprintf(bbox, sizeof(bbox), "%.1f,%.1f,%.1f,%.1f", low.x, low.y, high.x, high.y);
|
std::snprintf(bbox, sizeof(bbox), "%.1f,%.1f,%.1f,%.1f", low.x, low.y, high.x, high.y);
|
||||||
auto const url{kMapService + "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=osm&STYLES=&SRS=EPSG:2180&BBOX=" + bbox + "&WIDTH=" + std::to_string(width) + "&HEIGHT=" + std::to_string(height) + "&FORMAT=image/png"};
|
auto const url{kMapService + "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&LAYERS=osm&STYLES=&SRS=EPSG:2180&BBOX=" + bbox + "&WIDTH=" + std::to_string(width) + "&HEIGHT=" + std::to_string(height) + "&FORMAT=image/png"};
|
||||||
m_request = std::async(std::launch::async, [url, low, high]() {
|
m_request = std::async(std::launch::async, [url, low, high, closing = &m_closing]() {
|
||||||
image result;
|
image result;
|
||||||
result.low = low;
|
result.low = low;
|
||||||
result.high = high;
|
result.high = high;
|
||||||
std::vector<std::uint8_t> data;
|
std::vector<std::uint8_t> data;
|
||||||
if (false == editor_http_get(url, data, result.error))
|
thread_local editor_http::client client;
|
||||||
|
if (false == client.get(url, data, result.error, [closing]() { return closing->load(); }))
|
||||||
return result;
|
return result;
|
||||||
int w{0}, h{0}, components{0};
|
int w{0}, h{0}, components{0};
|
||||||
// the texture loader turns the stb flip on for the whole process only once it loads its first texture: this thread
|
// the texture loader turns the stb flip on for the whole process only once it loads its first texture: this thread
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -86,6 +87,7 @@ class editor_map
|
|||||||
glm::vec2 m_pressed{0.f};
|
glm::vec2 m_pressed{0.f};
|
||||||
bool m_dragging{false};
|
bool m_dragging{false};
|
||||||
std::future<image> m_request;
|
std::future<image> m_request;
|
||||||
|
std::atomic<bool> m_closing{false}; // the download of the map is given up: the map goes
|
||||||
std::uint64_t m_texture{0};
|
std::uint64_t m_texture{0};
|
||||||
glm::dvec2 m_image_low{0.0};
|
glm::dvec2 m_image_low{0.0};
|
||||||
glm::dvec2 m_image_high{0.0};
|
glm::dvec2 m_image_high{0.0};
|
||||||
|
|||||||
@@ -45,15 +45,209 @@ struct client::platform
|
|||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
DWORD constexpr kStopCheck{50}; // ms between the looks at whether the transfer is to be given up
|
||||||
|
|
||||||
|
// what the callback of WinHTTP hands over to the thread waiting for it
|
||||||
|
struct transfer_state
|
||||||
|
{
|
||||||
|
HANDLE done{::CreateEventW(nullptr, FALSE, FALSE, nullptr)}; // the operation begun finished or failed
|
||||||
|
HANDLE closed{::CreateEventW(nullptr, TRUE, FALSE, nullptr)}; // the request is called back no more
|
||||||
|
DWORD error{ERROR_SUCCESS}; // of the operation that failed
|
||||||
|
DWORD size{0}; // bytes available or read
|
||||||
|
|
||||||
|
transfer_state() = default;
|
||||||
|
transfer_state(transfer_state const &) = delete;
|
||||||
|
transfer_state &operator=(transfer_state const &) = delete;
|
||||||
|
~transfer_state()
|
||||||
|
{
|
||||||
|
if (done != nullptr)
|
||||||
|
::CloseHandle(done);
|
||||||
|
if (closed != nullptr)
|
||||||
|
::CloseHandle(closed);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void CALLBACK on_status(HINTERNET, DWORD_PTR const Context, DWORD const Status, LPVOID const Info, DWORD const Length)
|
||||||
|
{
|
||||||
|
auto *state{reinterpret_cast<transfer_state *>(Context)};
|
||||||
|
if (state == nullptr)
|
||||||
|
return;
|
||||||
|
switch (Status)
|
||||||
|
{
|
||||||
|
case WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE:
|
||||||
|
case WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE:
|
||||||
|
break;
|
||||||
|
case WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE:
|
||||||
|
state->size = *static_cast<DWORD const *>(Info);
|
||||||
|
break;
|
||||||
|
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
|
||||||
|
state->size = Length;
|
||||||
|
break;
|
||||||
|
case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
|
||||||
|
state->error = static_cast<WINHTTP_ASYNC_RESULT const *>(Info)->dwError;
|
||||||
|
break;
|
||||||
|
case WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING:
|
||||||
|
::SetEvent(state->closed);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
::SetEvent(state->done);
|
||||||
|
}
|
||||||
|
|
||||||
|
// one GET on a connection of its own. the steps of WinHTTP are asynchronous and waited for while the stop is looked at, so a
|
||||||
|
// transfer is given up at once also while a service keeps the connection waiting: the closing of the editor doesn't wait for
|
||||||
|
// the timeouts of a service that doesn't answer
|
||||||
|
class transfer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
transfer(stop_check const &Stop, std::string &Error) :
|
||||||
|
m_stop(Stop),
|
||||||
|
m_error(Error)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
~transfer()
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
transfer(transfer const &) = delete;
|
||||||
|
transfer &operator=(transfer const &) = delete;
|
||||||
|
|
||||||
|
bool open(HINTERNET const Session, std::wstring const &Host, INTERNET_PORT const Port, std::wstring const &Path, bool const Secure)
|
||||||
|
{
|
||||||
|
if (m_state.done == nullptr || m_state.closed == nullptr)
|
||||||
|
return failed(::GetLastError());
|
||||||
|
m_connection = ::WinHttpConnect(Session, Host.c_str(), Port, 0);
|
||||||
|
if (m_connection == nullptr)
|
||||||
|
return failed(::GetLastError());
|
||||||
|
m_request = ::WinHttpOpenRequest(m_connection, L"GET", Path.c_str(), nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, Secure ? WINHTTP_FLAG_SECURE : 0);
|
||||||
|
if (m_request == nullptr)
|
||||||
|
return failed(::GetLastError());
|
||||||
|
auto context{reinterpret_cast<DWORD_PTR>(&m_state)};
|
||||||
|
if (false == ::WinHttpSetOption(m_request, WINHTTP_OPTION_CONTEXT_VALUE, &context, sizeof(context)))
|
||||||
|
return failed(::GetLastError());
|
||||||
|
if (::WinHttpSetStatusCallback(m_request, on_status, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | WINHTTP_CALLBACK_FLAG_HANDLES, 0) == WINHTTP_INVALID_STATUS_CALLBACK)
|
||||||
|
return failed(::GetLastError());
|
||||||
|
m_called_back = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool send()
|
||||||
|
{
|
||||||
|
return step([this]() { return ::WinHttpSendRequest(m_request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, reinterpret_cast<DWORD_PTR>(&m_state)); });
|
||||||
|
}
|
||||||
|
|
||||||
|
bool receive()
|
||||||
|
{
|
||||||
|
return step([this]() { return ::WinHttpReceiveResponse(m_request, nullptr); });
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD status() const
|
||||||
|
{
|
||||||
|
DWORD status{0};
|
||||||
|
DWORD size{sizeof(status)};
|
||||||
|
::WinHttpQueryHeaders(m_request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &status, &size, WINHTTP_NO_HEADER_INDEX);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the whole body of the response
|
||||||
|
bool read_all(std::vector<std::uint8_t> &Out)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (m_stop())
|
||||||
|
{
|
||||||
|
m_error = "cancelled";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (false == step([this]() { return ::WinHttpQueryDataAvailable(m_request, nullptr); }))
|
||||||
|
return false;
|
||||||
|
auto const available{m_state.size};
|
||||||
|
if (available == 0)
|
||||||
|
return true;
|
||||||
|
if (Out.size() + available > kLargest)
|
||||||
|
{
|
||||||
|
m_error = kTooLarge;
|
||||||
|
Out.clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto const offset{Out.size()};
|
||||||
|
Out.resize(offset + available);
|
||||||
|
auto const read{step([this, &Out, offset, available]() { return ::WinHttpReadData(m_request, Out.data() + offset, available, nullptr); })};
|
||||||
|
Out.resize(offset + (read ? m_state.size : 0));
|
||||||
|
if (false == read)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
if (m_request != nullptr)
|
||||||
|
{
|
||||||
|
::WinHttpCloseHandle(m_request);
|
||||||
|
m_request = nullptr;
|
||||||
|
// the closing is the last callback: after it the state, and the buffer of a read broken off, are no longer written to
|
||||||
|
if (m_called_back)
|
||||||
|
::WaitForSingleObject(m_state.closed, INFINITE);
|
||||||
|
m_called_back = false;
|
||||||
|
}
|
||||||
|
if (m_connection != nullptr)
|
||||||
|
{
|
||||||
|
::WinHttpCloseHandle(m_connection);
|
||||||
|
m_connection = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool failed(DWORD const Code)
|
||||||
|
{
|
||||||
|
m_error = "WinHTTP error " + std::to_string(Code);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// begins an operation and waits for it: false when it failed, or a stop was asked for meanwhile
|
||||||
|
template <typename Begin> bool step(Begin const &Operation)
|
||||||
|
{
|
||||||
|
m_state.error = ERROR_SUCCESS;
|
||||||
|
if (false == Operation())
|
||||||
|
return failed(::GetLastError());
|
||||||
|
while (::WaitForSingleObject(m_state.done, kStopCheck) != WAIT_OBJECT_0)
|
||||||
|
{
|
||||||
|
if (m_stop())
|
||||||
|
{
|
||||||
|
// closed at once: the operation broken off doesn't write into the buffer any more once this returns
|
||||||
|
close();
|
||||||
|
m_error = "cancelled";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m_state.error != ERROR_SUCCESS)
|
||||||
|
return failed(m_state.error);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
stop_check const &m_stop;
|
||||||
|
std::string &m_error;
|
||||||
|
transfer_state m_state;
|
||||||
|
HINTERNET m_connection{nullptr};
|
||||||
|
HINTERNET m_request{nullptr};
|
||||||
|
bool m_called_back{false}; // the closing of the request is called back
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
client::client() :
|
client::client() :
|
||||||
m_platform(std::make_unique<platform>())
|
m_platform(std::make_unique<platform>())
|
||||||
{
|
{
|
||||||
wchar_t const *agent = L"MaSzyna-editor";
|
wchar_t const *agent = L"MaSzyna-editor";
|
||||||
#ifdef WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY
|
#ifdef WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY
|
||||||
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC);
|
||||||
#endif
|
#endif
|
||||||
if (m_platform->session == nullptr) // automatic proxy needs windows 8.1+
|
if (m_platform->session == nullptr) // automatic proxy needs windows 8.1+
|
||||||
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
m_platform->session = ::WinHttpOpen(agent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC);
|
||||||
if (m_platform->session != nullptr)
|
if (m_platform->session != nullptr)
|
||||||
::WinHttpSetTimeouts(m_platform->session, 10000, 10000, 15000, 20000);
|
::WinHttpSetTimeouts(m_platform->session, 10000, 10000, 15000, 20000);
|
||||||
}
|
}
|
||||||
@@ -94,64 +288,13 @@ bool client::get(std::string const &Url, std::vector<std::uint8_t> &Out, std::st
|
|||||||
std::wstring const host(parts.lpszHostName, parts.dwHostNameLength);
|
std::wstring const host(parts.lpszHostName, parts.dwHostNameLength);
|
||||||
std::wstring const path = std::wstring(parts.lpszUrlPath, parts.dwUrlPathLength) + std::wstring(parts.lpszExtraInfo, parts.dwExtraInfoLength);
|
std::wstring const path = std::wstring(parts.lpszUrlPath, parts.dwUrlPathLength) + std::wstring(parts.lpszExtraInfo, parts.dwExtraInfoLength);
|
||||||
|
|
||||||
HINTERNET connection = ::WinHttpConnect(m_platform->session, host.c_str(), parts.nPort, 0);
|
transfer request{Stop, Error};
|
||||||
HINTERNET request = connection ? ::WinHttpOpenRequest(connection, L"GET", path.c_str(), nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
|
if (false == request.open(m_platform->session, host, parts.nPort, path, parts.nScheme == INTERNET_SCHEME_HTTPS) || false == request.send() || false == request.receive())
|
||||||
parts.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0)
|
|
||||||
: nullptr;
|
|
||||||
auto const close = [&]() {
|
|
||||||
if (request != nullptr)
|
|
||||||
::WinHttpCloseHandle(request);
|
|
||||||
if (connection != nullptr)
|
|
||||||
::WinHttpCloseHandle(connection);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (request == nullptr || !::WinHttpSendRequest(request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0) || !::WinHttpReceiveResponse(request, nullptr))
|
|
||||||
{
|
|
||||||
Error = "WinHTTP error " + std::to_string(::GetLastError());
|
|
||||||
close();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
auto const status{request.status()};
|
||||||
|
if (false == request.read_all(Out))
|
||||||
DWORD status{0};
|
|
||||||
DWORD statussize{sizeof(status)};
|
|
||||||
::WinHttpQueryHeaders(request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &status, &statussize, WINHTTP_NO_HEADER_INDEX);
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (Stop())
|
|
||||||
{
|
|
||||||
Error = "cancelled";
|
|
||||||
close();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
request.close();
|
||||||
DWORD available{0};
|
|
||||||
if (!::WinHttpQueryDataAvailable(request, &available))
|
|
||||||
{
|
|
||||||
Error = "WinHTTP error " + std::to_string(::GetLastError());
|
|
||||||
close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (available == 0)
|
|
||||||
break;
|
|
||||||
if (Out.size() + available > kLargest)
|
|
||||||
{
|
|
||||||
Error = kTooLarge;
|
|
||||||
close();
|
|
||||||
Out.clear();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::size_t const offset = Out.size();
|
|
||||||
Out.resize(offset + available);
|
|
||||||
DWORD received{0};
|
|
||||||
if (!::WinHttpReadData(request, Out.data() + offset, available, &received))
|
|
||||||
{
|
|
||||||
Error = "WinHTTP error " + std::to_string(::GetLastError());
|
|
||||||
close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Out.resize(offset + received);
|
|
||||||
}
|
|
||||||
close();
|
|
||||||
|
|
||||||
if (status != 200)
|
if (status != 200)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -63,12 +63,6 @@ double steady_seconds()
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
bool editor_http_get(std::string const &Url, std::vector<std::uint8_t> &Out, std::string &Error)
|
|
||||||
{
|
|
||||||
thread_local http_client client;
|
|
||||||
return client.get(Url, Out, Error, []() { return false; });
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -688,7 +682,7 @@ std::string xml_element(std::string const &Xml, std::string const &Tag, std::siz
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
editor_orthophoto::service_info editor_orthophoto::describe(std::string const &Service)
|
editor_orthophoto::service_info editor_orthophoto::describe(std::string const &Service, std::function<bool()> const &Stop)
|
||||||
{
|
{
|
||||||
service_info info;
|
service_info info;
|
||||||
auto url{Service};
|
auto url{Service};
|
||||||
@@ -700,8 +694,10 @@ editor_orthophoto::service_info editor_orthophoto::describe(std::string const &S
|
|||||||
// a few tries, the services drop connections and time out now and then
|
// a few tries, the services drop connections and time out now and then
|
||||||
std::vector<std::uint8_t> data;
|
std::vector<std::uint8_t> data;
|
||||||
auto read{false};
|
auto read{false};
|
||||||
for (int attempt = 0; attempt < 3 && false == read; ++attempt)
|
stop_check const stopping{Stop ? Stop : stop_check{[]() { return false; }}};
|
||||||
read = editor_http_get(url, data, info.error);
|
thread_local http_client client;
|
||||||
|
for (int attempt = 0; attempt < 3 && false == read && false == stopping(); ++attempt)
|
||||||
|
read = client.get(url, data, info.error, stopping);
|
||||||
if (false == read)
|
if (false == read)
|
||||||
return info;
|
return info;
|
||||||
info.error.clear();
|
info.error.clear();
|
||||||
|
|||||||
@@ -28,8 +28,6 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "utilities/Classes.h" // material_handle
|
#include "utilities/Classes.h" // material_handle
|
||||||
#include "rendering/geometrybank.h" // gfx::geometry_handle
|
#include "rendering/geometrybank.h" // gfx::geometry_handle
|
||||||
|
|
||||||
bool editor_http_get(std::string const &Url, std::vector<std::uint8_t> &Out, std::string &Error);
|
|
||||||
|
|
||||||
namespace scene
|
namespace scene
|
||||||
{
|
{
|
||||||
class basic_region;
|
class basic_region;
|
||||||
@@ -183,7 +181,8 @@ class editor_orthophoto
|
|||||||
bool puwg{false};
|
bool puwg{false};
|
||||||
std::string error;
|
std::string error;
|
||||||
};
|
};
|
||||||
static service_info describe(std::string const &Service);
|
// Stop: true to give the reading up
|
||||||
|
static service_info describe(std::string const &Service, std::function<bool()> const &Stop = {});
|
||||||
static std::string cache_directory();
|
static std::string cache_directory();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ add_executable(${UNIT_TESTS_NAME}
|
|||||||
# and the fouling points of the switches, widened in the curves as the gauge is
|
# and the fouling points of the switches, widened in the curves as the gauge is
|
||||||
"${CMAKE_SOURCE_DIR}/editor/editorGauge.cpp"
|
"${CMAKE_SOURCE_DIR}/editor/editorGauge.cpp"
|
||||||
"${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp"
|
"${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp"
|
||||||
|
# and the http client the editor fetches its aids with
|
||||||
|
"${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp"
|
||||||
${UNIT_TEST_CASES})
|
${UNIT_TEST_CASES})
|
||||||
|
|
||||||
# the core library brings the game's build settings along
|
# the core library brings the game's build settings along
|
||||||
|
|||||||
138
tests/unit/test_editor_http.cpp
Normal file
138
tests/unit/test_editor_http.cpp
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
#include "stdafx.h"
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "editor/editorHttp.hpp"
|
||||||
|
|
||||||
|
#include <asio.hpp>
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <format>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// a service on this machine. with no reply it takes the connection and the request and never answers, as geoportal.gov.pl
|
||||||
|
// does now and then
|
||||||
|
class local_server {
|
||||||
|
public:
|
||||||
|
explicit local_server(std::string Reply = {}) :
|
||||||
|
m_reply(std::move(Reply)) {
|
||||||
|
m_acceptor.open(asio::ip::tcp::v4());
|
||||||
|
m_acceptor.bind({asio::ip::address_v4::loopback(), 0});
|
||||||
|
m_acceptor.listen();
|
||||||
|
m_port = m_acceptor.local_endpoint().port();
|
||||||
|
m_thread = std::jthread{[this]() { serve(); }};
|
||||||
|
}
|
||||||
|
~local_server() {
|
||||||
|
m_done = true;
|
||||||
|
// a connection of its own wakes the server waiting for one
|
||||||
|
asio::ip::tcp::socket wake{m_context};
|
||||||
|
asio::error_code ignored;
|
||||||
|
wake.connect({asio::ip::address_v4::loopback(), m_port}, ignored);
|
||||||
|
wake.close(ignored);
|
||||||
|
m_thread.join();
|
||||||
|
}
|
||||||
|
local_server(local_server const &) = delete;
|
||||||
|
local_server &operator=(local_server const &) = delete;
|
||||||
|
|
||||||
|
std::string url() const {
|
||||||
|
return std::format("http://127.0.0.1:{}/wcs?REQUEST=GetCapabilities", m_port);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void serve() {
|
||||||
|
while (false == m_done) {
|
||||||
|
asio::ip::tcp::socket socket{m_context};
|
||||||
|
asio::error_code error;
|
||||||
|
m_acceptor.accept(socket, error);
|
||||||
|
if (error)
|
||||||
|
return;
|
||||||
|
if (false == m_reply.empty()) {
|
||||||
|
answer(socket);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// the request is left without an answer, the connection open, until the server goes
|
||||||
|
while (false == m_done)
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// the reply once the whole request is read, then the connection closed
|
||||||
|
void answer(asio::ip::tcp::socket &Socket) const {
|
||||||
|
asio::error_code error;
|
||||||
|
asio::streambuf request;
|
||||||
|
asio::read_until(Socket, request, "\r\n\r\n", error);
|
||||||
|
if (!error)
|
||||||
|
asio::write(Socket, asio::buffer(m_reply), error);
|
||||||
|
Socket.shutdown(asio::ip::tcp::socket::shutdown_both, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string const m_reply;
|
||||||
|
asio::io_context m_context;
|
||||||
|
asio::ip::tcp::acceptor m_acceptor{m_context};
|
||||||
|
unsigned short m_port{0};
|
||||||
|
std::atomic<bool> m_done{false};
|
||||||
|
std::jthread m_thread;
|
||||||
|
};
|
||||||
|
|
||||||
|
// the requests to the local server would go to a proxy named in the environment, which can't reach it
|
||||||
|
bool proxied() {
|
||||||
|
for (char const *variable : {"http_proxy", "HTTP_PROXY", "all_proxy", "ALL_PROXY"}) {
|
||||||
|
if (char const *value{std::getenv(variable)}; value != nullptr && *value != '\0')
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_CASE("editor http: a request a service doesn't answer is given up as soon as a stop is asked for") {
|
||||||
|
if (false == editor_http::available() || proxied())
|
||||||
|
return; // a build with no http client, or a proxy in the way
|
||||||
|
local_server server;
|
||||||
|
auto const start{std::chrono::steady_clock::now()};
|
||||||
|
auto const elapsed = [start]() { return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count(); };
|
||||||
|
editor_http::client client;
|
||||||
|
std::vector<std::uint8_t> data;
|
||||||
|
std::string error;
|
||||||
|
// the stop comes while the client waits for the answer, long before any of its timeouts
|
||||||
|
auto const got{client.get(server.url(), data, error, [&elapsed]() { return elapsed() > 0.3; })};
|
||||||
|
CHECK_FALSE(got);
|
||||||
|
CHECK_FALSE(error.empty());
|
||||||
|
// the receive timeout is 20 s; the transfer ends within a moment of the stop (libcurl looks at it about once a second)
|
||||||
|
CHECK(elapsed() < 5.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("editor http: the whole body of a response is read") {
|
||||||
|
if (false == editor_http::available() || proxied())
|
||||||
|
return;
|
||||||
|
std::string body(100000, '\0');
|
||||||
|
for (std::size_t i = 0; i < body.size(); ++i)
|
||||||
|
body[i] = static_cast<char>('a' + i % 26);
|
||||||
|
local_server server{std::format("HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", body.size(), body)};
|
||||||
|
editor_http::client client;
|
||||||
|
std::vector<std::uint8_t> data;
|
||||||
|
std::string error;
|
||||||
|
CHECK(client.get(server.url(), data, error, []() { return false; }));
|
||||||
|
CHECK(error.empty());
|
||||||
|
CHECK(std::string(data.begin(), data.end()) == body);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("editor http: the body of an error is kept with the status in the error") {
|
||||||
|
if (false == editor_http::available() || proxied())
|
||||||
|
return;
|
||||||
|
// the terrain model service tells a place outside of its coverage in the body of an error
|
||||||
|
std::string const body{"<ServiceException>outside requested coverage</ServiceException>"};
|
||||||
|
local_server server{std::format("HTTP/1.1 400 Bad Request\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", body.size(), body)};
|
||||||
|
editor_http::client client;
|
||||||
|
std::vector<std::uint8_t> data;
|
||||||
|
std::string error;
|
||||||
|
CHECK_FALSE(client.get(server.url(), data, error, []() { return false; }));
|
||||||
|
CHECK(error == "HTTP 400");
|
||||||
|
CHECK(std::string(data.begin(), data.end()) == body);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user