-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.cpp
More file actions
162 lines (130 loc) · 3.97 KB
/
Copy pathhttp.cpp
File metadata and controls
162 lines (130 loc) · 3.97 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
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
#include "http.hpp"
#include <string.h>
HTTPClient::HTTPClient()
{
curl = curl_easy_init();
curl_headers = NULL;
method = "GET";
}
HTTPClient::~HTTPClient()
{
if (curl_headers)
{
curl_slist_free_all(curl_headers);
}
if (curl)
{
curl_easy_cleanup(curl);
}
}
HTTPClient& HTTPClient::setUrl(const std::string& url)
{
this->url = url;
return *this;
}
HTTPClient& HTTPClient::setMethod(const std::string& method)
{
this->method = method;
return *this;
}
HTTPClient& HTTPClient::setHeader(const std::string& key, const std::string& value)
{
headers[key] = value;
std::string header = key + ": " + value;
curl_headers = curl_slist_append(curl_headers, header.c_str());
return *this;
}
HTTPClient& HTTPClient::setReceiveCallback(const std::function<bool(const void* data, size_t size, size_t& actualSize)>& onReceive)
{
this->onReceive = onReceive;
return *this;
}
HTTPClient& HTTPClient::setSendCallback(const std::function<bool(void* data, size_t size, size_t& actualSize)>& onSend)
{
this->onSend = onSend;
return *this;
}
size_t HTTPClient::writeCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
HTTPClient* client = static_cast<HTTPClient*>(userp);
if (!client || !client->onReceive)
{
return size * nmemb;
}
size_t actualSize = 0;
return client->onReceive(contents, size * nmemb, actualSize) ? actualSize : 0;
}
size_t HTTPClient::readCallback(void* ptr, size_t size, size_t nmemb, void* userp)
{
HTTPClient* client = static_cast<HTTPClient*>(userp);
if (!client || !client->onSend)
{
return 0;
}
size_t actualSize = 0;
return client->onSend(ptr, size * nmemb, actualSize) ? actualSize : 0;
}
int HTTPClient::perform()
{
if (!curl)
{
return HTTPCLIENT_ERROR_INIT_FAILED;
}
// 지원하지 않는 메소드 체크
if (method != "GET" && method != "POST")
{
return HTTPCLIENT_ERROR_UNSUPPORTED;
}
// 기본 설정
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, readCallback);
curl_easy_setopt(curl, CURLOPT_READDATA, this);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "uNSS/1.0");
// HTTP 메서드 설정
if (method == "GET")
{
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
}
else if (method == "POST")
{
curl_easy_setopt(curl, CURLOPT_POST, 1L);
if (!onSend)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0L);
}
}
// 비동기 모드 설정
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
// libcurl 에러 코드를 우리의 에러 코드로 매핑
switch (res)
{
case CURLE_URL_MALFORMAT:
return HTTPCLIENT_ERROR_CURL_URL;
case CURLE_COULDNT_CONNECT:
return HTTPCLIENT_ERROR_CURL_CONNECT;
case CURLE_COULDNT_RESOLVE_HOST:
return HTTPCLIENT_ERROR_CURL_DNS;
case CURLE_SSL_CONNECT_ERROR:
return HTTPCLIENT_ERROR_CURL_SSL;
case CURLE_OPERATION_TIMEDOUT:
return HTTPCLIENT_ERROR_CURL_TIMEOUT;
case CURLE_OUT_OF_MEMORY:
return HTTPCLIENT_ERROR_CURL_MEMORY;
default:
return HTTPCLIENT_ERROR_CURL_OTHER;
}
}
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
return static_cast<int>(http_code);
}