Curl Scol plugin
sCurl.h
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25#ifndef S_CURL_H
26#define S_CURL_H
27
28#include <scolPlugin.h>
29#include <string>
30#include <vector>
31#ifdef _WIN32
32# include <ws2tcpip.h>
33#endif
34
35#include <boost/thread.hpp>
36
37#include "curl/curl.h"
38#include "sService.h"
39
40#include <string>
41#include <algorithm> // find
42
43struct Uri
44{
45public:
46 std::string QueryString, Path, Protocol, Host, Port;
47
48 static Uri Parse(const std::string &uri)
49 {
50 Uri result;
51
52 typedef std::string::const_iterator iterator_t;
53
54 if (uri.length() == 0)
55 return result;
56
57 iterator_t uriEnd = uri.end();
58
59 // get query start
60 iterator_t queryStart = std::find(uri.begin(), uriEnd, '?');
61
62 // protocol
63 iterator_t protocolStart = uri.begin();
64 iterator_t protocolEnd = std::find(protocolStart, uriEnd, ':'); //"://");
65
66 if (protocolEnd != uriEnd)
67 {
68 std::string prot = &*(protocolEnd);
69 if ((prot.length() >= 3) && (prot.substr(0, 3) == "://"))
70 {
71 result.Protocol = std::string(protocolStart, protocolEnd);
72 protocolEnd += 3; // ://
73 }
74 else
75 protocolEnd = uri.begin(); // no protocol
76 }
77 else
78 protocolEnd = uri.begin(); // no protocol
79
80 // host
81 iterator_t hostStart = protocolEnd;
82 iterator_t pathStart = std::find(hostStart, uriEnd, '/'); // get pathStart
83
84 iterator_t hostEnd = std::find(protocolEnd,
85 (pathStart != uriEnd) ? pathStart : queryStart,
86 ':'); // check for port
87
88 result.Host = std::string(hostStart, hostEnd);
89
90 // port
91 if ((hostEnd != uriEnd) && ((&*(hostEnd))[0] == ':')) // we have a port
92 {
93 hostEnd++;
94 iterator_t portEnd = (pathStart != uriEnd) ? pathStart : queryStart;
95 result.Port = std::string(hostEnd, portEnd);
96 }
97
98 // path
99 if (pathStart != uriEnd)
100 result.Path = std::string(pathStart, queryStart);
101
102 // query
103 if (queryStart != uriEnd)
104 result.QueryString = std::string(queryStart, uri.end());
105
106 return result;
107
108 } // Parse
109}; // uri
110
115{
116public:
117 char* data;
118 unsigned int size;
119 int code;
120protected:
121private:
122
123public:
124 SCbData()
125 {
126 data = 0;
127 size = 0;
128 code = -1;
129 };
130 SCbData(char* mdata, const unsigned int& msize, const int& mcode)
131 {
132 data = (char*) malloc(msize + 1);
133 memcpy(data, mdata, msize);
134 data[msize] = 0;
135 size = msize;
136 code = mcode;
137 };
138
139 ~SCbData()
140 {
141 if(data)
142 free(data);
143 };
144protected:
145private:
146};
147
149{
150public:
151 std::string name;
152 std::string value;
153 bool file;
154protected:
155private:
156
157public:
158 SFormField()
159 {
160 name = "";
161 value = "";
162 file = false;
163 };
164 SFormField(const std::string& mname, const std::string& mvalue, const bool& mfile)
165 {
166 name = mname;
167 value = mvalue;
168 file = mfile;
169 };
170
172 {
173 };
174protected:
175private:
176};
177
179{
180public:
181typedef struct {
182 char *buf;
183 int len;
184 int pos;
185} readArg;
186
187 CURLoption option;
188 std::string svalue;
189 long lvalue;
190 std::vector<std::string> values;
191 readArg uploadData;
192 int type;
193 int lines_read;
194protected:
195private:
196
197public:
199 {
200 lvalue = 0;
201 uploadData.buf = 0;
202 };
203
204 SCurlOption(const CURLoption& moption, const long& mvalue)
205 {
206 option = moption;
207 lvalue = mvalue;
208 uploadData.buf = 0;
209 type = 0;
210 };
211
212 SCurlOption(const CURLoption& moption, const std::string& mvalue)
213 {
214 option = moption;
215 svalue = mvalue;
216 uploadData.buf = 0;
217 type = 1;
218 };
219
220 SCurlOption(const CURLoption& moption, const std::vector<std::string>& mvalue)
221 {
222 option = moption;
223 values = mvalue;
224 uploadData.buf = 0;
225 type = 2;
226 };
227
228 SCurlOption(const CURLoption& moption, const char* mvalue, const unsigned int size)
229 {
230 option = moption;
231
232 uploadData.buf = (char*)malloc(size + 1);
233 memcpy(uploadData.buf, mvalue, size);
234 uploadData.len = size;
235 uploadData.pos = 0;
236 type = 3;
237 };
238
240 {
241 };
242
243 static size_t payloadSource(void *ptr, size_t size, size_t nmemb, void *userp)
244 {
245 SCurlOption* curOpt = (SCurlOption *)userp;
246
247 unsigned int len = curOpt->uploadData.len - curOpt->uploadData.pos;
248 if (len > size * nmemb)
249 len = size * nmemb;
250 memcpy(ptr, curOpt->uploadData.buf + curOpt->uploadData.pos, len);
251 curOpt->uploadData.pos += len;
252
253 if (curOpt->uploadData.pos >= curOpt->uploadData.len)
254 {
255 free(curOpt->uploadData.buf);
256 curOpt->uploadData.buf = 0;
257 }
258 //printf("readcb: %d bytes\n", len);
259 return len;
260 }
261protected:
262private:
263};
264
268class SCurl
269{
270public:
271protected:
272 bool m_terminate;
273private:
274 CURL* m_curl;
275 std::string m_url;
276 boost::mutex m_terminateMutex;
277 boost::mutex m_mutex;
278 boost::mutex m_thread_lock;
279
280 /* Options */
281 std::vector<SCurlOption> m_options;
282
283 /* Forms */
284 std::vector<SFormField> m_formFields;
285public:
286 static bool CheckUrl(const std::string& url_s);
287
291 SCurl(const std::string& url);
292
296 ~SCurl();
297
298 void CallAsync();
299 void CallSync();
300
301 void SetHeader(const std::vector<std::string>& header);
302 void SetOption(const CURLoption& opt, const std::string& value);
303 void SetOption(const CURLoption& opt, const int& value);
304 void SetOption(const CURLoption& opt, const std::vector<std::string>& value);
305 void SetOption(const CURLoption& opt, const char* mvalue, const unsigned int size);
306
307 void AddFormField(const std::string& name, const std::string& value, const bool& file);
308protected:
309private:
310 static size_t Write_data(char* ptr, size_t size, size_t nmemb, void* userdata);
311 int Progress(void *userdata, double dltotal, double dlnow, double ultotal, double ulnow);
312 void ExecSync();
313};
314
315#endif
Definition sCurl.h:269
~SCurl()
Definition sCurl.cpp:45
Definition sCurl.h:44