fastdo  0.6.8
eiennet_curl.hpp
浏览该文件的文档.
1 #ifndef __CURL_HPP__
2 #define __CURL_HPP__
3 
4 // curl.h的一些类型
5 typedef void CURL;
6 struct curl_slist;
7 struct curl_httppost;
8 
9 namespace eiennet
10 {
11 
14 {
15 public:
16  CUrlError( int errNo, char const * err ) : winux::Error( errNo, err ) { }
17 };
18 
21 {
22 public:
23  static char const * GetVersion();
24 
25  CUrlLib();
26  ~CUrlLib();
28 };
29 
32 {
33 public:
34  SList();
35  virtual ~SList();
36  void free();
37  void append( char const * s );
38  void append( winux::String const & str );
39 
40  operator struct curl_slist * () const { return _slist; }
41 private:
42  struct curl_slist * _slist;
43 
45 };
46 
49 {
50 public:
51  PostMultipart();
52  virtual ~PostMultipart();
53  void free();
55  void addVar( winux::String const & name, winux::String const & value );
57  void addFile( winux::String const & name, winux::String const & filename, winux::String const & showfilename = "" );
59  void addFile( winux::String const & name, char const * data, long size, winux::String const & showfilename );
60 
61  operator struct curl_httppost * () const { return _first; }
62 
63 private:
64  struct curl_httppost * _first;
65  struct curl_httppost * _last;
66 
68 };
69 
72 {
73 public:
75  CUrl( bool isInit = true );
76  CUrl( CUrl const & other );
77  virtual ~CUrl();
78 
80  CUrl & operator = ( CUrl const & other );
81 
83  void init();
84 
86  void cleanup();
87 
89  bool perform();
90 
92  void reset();
93 
94  // set options ------------------------------------------------
98  void setHttpGet( bool b = true );
99 
104  void setHttpPost( bool b = true );
105 
107  void setUrl( winux::String const & url );
108 
110  void setTimeout( winux::ulong timeout );
111 
113  void setErrorBuffer( char * errBuf );
114 
116  void setNoProgress( bool b );
117 
119  void setUsername( winux::String const & username );
120 
122  void setPassword( winux::String const & password );
123 
125  void setPostFields( winux::String const & data );
126 
128  void setPostFieldSize( long size );
129 
131  void setPostMultipart( PostMultipart const & data );
132 
134  void setHttpHeader( SList const & headers );
135 
137  void setVerbose( bool b );
138 
140  void setCookieJar( winux::String const & filename );
141 
145  void setSslVerifyPeer( bool b );
146 
150  void setSslVerifyHost( bool b );
151 
152  // set handlers -----------------------------------------------
153  typedef size_t (* WriteFunction)( char * buf, size_t itemSize, size_t count, void * data );
155  void setWriteHandler( WriteFunction handler );
157  void setWriteHandlerData( void * data );
158 
159  typedef size_t (* ReadFunction)( char * buf, size_t itemSize, size_t count, void * data );
161  void setReadHandler( ReadFunction handler );
163  void setReadHandlerData( void * data );
164 
165  typedef size_t (* HeaderFunction)( char * buf, size_t itemSize, size_t count, void * data );
167  void setHeaderHandler( HeaderFunction handler );
169  void setHeaderHandlerData( void * data );
170 
171  typedef int (* ProgressFunction)( void * data, double dltotal, double dlnow, double ultotal, double ulnow );
173  void setProgressHandler( ProgressFunction handler );
175  void setProgressHandlerData( void * data );
176 
177  // attributes -------------------------------------------------
178  operator CURL * () const { return _curl; }
179 
181  int errNo() const { return _errNo; }
182 
184  char const * errNoStr() const;
185 
187  char const * error() const;
188 
189 protected:
190  // '写'回调函数,回调函数将转到成员虚函数进行响应
191  static size_t WriteCallback( char * buf, size_t itemSize, size_t count, void * data );
192  // 当发生'写'动作
193  virtual size_t OnWrite( char * buf, size_t itemSize, size_t count );
194 
195  // '读'回调函数
196  static size_t ReadCallback( char * buf, size_t itemSize, size_t count, void * data );
197  // 当发生'读'动作
198  virtual size_t OnRead( char * buf, size_t itemSize, size_t count );
199 
200  // '头部'数据回调函数
201  static size_t HeaderCallback( char * buf, size_t itemSize, size_t count, void * data );
202  // 当'头部'数据到
203  virtual size_t OnHeader( char * buf, size_t itemSize, size_t count );
204 
205  // '进度'回调函数
206  static int ProgressCallback( void * data, double dltotal, double dlnow, double ultotal, double ulnow );
207  // 当'下载进度'操作
208  virtual int OnDownloadProgress( double dltotal, double dlnow );
209  // 当'上传进度'操作
210  virtual int OnUploadProgress( double ultotal, double ulnow );
211 
212 protected:
214  int _errNo;
216 };
217 
219 class EIENNET_DLL HttpCUrl : public CUrl
220 {
221 public:
222  HttpCUrl();
223  virtual ~HttpCUrl();
224 
228  winux::Buffer const & getResponse() const;
229 
233  char const * getResponseStr( size_t * size = NULL ) const;
234 
236  size_t getResponseSize() const;
237 
239  winux::String getResponseContentType() const;
240 
242  winux::String getResponseMime() const;
243 
245  winux::String getResponseCharset() const;
246 
248  bool get( winux::String const & url, http::Header const & headers = http::Header() );
250  bool post( winux::String const & url, winux::Mixed const & postVars = winux::Mixed(), http::Header const & headers = http::Header() );
252  bool post( winux::String const & url, PostMultipart const & multipart, http::Header const & headers = http::Header() );
254  bool post( winux::String const & url, winux::String const & contentType, winux::AnsiString const & postData, http::Header const & headers = http::Header() );
255 
256 protected:
257  // 当发生'写'动作
258  virtual size_t OnWrite( char * buf, size_t itemSize, size_t count );
259 
260 private:
261  winux::GrowBuffer _response;
262  winux::String _contentType;
263 };
264 
265 
266 } // namespace eiennet
267 
268 #endif // __CURL_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
代表HTTP头部
Definition: http_misc.hpp:10
curl库初始化
libcurl的字符串链表
XString< tchar > String
Definition: utilities.hpp:216
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
CUrlError(int errNo, char const *err)
void CURL
Definition: eiennet_curl.hpp:5
CURL的HTTP协议封装,默认30秒超时
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:528
#define EIENNET_DLL
winux::AnsiString _errBuf
网络通信库
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:659
http post请求以“multipart/formdata”方式发送数据的支持类
混合体,能表示多种类型的值
Definition: utilities.hpp:750
错误类
Definition: utilities.hpp:505
int errNo() const
执行后得到的错误码(CURLcode)
unsigned long ulong
Definition: utilities.hpp:171
int _errNo
CURLcode.
libcurl低层封装,主要提供了CURL句柄资源管理功能
跨平台基础功能库
Definition: archives.hpp:7