-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNetCxx.cpp
More file actions
112 lines (79 loc) · 3.09 KB
/
Copy pathNetCxx.cpp
File metadata and controls
112 lines (79 loc) · 3.09 KB
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
//
// Created by William Kamp on 10/25/15.
//
#include "NetCxx.h"
#include <curl/curl.h>
// libcurl with C++ http://curl.haxx.se/libcurl/c/libcurl-tutorial.html#HTTP
// The callbacks CANNOT be non-static class member functions
static size_t WriteMemoryCallback(char *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
auto mem = reinterpret_cast<NetCxx::Response*>(userp);
mem->body.assign(contents);
return realsize;
}
NetCxx::NetCxx() {
curl_global_init(CURL_GLOBAL_SSL);
}
NetCxx::Response NetCxx::get(std::string url) {
CURL *curl_handle;
CURLcode res;
NetCxx::Response chunk;
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* get the response code */
curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &chunk.response_code);
/* check for errors */
if(res != CURLE_OK) {
chunk.error.assign(curl_easy_strerror(res));
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return chunk;
}
NetCxx::Response NetCxx::post(std::string url, std::string body) {
CURL *curl_handle;
CURLcode res;
NetCxx::Response chunk;
/* init the curl session */
curl_handle = curl_easy_init();
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
/* post binary data */
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, body.c_str());
/* set the size of the postfields data */
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, body.size());
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* pass our list of custom made headers */
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl_handle); /* post away! */
/* get the response code */
curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &chunk.response_code);
curl_slist_free_all(headers); /* free the header list */
/* check for errors */
if(res != CURLE_OK) {
chunk.error.assign(curl_easy_strerror(res));
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return chunk;
}
NetCxx::~NetCxx() {
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
}