Commit 6f134a64 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron Committed by Generic Bliss account for Control Software
Browse files

Identify HTTP response codes in Commands; report error for codes 4xx and 5xx

parent ed21aab6
......@@ -67,11 +67,15 @@ namespace eigerapi
void handle_result(CURLcode result);
void _status_changed();
bool check_http_response(const char *ptr, size_t size);
static void *_callback_thread_runFunc(void *data);
CURL* m_handle;
Status m_status;
std::string m_error_code;
int m_http_code;
std::string m_http_msg;
// Synchro
mutable pthread_mutex_t m_lock;
mutable pthread_cond_t m_cond;
......
......@@ -28,6 +28,8 @@
#include "eigerapi/EigerDefines.h"
#include "AutoMutex.h"
#include <regex>
using namespace eigerapi;
// Constant
......@@ -50,6 +52,7 @@ struct CURL_INIT
CurlLoop::FutureRequest::FutureRequest(const std::string& url) :
m_status(IDLE),
m_http_code(0),
m_cbk(NULL),
m_url(url)
{
......@@ -78,7 +81,7 @@ CurlLoop::FutureRequest::~FutureRequest()
inline void CurlLoop::FutureRequest::handle_result(CURLcode result)
{
Lock lock(&m_lock);
if(m_status != FutureRequest::CANCEL)
if(m_status == FutureRequest::RUNNING)
{
switch(result)
{
......@@ -97,6 +100,24 @@ inline void CurlLoop::FutureRequest::handle_result(CURLcode result)
_request_finished();
}
bool CurlLoop::FutureRequest::check_http_response(const char *ptr, size_t size)
{
static const std::regex re("([1-5][0-9]{2}) (.+)");
std::cmatch m;
if(!std::regex_match(ptr, ptr + size, m, re))
return false;
Lock lock(&m_lock);
m_http_code = std::stoi(m[1].str());
m_http_msg = m[2].str();
std::cout << "HTTP [" << m_url << "]: " << m[0].str() << std::endl;
if((m_status == FutureRequest::RUNNING) && (m_http_code >= 400)) {
m_status = FutureRequest::ERROR;
m_error_code = m_http_msg;
}
return true;
}
void CurlLoop::FutureRequest::wait(double timeout,bool lock_flag) const
{
Lock lock(&m_lock,lock_flag);
......
......@@ -405,7 +405,10 @@ void Requests::Command::_fill_request()
size_t Requests::Command::_write_callback(char *ptr,size_t size,
size_t nmemb,Requests::Command *cmd)
{
int size_to_copy = std::min(size * nmemb,sizeof(m_data));
size_t len = size * nmemb;
if (cmd->check_http_response(ptr, len))
return len;
int size_to_copy = std::min(len, sizeof(m_data) - 1);
memcpy(cmd->m_data,ptr,size_to_copy);
cmd->m_data[size_to_copy] = '\0';
return size_to_copy;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment