GNU CommonC++
url.h
Go to the documentation of this file.
1 // Copyright (C) 2001-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_URL_H_
44 #define CCXX_URL_H_
45 
46 #ifndef CCXX_CONFIG_H_
47 #include <cc++/config.h>
48 #endif
49 
50 #ifndef CCXX_SOCKET_H_
51 #include <cc++/socket.h>
52 #endif
53 
54 #ifndef CCXX_MIME_H_
55 #include <cc++/mime.h>
56 #endif
57 
58 #ifdef CCXX_NAMESPACES
59 namespace ost {
60 #endif
61 
69 {
70 public:
74  typedef enum {
75  errSuccess = 0,
85  errInterface
86  } Error;
87 
91  typedef enum {
92  authAnonymous = 0,
93  authBasic
94  } Authentication;
95 
99  typedef enum {
100  encodingBinary = 0,
101  encodingChunked
102  } Encoding;
103 
107  typedef enum {
115  methodFilePut
116  } Method;
117 
121  typedef enum {
123  protocolHttp1_1
124  } Protocol;
125 
126 private:
127  const char *agent, *referer, *cookie, *pragma, *user, *password;
128  const char *proxyUser, *proxyPasswd;
129  const char *localif;
130  IPV4Host proxyHost;
131 #ifdef CCXX_IPV6
132  IPV6Host v6proxyHost;
133 #endif
134  tpport_t proxyPort;
135  Method urlmethod;
136  Encoding encoding;
137  Protocol protocol;
138  Authentication auth;
139  Authentication proxyAuth;
140  timeout_t timeout;
141  bool persistent;
142  bool follow;
143  unsigned chunk;
144 
145  Error getHTTPHeaders();
146  URLStream(const URLStream& rhs);
147 
148 protected:
149  ost::String m_host, m_address;
150 
159  Error sendHTTPHeader(const char *url, const char **vars, size_t bufsize);
160 
166  int underflow(void);
167 
176  virtual int aRead(char *buffer, size_t len, timeout_t timer);
177 
186  virtual int aWrite(char *buffer, size_t len, timeout_t timer);
187 
194  virtual void httpHeader(const char *header, const char *value);
195 
201  virtual char **extraHeader(void);
202 
203 public:
210  URLStream(Family family = IPV4, timeout_t timer = 0);
211 
219  URLStream &getline(char *buffer, size_t len);
220 
228  Error get(const char *url, size_t buffer = 512);
229 
238  Error get(size_t buffer = 512);
239 
249  Error submit(const char *url, const char **vars, size_t buffer = 512);
250 
259  Error post(const char *url, const char **vars, size_t buffer = 512);
260 
269  Error post(const char *url, MIMEMultipartForm &form, size_t buffer = 512);
270 
278  Error head(const char *url, size_t buffer = 512);
279 
283  void close();
284 
290  void setReferer(const char *str);
291 
297  inline void setHost(const char *str)
298  {m_host = str;};
299 
305  inline void setAddress(const char *str)
306  {m_address = str;};
307 
313  inline void setCookie(const char *str)
314  {cookie = str;};
315 
321  inline void setUser(const char *str)
322  {user = str;};
323 
329  inline void setPassword(const char *str)
330  {password = str;};
331 
338  void setAuthentication(Authentication a, const char *str = NULL);
339 
345  inline void setProxyUser(const char *str)
346  {proxyUser = str;};
347 
353  inline void setProxyPassword(const char *str)
354  {proxyPasswd = str;};
355 
362  void setProxyAuthentication(Authentication a, const char *str = NULL);
363 
369  inline void setPragma(const char *str)
370  {pragma = str;};
371 
378  void setProxy(const char *host, tpport_t port);
379 
385  inline void setAgent(const char *str)
386  {agent = str;};
387 
393  inline Method getMethod(void)
394  {return urlmethod;};
395 
402  inline void setTimeout(timeout_t to)
403  {timeout = to;};
404 
411  inline void setFollow(bool enable)
412  {follow = enable;};
413 
419  inline void setProtocol(Protocol pro)
420  {protocol = pro;};
426  inline void setLocalInterface(const char *intf)
427  {localif=intf;}
428 };
429 
435 __EXPORT char* urlDecode(char *source, char *dest = NULL);
436 
443 __EXPORT char* urlEncode(const char *source, char *dest, size_t size);
444 
455 __EXPORT char* b64Decode(char *src, char *dest = NULL);
456 
468 __EXPORT char* b64Encode(const char *source, char *dest, size_t size);
469 
481 __EXPORT size_t b64Encode(const unsigned char *src, size_t srcsize,
482  char *dst, size_t dstsize);
483 
493 __EXPORT size_t b64Decode(const char *src,
494  unsigned char *dst, size_t dstsize);
495 
501 __EXPORT String b64Encode(const String& src);
502 
510 __EXPORT String b64Decode(const String& src);
511 
518 __EXPORT String b64Encode(const unsigned char *src, size_t srcsize);
519 
529 __EXPORT size_t b64Decode(const String& src,
530  unsigned char *dst, size_t dstsize);
531 
532 
533 #ifdef CCXX_NAMESPACES
534 }
535 #endif
536 
537 #endif
538