url.h

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

Generated on Sun Mar 21 21:39:52 2010 for GNU CommonC++ by  doxygen 1.4.7