wsdlpull 1.23
XmlUtils.cpp
Go to the documentation of this file.
1/*
2* wsdlpull- A C++ parser for WSDL (Web services description language)
3* Copyright (C) 2005-2007 Vivek Krishna
4*
5* This library is free software; you can redistribute it and/or
6* modify it under the terms of the GNU Library General Public
7* License as published by the Free Software Foundation; either
8* version 2 of the License, or (at your option) any later version.
9*
10* This library is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13* Library General Public License for more details.
14*
15* You should have received a copy of the GNU Library General Public
16* License along with this library; if not, write to the Free
17* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*
19*
20*/
21
22#ifdef _WIN32
23#include <windows.h>
24#include <winreg.h>
25#include <wininet.h>
26#include <w3c.h>
27#pragma comment(lib, "wininet.lib")
28#endif
29
30#ifdef HAVE_CONFIG_H //
31#include <config.h>
32#endif
33
34#ifndef _WIN32
35#include <termios.h>
36#include <unistd.h>
37#include <errno.h>
38#endif
39
40#ifdef WITH_CURL
41#include <curl/curl.h>
42#endif
43
44#include <time.h>
45#include <cstdlib>
46#include <cstring>
47#include <fstream>
48#include <map>
49#include "xmlpull/XmlUtils.h"
50
51const std::string ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
52
53std::map<std::string,std::string> urlCache_;
54//put all I/O and std::string manip utiliy functions here
55
56int
57XmlUtils::parseInt (std::string s, int radix)
58{
59 int len = s.size ();
60 int value = 0;
61 if (s.empty ())
62 return -1;
63 for (int i = 0; i < len; i++) {
64 if (radix == 10) {
65 if (s[i] <= '9' && s[i] >= '0')
66 value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');
67
68 else
69 return value;//return what is parsed till then
70 }
71 else if (radix == 16) {
72 //assumes that the encoding format has arranges the alphabets and numbers in increasing order
73 if (s[i] <= '9' && s[i] >= 0)
74 value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');
75
76 else if (s[i] <= 'F' && s[i] >= 'A')
77 value =
78 (i ==
79 0) ? (s[i] - 'A') + 10 : radix * value + (s[i] - 'A') + 10;
80
81 else if (s[i] <= 'f' && s[i] >= 'a')
82 value =(i ==0) ? (s[i] - 'a') + 10 : radix * value + (s[i] - 'a') + 10;
83 }
84 }
85 return value;
86}
87
88
89std::ostream &
90XmlUtils::dbsp (std::ostream & str)
91{
92 return str << " ";
93}
94
95
96std::ostream &
97XmlUtils::blk (std::ostream & str)
98{
99 return str << std::endl << "*************" << std::endl;
100}
101
102/*
103* Fetch a document at the given URI and store it in a file
104*/
105
106bool
108XmlUtils::fetchUri(std::string uri,
109 std::string& filename)
110{
111 if(uri.find("http://")!=std::string::npos ||
112 uri.find("https://")!=std::string::npos ||
113 uri.find("ftp://") !=std::string::npos)
114 {
115
116 if (urlCache_.find(uri) != urlCache_.end()) {
117
118 filename=urlCache_[uri];
119
120 std::ifstream tfs;
121 tfs.open(filename.c_str());
122 if (tfs.fail()) {
123 urlCache_.erase(uri);
124 }else {
125
126 return true;
127 }
128 }
129
130#ifndef _WIN32
131 filename=uri.substr(uri.rfind('/')+1);
132 if (filename.empty()) {
133 //for *nix try to use the name from the url
134#endif
135 // Generate a random "[8 chars].[3 chars]" filename
136 srand(time(NULL));
137 filename.clear();
138 for (int i = 0; i < 8; i++){
139
140 filename += ALPHA.at(rand()%52);
141 }
142 filename.append(".wp-tmp");
143#ifndef _WIN32
144 }
145 std::string dir="/tmp/";
146 filename = dir + filename;
147#endif
148
149 urlCache_[uri]=filename;
150#ifdef WITH_CURL
151 CURL * curl;
152 CURLcode res;
153 curl=curl_easy_init();
154 FILE * file;
155 if(curl){
156 file=fopen(filename.c_str(),"w");
157
158 if (file == NULL) {
159 fprintf(stderr, "Can't open file %s: %s\n", filename.c_str(),
160 strerror(errno));
161 exit(-1);
162 }
163
164 curl_easy_setopt(curl, CURLOPT_URL,uri.c_str());
165 curl_easy_setopt(curl,CURLOPT_FILE,(void*)file);
166 curl_easy_setopt(curl,CURLOPT_TIMEOUT,60);
167 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
168 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
169
170 if (XmlUtils::getProxy()){
171 curl_easy_setopt(curl,CURLOPT_PROXY,XmlUtils::getProxyHost().c_str());
172 std::string tmp=XmlUtils::getProxyUser()+":"+XmlUtils::getProxyPass();
173 curl_easy_setopt(curl,CURLOPT_PROXYUSERPWD,tmp.c_str());
174 }
175 res = curl_easy_perform(curl);
176
177 curl_easy_cleanup(curl);
178 fclose(file);
179 if(res)
180 return false;
181 else
182 return true;
183 }
184#elif _WIN32
185 std::ofstream ofs(filename.c_str());
186 unsigned long nread;
187 W3Client w3;
188
189 if(w3.Connect(uri.c_str())){
190 if(w3.Request(w3.GetURI())){
191 unsigned char buf[1024]="\0";
192 while((nread=w3.Response(buf, 1023))){
193 buf[nread]='\0';
194 ofs << buf;
195 }
196 }
197 w3.Close();
198 }
199 ofs.close();
200 return true;
201
202#else
203 return false;
204#endif
205 }
206 else {
207 /*
208 * assume its a file on the disk
209 */
210 // modifications from F.V. Fri Nov 30 09:42:06 CET 2007:
211 // - #ifdef replaced by #if !defined,
212 // - added p to get the start position,
213 // - revised offsets
214 // - use-case with a single '/' added (I am not sure this is conform to spec)
215// #ifdef _WIN32
216#if !defined(_WIN32)
217 unsigned int p;
218 if ((p=uri.find("file:///"))!=std::string::npos)
219 {
220 uri = uri.substr(p+7, uri.length()-p-7);
221 }
222 else if ((p=uri.find("file://"))!=std::string::npos)
223 {
224 uri = uri.substr(p+6, uri.length()-p-6);
225 }
226 else if ((p=uri.find("file:/"))!=std::string::npos)
227 {
228 uri = uri.substr(p+5, uri.length()-p-5);
229 }
230#endif
231
232 filename=uri;
233 std::ifstream ifs;
234 ifs.open(filename.c_str(),std::ios::in);
235 if(ifs.fail()) {
236 ifs.close();
237 return false;
238 }
239 else {
240 ifs.close();
241 return true;
242 }
243 }
244 return true;
245}
246
247
248std::string
250XmlUtils::acceptSecretKey(const std::string& field)
251{
252 std::cerr<<field<<": ";
253 char password [50];
254#ifndef _WIN32
255 tcflag_t oflags;
256 struct termios term;
257 tcgetattr(STDIN_FILENO, &term);
258 oflags = term.c_lflag;
259 term.c_lflag = oflags & ~(ECHO | ECHOK | ICANON);
260 term.c_cc[VTIME] = 1;
261 tcsetattr(STDIN_FILENO, TCSANOW, &term);
262
263 scanf("%s", password);
264
265 term.c_lflag = oflags;
266 term.c_cc[VTIME] = 0;
267 tcsetattr(STDIN_FILENO, TCSANOW, &term);
268#else
269 scanf("%s", password);
270#endif
271 return password;
272}
273
274#ifdef _WIN32
275void
276XmlUtils::winPost(const std::string uri,const std::string username,
277 const std::string password,const std::string data,
278 std::string action,char* &results)
279{
280 unsigned long nread;
281 W3Client w3;
282 const char* d = data.c_str() ;
283 if(w3.Connect(uri.c_str())){
284 w3.InitializePostArguments();
285 w3.setContentType("Content-Type: text/xml; charset=UTF-8\r\n");
286 w3.setAcceptTypes("Accept: text/xml\r\n");
287 w3.AddPostArgument(d,data.length());
288 std::string tmp="SOAPAction: ";
289 tmp+='"';
290 tmp+=action;
291 tmp+='"';
292 tmp+="\r\n";
293 w3.setSoapAction(tmp.c_str());
294
295 if(w3.RequestPost(w3.GetURI())){
296 unsigned long nread = 0,tot=0;
297 char buf[1024]="\0";
298
299 while((nread=w3.Response(reinterpret_cast<unsigned char *>(buf), 1023))){
300
301
302 if (results == 0){
303 results = (char*)malloc(sizeof(unsigned char) * (nread+1));
304 }
305 else{
306 results = (char*) realloc(results,sizeof(unsigned char) * (nread + tot+1));
307 }
308 memcpy (results+tot,buf,nread);
309 tot+=nread;
310 results[tot]='\0';
311 }
312 }
313 //std::cout<<results;
314 w3.Close();
315 }
316}
317#endif
318
319static bool g_bProxy = false;
320static std::string g_sProxyHost; //host:port format
321static std::string g_sProxyUser;
322static std::string g_sProxyPass;
323
324bool
327{
328 return g_bProxy;
329}
330
331void
333XmlUtils::setProxy (const bool bProxy)
334{
335 g_bProxy = bProxy;
336}
337
338std::string
341{
342 return g_sProxyHost;
343}
344
345void
347XmlUtils::setProxyHost (const std::string& sProxyHost)
348{
349 g_sProxyHost = sProxyHost;
350}
351
352std::string
355{
356 return g_sProxyUser;
357}
358
359void
361XmlUtils::setProxyUser (const std::string& sProxyUser)
362{
363 g_sProxyUser = sProxyUser;
364}
365
366std::string
369{
370 return g_sProxyPass;
371}
372
373void
375XmlUtils::setProxyPass (const std::string& sProxyPass)
376{
377 g_sProxyPass = sProxyPass;
378}
const std::string ALPHA
Definition: XmlUtils.cpp:51
static std::string g_sProxyUser
Definition: XmlUtils.cpp:321
static std::string g_sProxyHost
Definition: XmlUtils.cpp:320
static std::string g_sProxyPass
Definition: XmlUtils.cpp:322
std::map< std::string, std::string > urlCache_
Definition: XmlUtils.cpp:53
static bool g_bProxy
Definition: XmlUtils.cpp:319
std::ostream & blk(std::ostream &str)
Definition: XmlUtils.cpp:97
bool WSDLPULL_EXPORT getProxy()
Definition: XmlUtils.cpp:326
std::string WSDLPULL_EXPORT getProxyHost()
Definition: XmlUtils.cpp:340
int parseInt(std::string s, int radix=10)
Definition: XmlUtils.cpp:57
void WSDLPULL_EXPORT setProxyPass(const std::string &sProxyPass)
Definition: XmlUtils.cpp:375
bool WSDLPULL_EXPORT fetchUri(std::string uri, std::string &path)
Definition: XmlUtils.cpp:108
std::string WSDLPULL_EXPORT acceptSecretKey(const std::string &field)
Definition: XmlUtils.cpp:250
void WSDLPULL_EXPORT setProxy(const bool bProxy)
Definition: XmlUtils.cpp:333
std::string WSDLPULL_EXPORT getProxyUser()
Definition: XmlUtils.cpp:354
void WSDLPULL_EXPORT setProxyUser(const std::string &sProxyUser)
Definition: XmlUtils.cpp:361
std::string WSDLPULL_EXPORT getProxyPass()
Definition: XmlUtils.cpp:368
void WSDLPULL_EXPORT setProxyHost(const std::string &sProxyHost)
Definition: XmlUtils.cpp:347
std::ostream & dbsp(std::ostream &str)
Definition: XmlUtils.cpp:90
#define WSDLPULL_EXPORT