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     // modifications from F.V. Fri Nov 30 09:42:06 CET 2007:
00201     // - #ifdef replaced by #if !defined, 
00202     // - added p to get the start position, 
00203     // - revised offsets
00204     // - use-case with a single '/' added (I am not sure this is conform to spec)
00205 // #ifdef _WIN32
00206 #if     !defined(_WIN32)
00207     unsigned int p;
00208     if ((p=uri.find("file:///"))!=std::string::npos)
00209       {
00210         uri = uri.substr(p+7, uri.length()-p-7);
00211       }
00212     else if ((p=uri.find("file://"))!=std::string::npos)
00213       {
00214         uri = uri.substr(p+6, uri.length()-p-6);
00215       }
00216     else if ((p=uri.find("file:/"))!=std::string::npos)
00217       {
00218         uri = uri.substr(p+5, uri.length()-p-5);
00219       }
00220 #endif
00221 
00222     filename=uri;
00223     std::ifstream ifs;
00224     ifs.open(filename.c_str(),std::ios::in);
00225     if(ifs.fail()) {
00226       ifs.close();
00227       return false;
00228     }
00229     else {
00230       ifs.close();
00231       return true;
00232     }
00233   }
00234   return true;
00235 }
00236 
00237 
00238 std::string
00239 WSDLPULL_EXPORT
00240 XmlUtils::acceptSecretKey(const std::string& field)
00241 {
00242   std::cerr<<field<<": ";
00243   char password [50];
00244 #ifndef _WIN32  
00245   tcflag_t oflags;
00246   struct termios term;
00247   tcgetattr(STDIN_FILENO, &term);
00248   oflags = term.c_lflag;
00249   term.c_lflag = oflags & ~(ECHO | ECHOK | ICANON);
00250   term.c_cc[VTIME] = 1;
00251   tcsetattr(STDIN_FILENO, TCSANOW, &term);
00252 
00253   scanf("%s", password);
00254 
00255   term.c_lflag = oflags;
00256   term.c_cc[VTIME] = 0;
00257   tcsetattr(STDIN_FILENO, TCSANOW, &term);
00258 #else
00259   scanf("%s", password);
00260 #endif
00261   return password;
00262 }
00263 
00264 #ifdef _WIN32
00265 void 
00266 XmlUtils::winPost(const std::string uri,const std::string username,
00267                   const std::string password,const std::string data,
00268                   std::string action,char* &results)
00269 {
00270   unsigned long nread;
00271   W3Client w3;
00272   const char* d = data.c_str()   ;       
00273   if(w3.Connect(uri.c_str())){
00274     w3.InitializePostArguments();
00275     w3.setContentType("Content-Type: text/xml; charset=UTF-8\r\n");
00276     w3.setAcceptTypes("Accept: text/xml\r\n");
00277     w3.AddPostArgument(d,data.length());
00278     std::string tmp="SOAPAction: ";
00279     tmp+='"';
00280     tmp+=action;
00281     tmp+='"';
00282     tmp+="\r\n";
00283     w3.setSoapAction(tmp.c_str());
00284                  
00285     if(w3.RequestPost(w3.GetURI())){
00286       unsigned long nread = 0,tot=0;
00287       char buf[1024]="\0";
00288                           
00289       while((nread=w3.Response(reinterpret_cast<unsigned char *>(buf), 1023))){
00290 
00291                  
00292         if (results == 0){
00293           results = (char*)malloc(sizeof(unsigned char) * nread);
00294         }
00295         else{
00296           results = (char*) realloc(results,sizeof(unsigned char) *  (nread + tot));
00297         }
00298         memcpy (results+tot,buf,nread);
00299         tot+=nread;
00300       }
00301     }
00302     //std::cout<<results;
00303   }
00304 }
00305 #endif
00306 
00307 static bool g_bProxy = false;
00308 static std::string g_sProxyHost; //host:port format
00309 static std::string g_sProxyUser;
00310 static std::string g_sProxyPass;
00311 
00312 bool
00313 WSDLPULL_EXPORT
00314 XmlUtils::getProxy ()
00315 {
00316   return g_bProxy;
00317 }
00318 
00319 void
00320 WSDLPULL_EXPORT
00321 XmlUtils::setProxy (const bool bProxy)
00322 {
00323   g_bProxy = bProxy;
00324 }
00325 
00326 std::string
00327 WSDLPULL_EXPORT
00328 XmlUtils::getProxyHost ()
00329 {
00330   return g_sProxyHost;
00331 }
00332 
00333 void
00334 WSDLPULL_EXPORT
00335 XmlUtils::setProxyHost (const std::string& sProxyHost)
00336 {
00337   g_sProxyHost = sProxyHost;
00338 }
00339 
00340 std::string
00341 WSDLPULL_EXPORT
00342 XmlUtils::getProxyUser ()
00343 {
00344   return g_sProxyUser;
00345 }
00346 
00347 void
00348 WSDLPULL_EXPORT
00349 XmlUtils::setProxyUser (const std::string& sProxyUser)
00350 {
00351   g_sProxyUser = sProxyUser;
00352 }
00353 
00354 std::string
00355 WSDLPULL_EXPORT
00356 XmlUtils::getProxyPass ()
00357 {
00358   return g_sProxyPass;
00359 }
00360 
00361 void
00362 WSDLPULL_EXPORT
00363 XmlUtils::setProxyPass (const std::string& sProxyPass)
00364 {
00365   g_sProxyPass = sProxyPass;
00366 }

Generated on Sat May 3 16:29:01 2008 for wsdlpull by  doxygen 1.4.6