src/xmlpull/XmlUtils.cpp

00001 /* 
00002 * wsdlpull- A C++ parser  for WSDL  (Web services description language)
00003 * Copyright (C) 2005-2007 Vivek Krishna
00004 *
00005 * This library is free software; you can redistribute it and/or
00006 * modify it under the terms of the GNU Library General Public
00007 * License as published by the Free Software Foundation; either
00008 * version 2 of the License, or (at your option) any later version.
00009 *
00010 * This library is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 * Library General Public License for more details.
00014 *
00015 * You should have received a copy of the GNU Library General Public
00016 * License along with this library; if not, write to the Free
00017 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 #ifdef WITH_CURL
00039 #include <curl/curl.h>
00040 #endif
00041 
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 //put all I/O and std::string manip  utiliy functions here
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         throw std::exception ();
00068     }
00069     else if (radix == 16) {
00070       //assumes that the encoding format has arranges the alphabets and numbers in increasing order
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 * Fetch a document at the given URI  and store it in a file
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         return true;
00118       }
00119 
00120 #ifndef _WIN32 
00121       filename=uri.substr(uri.rfind('/')+1);
00122       if (filename.empty()) {
00123         //for *nix try to use the name from the url 
00124 #endif
00125       // Generate a random "[8 chars].[3 chars]" filename
00126       srand(time(NULL));
00127       filename.clear();
00128       for (int i = 0; i < 8; i++){
00129         
00130         filename += ALPHA.at(rand()%52);
00131       }
00132       filename.append(".wp-tmp");
00133 #ifndef _WIN32 
00134       }
00135       std::string dir="/tmp/";
00136       filename = dir + filename;
00137 #endif
00138 
00139       urlCache_[uri]=filename;
00140 #ifdef WITH_CURL
00141       CURL * curl;
00142       CURLcode res;
00143       curl=curl_easy_init();
00144       FILE * file;
00145       if(curl){
00146         file=fopen(filename.c_str(),"w");
00147 
00148         if (file == NULL) {
00149           fprintf(stderr, "Can't open file %s: %s\n", filename.c_str(),
00150                   strerror(errno));
00151           exit(-1);
00152         }
00153 
00154         curl_easy_setopt(curl, CURLOPT_URL,uri.c_str());
00155         curl_easy_setopt(curl,CURLOPT_FILE,(void*)file);
00156         curl_easy_setopt(curl,CURLOPT_TIMEOUT,60);
00157         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);      
00158         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
00159 
00160         if (XmlUtils::getProxy()){
00161           curl_easy_setopt(curl,CURLOPT_PROXY,XmlUtils::getProxyHost().c_str());
00162           std::string tmp=XmlUtils::getProxyUser()+":"+XmlUtils::getProxyPass();
00163           curl_easy_setopt(curl,CURLOPT_PROXYUSERPWD,tmp.c_str());
00164         }
00165         res = curl_easy_perform(curl);
00166 
00167         curl_easy_cleanup(curl);
00168         fclose(file);
00169         if(res)
00170           return false;
00171         else 
00172           return true;
00173       }
00174 #elif _WIN32 
00175       std::ofstream ofs(filename.c_str());
00176       unsigned long nread;
00177       W3Client w3;
00178           
00179       if(w3.Connect(uri.c_str())){
00180         if(w3.Request(w3.GetURI())){
00181           unsigned char buf[1024]="\0";
00182           while((nread=w3.Response(buf, 1023))){
00183             buf[nread]='\0';
00184             ofs << buf;
00185           }
00186         }
00187         w3.Close();
00188       }
00189       ofs.close();          
00190       return true;
00191     
00192 #else
00193       return false;
00194 #endif
00195     }
00196   else  { 
00197     /*
00198      * assume its a file on the disk
00199      */
00200 #ifdef _WIN32
00201     if (uri.find("file:///")!=std::string::npos)
00202       {
00203         uri = uri.substr(8, uri.length()-8);
00204       }
00205     else if (uri.find("file://")!=std::string::npos)
00206       {
00207         uri = uri.substr(8, uri.length()-7);
00208       }
00209 #endif
00210 
00211     filename=uri;
00212     std::ifstream ifs;
00213     ifs.open(filename.c_str(),std::ios::in);
00214     if(ifs.fail()) {
00215       ifs.close();
00216       return false;
00217     }
00218     else {
00219       ifs.close();
00220       return true;
00221     }
00222   }
00223   return true;
00224 }
00225 
00226 
00227 std::string
00228 WSDLPULL_EXPORT
00229 XmlUtils::acceptSecretKey(const std::string& field)
00230 {
00231   std::cerr<<field<<": ";
00232   char password [50];
00233 #ifndef _WIN32  
00234   tcflag_t oflags;
00235   struct termios term;
00236   tcgetattr(STDIN_FILENO, &term);
00237   oflags = term.c_lflag;
00238   term.c_lflag = oflags & ~(ECHO | ECHOK | ICANON);
00239   term.c_cc[VTIME] = 1;
00240   tcsetattr(STDIN_FILENO, TCSANOW, &term);
00241 
00242   scanf("%s", password);
00243 
00244   term.c_lflag = oflags;
00245   term.c_cc[VTIME] = 0;
00246   tcsetattr(STDIN_FILENO, TCSANOW, &term);
00247 #else
00248   scanf("%s", password);
00249 #endif
00250   return password;
00251 }
00252 
00253 #ifdef _WIN32
00254 void 
00255 XmlUtils::winPost(const std::string uri,const std::string username,
00256                   const std::string password,const std::string data,
00257                   std::string action,char* &results)
00258 {
00259   unsigned long nread;
00260   W3Client w3;
00261   const char* d = data.c_str()   ;       
00262   if(w3.Connect(uri.c_str())){
00263     w3.InitializePostArguments();
00264     w3.setContentType("Content-Type: text/xml; charset=UTF-8\r\n");
00265     w3.setAcceptTypes("Accept: text/xml\r\n");
00266     w3.AddPostArgument(d,data.length());
00267     std::string tmp="SOAPAction: ";
00268     tmp+='"';
00269     tmp+=action;
00270     tmp+='"';
00271     tmp+="\r\n";
00272     w3.setSoapAction(tmp.c_str());
00273                  
00274     if(w3.RequestPost(w3.GetURI())){
00275       unsigned long nread = 0,tot=0;
00276       char buf[1024]="\0";
00277                           
00278       while((nread=w3.Response(reinterpret_cast<unsigned char *>(buf), 1023))){
00279 
00280                  
00281         if (results == 0){
00282           results = (char*)malloc(sizeof(unsigned char) * nread);
00283         }
00284         else{
00285           results = (char*) realloc(results,sizeof(unsigned char) *  (nread + tot));
00286         }
00287         memcpy (results+tot,buf,nread);
00288         tot+=nread;
00289       }
00290     }
00291     //std::cout<<results;
00292   }
00293 }
00294 #endif
00295 
00296 static bool g_bProxy = false;
00297 static std::string g_sProxyHost; //host:port format
00298 static std::string g_sProxyUser;
00299 static std::string g_sProxyPass;
00300 
00301 bool
00302 WSDLPULL_EXPORT
00303 XmlUtils::getProxy ()
00304 {
00305   return g_bProxy;
00306 }
00307 
00308 void
00309 WSDLPULL_EXPORT
00310 XmlUtils::setProxy (const bool bProxy)
00311 {
00312   g_bProxy = bProxy;
00313 }
00314 
00315 std::string
00316 WSDLPULL_EXPORT
00317 XmlUtils::getProxyHost ()
00318 {
00319   return g_sProxyHost;
00320 }
00321 
00322 void
00323 WSDLPULL_EXPORT
00324 XmlUtils::setProxyHost (const std::string& sProxyHost)
00325 {
00326   g_sProxyHost = sProxyHost;
00327 }
00328 
00329 std::string
00330 WSDLPULL_EXPORT
00331 XmlUtils::getProxyUser ()
00332 {
00333   return g_sProxyUser;
00334 }
00335 
00336 void
00337 WSDLPULL_EXPORT
00338 XmlUtils::setProxyUser (const std::string& sProxyUser)
00339 {
00340   g_sProxyUser = sProxyUser;
00341 }
00342 
00343 std::string
00344 WSDLPULL_EXPORT
00345 XmlUtils::getProxyPass ()
00346 {
00347   return g_sProxyPass;
00348 }
00349 
00350 void
00351 WSDLPULL_EXPORT
00352 XmlUtils::setProxyPass (const std::string& sProxyPass)
00353 {
00354   g_sProxyPass = sProxyPass;
00355 }

Generated on Sun Aug 5 20:01:35 2007 for wsdlpull by  doxygen 1.4.6