Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

dsURL.h

Go to the documentation of this file.
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 /* http responses:
00034  100 Continue             HTTP/1.1 only
00035  101 Switching Protocols  HTTP/1.1 only
00036  200 OK
00037  201 Created
00038  202 Accepted
00039  203 Non-Authoritative Information
00040  204 No Content
00041  205 Reset Content
00042  206 Partial Content
00043  300 Multiple Choices
00044  301 Moved Permanently
00045  302 Moved Temporarily
00046  303 See Other
00047  304 Not Modified
00048  305 Use Proxy
00049  400 Bad Request
00050  401 Unauthorized
00051  402 Payment Required
00052  403 Forbidden
00053  404 Not Found
00054  405 Method Not Allowed
00055  406 Not Acceptable
00056  407 Proxy Authentication Required
00057  408 Request Time-out
00058  409 Conflict
00059  410 Gone
00060  411 Length Required
00061  412 Precondition Failed
00062  413 Request Entity Too Large
00063  414 Request-URI Too Large
00064  415 Unsupported Media Type
00065  500 Internal Server Error
00066  501 Not Implemented
00067  502 Bad Gateway
00068  503 Service Unavailable
00069  504 Gateway Time-out
00070  505 HTTP Version not supported
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   /* Request parameters */
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 /* http://www.test.ru:8080/images/banner.gif?........ */
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

Generated on Mon May 16 18:26:58 2005 for libdms4 by doxygen1.3-rc2