forked from chenshuo/muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurl.h
143 lines (110 loc) · 2.9 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
#ifndef MUDUO_EXAMPLES_CURL_CURL_H
#define MUDUO_EXAMPLES_CURL_CURL_H
#include "muduo/base/noncopyable.h"
#include "muduo/base/StringPiece.h"
#include "muduo/net/Callbacks.h"
extern "C"
{
typedef void CURLM;
typedef void CURL;
}
namespace muduo
{
namespace net
{
class Channel;
class EventLoop;
}
}
namespace curl
{
class Curl;
class Request : public std::enable_shared_from_this<Request>,
muduo::noncopyable
{
public:
typedef std::function<void(const char*, int)> DataCallback;
typedef std::function<void(Request*, int)> DoneCallback;
Request(Curl*, const char* url);
~Request();
void setDataCallback(const DataCallback& cb)
{ dataCb_ = cb; }
void setDoneCallback(const DoneCallback& cb)
{ doneCb_ = cb; }
void setHeaderCallback(const DataCallback& cb)
{ headerCb_ = cb; }
// void allowRedirect(int redirects);
void headerOnly();
void setRange(const muduo::StringArg range);
template<typename OPT>
int setopt(OPT opt, long p)
{
return curl_easy_setopt(curl_, opt, p);
}
template<typename OPT>
int setopt(OPT opt, const char* p)
{
return curl_easy_setopt(curl_, opt, p);
}
template<typename OPT>
int setopt(OPT opt, void* p)
{
return curl_easy_setopt(curl_, opt, p);
}
template<typename OPT>
int setopt(OPT opt, size_t (*p)(char *, size_t , size_t , void *))
{
return curl_easy_setopt(curl_, opt, p);
}
const char* getEffectiveUrl();
const char* getRedirectUrl();
int getResponseCode();
// internal
muduo::net::Channel* setChannel(int fd);
void removeChannel();
void done(int code);
CURL* getCurl() { return curl_; }
muduo::net::Channel* getChannel() { return muduo::get_pointer(channel_); }
private:
void dataCallback(const char* buffer, int len);
void headerCallback(const char* buffer, int len);
static size_t writeData(char *buffer, size_t size, size_t nmemb, void *userp);
static size_t headerData(char *buffer, size_t size, size_t nmemb, void *userp);
void doneCallback();
class Curl* owner_;
CURL* curl_;
std::shared_ptr<muduo::net::Channel> channel_;
DataCallback dataCb_;
DataCallback headerCb_;
DoneCallback doneCb_;
};
typedef std::shared_ptr<Request> RequestPtr;
class Curl : muduo::noncopyable
{
public:
enum Option
{
kCURLnossl = 0,
kCURLssl = 1,
};
explicit Curl(muduo::net::EventLoop* loop);
~Curl();
RequestPtr getUrl(muduo::StringArg url);
static void initialize(Option opt = kCURLnossl);
// internal
CURLM* getCurlm() { return curlm_; }
muduo::net::EventLoop* getLoop() { return loop_; }
private:
void onTimer();
void onRead(int fd);
void onWrite(int fd);
void checkFinish();
static int socketCallback(CURL*, int, int, void*, void*);
static int timerCallback(CURLM*, long, void*);
muduo::net::EventLoop* loop_;
CURLM* curlm_;
int runningHandles_;
int prevRunningHandles_;
};
} // namespace curl
#endif // MUDUO_EXAMPLES_CURL_CURL_H