fastdo  0.5.12
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  struct curl_slist * _slist;
34 public:
35  SList();
36  virtual ~SList();
37  void free();
38  void append( char const * s );
39 
40  operator struct curl_slist * () const { return _slist; }
42 };
43 
46 {
47  struct curl_httppost * _first;
48  struct curl_httppost * _last;
49 public:
50  PostMultipart();
51  virtual ~PostMultipart();
52  void free();
54  void addVar( winux::String const & name, winux::String const & value );
56  void addFile( winux::String const & name, winux::String const & filename, winux::String const & showfilename = "" );
58  void addFile( winux::String const & name, char const * data, long size, winux::String const & showfilename );
59 
60  operator struct curl_httppost * () const { return _first; }
62 };
63 
66 {
67 private:
68  CURL * _curl;
69  int _errNo;
70  winux::AnsiString _errBuf;
71 protected:
72  // '写'回调函数,回调函数将转到成员虚函数进行响应
73  static size_t WriteCallback( char * buf, size_t itemSize, size_t count, void * data );
74  // 当发生'写'动作
75  virtual size_t OnWrite( char * buf, size_t itemSize, size_t count );
76 
77  // '读'回调函数
78  static size_t ReadCallback( char * buf, size_t itemSize, size_t count, void * data );
79  // 当发生'读'动作
80  virtual size_t OnRead( char * buf, size_t itemSize, size_t count );
81 
82  // '头部'数据回调函数
83  static size_t HeaderCallback( char * buf, size_t itemSize, size_t count, void * data );
84  // 当'头部'数据到
85  virtual size_t OnHeader( char * buf, size_t itemSize, size_t count );
86 
87  // '进度'回调函数
88  static int ProgressCallback( void * data, double dltotal, double dlnow, double ultotal, double ulnow );
89  // 当'下载进度'操作
90  virtual int OnDownloadProgress( double dltotal, double dlnow );
91  // 当'上传进度'操作
92  virtual int OnUploadProgress( double ultotal, double ulnow );
93 public:
94  // 错误码转成文本信息
95  //static char const * getErrNoStr( int errNo );
96 
98  CUrl( bool isInit = true );
99  CUrl( CUrl const & other );
100  virtual ~CUrl();
101 
103  CUrl & operator = ( CUrl const & other );
104 
106  void init();
108  void cleanup();
109 
111  bool perform();
112 
114  void reset();
115 
116  // set options ------------------------------------------------
120  void setHttpGet( bool b = true );
121 
126  void setHttpPost( bool b = true );
127 
129  void setUrl( winux::String const & url );
130 
132  void setTimeout( winux::ulong timeout );
133 
135  void setErrorBuffer( char * errBuf );
136 
138  void setNoProgress( bool b );
139 
141  void setUsername( winux::String const & username );
142 
144  void setPassword( winux::String const & password );
145 
147  void setPostFields( char const * data );
148 
150  void setPostFieldSize( long size );
151 
153  void setPostMultipart( PostMultipart const & data );
154 
156  void setHttpHeader( SList const & headers );
157 
159  void setVerbose( bool b );
160 
162  void setCookieJar( winux::String const & filename );
163 
167  void setSslVerifyPeer( bool b );
168 
172  void setSslVerifyHost( bool b );
173 
174  // set handlers -----------------------------------------------
175  typedef size_t (* WriteFunction)( char * buf, size_t itemSize, size_t count, void * data );
177  void setWriteHandler( WriteFunction handler );
179  void setWriteHandlerData( void * data );
180 
181  typedef size_t (* ReadFunction)( char * buf, size_t itemSize, size_t count, void * data );
182  void setReadHandler( ReadFunction handler );
183  void setReadHandlerData( void * data );
184 
185  typedef size_t (* HeaderFunction)( char * buf, size_t itemSize, size_t count, void * data );
186  void setHeaderHandler( HeaderFunction handler );
187  void setHeaderHandlerData( void * data );
188 
189  typedef int (* ProgressFunction)( void * data, double dltotal, double dlnow, double ultotal, double ulnow );
190  void setProgressHandler( ProgressFunction handler );
191  void setProgressHandlerData( void * data );
192 
193  // attributes -------------------------------------------------
194  operator CURL * () const { return _curl; }
195 
197  int errNo() const { return _errNo; }
198 
200  char const * errNoStr() const;
201 
203  char const * error() const;
204 
205 };
206 
208 class EIENNET_DLL HttpCUrl : public CUrl
209 {
210 protected:
211  // 当发生'写'动作
212  virtual size_t OnWrite( char * buf, size_t itemSize, size_t count );
213 
214 private:
215  winux::GrowBuffer _response;
216 public:
217  HttpCUrl();
218  virtual ~HttpCUrl();
219 
223  winux::Buffer const & getResponse() const;
224 
226  winux::ulong getResponseSize() const;
227 
231  char const * getResponseStr( winux::ulong * size = NULL ) const;
232 
234  bool get( winux::String const & url, http::Header const & headers = http::Header() );
236  bool post( winux::String const & url, winux::Mixed const & postVars = winux::Mixed(), http::Header const & headers = http::Header() );
238  bool post( winux::String const & url, PostMultipart const & multipart, http::Header const & headers = http::Header() );
239 
240 };
241 
242 
243 } // namespace eiennet
244 
245 #endif // __CURL_HPP__
代表HTTP头部
Definition: http_misc.hpp:10
curl库初始化
libcurl的字符串链表
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:78
CUrlError(int errNo, char const *err)
void CURL
Definition: eiennet_curl.hpp:5
CURL的HTTP协议封装,默认30秒超时
缓冲区,表示内存中一块2进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:436
#define EIENNET_DLL
网络通信库
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:531
http post请求以“multipart/formdata”方式发送数据的支持类
混合体,能表示多种类型的值
Definition: utilities.hpp:586
错误类
Definition: utilities.hpp:413
int errNo() const
执行后得到的错误码(CURLcode)
unsigned long ulong
Definition: utilities.hpp:129
libcurl低层封装,主要提供了CURL句柄资源管理功能
std::basic_string< tchar > String
Definition: utilities.hpp:162
跨平台基础功能库
Definition: archives.hpp:7