XmlUtils.cpp

Go to the documentation of this file.
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 #endif
00039 
00040 #ifdef WITH_CURL
00041 #include <curl/curl.h>
00042 #endif
00043 
00044 #include <time.h>
00045 #include <cstdlib>
00046 #include <cstring>
00047 #include <fstream>
00048 #include <map>
00049 #include "xmlpull/XmlUtils.h"
00050 
00051 const std::string ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
00052 
00053 std::map<std::string,std::string> urlCache_;
00054 //put all I/O and std::string manip  utiliy functions here
00055 
00056 int
00057 XmlUtils::parseInt (std::string s, int radix)
00058 {
00059   int len = s.size ();
00060   int value = 0;
00061   if (s.empty ())
00062     return -1;
00063   for (int i = 0; i < len; i++) {
00064     if (radix == 10) {
00065       if (s[i] <= '9' && s[i] >= '0')
00066         value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');
00067 
00068       else
00069         return value;//return what is parsed till then
00070     }
00071     else if (radix == 16) {
00072       //assumes that the encoding format has arranges the alphabets and numbers in increasing order
00073       if (s[i] <= '9' && s[i] >= 0)
00074         value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');
00075 
00076       else if (s[i] <= 'F' && s[i] >= 'A')
00077         value =
00078           (i ==
00079            0) ? (s[i] - 'A') + 10 : radix * value + (s[i] - 'A') + 10;
00080 
00081       else if (s[i] <= 'f' && s[i] >= 'a')
00082         value =(i ==0) ? (s[i] - 'a') + 10 : radix * value + (s[i] - 'a') + 10;
00083     }
00084   }
00085   return value;
00086 }
00087 
00088 
00089 std::ostream &
00090 XmlUtils::dbsp (std::ostream & str)
00091 {
00092   return str << "  ";
00093 }
00094 
00095 
00096 std::ostream &
00097 XmlUtils::blk (std::ostream & str)
00098 {
00099   return str << std::endl << "*************" << std::endl;
00100 }
00101 
00102 /*
00103 * Fetch a document at the given URI  and store it in a file
00104 */
00105 
00106 bool
00107 WSDLPULL_EXPORT
00108 XmlUtils::fetchUri(std::string uri,
00109                    std::string& filename)
00110 {
00111   if(uri.find("http://")!=std::string::npos || 
00112      uri.find("https://")!=std::string::npos || 
00113      uri.find("ftp://") !=std::string::npos)
00114     {  
00115 
00116       if (urlCache_.find(uri) != urlCache_.end()) {
00117         
00118         filename=urlCache_[uri];
00119         
00120         std::ifstream tfs;
00121         tfs.open(filename.c_str());
00122         if (tfs.fail()) {
00123           urlCache_.erase(uri);
00124         }else {
00125           
00126           return true;
00127         }
00128       }
00129 
00130 #ifndef _WIN32 
00131       filename=uri.substr(uri.rfind('/')+1);
00132       if (filename.empty()) {
00133         //for *nix try to use the name from the url 
00134 #endif
00135       // Generate a random "[8 chars].[3 chars]" filename
00136       srand(time(NULL));
00137       filename.clear();
00138       for (int i = 0; i < 8; i++){
00139         
00140         filename += ALPHA.at(rand()%52);
00141       }
00142       filename.append(".wp-tmp");
00143 #ifndef _WIN32 
00144       }
00145       std::string dir="/tmp/";
00146       filename = dir + filename;
00147 #endif
00148 
00149       urlCache_[uri]=filename;
00150 #ifdef WITH_CURL
00151       CURL * curl;
00152       CURLcode res;
00153       curl=curl_easy_init();
00154       FILE * file;
00155       if(curl){
00156         file=fopen(filename.c_str(),"w");
00157 
00158         if (file == NULL) {
00159           fprintf(stderr, "Can't open file %s: %s\n", filename.c_str(),
00160                   strerror(errno));
00161           exit(-1);
00162         }
00163 
00164         curl_easy_setopt(curl, CURLOPT_URL,uri.c_str());
00165         curl_easy_setopt(curl,CURLOPT_FILE,(void*)file);
00166         curl_easy_setopt(curl,CURLOPT_TIMEOUT,60);
00167         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);      
00168         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
00169 
00170         if (XmlUtils::getProxy()){
00171           curl_easy_setopt(curl,CURLOPT_PROXY,XmlUtils::getProxyHost().c_str());
00172           std::string tmp=XmlUtils::getProxyUser()+":"+XmlUtils::getProxyPass();
00173           curl_easy_setopt(curl,CURLOPT_PROXYUSERPWD,tmp.c_str());
00174         }
00175         res = curl_easy_perform(curl);
00176 
00177         curl_easy_cleanup(curl);
00178         fclose(file);
00179         if(res)
00180           return false;
00181         else 
00182           return true;
00183       }
00184 #elif _WIN32 
00185       std::ofstream ofs(filename.c_str());
00186       unsigned long nread;
00187       W3Client w3;
00188           
00189       if(w3.Connect(uri.c_str())){
00190         if(w3.Request(w3.GetURI())){
00191           unsigned char buf[1024]="\0";
00192           while((nread=w3.Response(buf, 1023))){
00193             buf[nread]='\0';
00194             ofs << buf;
00195           }
00196         }
00197         w3.Close();
00198       }
00199       ofs.close();          
00200       return true;
00201     
00202 #else
00203       return false;
00204 #endif
00205     }
00206   else  { 
00207     /*
00208      * assume its a file on the disk
00209      */
00210     // modifications from F.V. Fri Nov 30 09:42:06 CET 2007:
00211     // - #ifdef replaced by #if !defined, 
00212     // - added p to get the start position, 
00213     // - revised offsets
00214     // - use-case with a single '/' added (I am not sure this is conform to spec)
00215 // #ifdef _WIN32
00216 #if     !defined(_WIN32)
00217     unsigned int p;
00218     if ((p=uri.find("file:///"))!=std::string::npos)
00219       {
00220         uri = uri.substr(p+7, uri.length()-p-7);
00221       }
00222     else if ((p=uri.find("file://"))!=std::string::npos)
00223       {
00224         uri = uri.substr(p+6, uri.length()-p-6);
00225       }
00226     else if ((p=uri.find("file:/"))!=std::string::npos)
00227       {
00228         uri = uri.substr(p+5, uri.length()-p-5);
00229       }
00230 #endif
00231 
00232     filename=uri;
00233     std::ifstream ifs;
00234     ifs.open(filename.c_str(),std::ios::in);
00235     if(ifs.fail()) {
00236       ifs.close();
00237       return false;
00238     }
00239     else {
00240       ifs.close();
00241       return true;
00242     }
00243   }
00244   return true;
00245 }
00246 
00247 
00248 std::string
00249 WSDLPULL_EXPORT
00250 XmlUtils::acceptSecretKey(const std::string& field)
00251 {
00252   std::cerr<<field<<": ";
00253   char password [50];
00254 #ifndef _WIN32  
00255   tcflag_t oflags;
00256   struct termios term;
00257   tcgetattr(STDIN_FILENO, &term);
00258   oflags = term.c_lflag;
00259   term.c_lflag = oflags & ~(ECHO | ECHOK | ICANON);
00260   term.c_cc[VTIME] = 1;
00261   tcsetattr(STDIN_FILENO, TCSANOW, &term);
00262 
00263   scanf("%s", password);
00264 
00265   term.c_lflag = oflags;
00266   term.c_cc[VTIME] = 0;
00267   tcsetattr(STDIN_FILENO, TCSANOW, &term);
00268 #else
00269   scanf("%s", password);
00270 #endif
00271   return password;
00272 }
00273 
00274 #ifdef _WIN32
00275 void 
00276 XmlUtils::winPost(const std::string uri,const std::string username,
00277                   const std::string password,const std::string data,
00278                   std::string action,char* &results)
00279 {
00280   unsigned long nread;
00281   W3Client w3;
00282   const char* d = data.c_str()   ;       
00283   if(w3.Connect(uri.c_str())){
00284     w3.InitializePostArguments();
00285     w3.setContentType("Content-Type: text/xml; charset=UTF-8\r\n");
00286     w3.setAcceptTypes("Accept: text/xml\r\n");
00287     w3.AddPostArgument(d,data.length());
00288     std::string tmp="SOAPAction: ";
00289     tmp+='"';
00290     tmp+=action;
00291     tmp+='"';
00292     tmp+="\r\n";
00293     w3.setSoapAction(tmp.c_str());
00294                  
00295     if(w3.RequestPost(w3.GetURI())){
00296       unsigned long nread = 0,tot=0;
00297       char buf[1024]="\0";
00298                           
00299       while((nread=w3.Response(reinterpret_cast<unsigned char *>(buf), 1023))){
00300 
00301                  
00302         if (results == 0){
00303           results = (char*)malloc(sizeof(unsigned char) * (nread+1));
00304         }
00305         else{
00306           results = (char*) realloc(results,sizeof(unsigned char) *  (nread + tot+1));
00307         }
00308         memcpy (results+tot,buf,nread);
00309         tot+=nread;
00310         results[tot]='\0';
00311       }
00312     }
00313     //std::cout<<results;
00314     w3.Close();
00315   }
00316 }
00317 #endif
00318 
00319 static bool g_bProxy = false;
00320 static std::string g_sProxyHost; //host:port format
00321 static std::string g_sProxyUser;
00322 static std::string g_sProxyPass;
00323 
00324 bool
00325 WSDLPULL_EXPORT
00326 XmlUtils::getProxy ()
00327 {
00328   return g_bProxy;
00329 }
00330 
00331 void
00332 WSDLPULL_EXPORT
00333 XmlUtils::setProxy (const bool bProxy)
00334 {
00335   g_bProxy = bProxy;
00336 }
00337 
00338 std::string
00339 WSDLPULL_EXPORT
00340 XmlUtils::getProxyHost ()
00341 {
00342   return g_sProxyHost;
00343 }
00344 
00345 void
00346 WSDLPULL_EXPORT
00347 XmlUtils::setProxyHost (const std::string& sProxyHost)
00348 {
00349   g_sProxyHost = sProxyHost;
00350 }
00351 
00352 std::string
00353 WSDLPULL_EXPORT
00354 XmlUtils::getProxyUser ()
00355 {
00356   return g_sProxyUser;
00357 }
00358 
00359 void
00360 WSDLPULL_EXPORT
00361 XmlUtils::setProxyUser (const std::string& sProxyUser)
00362 {
00363   g_sProxyUser = sProxyUser;
00364 }
00365 
00366 std::string
00367 WSDLPULL_EXPORT
00368 XmlUtils::getProxyPass ()
00369 {
00370   return g_sProxyPass;
00371 }
00372 
00373 void
00374 WSDLPULL_EXPORT
00375 XmlUtils::setProxyPass (const std::string& sProxyPass)
00376 {
00377   g_sProxyPass = sProxyPass;
00378 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by  doxygen 1.6.2