Version: 0.6.0
curl.h
1 /******************************************************************************
2  * Project: wxGIS (GIS Catalog)
3  * Purpose: cURL class. This is smart class for cURL handler
4  * Author: Dmitry Baryshnikov (aka Bishop), polimax@mail.ru
5  ******************************************************************************
6 * Copyright (C) 2008,2010-2012,2014 Dmitry Baryshnikov
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  ****************************************************************************/
21 #pragma once
22 
23 #include "wxgis/net/net.h"
24 
25 #ifdef wxGIS_USE_CURL
26 
27 #include <curl/curl.h>
28 #include <stdlib.h>
29 
30 typedef struct _perform_result
31 {
32  wxString sHead;
33  wxString sBody;
34  unsigned int iSize;
35  bool IsValid;
36  long nHTTPCode;
38 
46 class WXDLLIMPEXP_GIS_NET wxGISCurl : public wxObject
47 {
48  DECLARE_CLASS(wxGISCurl)
49 public:
50  wxGISCurl(const wxString &proxy, const wxString &sHeaders, int dnscachetimeout, int timeout, int conntimeout);
51  wxGISCurl(bool bReplaceUserAgent = true);
52  virtual ~wxGISCurl(void);
53  bool IsOk() const;
54 
55  bool operator == ( const wxGISCurl& obj ) const;
56  bool operator != (const wxGISCurl& obj) const { return !(*this == obj); };
57 
58 public:
59  virtual void AppendHeader(const wxString & sHeadStr);
60  virtual void SetDefaultHeader(void);
61  virtual void FollowLocation(bool bSet, unsigned short iMaxRedirs);
62  virtual void SetSSLVersion(long nVer = CURL_SSLVERSION_SSLv3);
63  virtual PERFORMRESULT Get(const wxString & sURL);
64  virtual bool GetFile(const wxString & sURL, const wxString & sPath);
65  virtual PERFORMRESULT Post(const wxString & sURL, const wxString & sPostData);
66  virtual PERFORMRESULT Delete(const wxString & sURL);
67  virtual PERFORMRESULT PutData(const wxString & sURL, const wxString& sPostData);
68 protected:
69  virtual wxObjectRefData *CreateRefData() const;
70  virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
71 };
72 
80 class wxGISCurlRefData : public wxObjectRefData
81 {
82  friend class wxGISCurl;
83 public:
84  wxGISCurlRefData(const wxString & proxy, const wxString & sHeaders, int dnscachetimeout, int timeout, int conntimeout);
85  virtual ~wxGISCurlRefData(void);
86  wxGISCurlRefData(const wxGISCurlRefData& data);
87  bool operator == (const wxGISCurlRefData& data) const;
88  void SetDefaultHeader(void);
89  void AppendHeader(const wxString & sHeadStr);
90  void FollowLocation(bool bSet, unsigned short iMaxRedirs);
91  void SetSSLVersion(long nVer = CURL_SSLVERSION_SSLv3);
92  PERFORMRESULT Get(const wxString & sURL);
93  bool GetFile(const wxString & sURL, const wxString & sPath);
94  PERFORMRESULT Post(const wxString & sURL, const wxString & sPostData);
95  PERFORMRESULT Delete(const wxString & sURL);
96  PERFORMRESULT PutData(const wxString & sURL, const wxString& sPostData);
97 protected:
98  struct curl_slist *slist;
99  CURL *m_pCurl;
100  wxString m_sHeaders;
101  bool m_bUseProxy;
102 
103 protected:
104  CURLcode res;
105  struct MemoryStruct
106  {
107  char *memory;
108  size_t size;
109  };
110  struct MemoryStruct bodystruct;
111  struct MemoryStruct headstruct;
112 
113  static void *myrealloc(void *ptr, size_t size)
114  {
115  /* There might be a realloc() out there that doesn't like reallocing
116  NULL pointers, so we take care of it here */
117  if (ptr)
118  return realloc(ptr, size);
119  else
120  return malloc(size);
121  }
122 
123  static size_t write_data(void *ptr, size_t size, size_t nmemb, void *data)
124  {
125  size_t realsize = size * nmemb;
126  struct MemoryStruct *mem = (struct MemoryStruct *)data;
127 
128  mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
129  if (mem->memory)
130  {
131  memcpy(&(mem->memory[mem->size]), ptr, realsize);
132  mem->size += realsize;
133  mem->memory[mem->size] = 0;
134  }
135  return realsize;
136  }
137 };
138 
139 #endif //wxGIS_USE_CURL
Definition: curl.h:28
Definition: curl.h:35