GNU CommonC++
url.h
Go to the documentation of this file.
1 // Copyright (C) 2001-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_URL_H_
45 #define CCXX_URL_H_
46 
47 #ifndef CCXX_CONFIG_H_
48 #include <cc++/config.h>
49 #endif
50 
51 #ifndef CCXX_SOCKET_H_
52 #include <cc++/socket.h>
53 #endif
54 
55 #ifndef CCXX_MIME_H_
56 #include <cc++/mime.h>
57 #endif
58 
59 #ifdef CCXX_NAMESPACES
60 namespace ost {
61 #endif
62 
70 {
71 public:
75  typedef enum {
76  errSuccess = 0,
86  errInterface
87  } Error;
88 
92  typedef enum {
93  authAnonymous = 0,
94  authBasic
95  } Authentication;
96 
100  typedef enum {
101  encodingBinary = 0,
102  encodingChunked
103  } Encoding;
104 
108  typedef enum {
116  methodFilePut
117  } Method;
118 
122  typedef enum {
124  protocolHttp1_1
125  } Protocol;
126 
127 private:
128  const char *agent, *referer, *cookie, *pragma, *user, *password;
129  const char *proxyUser, *proxyPasswd;
130  const char *localif;
131  IPV4Host proxyHost;
132 #ifdef CCXX_IPV6
133  IPV6Host v6proxyHost;
134 #endif
135  tpport_t proxyPort;
136  Method urlmethod;
137  Encoding encoding;
138  Protocol protocol;
139  Authentication auth;
140  Authentication proxyAuth;
141  timeout_t timeout;
142  bool persistent;
143  bool follow;
144  unsigned chunk;
145 
146  Error getHTTPHeaders();
147  URLStream(const URLStream& rhs);
148 
149 protected:
150  ost::String m_host, m_address;
151 
160  Error sendHTTPHeader(const char *url, const char **vars, size_t bufsize);
161 
167  int underflow(void);
168 
177  virtual int aRead(char *buffer, size_t len, timeout_t timer);
178 
187  virtual int aWrite(char *buffer, size_t len, timeout_t timer);
188 
195  virtual void httpHeader(const char *header, const char *value);
196 
202  virtual char **extraHeader(void);
203 
204 public:
211  URLStream(Family family = IPV4, timeout_t timer = 0);
212 
220  URLStream &getline(char *buffer, size_t len);
221 
229  Error get(const char *url, size_t buffer = 512);
230 
239  Error get(size_t buffer = 512);
240 
250  Error submit(const char *url, const char **vars, size_t buffer = 512);
251 
260  Error post(const char *url, const char **vars, size_t buffer = 512);
261 
270  Error post(const char *url, MIMEMultipartForm &form, size_t buffer = 512);
271 
279  Error head(const char *url, size_t buffer = 512);
280 
284  void close();
285 
291  void setReferer(const char *str);
292 
298  inline void setHost(const char *str)
299  {m_host = str;};
300 
306  inline void setAddress(const char *str)
307  {m_address = str;};
308 
314  inline void setCookie(const char *str)
315  {cookie = str;};
316 
322  inline void setUser(const char *str)
323  {user = str;};
324 
330  inline void setPassword(const char *str)
331  {password = str;};
332 
339  void setAuthentication(Authentication a, const char *str = NULL);
340 
346  inline void setProxyUser(const char *str)
347  {proxyUser = str;};
348 
354  inline void setProxyPassword(const char *str)
355  {proxyPasswd = str;};
356 
363  void setProxyAuthentication(Authentication a, const char *str = NULL);
364 
370  inline void setPragma(const char *str)
371  {pragma = str;};
372 
379  void setProxy(const char *host, tpport_t port);
380 
386  inline void setAgent(const char *str)
387  {agent = str;};
388 
394  inline Method getMethod(void)
395  {return urlmethod;};
396 
403  inline void setTimeout(timeout_t to)
404  {timeout = to;};
405 
412  inline void setFollow(bool enable)
413  {follow = enable;};
414 
420  inline void setProtocol(Protocol pro)
421  {protocol = pro;};
427  inline void setLocalInterface(const char *intf)
428  {localif=intf;}
429 };
430 
436 __EXPORT char* urlDecode(char *source, char *dest = NULL);
437 
444 __EXPORT char* urlEncode(const char *source, char *dest, size_t size);
445 
456 __EXPORT char* b64Decode(char *src, char *dest = NULL);
457 
469 __EXPORT char* b64Encode(const char *source, char *dest, size_t size);
470 
482 __EXPORT size_t b64Encode(const unsigned char *src, size_t srcsize,
483  char *dst, size_t dstsize);
484 
494 __EXPORT size_t b64Decode(const char *src,
495  unsigned char *dst, size_t dstsize);
496 
503 
512 
519 __EXPORT String b64Encode(const unsigned char *src, size_t srcsize);
520 
530 __EXPORT size_t b64Decode(const String& src,
531  unsigned char *dst, size_t dstsize);
532 
533 
534 #ifdef CCXX_NAMESPACES
535 }
536 #endif
537 
538 #endif
539 
ost::tpport_t
unsigned short tpport_t
Transport Protocol Ports.
Definition: address.h:86
ost::URLStream::methodHttpPostMultipart
@ methodHttpPostMultipart
Definition: url.h:112
ost::URLStream::setPragma
void setPragma(const char *str)
Set the pragmas.
Definition: url.h:370
ost::URLStream::b64Encode
__EXPORT size_t b64Encode(const unsigned char *src, size_t srcsize, char *dst, size_t dstsize)
ost::URLStream::setAddress
void setAddress(const char *str)
Set the address for the url.
Definition: url.h:306
ost::URLStream::underflow
int underflow(void)
Called if stream buffer needs refilling.
ost::IPV4Host
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:562
ost::URLStream::setAuthentication
void setAuthentication(Authentication a, const char *str=NULL)
Set authentication type for the url.
ost::URLStream::b64Encode
__EXPORT char * b64Encode(const char *source, char *dest, size_t size)
ost::URLStream::setCookie
void setCookie(const char *str)
Set the cookie to pass.
Definition: url.h:314
ost::URLStream::Method
Method
Type of fetch.
Definition: url.h:108
timeout_t
unsigned long timeout_t
Definition: thread.h:74
ost::URLStream::Authentication
Authentication
Type of authentication.
Definition: url.h:92
ost::URLStream::methodFileGet
@ methodFileGet
Definition: url.h:115
ost::IPV6Host
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:949
ost::URLStream::httpHeader
virtual void httpHeader(const char *header, const char *value)
Derived method to receive and parse http "headers".
ost::URLStream::errRelocated
@ errRelocated
Definition: url.h:83
ost::URLStream::errMissing
@ errMissing
Definition: url.h:78
ost::Socket::Family
Family
Definition: socket.h:122
ost::URLStream::errUnauthorized
@ errUnauthorized
Definition: url.h:82
ost::URLStream::setProxyPassword
void setProxyPassword(const char *str)
Set proxy password for the url.
Definition: url.h:354
ost::URLStream::close
void close()
Close the URL stream for a new connection.
ost::URLStream::b64Decode
__EXPORT size_t b64Decode(const char *src, unsigned char *dst, size_t dstsize)
ost::URLStream::get
Error get(size_t buffer=512)
Get URL data from a named stream of a known buffer size.
ost::URLStream::b64Encode
__EXPORT String b64Encode(const unsigned char *src, size_t srcsize)
ost::URLStream::setPassword
void setPassword(const char *str)
Set password for the url.
Definition: url.h:330
ost::Socket::Error
Error
Definition: socket.h:131
ost::URLStream::b64Decode
__EXPORT String b64Decode(const String &src)
ost::URLStream::methodFtpGet
@ methodFtpGet
Definition: url.h:113
ost::URLStream::setUser
void setUser(const char *str)
Set user id for the url.
Definition: url.h:322
ost::URLStream::extraHeader
virtual char ** extraHeader(void)
A virtual to insert additional header info into the request.
ost::URLStream::errInvalid
@ errInvalid
Definition: url.h:80
ost::TCPStream
TCP streams are used to represent TCP client connections to a server by TCP protocol servers for acce...
Definition: socket.h:1632
ost::URLStream::m_host
ost::String m_host
Definition: url.h:150
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::URLStream::urlEncode
__EXPORT char * urlEncode(const char *source, char *dest, size_t size)
ost::URLStream::URLStream
URLStream(Family family=IPV4, timeout_t timer=0)
Construct an instance of URL stream.
ost::URLStream::getline
URLStream & getline(char *buffer, size_t len)
Line parsing with conversion.
ost::URLStream::errDenied
@ errDenied
Definition: url.h:79
ost::URLStream::Protocol
Protocol
http protocol version
Definition: url.h:122
ost::URLStream::b64Decode
__EXPORT size_t b64Decode(const String &src, unsigned char *dst, size_t dstsize)
ost::URLStream::protocolHttp1_0
@ protocolHttp1_0
Definition: url.h:123
ost::URLStream::head
Error head(const char *url, size_t buffer=512)
Used to fetch header information for a resource.
ost::URLStream::post
Error post(const char *url, const char **vars, size_t buffer=512)
Post URL vars with post method.
ost::URLStream::getMethod
Method getMethod(void)
Get url method (and protocol) employed.
Definition: url.h:394
ost::URLStream::methodFtpPut
@ methodFtpPut
Definition: url.h:114
ost::URLStream::b64Decode
__EXPORT char * b64Decode(char *src, char *dest=NULL)
ost::URLStream::setTimeout
void setTimeout(timeout_t to)
Set socket timeout characteristics for processing URL requests.
Definition: url.h:403
ost::URLStream::aWrite
virtual int aWrite(char *buffer, size_t len, timeout_t timer)
Derived method for async or timed I/O function on url stream.
ost::URLStream::methodHttpPut
@ methodHttpPut
Definition: url.h:110
ost::URLStream::errForbidden
@ errForbidden
Definition: url.h:81
ost::URLStream::errTimeout
@ errTimeout
Definition: url.h:85
ost::URLStream::setAgent
void setAgent(const char *str)
Set the agent.
Definition: url.h:386
ost::URLStream::submit
Error submit(const char *url, const char **vars, size_t buffer=512)
Submit URL with vars passed as argument array.
ost
Definition: address.h:64
ost::URLStream::aRead
virtual int aRead(char *buffer, size_t len, timeout_t timer)
Derived method for async or timed I/O function on url stream.
ost::URLStream::sendHTTPHeader
Error sendHTTPHeader(const char *url, const char **vars, size_t bufsize)
Send http header to server.
ost::URLStream::errUnreachable
@ errUnreachable
Definition: url.h:77
ost::MIMEMultipartForm
The Multipart form is a MIME multipart document specific for the construction and delivery of form da...
Definition: mime.h:123
ost::URLStream::b64Encode
__EXPORT String b64Encode(const String &src)
ost::URLStream::setReferer
void setReferer(const char *str)
Set the referer url.
ost::URLStream::setLocalInterface
void setLocalInterface(const char *intf)
Specify local interface to use.
Definition: url.h:427
socket.h
Network addresses and sockets related classes.
ost::URLStream::setHost
void setHost(const char *str)
Set the host for the url.
Definition: url.h:298
config.h
mime.h
MIME document abstractions.
ost::URLStream::methodHttpPost
@ methodHttpPost
Definition: url.h:111
ost::URLStream::methodHttpGet
@ methodHttpGet
Definition: url.h:109
ost::URLStream::get
Error get(const char *url, size_t buffer=512)
Get URL data from a named stream of a known buffer size.
ost::URLStream::setFollow
void setFollow(bool enable)
Specify url following.
Definition: url.h:412
ost::URLStream
A URL processing version of TCPStream.
Definition: url.h:70
ost::String
This is a generic and portable string class.
Definition: string.h:81
ost::URLStream::urlDecode
__EXPORT char * urlDecode(char *source, char *dest=NULL)
ost::URLStream::setProxy
void setProxy(const char *host, tpport_t port)
Set the proxy server used.
ost::URLStream::setProxyUser
void setProxyUser(const char *str)
Set proxy user id for the url.
Definition: url.h:346
ost::URLStream::errFailure
@ errFailure
Definition: url.h:84
ost::URLStream::post
Error post(const char *url, MIMEMultipartForm &form, size_t buffer=512)
Post URL with MIME multipart form.
ost::URLStream::setProtocol
void setProtocol(Protocol pro)
Specify http protocol level being used.
Definition: url.h:420
ost::URLStream::setProxyAuthentication
void setProxyAuthentication(Authentication a, const char *str=NULL)
Set proxy authentication type for the url.