-
Notifications
You must be signed in to change notification settings - Fork 1
/
Request.h
94 lines (69 loc) · 2.33 KB
/
Request.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
#pragma once
#include <string>
#include <curl/curl.h>
#include <boost/algorithm/string.hpp>
#include <sys/time.h>
#include <Headers.h>
#include <Response.h>
#include <StatusCode.h>
namespace HTTP {
static size_t __curl_write_data(void * ptr,
size_t size,
size_t nmemb,
std::string * data)
{
data->append((char*) ptr, size * nmemb);
return size * nmemb;
}
static size_t __curl_write_headers(char * buffer,
size_t size,
size_t nitems,
HTTP::Headers * headers)
{
if (buffer[0] == '\r' || buffer[0] == '\n')
return nitems * size;
std::string header(buffer, size*nitems);
std::string::size_type index = header.find(':', 0);
if (index != std::string::npos) {
headers->set(
boost::algorithm::trim_copy(header.substr(0, index)),
boost::algorithm::trim_copy(header.substr(index + 1))
);
}
else {
headers->set(header);
}
return nitems * size;
}
class Request
{
public:
Request();
~Request();
HTTP::Response get(const std::string & url);
HTTP::Response get(const std::string & url,
const HTTP::Headers & headers);
HTTP::Response post(const std::string & url,
const std::string & data);
HTTP::Response post(const std::string & url,
const HTTP::Headers & headers,
const std::string & data);
HTTP::Response put(const std::string & url,
const std::string & data);
HTTP::Response put(const std::string & url,
const HTTP::Headers & headers,
const std::string & data);
HTTP::Response del(const std::string & url);
HTTP::Response del(const std::string & url,
const HTTP::Headers & headers);
HTTP::Response patch(const std::string & url,
const std::string & data);
HTTP::Response patch(const std::string & url,
const HTTP::Headers & headers,
const std::string & data);
private:
void curlReset();
void perform(HTTP::Response & response);
CURL * curl_;
};
}