30extern int CURL_DATA_CB;
32bool SCurl::CheckUrl(
const std::string& url_s)
34 Uri checkUri = Uri::Parse(url_s);
35 return !checkUri.Host.empty();
49 boost::mutex::scoped_lock lock(m_terminateMutex);
57 boost::mutex::scoped_lock lock(m_mutex);
63 for (
size_t i = 0; i < m_options.size(); i++)
65 switch (m_options[i].type)
69 if (m_options[i].option == CURLOPT_UPLOAD)
71 if (m_options[i].uploadData.buf)
72 free(m_options[i].uploadData.buf);
79 m_thread_lock.unlock();
82size_t SCurl::Write_data(
char* ptr,
size_t size,
size_t nmemb,
void* userdata)
85 size_t realsize = size * nmemb;
87 OBJpostEvent(CURL_DATA_CB, SCOL_PTR userdata, SCOL_PTR data);
91int SCurl::Progress(
void *userdata,
double dltotal,
double dlnow,
double ultotal,
double ulnow)
95 boost::mutex::scoped_lock lock(curlObj->m_terminateMutex);
97 if (curlObj->m_terminate)
103void SCurl::ExecSync()
105 boost::mutex::scoped_lock lock(m_mutex);
106 CURLcode res = CURLE_FAILED_INIT;
107 m_curl = curl_easy_init();
112 curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1L);
113 curl_easy_setopt(m_curl, CURLOPT_USERAGENT,
"ScolCurl-agent/1.0");
114 curl_easy_setopt(m_curl, CURLOPT_URL, m_url.c_str());
117 curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, (
void *)
this);
118 curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, SCurl::Write_data);
120 curl_easy_setopt(m_curl, CURLOPT_PROGRESSDATA, (
void *)
this);
121 curl_easy_setopt(m_curl, CURLOPT_PROGRESSFUNCTION, SCurl::Write_data);
124 struct curl_httppost* formPost = 0;
125 struct curl_httppost* lastFormPost = 0;
127 if(!m_formFields.empty())
129 for (
size_t i = 0; i < m_formFields.size(); i++)
131 curl_formadd(&formPost, &lastFormPost, CURLFORM_COPYNAME, m_formFields[i].name.c_str(), m_formFields[i].file ? CURLFORM_FILE : CURLFORM_COPYCONTENTS, m_formFields[i].value.c_str(), CURLFORM_END);
136 curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, formPost);
139 std::vector<curl_slist*> clist;
140 for (
size_t i = 0; i < m_options.size(); i++)
142 switch (m_options[i].type)
145 curl_easy_setopt(m_curl, m_options[i].option, m_options[i].lvalue);
149 curl_easy_setopt(m_curl, m_options[i].option, m_options[i].svalue.c_str());
154 struct curl_slist* vlist = 0;
155 for (
size_t j = 0; j < m_options[i].values.size(); j++)
157 vlist = curl_slist_append(vlist, m_options[i].values[j].c_str());
159 clist.push_back(vlist);
161 curl_easy_setopt(m_curl, m_options[i].option, vlist);
167 if (m_options[i].option == CURLOPT_UPLOAD)
169 curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, m_options[i].payloadSource);
170 curl_easy_setopt(m_curl, CURLOPT_READDATA, &(m_options[i]));
171 curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 1L);
179 res = curl_easy_perform(m_curl);
182 for (
size_t i = 0; i < clist.size(); i++)
184 curl_slist_free_all(clist[i]);
190 curl_formfree(formPost);
193 curl_easy_cleanup(m_curl);
198 MMechostr(MSKRUNTIME,
"Curl error (%i) : %s\n", res, curl_easy_strerror(res));
201 OBJpostEvent(CURL_DATA_CB, SCOL_PTR
this, SCOL_PTR data);
203 m_thread_lock.unlock();
206void SCurl::CallAsync()
208 m_thread_lock.lock();
211 SService* service = SService::GetInstance();
212 service->addWork(boost::bind(&SCurl::ExecSync,
this));
214 catch(std::exception &e)
216 MMechostr(MSKRUNTIME,
"SCurl thread error : %s, call the request synchronously.\n", e.what());
221void SCurl::CallSync()
223 m_thread_lock.lock();
227void SCurl::AddFormField(
const std::string& name,
const std::string& value,
const bool& file)
230 m_formFields.push_back(field);
233void SCurl::SetHeader(
const std::vector<std::string>& header)
236 m_options.push_back(option);
239void SCurl::SetOption(
const CURLoption& opt,
const int& value)
243 case CURLOPT_HEADER :
244 case CURLOPT_PROXYPORT :
245 case CURLOPT_PROXYTYPE :
246 case CURLOPT_HTTPPROXYTUNNEL :
247 case CURLOPT_SOCKS5_GSSAPI_NEC :
248 case CURLOPT_LOCALPORT :
249 case CURLOPT_LOCALPORTRANGE :
250 case CURLOPT_DNS_CACHE_TIMEOUT :
251 case CURLOPT_BUFFERSIZE :
253 case CURLOPT_TCP_NODELAY :
254 case CURLOPT_ADDRESS_SCOPE :
255 case CURLOPT_TCP_KEEPALIVE :
256 case CURLOPT_TCP_KEEPIDLE :
257 case CURLOPT_TCP_KEEPINTVL :
259 case CURLOPT_HTTPAUTH :
260 case CURLOPT_PROXYAUTH :
261 case CURLOPT_AUTOREFERER :
262 case CURLOPT_FOLLOWLOCATION :
263 case CURLOPT_UNRESTRICTED_AUTH :
264 case CURLOPT_MAXREDIRS :
265 case CURLOPT_POSTREDIR :
268 case CURLOPT_POSTFIELDSIZE :
269 case CURLOPT_POSTFIELDSIZE_LARGE :
270 case CURLOPT_COOKIESESSION :
271 case CURLOPT_HTTPGET :
272 case CURLOPT_HTTPPOST :
273 case CURLOPT_HTTP_VERSION :
274 case CURLOPT_IGNORE_CONTENT_LENGTH :
275 case CURLOPT_HTTP_CONTENT_DECODING :
276 case CURLOPT_HTTP_TRANSFER_DECODING :
277 case CURLOPT_TFTP_BLKSIZE :
278 case CURLOPT_DIRLISTONLY :
279 case CURLOPT_APPEND :
280 case CURLOPT_FTP_USE_EPRT :
281 case CURLOPT_FTP_USE_EPSV :
282 case CURLOPT_FTP_CREATE_MISSING_DIRS :
283 case CURLOPT_FTP_RESPONSE_TIMEOUT :
284 case CURLOPT_FTP_SKIP_PASV_IP :
285 case CURLOPT_FTPSSLAUTH :
286 case CURLOPT_FTP_SSL_CCC :
287 case CURLOPT_FTP_FILEMETHOD :
288 case CURLOPT_RTSP_REQUEST :
289 case CURLOPT_RTSP_CLIENT_CSEQ :
290 case CURLOPT_RTSP_SERVER_CSEQ :
291 case CURLOPT_TRANSFERTEXT :
292 case CURLOPT_PROXY_TRANSFER_MODE :
295 case CURLOPT_RESUME_FROM :
296 case CURLOPT_RESUME_FROM_LARGE :
297 case CURLOPT_CUSTOMREQUEST :
298 case CURLOPT_FILETIME :
299 case CURLOPT_NOBODY :
300 case CURLOPT_INFILESIZE :
301 case CURLOPT_INFILESIZE_LARGE :
302 case CURLOPT_UPLOAD :
303 case CURLOPT_MAXFILESIZE :
304 case CURLOPT_MAXFILESIZE_LARGE :
305 case CURLOPT_TIMECONDITION :
306 case CURLOPT_TIMEVALUE :
307 case CURLOPT_TIMEOUT :
308 case CURLOPT_TIMEOUT_MS :
309 case CURLOPT_LOW_SPEED_LIMIT :
310 case CURLOPT_LOW_SPEED_TIME :
311 case CURLOPT_MAX_SEND_SPEED_LARGE :
312 case CURLOPT_MAX_RECV_SPEED_LARGE :
313 case CURLOPT_MAXCONNECTS :
314 case CURLOPT_FRESH_CONNECT :
315 case CURLOPT_FORBID_REUSE :
316 case CURLOPT_CONNECTTIMEOUT :
317 case CURLOPT_CONNECTTIMEOUT_MS :
318 case CURLOPT_IPRESOLVE :
319 case CURLOPT_CONNECT_ONLY :
320 case CURLOPT_USE_SSL :
321 case CURLOPT_ACCEPTTIMEOUT_MS :
322 case CURLOPT_SSLVERSION :
323 case CURLOPT_SSL_VERIFYPEER :
324 case CURLOPT_SSL_VERIFYHOST :
325 case CURLOPT_CERTINFO :
326 case CURLOPT_SSL_SESSIONID_CACHE :
327 case CURLOPT_SSL_OPTIONS :
328 case CURLOPT_GSSAPI_DELEGATION :
329 case CURLOPT_SSH_AUTH_TYPES :
330 case CURLOPT_NEW_FILE_PERMS :
331 case CURLOPT_NEW_DIRECTORY_PERMS :
334 m_options.push_back(option);
339 MMechostr(MSKRUNTIME,
"CURL option not found or not implemented with this param type\n");
343void SCurl::SetOption(
const CURLoption& opt,
const std::string& value)
349 case CURLOPT_NOPROXY :
350 case CURLOPT_SOCKS5_GSSAPI_SERVICE :
351 case CURLOPT_INTERFACE :
352 case CURLOPT_NETRC_FILE :
353 case CURLOPT_USERPWD :
354 case CURLOPT_PROXYUSERPWD :
355 case CURLOPT_USERNAME :
356 case CURLOPT_PASSWORD :
357 case CURLOPT_PROXYUSERNAME :
358 case CURLOPT_PROXYPASSWORD :
359 case CURLOPT_TLSAUTH_USERNAME :
360 case CURLOPT_TLSAUTH_PASSWORD :
361 case CURLOPT_ACCEPT_ENCODING :
362 case CURLOPT_TRANSFER_ENCODING :
363 case CURLOPT_COPYPOSTFIELDS :
364 case CURLOPT_REFERER :
365 case CURLOPT_USERAGENT :
366 case CURLOPT_COOKIE :
367 case CURLOPT_COOKIEFILE :
368 case CURLOPT_COOKIEJAR :
369 case CURLOPT_COOKIELIST :
370 case CURLOPT_MAIL_FROM :
371 case CURLOPT_MAIL_AUTH :
372 case CURLOPT_FTPPORT :
373 case CURLOPT_FTP_ALTERNATIVE_TO_USER :
374 case CURLOPT_FTP_ACCOUNT :
375 case CURLOPT_RTSP_SESSION_ID :
376 case CURLOPT_RTSP_STREAM_URI :
377 case CURLOPT_RTSP_TRANSPORT :
379 case CURLOPT_CUSTOMREQUEST :
380 case CURLOPT_SSLCERT :
381 case CURLOPT_SSLCERTTYPE :
382 case CURLOPT_SSLKEY :
383 case CURLOPT_SSLKEYTYPE :
384 case CURLOPT_KEYPASSWD :
385 case CURLOPT_SSLENGINE :
386 case CURLOPT_SSLENGINE_DEFAULT :
387 case CURLOPT_CAINFO :
388 case CURLOPT_ISSUERCERT :
389 case CURLOPT_CAPATH :
390 case CURLOPT_CRLFILE :
391 case CURLOPT_RANDOM_FILE :
392 case CURLOPT_EGDSOCKET :
393 case CURLOPT_SSL_CIPHER_LIST :
394 case CURLOPT_KRBLEVEL :
395 case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5 :
396 case CURLOPT_SSH_PUBLIC_KEYFILE :
397 case CURLOPT_SSH_PRIVATE_KEYFILE :
398 case CURLOPT_SSH_KNOWNHOSTS :
399 case CURLOPT_POSTFIELDS :
402 m_options.push_back(option);
407 MMechostr(MSKRUNTIME,
"CURL option not found or not implemented with this param type\n");
411void SCurl::SetOption(
const CURLoption& opt,
const std::vector<std::string>& value)
415 case CURLOPT_HTTPHEADER :
416 case CURLOPT_MAIL_RCPT :
418 case CURLOPT_POSTQUOTE :
419 case CURLOPT_PREQUOTE :
420 case CURLOPT_RESOLVE :
421 case CURLOPT_DNS_SERVERS :
422 case CURLOPT_TELNETOPTIONS :
425 m_options.push_back(option);
430 MMechostr(MSKRUNTIME,
"CURL option not found or not implemented with this param type\n");
434void SCurl::SetOption(
const CURLoption& opt,
const char* mvalue,
const unsigned int size)
438 case CURLOPT_UPLOAD :
441 m_options.push_back(option);
446 MMechostr(MSKRUNTIME,
"CURL option not found or not implemented with this param type\n");
SCurl(const std::string &url)