Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
xlz-jbleclere committed Sep 4, 2020
1 parent 1ba17ab commit 875c390
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 118 deletions.
105 changes: 60 additions & 45 deletions internal_inc/ws_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CurlEasyPost {
const uint32_t cConnectionTimeoutMS = 30000; // Timeout default value in milliseconds

CURL *curl = NULL;
std::string mUrl;
struct curl_slist *mHeaders_p = NULL;
struct curl_slist *mHostResolveList = NULL;
std::list<std::string> data; // keep data until request performed
Expand Down Expand Up @@ -81,90 +82,104 @@ class CurlEasyPost {
|| resp_code == 560 // Accelize License generation temporary issue
;
}
static DRM_ErrorCode httpCode2DrmCode( const uint32_t http_resp_code ) {
if ( http_resp_code == 200 )
return DRM_OK;
if ( CurlEasyPost::is_error_retryable( http_resp_code ) )
return DRM_WSMayRetry;
if ( ( http_resp_code >= 400 ) && ( http_resp_code < 500 ) )
return DRM_WSReqError;
return DRM_WSError;
}

CurlEasyPost();
~CurlEasyPost();

long perform( std::string* resp, std::chrono::steady_clock::time_point& deadline );
long perform( std::string* resp, std::chrono::milliseconds& timeout );
long perform_put( std::string* resp, std::string url, const uint32_t& timeout_ms );
double getTotalTime();

void setVerbosity( const uint32_t verbosity );

void setHostResolves( const Json::Value& host_json );

void setURL( std::string url );

template<class T>
void appendHeader( T&& header ) {
data.push_back( std::forward<T>(header) );
Debug2( "Add {} to CURL header", std::forward<T>(header) );
mHeaders_p = curl_slist_append( mHeaders_p, data.back().c_str() );
}

template<class T>
void setPostFields( T&& postfields ) {
data.push_back( std::forward<T>(postfields) );
curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, data.back().size() );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, data.back().c_str() );
}

void setConnectionTimeoutMS( const uint32_t timeoutMS ) { mConnectionTimeoutMS = timeoutMS; }
uint32_t getConnectionTimeoutMS() const { return mConnectionTimeoutMS; }

uint32_t perform( std::string* resp, std::chrono::steady_clock::time_point& deadline );
uint32_t perform( std::string* resp, int32_t timeout );
std::string perform_put( std::string url, const uint32_t& timeout_ms );

template<class T>
T perform( std::string url, const uint32_t& timeout_ms ) {
T resp;
T response;
uint32_t resp_code;

// Configure and execute CURL command
curl_easy_setopt( curl, CURLOPT_URL, url.c_str() );
if ( mHeaders_p ) {
curl_easy_setopt( curl, CURLOPT_HTTPHEADER, mHeaders_p );
}
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, &CurlEasyPost::write_callback );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &resp );
curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, mErrBuff.data() );
curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1L );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &response );
curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT_MS, mConnectionTimeoutMS );
if ( timeout_ms <= 0 )
Throw( DRM_WSTimedOut, "Did not perform HTTP request to Accelize webservice because deadline is reached." );
curl_easy_setopt( curl, CURLOPT_TIMEOUT_MS, timeout_ms );
CURLcode res = curl_easy_perform( curl );

// Analyze HTTP answer
long resp_code;
curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &resp_code );
Debug( "Received code {} from {} in {} ms", resp_code, url, getTotalTime() * 1000 );

// Analyze libcurl response
if ( res != CURLE_OK ) {
if ( res == CURLE_COULDNT_RESOLVE_PROXY
|| res == CURLE_COULDNT_RESOLVE_HOST
|| res == CURLE_COULDNT_CONNECT
|| res == CURLE_OPERATION_TIMEDOUT ) {
Throw( DRM_WSMayRetry, "Failed performing HTTP request to Accelize webservice ({}) : {}",
curl_easy_strerror( res ), mErrBuff.data() );
curl_easy_strerror( res ), mErrBuff.data() ); //LCOV_EXCL_LINE
} else {
Throw( DRM_ExternFail, "Failed performing HTTP request to Accelize webservice ({}) : {}",
curl_easy_strerror( res ), mErrBuff.data() );
curl_easy_strerror( res ), mErrBuff.data() ); //LCOV_EXCL_LINE
}
}
return resp;
}

double getTotalTime();

void setVerbosity( const uint32_t verbosity );

void setHostResolves( const Json::Value& host_json );

template<class T>
void setURL(T&& url) {
data.push_back( std::forward<T>(url) );
curl_easy_setopt( curl, CURLOPT_URL, data.back().c_str() );
}

template<class T>
void appendHeader( T&& header ) {
data.push_back( std::forward<T>(header) );
Debug2( "Add {} to CURL header", std::forward<T>(header) );
mHeaders_p = curl_slist_append( mHeaders_p, data.back().c_str() );
}
curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &resp_code );
Debug( "Received code {} from {} in {} ms", resp_code, url, getTotalTime() * 1000 );

