-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.cpp
161 lines (125 loc) · 4.32 KB
/
Request.cpp
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
/*
* Copyright (C) 2016 Gianluca Iselli <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Request.h"
#include "CurlGlobal.h"
#include "Log.h"
#include "Exceptions.h"
#include "Utilities.h"
Request::Request(std::string url)
: m_Url(url) {
LogD(0, "Request(%s)\n", url.c_str());
this->m_Timeout = 5;
this->m_MaxRedirect = 1;
}
Request::~Request() {
LogD(0, "~Request()\n");
}
void Request::SetTimeout(int sec) {
LogD(0, "SetTimeout(%d)\n", sec);
this->m_Timeout = sec;
}
void Request::SetMaxRedirect(int num) {
LogD(0, "SetMaxRedirect(%d)\n", num);
this->m_MaxRedirect = num;
}
size_t append_callback(void *contents, size_t size, size_t nmemb, void *p_answer) {
LogD(0, "append_callback(*contents*, %d, %d, *p_answer*)\n", size, nmemb);
size_t buffsize = size * nmemb;
struct curl_answer_s *p = (struct curl_answer_s *) p_answer;
char *tmp = (char *) realloc(p->payload, p->size + buffsize + 1);
if (tmp == nullptr)
DieWithErrno(1);
p->payload = tmp;
memcpy(&(p->payload[p->size]), contents, buffsize);
p->size += buffsize;
p->payload[p->size] = '\0';
return buffsize;
}
CURLcode Request::PerformPost(const char *data, struct curl_slist *headers, struct curl_answer_s *p_answer) {
LogD(0, "PerformPost(*data*, *headers*, *p_answer*)\n");
CURL *curl_handle;
CURLcode ret;
// init the curl session
curl_handle = curl_easy_init();
if (curl_handle == nullptr)
DieWithError(1, "Failed to create curl handle");
// specify URL to get
curl_easy_setopt(curl_handle, CURLOPT_URL, this->m_Url.c_str());
// set post type request
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
// set accept and content type
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
// append json string
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data);
// send all data to this function
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, append_callback);
// pass our 'answer' struct to the callback function
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) p_answer);
// provide a user-agent field
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
// set timeout
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, this->m_Timeout);
// enable location redirects
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
// set maximum allowed redirects
curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, this->m_MaxRedirect);
// get it!
ret = curl_easy_perform(curl_handle);
// cleanup curl stuff
curl_easy_cleanup(curl_handle);
return ret;
}
CURLcode Request::PerformJSONPost(JSON& obj, struct curl_answer_s *p_answer) {
LogD(0, "PerformJSONPost(*obj*, *p_answer*)\n");
CURLcode ret;
struct curl_slist *headers = nullptr;
// set content type
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
// perform post
ret = this->PerformPost(obj.dump(-1).c_str(), headers, p_answer);
// free headers
curl_slist_free_all(headers);
return ret;
}
JSON Request::PostJSON(JSON& data) {
LogD(0, "PostJSON(*data*)\n");
CURLcode ret;
struct curl_answer_s answer;
// initialize answer struct
answer.payload = nullptr;
answer.size = 0;
ret = this->PerformJSONPost(data, &answer);
// check for errors
if (ret != CURLE_OK) {
LogE("curl_easy_perform() failed: %s\n", curl_easy_strerror(ret));
if (answer.payload != nullptr)
FREE(answer.payload);
throw MakeException(CURLErrorException, curl_easy_strerror(ret));
} else {
try {
JSON res = JSON::parse(answer.payload);
FREE(answer.payload);
return res;
} catch (std::invalid_argument &e) {
throw MakeException(JsonParseException, e.what());
}
}
}