1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*
* JsonRpc-Cpp - JSON-RPC implementation.
* Copyright (C) 2008-2011 Sebastien Vincent <sebastien.vincent@cppextrem.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file jsonrpc_httpclient.cpp
* \brief JSON-RPC HTTP client.
* \author Brian Panneton
*/
#include <sstream>
#include "jsonrpc_httpclient.h"
#include "netstring.h"
namespace Json
{
namespace Rpc
{
HttpClient::HttpClient()
{
this->Initialize();
}
HttpClient::HttpClient(const std::string& address)
{
this->Initialize();
this->ChangeAddress(address);
}
HttpClient::HttpClient(const std::string& address, uint16_t port)
{
this->Initialize();
this->ChangeAddress(address);
this->ChangePort(port);
}
void HttpClient::Initialize()
{
/* Init all necessary curl stuff (in windows it will do winsock) */
curl_global_init(CURL_GLOBAL_ALL);
m_curlHandle = curl_easy_init();
/* Set up curl to catch the servers response */
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION,
HttpClient::StaticRecv);
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this);
/* Set up JSON specific HTTP Headers */
size_t error = 0;
m_headers = NULL;
m_headers = curl_slist_append(m_headers,
"Content-Type: application/json-rpc");
m_headers = curl_slist_append(m_headers,"Accept: application/json-rpc");
if((error = (size_t)curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER,
m_headers))!=0)
{
std::cerr << curl_easy_strerror((CURLcode)error);
// TODO maybe throw exception here
return;
}
/* Force curl to use POST */
if((error = (size_t)curl_easy_setopt(m_curlHandle, CURLOPT_POST,
1)) != 0)
{
std::cerr << curl_easy_strerror((CURLcode)error);
// TODO maybe throw exception here
return;
}
// test if mutex is valid
if(m_mutex.Lock())
{
std::cerr << "Can't create mutex for receive list.\n";
// TODO maybe throw exception here
return;
}
m_mutex.Unlock();
this->SetRecvListSize(-1);
this->SetAddress("");
this->SetPort(0);
this->SetEncapsulatedFormat(Json::Rpc::RAW);
}
HttpClient::~HttpClient()
{
/* Clean up after we are done */
curl_slist_free_all(m_headers);
curl_global_cleanup();
}
void HttpClient::ChangeAddress(const std::string& address)
{
this->SetAddress(address);
}
void HttpClient::ChangePort(uint16_t port)
{
this->SetPort(port);
size_t error = 0;
if((error = (size_t)curl_easy_setopt(m_curlHandle, CURLOPT_PORT,
(long)port)) != 0)
{
std::cerr << curl_easy_strerror((CURLcode)error);
// TODO maybe throw exception here
return;
}
}
ssize_t HttpClient::Send(const std::string& data)
{
if(this->GetAddress() == "")
{
return -1;
}
std::string rep = data;
/* encoding if any */
if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING)
{
rep = netstring::encode(rep);
}
#ifdef DEBUG
curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE);
#endif
size_t error = 0;
/* Tell curl which address to use */
if((error = (size_t)curl_easy_setopt(m_curlHandle, CURLOPT_URL,
this->GetAddress().c_str())) != 0)
{
std::cerr << curl_easy_strerror((CURLcode)error);
return error;
}
/* Tell curl what data to send */
if((error = (size_t)curl_easy_setopt(m_curlHandle, CURLOPT_POSTFIELDS,
data.c_str())) != 0)
{
std::cerr << curl_easy_strerror((CURLcode)error);
return error;
}
if((error = (size_t)curl_easy_setopt(m_curlHandle,
CURLOPT_POSTFIELDSIZE,
data.length())) != 0)
{
std::cerr << curl_easy_strerror((CURLcode)error);
return error;
}
/* Send the data */
if((error = (size_t)curl_easy_perform(m_curlHandle)) != 0)
{
std::cerr << curl_easy_strerror((CURLcode)error);
return error;
}
return 0;
}
size_t HttpClient::StaticRecv(void *buffer, size_t size, size_t nmemb,
void*f)
{
/* Get the data without the null term char */
std::string data;
data.assign((char*)buffer, (size_t)0, size*nmemb);
/* Send our data to the current class instance */
static_cast<HttpClient*>(f)->StoreRecv(data);
/* return the size of data received */
return size*nmemb;
}
void HttpClient::StoreRecv(const std::string &data)
{
m_mutex.Lock();
this->m_lastReceivedList.push_back(data);
if(m_lastReceivedListSize == -1)
{
m_mutex.Unlock();
return;
}
if(m_lastReceivedList.size() > (unsigned int)m_lastReceivedListSize)
{
m_lastReceivedList.pop_front();
}
m_mutex.Unlock();
}
ssize_t HttpClient::Recv(std::string &data)
{
if(m_lastReceivedList.empty())
{
return 0;
}
m_mutex.Lock();
data = this->m_lastReceivedList.front();
ssize_t length = this->m_lastReceivedList.front().length();
this->m_lastReceivedList.pop_front();
m_mutex.Unlock();
return length;
}
ssize_t HttpClient::WaitRecv(std::string &data)
{
while(data.size() == 0)
{
this->Recv(data);
}
return data.size();
}
ssize_t HttpClient::WaitRecv(std::string &data, unsigned int timeout)
{
time_t cur_time = time(NULL);
while(data.size() == 0 &&
static_cast<time_t>(cur_time + timeout) > time(NULL))
{
this->Recv(data);
}
return data.size();
}
void HttpClient::SetRecvListSize(int size)
{
if(size < -1)
{
size = -1;
}
this->m_lastReceivedListSize = size;
}
int HttpClient::GetRecvListSize()
{
return this->m_lastReceivedListSize;
}
/* We don't need these functions */
bool HttpClient::Connect()
{
return false;
}
void HttpClient::Close()
{
}
int HttpClient::GetSocket() const
{
return -1;
}
} /* namespace Rpc */
} /* namespace Json */
|