-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curl.h
159 lines (131 loc) · 3.3 KB
/
Curl.h
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
//
// Copyright (c) 2016 James A. Chappell
//
// A very simple c++ wrapper around libcurl
//
#ifndef STORAGE_B_CURL_H_
#define STORAGE_B_CURL_H_
#include <curl/curl.h>
#include <string>
#include <cctype>
namespace Storage_B
{
namespace Curlpp
{
class Curl
{
public:
Curl()
: curl_(curl_easy_init())
, headers_(nullptr)
, post_(nullptr)
, last_(nullptr)
{
SetOpt(CURLOPT_NOSIGNAL, 1);
SetOpt(CURLOPT_ERRORBUFFER, error_buffer_);
}
// no copy
Curl(const Curl&) = delete;
Curl& operator=(const Curl&) = delete;
~Curl()
{
ClearHeaders();
ClearForm();
curl_easy_cleanup(curl_);
}
CURL *curl() const { return curl_; }
CURLcode Url(const std::string& url)
{
return SetOpt(CURLOPT_URL, url.c_str());
}
CURLcode SetOpt(CURLoption option, const char *parameter)
{
return curl_easy_setopt(curl_, option, parameter);
}
CURLcode SetOpt(CURLoption option, int parameter)
{
return curl_easy_setopt(curl_, option, parameter);
}
CURLcode SetOpt(CURLoption option, size_t (*f)(void *, size_t, size_t,
void *))
{
return curl_easy_setopt(curl_, option, f);
}
CURLcode SetOpt(CURLoption option, void *parameter)
{
return curl_easy_setopt(curl_, option, parameter);
}
CURLcode SetVerbose()
{
return SetOpt(CURLOPT_VERBOSE, 1);
}
void AppendHeaders(const std::string& header)
{
headers_ = curl_slist_append(headers_, header.c_str());
}
void Header()
{
SetOpt(CURLOPT_HTTPHEADER, headers_);
}
void ClearHeaders()
{
if (headers_)
{
curl_slist_free_all(headers_);
headers_ = nullptr;
}
}
void FormAdd(const std::string& name, const std::string& value)
{
curl_formadd(&post_, &last_, CURLFORM_COPYNAME, name.c_str(),
CURLFORM_COPYCONTENTS, value.c_str(), CURLFORM_END);
}
void Form()
{
SetOpt(CURLOPT_HTTPPOST, post_);
}
void ClearForm()
{
if (post_)
{
curl_formfree(post_);
post_ = nullptr;
last_ = nullptr;
}
}
CURLcode Perform(long& code)
{
CURLcode status = curl_easy_perform(curl_);
if (curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &code) != CURLE_OK)
{
code = -1;
}
return status;
}
static bool httpStatusOK(long status)
{
return (status >= 200) && (status < 300);
}
static bool httpClientError(long status)
{
return (status >= 400) && (status < 500);
}
const char *error() const { return error_buffer_; }
static CURLcode GlobalInit(long flags = CURL_GLOBAL_DEFAULT)
{
return curl_global_init(flags);
}
static void GlobalCleanup()
{
curl_global_cleanup();
}
private:
CURL *curl_;
char error_buffer_[CURL_ERROR_SIZE];
struct curl_slist *headers_;
struct curl_httppost *post_;
struct curl_httppost *last_;
};
}
}
#endif