GNU CommonC++
|
00001 // Copyright (C) 2001-2005 Open Source Telecom Corporation. 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // Common C++. If you copy code from other releases into a copy of GNU 00028 // Common C++, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU Common C++, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 00036 // 00037 00043 #ifndef CCXX_URL_H_ 00044 #define CCXX_URL_H_ 00045 00046 #ifndef CCXX_CONFIG_H_ 00047 #include <cc++/config.h> 00048 #endif 00049 00050 #ifndef CCXX_SOCKET_H_ 00051 #include <cc++/socket.h> 00052 #endif 00053 00054 #ifndef CCXX_MIME_H_ 00055 #include <cc++/mime.h> 00056 #endif 00057 00058 #ifdef CCXX_NAMESPACES 00059 namespace ost { 00060 #endif 00061 00068 class __EXPORT URLStream : public TCPStream 00069 { 00070 public: 00074 typedef enum { 00075 errSuccess = 0, 00076 errUnreachable, 00077 errMissing, 00078 errDenied, 00079 errInvalid, 00080 errForbidden, 00081 errUnauthorized, 00082 errRelocated, 00083 errFailure, 00084 errTimeout, 00085 errInterface 00086 } Error; 00087 00091 typedef enum { 00092 authAnonymous = 0, 00093 authBasic 00094 } Authentication; 00095 00099 typedef enum { 00100 encodingBinary = 0, 00101 encodingChunked 00102 } Encoding; 00103 00107 typedef enum { 00108 methodHttpGet, 00109 methodHttpPut, 00110 methodHttpPost, 00111 methodHttpPostMultipart, 00112 methodFtpGet, 00113 methodFtpPut, 00114 methodFileGet, 00115 methodFilePut 00116 } Method; 00117 00121 typedef enum { 00122 protocolHttp1_0, 00123 protocolHttp1_1 00124 } Protocol; 00125 00126 private: 00127 const char *agent, *referer, *cookie, *pragma, *user, *password; 00128 const char *proxyUser, *proxyPasswd; 00129 const char *localif; 00130 IPV4Host proxyHost; 00131 #ifdef CCXX_IPV6 00132 IPV6Host v6proxyHost; 00133 #endif 00134 tpport_t proxyPort; 00135 Method urlmethod; 00136 Encoding encoding; 00137 Protocol protocol; 00138 Authentication auth; 00139 Authentication proxyAuth; 00140 timeout_t timeout; 00141 bool persistent; 00142 bool follow; 00143 unsigned chunk; 00144 00145 Error getHTTPHeaders(); 00146 URLStream(const URLStream& rhs); 00147 00148 protected: 00149 ost::String m_host, m_address; 00150 00159 Error sendHTTPHeader(const char *url, const char **vars, size_t bufsize); 00160 00166 int underflow(void); 00167 00176 virtual int aRead(char *buffer, size_t len, timeout_t timer); 00177 00186 virtual int aWrite(char *buffer, size_t len, timeout_t timer); 00187 00194 virtual void httpHeader(const char *header, const char *value); 00195 00201 virtual char **extraHeader(void); 00202 00203 public: 00210 URLStream(Family family = IPV4, timeout_t timer = 0); 00211 00219 URLStream &getline(char *buffer, size_t len); 00220 00228 Error get(const char *url, size_t buffer = 512); 00229 00238 Error get(size_t buffer = 512); 00239 00249 Error submit(const char *url, const char **vars, size_t buffer = 512); 00250 00259 Error post(const char *url, const char **vars, size_t buffer = 512); 00260 00269 Error post(const char *url, MIMEMultipartForm &form, size_t buffer = 512); 00270 00278 Error head(const char *url, size_t buffer = 512); 00279 00283 void close(); 00284 00290 void setReferer(const char *str); 00291 00297 inline void setHost(const char *str) 00298 {m_host = str;}; 00299 00305 inline void setAddress(const char *str) 00306 {m_address = str;}; 00307 00313 inline void setCookie(const char *str) 00314 {cookie = str;}; 00315 00321 inline void setUser(const char *str) 00322 {user = str;}; 00323 00329 inline void setPassword(const char *str) 00330 {password = str;}; 00331 00338 void setAuthentication(Authentication a, const char *str = NULL); 00339 00345 inline void setProxyUser(const char *str) 00346 {proxyUser = str;}; 00347 00353 inline void setProxyPassword(const char *str) 00354 {proxyPasswd = str;}; 00355 00362 void setProxyAuthentication(Authentication a, const char *str = NULL); 00363 00369 inline void setPragma(const char *str) 00370 {pragma = str;}; 00371 00378 void setProxy(const char *host, tpport_t port); 00379 00385 inline void setAgent(const char *str) 00386 {agent = str;}; 00387 00393 inline Method getMethod(void) 00394 {return urlmethod;}; 00395 00402 inline void setTimeout(timeout_t to) 00403 {timeout = to;}; 00404 00411 inline void setFollow(bool enable) 00412 {follow = enable;}; 00413 00419 inline void setProtocol(Protocol pro) 00420 {protocol = pro;}; 00426 inline void setLocalInterface(const char *intf) 00427 {localif=intf;} 00428 }; 00429 00435 __EXPORT char* urlDecode(char *source, char *dest = NULL); 00436 00443 __EXPORT char* urlEncode(const char *source, char *dest, size_t size); 00444 00455 __EXPORT char* b64Decode(char *src, char *dest = NULL); 00456 00468 __EXPORT char* b64Encode(const char *source, char *dest, size_t size); 00469 00481 __EXPORT size_t b64Encode(const unsigned char *src, size_t srcsize, 00482 char *dst, size_t dstsize); 00483 00493 __EXPORT size_t b64Decode(const char *src, 00494 unsigned char *dst, size_t dstsize); 00495 00501 __EXPORT String b64Encode(const String& src); 00502 00510 __EXPORT String b64Decode(const String& src); 00511 00518 __EXPORT String b64Encode(const unsigned char *src, size_t srcsize); 00519 00529 __EXPORT size_t b64Decode(const String& src, 00530 unsigned char *dst, size_t dstsize); 00531 00532 00533 #ifdef CCXX_NAMESPACES 00534 } 00535 #endif 00536 00537 #endif 00538