Commit df93261f authored by mr-ti's avatar mr-ti
Browse files

No commit message

No commit message
parent cadf8033
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
PROJECT(ddnssbase) PROJECT(ddnssbase)
# ### COMMUN ### # ### COMMUN ###
SET(ddnssbase_SRCS ddnssbase.cpp) SET(ddnssbase_SRCS ddnsshttpcl.cpp)
SET(ddnssbase_LIBS unicomcl) SET(ddnssbase_LIBS unicomcl curl)
SET(ddnssbase_DEFS ${global_unicomctrl_DEFS}) SET(ddnssbase_DEFS ${global_unicomctrl_DEFS})
SET(ddnssbase_LDFS ) SET(ddnssbase_LDFS )
# add " -D_UNI_INTENSIVE_VERBOSE" for more debub message # add " -D_UNI_INTENSIVE_VERBOSE" for more debub message
......
...@@ -10,15 +10,67 @@ ...@@ -10,15 +10,67 @@
// //
// //
#include "ddnsshttpcl.h" #include "ddnsshttpcl.h"
string *DdnssHttpCl::resBuf=NULL;
CUniMutex DdnssHttpCl::mutResBuf;
DdnssHttpCl::DdnssHttpCl() DdnssHttpCl::DdnssHttpCl()
: CUniMutex() { : CUniObj() {
publicIpGetAdress="http://checkip.dyndns.org/"; publicIpGetAdress="http://checkip.dyndns.org/";
//curl_global_init(CURL_GLOBAL_DEFAULT);
handle=curl_easy_init(); //Initialise la lib cURL
curl_easy_setopt(handle, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,writeInRepBuf);
curl_easy_setopt(handle,CURLOPT_WRITEDATA,NULL);
//curl_easy_setopt(handle,CURLOPT_TIMEOUT,timeout);
//curl_easy_setopt(handle,CURLOPT_CONNECTTIMEOUT,timeout);
curl_easy_setopt(handle,CURLOPT_SSL_VERIFYPEER,0);
curl_easy_setopt(handle,CURLOPT_SSL_VERIFYHOST,0);
} }
DdnssHttpCl::~DdnssHttpCl() { DdnssHttpCl::~DdnssHttpCl() {
curl_easy_cleanup(handle); //Détrut le handle cURL
} }
void DdnssHttpCl::setHttpUserPwd(const string &login, const string &passwd){
string tmp=login+":"+passwd;
curl_easy_setopt(handle,CURLOPT_USERPWD,tmp.c_str());
}
bool DdnssHttpCl::get(string &result, const string &url){
bool ret=true;
mutResBuf.lock();
curl_easy_setopt(handle,CURLOPT_CUSTOMREQUEST,"GET"); //Paramètre le type de requête
curl_easy_setopt(handle,CURLOPT_URL,url.c_str());
resBuf=&result;
if(curl_easy_perform(handle)!=0)
ret=false;
mutResBuf.unlock();
return ret;
}
bool DdnssHttpCl::post(string &result, const string &url){
bool ret=true;
mutResBuf.lock();
string urlTmp,data;
string::size_type n=url.find('?');
if(n!=string::npos){
urlTmp=url.substr(0,n);
data=url.substr(n+1);
}else{
urlTmp=url;
}
curl_easy_setopt(handle,CURLOPT_CUSTOMREQUEST,"POST"); //Paramètre le type de requête
curl_easy_setopt(handle,CURLOPT_POSTFIELDS,data.c_str()); //Enregistre les données de la requête
curl_easy_setopt(handle,CURLOPT_URL,urlTmp.c_str());
resBuf=&result;
if(curl_easy_perform(handle)!=0)
ret=false;
mutResBuf.unlock();
return ret;
}
size_t DdnssHttpCl::writeInRepBuf(void *ptr, size_t size, size_t nmemb, FILE *out){
resBuf->append((char*)ptr,size*nmemb);
return size*nmemb;
}
...@@ -11,16 +11,24 @@ ...@@ -11,16 +11,24 @@
// //
#ifndef DDNSSHTTPCL_H #ifndef DDNSSHTTPCL_H
#define DDNSSHTTPCL_H #define DDNSSHTTPCL_H
#include <curl/curl.h>
#include <unicomctrl/cunimutex.h> #include <unicomctrl/cunimutex.h>
class DdnssHttpCl : public CUniMutex { class DdnssHttpCl : public CUniObj{
public: public:
DdnssHttpCl(); DdnssHttpCl();
~DdnssHttpCl(); ~DdnssHttpCl();
string getPublicIp(); string getPublicIp();
void setHttpUserPwd(const string &login, const string &passwd);
bool get(string &result, const string &url);
bool post(string &result, const string &url);
private: private:
string publicIpGetAdress; string publicIpGetAdress;
CURL *handle;
static size_t writeInRepBuf(void *ptr, size_t size, size_t nmemb, FILE *out);
static CUniMutex mutResBuf;
static string *resBuf;
}; };
#endif #endif
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
PROJECT(ddnssync) PROJECT(ddnssync)
# ### COMMUN ### # ### COMMUN ###
SET(ddnssync_SRCS ) SET(ddnssync_SRCS main.cpp)
SET(ddnssync_LIBS ddnssbase unicomdb) SET(ddnssync_LIBS ddnssbase unicomdb)
SET(ddnssync_DEFS ${global_unicomctrl_DEFS}) SET(ddnssync_DEFS ${global_unicomctrl_DEFS})
SET(ddnssync_LDFS ) SET(ddnssync_LDFS )
......
#include <iostream>
#include <string>
#include "../ddnssbase/ddnsshttpcl.h"
int main(int argc, char **argv) {
DdnssHttpCl cl;
string res;
cl.get(res,string(argv[1]));
cout << res << endl;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment