00001 #ifndef dsURL_h
00002 #define dsURL_h
00003
00004 #ifdef HAVE_CONFIG_H
00005 # include <config.h>
00006 #endif
00007
00008 #include <stdio.h>
00009 #include <stdarg.h>
00010
00011 #ifdef HAVE_SYS_TIME_H
00012 # include <sys/time.h>
00013 #endif
00014
00015 #include <iostream>
00016 #include <fstream>
00017
00018 #include <dsSmartException.h>
00019 #include <dsSocket.h>
00020 #include <dsutil.h>
00021 #include <dsStrstream.h>
00022 #include <dsSOAP.h>
00023 #include <dsMime.h>
00024
00025 #ifdef HAVE_LIBSSL
00026 # include <dsSSLSocket.h>
00027 #endif
00028
00029
00030
00031 DECLARE_EXCEPTION(dsURL);
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 #define URL_RAW_OUTPUT 1
00074
00081 #define hex(ch) ( (char) ( ((ch) >= 10) ? ((ch) - 10) + 'A' : ((ch) + '0') ) )
00082
00083 #ifndef is_alnum
00084 #define is_alnum(ch) ( ((ch) >= '0' && (ch) <= '9') || \
00085 ((ch) >= 'A' && (ch) <= 'Z') || \
00086 ((ch) >= 'a' && (ch) <= 'z') )
00087 #endif
00088
00089
00090 inline char *urlencode(char *dest, char *src, int len)
00091 {
00092 for(int i = 0; i < len; ++i )
00093 {
00094 if( is_alnum(src[i]))
00095 { *dest++ = src[i];
00096 }
00097 else if( src[i] == ' ')
00098 { *dest++ = '+';
00099 }
00100 else
00101 { *dest++ = '%';
00102 *dest++ = hex(src[i] >> 4);
00103 *dest++ = hex(src[i] & 0xF);
00104 }
00105 }
00106
00107 *dest = 0;
00108
00109 return dest;
00110 }
00111
00112 inline std::ostream &urlencode(std::ostream& dest, char *src, int len)
00113 {
00114
00115 for(int i = 0; i < len; ++i )
00116 {
00117 if ( is_alnum(src[i]) )
00118 { dest << (char) src[i];
00119 }
00120 else if( src[i] == ' ')
00121 { dest << '+';
00122
00123 }
00124 else
00125 {
00126 dest << '%';
00127 dest << hex(src[i] >> 4);
00128 dest << hex(src[i] & 0xF);
00129 }
00130 }
00131
00132 return dest;
00133 }
00134
00135 inline std::ostream &urlencode(std::ostream& dest, char *src)
00136 {
00137 return urlencode(dest, src, strlen(src) );
00138 }
00139
00140 class dsURL
00141 {
00142
00143 public:
00144 enum Proto{ ftp, http, https, file, mailto, oracle, postgres, sybase, iso8583, spdh, none = -1 };
00145 enum HttpMethods{ GET, POST, SOAP, unknown };
00146 enum dsURLFlags{ urlNO_FLAGS = 0, urlRAW_OUT = 0x1, urlSHORT_READ = 0x2 };
00147
00148 protected:
00149
00150 int _flags;
00151
00152 char *_user_str;
00153
00154 char *_host;
00155 char *_location;
00156 char *_cgidata;
00157 char *_auth_user;
00158 char *_auth_pw;
00159 int _port;
00160 int _retcode;
00161 Proto _protocol;
00162
00163
00164
00165 private:
00166
00167 void get_build_header(std::ostream &req, HttpMethods meth);
00168 void post_build_header(std::ostream &req, HttpMethods meth, int contLen);
00169 void soap_build_header(std::ostream &req, HttpMethods meth, int contLen, char *SOAPAction);
00170
00171 void build_location(std::ostream &req, char *method);
00172
00173 protected:
00174 virtual int read_answer(dsSocket *ms, std::ostream *ostr);
00175 virtual void va_build_header(std::ostream &req, HttpMethods meth, int contLen, ... );
00176
00177 public:
00178 dsURL(int flags = urlNO_FLAGS );
00179 dsURL(const char *str, int flags = urlNO_FLAGS );
00180 virtual ~dsURL();
00181
00182
00183 void parse(const char *url);
00184
00185 int get(std::ostream &ostr);
00186
00187 int post(std::ostream &ostr);
00188 int post(std::ostream &ostr, dsStrstream& data);
00189 int post(std::ostream &ostr, char *data, int pcount);
00190
00191 int soap(std::ostream &ostr, dsStrstream& data, char *SOAPAction);
00192 int soap(std::ostream &ostr, char *data, int pcount, char *SOAPAction);
00193
00194 void dump(std::ostream& os);
00195
00196 const char *host() { return _host; }
00197 const char *location() { return _location; }
00198 const char *cgidata() { return _cgidata; }
00199 const char *auth_user(){ return _auth_user; }
00200 const char *auth_pw() { return _auth_pw; }
00201 const int port() { return _port; }
00202 const Proto protocol() { return _protocol; }
00203 const char *protoName();
00204
00205 const char *userStr() { return _user_str; }
00206
00207 };
00208
00209 #endif