PRCYCoin  2.0.0.7rc1
P2P Digital Currency
curl_json.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "curl_json.h"
6 #include "util.h"
7 
8 #include <openssl/evp.h>
9 
11 
12 static size_t writer(char *in, size_t size, size_t nmemb, std::string *out)
13 {
14  out->append((char*)in, size * nmemb);
15  return size * nmemb;
16 }
17 
18 void getHttpsJson(std::string url)
19 {
20  {
21  JsonDownload newDownload;
22  downloadedJSON = newDownload;
23  }
24 
25  downloadedJSON.failed = false;
26  downloadedJSON.complete = false;
28  std::string response_string;
29 
30  curl_global_init(CURL_GLOBAL_ALL);
31  CURL *curl;
32  CURLcode res;
33 
34  struct curl_slist *headers=NULL; // init to NULL is important
35 
36  headers = curl_slist_append(headers, "Accept: application/json");
37  headers = curl_slist_append(headers, "Content-Type: application/json");
38  headers = curl_slist_append(headers, "charset: utf-8");
39 
40  curl = curl_easy_init();
41  if(curl) {
42 
43  curl_easy_setopt(curl, CURLOPT_URL, downloadedJSON.URL.c_str());
44  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
45  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
46  curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
47  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
48  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
49  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
50  res = curl_easy_perform(curl);
51 
52  if(CURLE_OK == res) {
53  char *ct;
54  /* ask for the content-type */
55  res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
56  if((CURLE_OK == res) && ct) {
57  downloadedJSON.response = response_string;
58  downloadedJSON.failed = false;
59  downloadedJSON.complete = true;
60  }
61  } else {
63  downloadedJSON.failed = false;
64  downloadedJSON.complete = false;
65  }
66  }
67  /* always cleanup */
68  curl_easy_cleanup(curl);
69  curl_slist_free_all(headers);
70  curl_global_cleanup();
71 
72 }
JsonDownload::response
std::string response
Definition: curl_json.h:24
getHttpsJson
void getHttpsJson(std::string url)
Definition: curl_json.cpp:18
JsonDownload::complete
bool complete
Definition: curl_json.h:26
curl_json.h
url
const char * url
Definition: rpcconsole.cpp:61
downloadedJSON
JsonDownload downloadedJSON
Definition: curl_json.cpp:10
JsonDownload
Definition: curl_json.h:22
L
#define L(x0, x1, x2, x3, x4, x5, x6, x7)
Definition: jh.c:501
JsonDownload::failed
bool failed
Definition: curl_json.h:25
util.h
JsonDownload::URL
std::string URL
Definition: curl_json.h:23