From a9802b4d47a56d7d9e9b2a80e4201b0e7ca0da57 Mon Sep 17 00:00:00 2001 From: zvon Date: Fri, 17 Jan 2020 14:01:10 +0100 Subject: [PATCH] Network: add lastResponseCode --- network.hpp | 4 +++- unix/network.cpp | 6 ++++++ windows/network.cpp | 23 ++++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/network.hpp b/network.hpp index b33bebf..8d9b0f9 100644 --- a/network.hpp +++ b/network.hpp @@ -3,8 +3,8 @@ #ifdef _WIN32 -#include #include +#include #include using string = std::wstring; @@ -28,12 +28,14 @@ public: void clearHeader(); bool initSuccessful(); void setServer( const string &server ); + int lastResponseCode(); private: #ifdef _WIN32 HINTERNET _hInternet = nullptr; HINTERNET _hConnect = nullptr; std::wstring _headers = L""; + int status_code{}; #else CURL *_curl_handle = nullptr; struct curl_slist *_chunk = nullptr; diff --git a/unix/network.cpp b/unix/network.cpp index 2019873..505ba0c 100644 --- a/unix/network.cpp +++ b/unix/network.cpp @@ -83,3 +83,9 @@ bool Request::initSuccessful() { void Request::setServer( const string &server ) { _server = server; } + +int Request::lastResponseCode() { + long code{}; + curl_easy_getinfo( _curl_handle, CURLINFO_RESPONSE_CODE, &code ); + return code; +} diff --git a/windows/network.cpp b/windows/network.cpp index 03abfc1..0c1bc41 100644 --- a/windows/network.cpp +++ b/windows/network.cpp @@ -28,11 +28,15 @@ std::string sendRequest( HINTERNET hRequest, const std::wstring &headers, std::unique_ptr< char > opt( new char[optional.length()] ); memcpy( opt.get(), optional.c_str(), optional.length() ); - auto requestSent = HttpSendRequest( - hRequest, headers.c_str(), headers.length(), opt.get(), optional.length() ); + auto requestSent = + HttpSendRequest( hRequest, headers.c_str(), headers.length(), opt.get(), + optional.length() ); - if ( !requestSent ) + if ( !requestSent ) { + status_code = -1; std::wcerr << "ERROR HttpSendRequest: " << GetLastError() << std::endl; + return ""; + } char dataReceived[4096]; unsigned long numberOfBytesRead = 0; @@ -43,6 +47,13 @@ std::string sendRequest( HINTERNET hRequest, const std::wstring &headers, response.append( dataReceived, numberOfBytesRead ); } + wchar_t responseText[256]; + DWORD responseTextSize = sizeof( responseText ); + + HttpQueryInfo( hRequest, HTTP_QUERY_STATUS_CODE, &responseText, + &responseTextSize, NULL ); + status_code = std::stoi( responseText ); + return response; } @@ -86,3 +97,9 @@ void Request::setServer( const string &server ) { if ( !_hConnect ) std::wcerr << "ERROR InternetConnect: " << GetLastError() << std::endl; } + +int Request::lastResponseCode() { + long code{}; + curl_easy_getinfo( _curl_handle, CURLINFO_RESPONSE_CODE, &code ); + return code; +}