00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef _WIN32
00023 #include <windows.h>
00024 #include <winreg.h>
00025 #include <wininet.h>
00026 #include <w3c.h>
00027 #pragma comment(lib, "wininet.lib")
00028 #endif
00029
00030 #ifdef HAVE_CONFIG_H //
00031 #include <config.h>
00032 #endif
00033
00034 #ifndef _WIN32
00035 #include <termios.h>
00036 #include <unistd.h>
00037 #include <errno.h>
00038 #endif
00039
00040 #ifdef WITH_CURL
00041 #include <curl/curl.h>
00042 #endif
00043
00044 #include <time.h>
00045 #include <fstream>
00046 #include <map>
00047 #include "xmlpull/XmlUtils.h"
00048
00049 const std::string ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
00050
00051 std::map<std::string,std::string> urlCache_;
00052
00053
00054 int
00055 XmlUtils::parseInt (std::string s, int radix)
00056 {
00057 int len = s.size ();
00058 int value = 0;
00059 if (s.empty ())
00060 return -1;
00061 for (int i = 0; i < len; i++) {
00062 if (radix == 10) {
00063 if (s[i] <= '9' && s[i] >= '0')
00064 value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');
00065
00066 else
00067 return value;
00068 }
00069 else if (radix == 16) {
00070
00071 if (s[i] <= '9' && s[i] >= 0)
00072 value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');
00073
00074 else if (s[i] <= 'F' && s[i] >= 'A')
00075 value =
00076 (i ==
00077 0) ? (s[i] - 'A') + 10 : radix * value + (s[i] - 'A') + 10;
00078
00079 else if (s[i] <= 'f' && s[i] >= 'a')
00080 value =(i ==0) ? (s[i] - 'a') + 10 : radix * value + (s[i] - 'a') + 10;
00081 }
00082 }
00083 return value;
00084 }
00085
00086
00087 std::ostream &
00088 XmlUtils::dbsp (std::ostream & str)
00089 {
00090 return str << " ";
00091 }
00092
00093
00094 std::ostream &
00095 XmlUtils::blk (std::ostream & str)
00096 {
00097 return str << std::endl << "*************" << std::endl;
00098 }
00099
00100
00101
00102
00103
00104 bool
00105 WSDLPULL_EXPORT
00106 XmlUtils::fetchUri(std::string uri,
00107 std::string& filename)
00108 {
00109 if(uri.find("http://")!=std::string::npos ||
00110 uri.find("https://")!=std::string::npos ||
00111 uri.find("ftp://") !=std::string::npos)
00112 {
00113
00114 if (urlCache_.find(uri) != urlCache_.end()) {
00115
00116 filename=urlCache_[uri];
00117
00118 std::ifstream tfs;
00119 tfs.open(filename.c_str());
00120 if (tfs.fail()) {
00121 urlCache_.erase(uri);
00122 }else {
00123
00124 return true;
00125 }
00126 }
00127
00128 #ifndef _WIN32
00129 filename=uri.substr(uri.rfind('/')+1);
00130 if (filename.empty()) {
00131
00132 #endif
00133
00134 srand(time(NULL));
00135 filename.clear();
00136 for (int i = 0; i < 8; i++){
00137
00138 filename += ALPHA.at(rand()%52);
00139 }
00140 filename.append(".wp-tmp");
00141 #ifndef _WIN32
00142 }
00143 std::string dir="/tmp/";
00144 filename = dir + filename;
00145 #endif
00146
00147 urlCache_[uri]=filename;
00148 #ifdef WITH_CURL
00149 CURL * curl;
00150 CURLcode res;
00151 curl=curl_easy_init();
00152 FILE * file;
00153 if(curl){
00154 file=fopen(filename.c_str(),"w");
00155
00156 if (file == NULL) {
00157 fprintf(stderr, "Can't open file %s: %s\n", filename.c_str(),
00158 strerror(errno));
00159 exit(-1);
00160 }
00161
00162 curl_easy_setopt(curl, CURLOPT_URL,uri.c_str());
00163 curl_easy_setopt(curl,CURLOPT_FILE,(void*)file);
00164 curl_easy_setopt(curl,CURLOPT_TIMEOUT,60);
00165 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
00166 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
00167
00168 if (XmlUtils::getProxy()){
00169 curl_easy_setopt(curl,CURLOPT_PROXY,XmlUtils::getProxyHost().c_str());
00170 std::string tmp=XmlUtils::getProxyUser()+":"+XmlUtils::getProxyPass();
00171 curl_easy_setopt(curl,CURLOPT_PROXYUSERPWD,tmp.c_str());
00172 }
00173 res = curl_easy_perform(curl);
00174
00175 curl_easy_cleanup(curl);
00176 fclose(file);
00177 if(res)
00178 return false;
00179 else
00180 return true;
00181 }
00182 #elif _WIN32
00183 std::ofstream ofs(filename.c_str());
00184 unsigned long nread;
00185 W3Client w3;
00186
00187 if(w3.Connect(uri.c_str())){
00188 if(w3.Request(w3.GetURI())){
00189 unsigned char buf[1024]="\0";
00190 while((nread=w3.Response(buf, 1023))){
00191 buf[nread]='\0';
00192 ofs << buf;
00193 }
00194 }
00195 w3.Close();
00196 }
00197 ofs.close();
00198 return true;
00199
00200 #else
00201 return false;
00202 #endif
00203 }
00204 else {
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 #if !defined(_WIN32)
00215 unsigned int p;
00216 if ((p=uri.find("file:///"))!=std::string::npos)
00217 {
00218 uri = uri.substr(p+7, uri.length()-p-7);
00219 }
00220 else if ((p=uri.find("file://"))!=std::string::npos)
00221 {
00222 uri = uri.substr(p+6, uri.length()-p-6);
00223 }
00224 else if ((p=uri.find("file:/"))!=std::string::npos)
00225 {
00226 uri = uri.substr(p+5, uri.length()-p-5);
00227 }
00228 #endif
00229
00230 filename=uri;
00231 std::ifstream ifs;
00232 ifs.open(filename.c_str(),std::ios::in);
00233 if(ifs.fail()) {
00234 ifs.close();
00235 return false;
00236 }
00237 else {
00238 ifs.close();
00239 return true;
00240 }
00241 }
00242 return true;
00243 }
00244
00245
00246 std::string
00247 WSDLPULL_EXPORT
00248 XmlUtils::acceptSecretKey(const std::string& field)
00249 {
00250 std::cerr<<field<<": ";
00251 char password [50];
00252 #ifndef _WIN32
00253 tcflag_t oflags;
00254 struct termios term;
00255 tcgetattr(STDIN_FILENO, &term);
00256 oflags = term.c_lflag;
00257 term.c_lflag = oflags & ~(ECHO | ECHOK | ICANON);
00258 term.c_cc[VTIME] = 1;
00259 tcsetattr(STDIN_FILENO, TCSANOW, &term);
00260
00261 scanf("%s", password);
00262
00263 term.c_lflag = oflags;
00264 term.c_cc[VTIME] = 0;
00265 tcsetattr(STDIN_FILENO, TCSANOW, &term);
00266 #else
00267 scanf("%s", password);
00268 #endif
00269 return password;
00270 }
00271
00272 #ifdef _WIN32
00273 void
00274 XmlUtils::winPost(const std::string uri,const std::string username,
00275 const std::string password,const std::string data,
00276 std::string action,char* &results)
00277 {
00278 unsigned long nread;
00279 W3Client w3;
00280 const char* d = data.c_str() ;
00281 if(w3.Connect(uri.c_str())){
00282 w3.InitializePostArguments();
00283 w3.setContentType("Content-Type: text/xml; charset=UTF-8\r\n");
00284 w3.setAcceptTypes("Accept: text/xml\r\n");
00285 w3.AddPostArgument(d,data.length());
00286 std::string tmp="SOAPAction: ";
00287 tmp+='"';
00288 tmp+=action;
00289 tmp+='"';
00290 tmp+="\r\n";
00291 w3.setSoapAction(tmp.c_str());
00292
00293 if(w3.RequestPost(w3.GetURI())){
00294 unsigned long nread = 0,tot=0;
00295 char buf[1024]="\0";
00296
00297 while((nread=w3.Response(reinterpret_cast<unsigned char *>(buf), 1023))){
00298
00299
00300 if (results == 0){
00301 results = (char*)malloc(sizeof(unsigned char) * (nread+1));
00302 }
00303 else{
00304 results = (char*) realloc(results,sizeof(unsigned char) * (nread + tot+1));
00305 }
00306 memcpy (results+tot,buf,nread);
00307 tot+=nread;
00308 results[tot]='\0';
00309 }
00310 }
00311
00312 w3.Close();
00313 }
00314 }
00315 #endif
00316
00317 static bool g_bProxy = false;
00318 static std::string g_sProxyHost;
00319 static std::string g_sProxyUser;
00320 static std::string g_sProxyPass;
00321
00322 bool
00323 WSDLPULL_EXPORT
00324 XmlUtils::getProxy ()
00325 {
00326 return g_bProxy;
00327 }
00328
00329 void
00330 WSDLPULL_EXPORT
00331 XmlUtils::setProxy (const bool bProxy)
00332 {
00333 g_bProxy = bProxy;
00334 }
00335
00336 std::string
00337 WSDLPULL_EXPORT
00338 XmlUtils::getProxyHost ()
00339 {
00340 return g_sProxyHost;
00341 }
00342
00343 void
00344 WSDLPULL_EXPORT
00345 XmlUtils::setProxyHost (const std::string& sProxyHost)
00346 {
00347 g_sProxyHost = sProxyHost;
00348 }
00349
00350 std::string
00351 WSDLPULL_EXPORT
00352 XmlUtils::getProxyUser ()
00353 {
00354 return g_sProxyUser;
00355 }
00356
00357 void
00358 WSDLPULL_EXPORT
00359 XmlUtils::setProxyUser (const std::string& sProxyUser)
00360 {
00361 g_sProxyUser = sProxyUser;
00362 }
00363
00364 std::string
00365 WSDLPULL_EXPORT
00366 XmlUtils::getProxyPass ()
00367 {
00368 return g_sProxyPass;
00369 }
00370
00371 void
00372 WSDLPULL_EXPORT
00373 XmlUtils::setProxyPass (const std::string& sProxyPass)
00374 {
00375 g_sProxyPass = sProxyPass;
00376 }