Skip to content

Commit

Permalink
Eliminate CURL dependency on windows and refactor http code
Browse files Browse the repository at this point in the history
  • Loading branch information
navv1234 committed May 2, 2020
1 parent 527dd05 commit f2b1229
Show file tree
Hide file tree
Showing 20 changed files with 1,186 additions and 334 deletions.
2 changes: 1 addition & 1 deletion cli/cli.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include(../simulationcraft.pri)
TEMPLATE = app
TARGET = simc
CONFIG -= qt app_bundle link_prl
LIBS += -L../lib -lsimcengine
LIBS += -L../lib
QT -= core gui

# Linux puts binaries to a different place (see simulationcraft.pri)
Expand Down
17 changes: 11 additions & 6 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ endif()

# Networking libs
if(NOT SC_NO_NETWORKING)
find_package(CURL REQUIRED)
if (CURL_FOUND)
target_link_libraries(engine ${CURL_LIBRARIES})
target_include_directories(engine PUBLIC ${CURL_INCLUDE_DIRS})
else(CURL_FOUND)
message(FATAL_ERROR "libcurl not found")
if(NOT WIN32)
find_package(CURL REQUIRED)
if (CURL_FOUND)
target_link_libraries(engine ${CURL_LIBRARIES})
target_include_directories(engine PUBLIC ${CURL_INCLUDE_DIRS})
else(CURL_FOUND)
message(FATAL_ERROR "libcurl not found")
endif()
else()
target_link_libraries(engine crypt32)
target_link_libraries(engine wininet)
endif()
endif()

Expand Down
3 changes: 3 additions & 0 deletions engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ endif
ifneq (${DEBUG},)
CPP_FLAGS += -g
endif
ifneq (${CURL_DEBUG},)
CPP_FLAGS += -DCURL_DEBUG
endif
ifneq (${EVENT_QUEUE_DEBUG},)
CPP_FLAGS += -DEVENT_QUEUE_DEBUG
endif
Expand Down
10 changes: 10 additions & 0 deletions engine/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@
# define SC_FALLTHROUGH
#endif

// ==========================================================================
// Networking library
// ==========================================================================

#if !defined( SC_NO_NETWORKING ) && !defined( SC_WINDOWS )
# define SC_USE_CURL
#elif !defined( SC_NO_NETWORKING ) && defined( SC_WINDOWS )
# define SC_USE_WININET
#endif

// ==========================================================================
// Floating Point
// ==========================================================================
Expand Down
54 changes: 12 additions & 42 deletions engine/interfaces/sc_bcp_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#include "util/rapidjson/stringbuffer.h"
#include "util/rapidjson/prettywriter.h"

#ifndef SC_NO_NETWORKING
#include <curl/curl.h>
#endif

#include "../util/utf8-h/utf8.h"

// ==========================================================================
Expand Down Expand Up @@ -67,13 +63,6 @@ mutex_t token_mutex;

#ifndef SC_NO_NETWORKING

size_t data_cb( void* contents, size_t size, size_t nmemb, void* usr )
{
std::string* obj = reinterpret_cast<std::string*>( usr );
obj->append( reinterpret_cast<const char*>( contents ), size * nmemb );
return size * nmemb;
}

std::vector<std::string> token_paths()
{
std::vector<std::string> paths;
Expand Down Expand Up @@ -121,8 +110,6 @@ bool authorize( sim_t* sim, const std::string& region )
return true;
}

std::string ua_str = "Simulationcraft/" + std::string( SC_VERSION );

std::string oauth_endpoint;
if ( util::str_compare_ci( region, "eu" ) || util::str_compare_ci( region, "us" ) )
{
Expand All @@ -137,56 +124,39 @@ bool authorize( sim_t* sim, const std::string& region )
oauth_endpoint = CHINA_OAUTH_ENDPOINT_URI;
}

auto handle = curl_easy_init();
std::string buffer;
char error_buffer[ CURL_ERROR_SIZE ];
error_buffer[ 0 ] = '\0';

curl_easy_setopt( handle, CURLOPT_URL, oauth_endpoint.c_str() );
//curl_easy_setopt( handle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt( handle, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt( handle, CURLOPT_POSTFIELDS, "grant_type=client_credentials" );
curl_easy_setopt( handle, CURLOPT_USERPWD, sim->apikey.c_str() );
curl_easy_setopt( handle, CURLOPT_TIMEOUT, 15L );
curl_easy_setopt( handle, CURLOPT_FOLLOWLOCATION, 1L );
curl_easy_setopt( handle, CURLOPT_MAXREDIRS, 5L );
curl_easy_setopt( handle, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt( handle, CURLOPT_USERAGENT, ua_str.c_str() );
curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, data_cb );
curl_easy_setopt( handle, CURLOPT_WRITEDATA, reinterpret_cast<void*>( &buffer ) );
curl_easy_setopt( handle, CURLOPT_ERRORBUFFER, error_buffer );

auto res = curl_easy_perform( handle );
if ( res != CURLE_OK )
{
std::cerr << "Unable to fetch bearer token from " << oauth_endpoint << ", " << error_buffer << std::endl;
curl_easy_cleanup( handle );
auto pool = http::pool();
auto handle = pool->handle( oauth_endpoint );

handle->set_basic_auth( sim->apikey.c_str() );
auto res = handle->post( oauth_endpoint,
"grant_type=client_credentials", "application/x-www-form-urlencoded" );

if ( !res || handle->response_code() != 200 )
{
std::cerr << "Unable to fetch bearer token from " <<
oauth_endpoint << ", response=" << handle->response_code() << ", error=" << handle->error() << ", result=" << handle->result() << std::endl;
authorization_failed = true;
return false;
}

rapidjson::Document response;
response.Parse< 0 >( buffer );
response.Parse< 0 >( handle->result() );
if ( response.HasParseError() )
{
std::cerr << "Unable to parse response message from " << oauth_endpoint << std::endl;
curl_easy_cleanup( handle );
authorization_failed = true;
return false;
}

if ( !response.HasMember( "access_token" ) )
{
std::cerr << "Malformed JSON object from " << oauth_endpoint << std::endl;
curl_easy_cleanup( handle );
authorization_failed = true;
return false;
}

token = response[ "access_token" ].GetString();

curl_easy_cleanup( handle );

return true;
}
#else
Expand Down
Loading

0 comments on commit f2b1229

Please sign in to comment.