template<class T>
void setPostFields( T&& postfields ) {
data.push_back( std::forward<T>(postfields) );
curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, data.back().size() );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, data.back().c_str() );
// Analyze HTTP response
if ( resp_code != 200 ) {
// An error occurred
DRM_ErrorCode drm_error;
if ( CurlEasyPost::is_error_retryable( resp_code ) )
drm_error = DRM_WSMayRetry;
else if ( ( resp_code >= 400 ) && ( resp_code < 500 ) )
drm_error = DRM_WSReqError;
else
drm_error = DRM_WSError;
Throw( drm_error, "OAuth2 Web Service error {}: {}", resp_code, response );
}
return response;
}

void setConnectionTimeoutMS( const uint32_t timeoutMS ) { mConnectionTimeoutMS = timeoutMS; }
uint32_t getConnectionTimeoutMS() const { return mConnectionTimeoutMS; }

protected:

static size_t write_callback( void *contents, size_t size, size_t nmemb, std::string *userp ) {
size_t realsize = size * nmemb;
try {
userp->append( (const char*)contents, realsize );
} catch( const std::bad_alloc& e ) {
Throw( DRM_ExternFail, "Curl write callback exception: {}", e.what() );
Throw( DRM_ExternFail, "Curl write callback exception: {}", e.what() ); //LCOV_EXCL_LINE
}
return realsize;
}
Expand Down
18 changes: 6 additions & 12 deletions source/csp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,27 @@ Aws::Aws():CspBase( "Aws", 100 ) {}

Json::Value Aws::get_metadata() {
Json::Value metadata = Json::nullValue;
std::string token, resp;
std::string token;
uint32_t timeout_ms = mHTTPRequest.getConnectionTimeoutMS();

// Using IMDSv2 method

// Get token
CurlEasyPost tokenReq;
tokenReq.setURL( "http://169.254.169.254/latest/api/token" );
tokenReq.setConnectionTimeoutMS( timeout_ms );
tokenReq.setVerbosity( mVerbosity );
tokenReq.appendHeader( "X-aws-ec2-metadata-token-ttl-seconds: 21600" );
tokenReq.perform_put( &token, "http://169.254.169.254/latest/api/token", timeout_ms );
mHTTPRequest.appendHeader( fmt::format("X-aws-ec2-metadata-token: {}", token) );
token = tokenReq.perform_put( "http://169.254.169.254/latest/api/token", timeout_ms );

// Collect Alibaba information
mHTTPRequest.appendHeader( fmt::format("X-aws-ec2-metadata-token: {}", token) );
std::string base_url("http://169.254.169.254/latest");
metadata["instance_id"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/meta-data/instance-id", base_url ), timeout_ms );
metadata["instance_type"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/meta-data/instance-type", base_url ), timeout_ms );
metadata["ami_id"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/meta-data/ami-id", base_url ), timeout_ms );
std::string inst_doc = mHTTPRequest.perform<std::string>( fmt::format( "{}/dynamic/instance-identity/document", base_url ), timeout_ms );
std::cout << "inst_doc=" << inst_doc << std::endl;
std::string region;
metadata["region"] = "region";

std::cout << "metadata=" << metadata.toStyledString() << std::endl;

std::string doc_string = mHTTPRequest.perform<std::string>( fmt::format( "{}/dynamic/instance-identity/document", base_url ), timeout_ms );
Json::Value doc_json = parseJsonString( doc_string );
metadata["region"] = doc_json["region"];
return metadata;
}

Expand All @@ -108,7 +103,6 @@ Json::Value Alibaba::get_metadata() {
metadata["instance_type"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/instance/instance-type", base_url ), timeout_ms );
metadata["ami_id"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/image-id", base_url ), timeout_ms );
metadata["region"] = mHTTPRequest.perform<std::string>( fmt::format( "{}/region-id", base_url ), timeout_ms );
std::cout << "metadata=" << metadata.toStyledString() << std::endl;
return metadata;
}

Expand Down
7 changes: 4 additions & 3 deletions source/drm_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,6 @@ class DRM_LOCAL DrmManager::Impl {
mBypassFrequencyDetection ).asBool();
}

// If possible extract host and card information
getHostAndCardInfo();

} catch( const Exception &e ) {
if ( e.getErrCode() != DRM_BadFormat )
throw;
Expand Down Expand Up @@ -903,6 +900,10 @@ class DRM_LOCAL DrmManager::Impl {
} else {
mWsClient.reset( new DrmWSClient( mConfFilePath, mCredFilePath ) );
}

// Collect host and card information when possible
getHostAndCardInfo();

}

// Get DRM HDK version
Expand Down
2 changes: 1 addition & 1 deletion source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Json::Value parseJsonString( const std::string &json_string ) {
if ( json_node.empty() || json_node.isNull() )
Throw( DRM_BadArg, "JSON string is empty" );

Debug( "Extracted JSON Object: {}", json_node.toStyledString() );
Debug2( "Extracted JSON Object: {}", json_node.toStyledString() );

return json_node;
}
Expand Down
Loading

0 comments on commit 875c390

Please sign in to comment.