tv_rename/windows/network.cpp

86 lines
2.8 KiB
C++
Raw Normal View History

2020-01-15 15:30:30 +00:00
#include "../network.hpp"
#include <iostream>
#include <memory>
const wchar_t *acceptTypes[] = { L"text/*", L"application/json", nullptr };
Request::Request( const string &server ) {
// Start connection
_hInternet = InternetOpen( L"WinInet/1.0", INTERNET_OPEN_TYPE_PRECONFIG,
nullptr, nullptr, 0 );
if ( !_hInternet )
std::wcerr << "ERROR InternetOpen: " << GetLastError() << std::endl;
// connect to server
_hConnect = InternetConnect( _hInternet, server.c_str(),
INTERNET_DEFAULT_HTTPS_PORT, nullptr, nullptr,
INTERNET_SERVICE_HTTP, 0, 0 );
if ( !_hConnect )
std::wcerr << "ERROR InternetConnect: " << GetLastError() << std::endl;
}
Request::~Request() {
if ( _hConnect )
InternetCloseHandle( _hConnect );
if ( _hInternet )
InternetCloseHandle( _hInternet );
}
std::string sendRequest( HINTERNET hRequest, const std::wstring &headers,
const std::string &optional ) {
std::string response{};
response.reserve( 10000 );
// have to do a copy because c_str returns const
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, optional.length() );
if ( !requestSent )
std::wcerr << "ERROR HttpSendRequest: " << GetLastError() << std::endl;
char dataReceived[4096];
unsigned long numberOfBytesRead = 0;
// read while InternetReadFile returns true and reads more than 0 bytes
while (
InternetReadFile( hRequest, dataReceived, 4096, &numberOfBytesRead ) &&
numberOfBytesRead ) {
response.append( dataReceived, numberOfBytesRead );
}
return response;
}
std::string Request::get( const std::wstring &url ) {
HINTERNET hRequest =
HttpOpenRequest( _hConnect, L"GET", url.c_str(), nullptr, nullptr,
acceptTypes, INTERNET_FLAG_SECURE, 0 );
if ( !hRequest )
std::wcerr << "ERROR HttpOpenRequest: " << GetLastError() << std::endl;
return sendRequest( hRequest, _headers, "" );
}
std::string Request::post( const std::wstring &url, const std::string &data ) {
HINTERNET hRequest =
HttpOpenRequest( _hConnect, L"POST", url.c_str(), nullptr, nullptr,
acceptTypes, INTERNET_FLAG_SECURE, 0 );
if ( !hRequest )
std::wcerr << "ERROR HttpOpenRequest: " << GetLastError() << std::endl;
return sendRequest( hRequest, _headers, data );
}
void Request::addHeader( const std::wstring &header ) {
_headers += header + L"\r\n";
}
void Request::clearHeader() {
_headers = L"";
}
bool Request::initSuccessful() {
return _hInternet != nullptr && _hConnect != nullptr;
}