This fixes the crash and the unrequested printf of the downloaded json
This commit is contained in:
parent
666d25c4d1
commit
fb49c84899
5 changed files with 109 additions and 41 deletions
|
@ -18,10 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
|
||||
#include "main.hpp"
|
||||
#include "WordReference.hpp"
|
||||
#include "HttpReader.hpp"
|
||||
#include "libjson.h"
|
||||
#include <sstream>
|
||||
#include <boost/algorithm/string/trim_all.hpp>
|
||||
#include <curl/curl.h>
|
||||
#include "libjson.h"
|
||||
|
||||
namespace {
|
||||
const char* ApiVersion = "0.8";
|
||||
|
@ -54,43 +54,6 @@ namespace {
|
|||
return parWord;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
void DownloadHttpPage (const std::string& parAddress, std::string& parDest) {
|
||||
struct CurlAuto {
|
||||
CurlAuto ( void ) : curl(curl_easy_init()) {}
|
||||
~CurlAuto ( void ) { if (curl) curl_easy_cleanup(curl); }
|
||||
CURL* const curl;
|
||||
};
|
||||
|
||||
CurlAuto curl;
|
||||
if (curl.curl) {
|
||||
curl_easy_setopt(curl.curl, CURLOPT_URL, parAddress.c_str());
|
||||
//Tell curl to follow redirection when needed
|
||||
curl_easy_setopt(curl.curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
|
||||
const CURLcode res = curl_easy_perform(curl.curl);
|
||||
if (CURLE_OK == res) {
|
||||
char* reading;
|
||||
const CURLcode res = curl_easy_getinfo(curl.curl, CURLINFO_CONTENT_TYPE, &reading);
|
||||
if (CURLE_OK != res or not reading) {
|
||||
std::ostringstream oss;
|
||||
oss << "Error while requesting http page: " << curl_easy_strerror(res);
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
parDest = reading;
|
||||
}
|
||||
else {
|
||||
std::ostringstream oss;
|
||||
oss << "Error during the CURL request: " << curl_easy_strerror(res);
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Error initializing CURL");
|
||||
}
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
JSONNode QueryJSon (const std::string& parFrom, const std::string& parTo, const std::string& parKey, const std::string& parWord) {
|
||||
|
@ -98,8 +61,8 @@ namespace {
|
|||
oss << "http://api.wordreference.com/" << ApiVersion << "/"
|
||||
<< parKey << "/json/" << parFrom << parTo << "/" << GetCleanWord(parWord);
|
||||
|
||||
std::string jsonResponse;
|
||||
DownloadHttpPage(oss.str(), jsonResponse);
|
||||
HttpReader http;
|
||||
std::string jsonResponse(http.GetPage(oss.str()));
|
||||
return libjson::parse(libjson::to_json_string(jsonResponse));
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue