Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Url.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Stream.h"
27 #include <iostream>
28 
29 namespace cinder {
30 
31 class Url {
32  public:
33  Url() {}
35  explicit Url( const std::string &urlString, bool isEscaped = false );
36 
38  std::string str() const { return mStr; }
40  const char* c_str() const { return mStr.c_str(); }
41 
43  static std::string encode( const std::string &unescaped );
44 
45  private:
46  std::string mStr;
47 };
48 
49 inline std::ostream& operator<<( std::ostream &out, const Url &url )
50 {
51  out << url.str();
52  return out;
53 }
54 
55 inline std::istream& operator>>( std::istream &is, Url &url )
56 {
57  std::string temp;
58  is >> temp;
59  url = Url( temp );
60  return is;
61 }
62 
64 class UrlOptions {
65  public:
66  UrlOptions( bool ignoreCache = false, float timeoutSeconds = 30.0f )
67  : mIgnoreCache( ignoreCache ), mTimeout( timeoutSeconds )
68  {}
69 
70  UrlOptions& ignoreCache( bool ignore = true ) { mIgnoreCache = ignore; return *this; }
71  bool getIgnoreCache() const { return mIgnoreCache; }
72  void setIgnoreCache( bool ignore = true ) { mIgnoreCache = ignore; }
73 
74  UrlOptions& timeout( float seconds ) { mTimeout = seconds; return *this; }
75  float getTimeout() const { return mTimeout; }
76  void setTimeout( float seconds ) { mTimeout = seconds; }
77 
78  private:
79  bool mIgnoreCache;
80  float mTimeout;
81 
82 };
83 
85 // This is an abstract base class for implementing IStreamUrl
86 class IStreamUrlImpl {
87  protected:
88  IStreamUrlImpl( const std::string &user, const std::string &password, const UrlOptions &options )
89  : mUser( user ), mPassword( password ), mOptions( options ) {}
90 
91  public:
92  virtual ~IStreamUrlImpl() {}
93 
94  std::string getUser() const { return mUser; }
95  std::string getPassword() const { return mPassword; }
96  const UrlOptions& getOptions() const { return mOptions; }
97 
98  virtual size_t readDataAvailable( void *dest, size_t maxSize ) = 0;
99  virtual void seekAbsolute( off_t absoluteOffset ) = 0;
100  virtual void seekRelative( off_t relativeOffset ) = 0;
101  virtual off_t tell() const = 0;
102  virtual off_t size() const = 0;
103 
104  virtual bool isEof() const = 0;
105  virtual void IORead( void *t, size_t size ) = 0;
106 
107  protected:
108  const std::string mUser, mPassword;
109  const UrlOptions mOptions;
110 };
112 
114 typedef std::shared_ptr<class IStreamUrl> IStreamUrlRef;
115 
117 class IStreamUrl : public IStreamCinder {
118  public:
120  static IStreamUrlRef create( const Url &url, const std::string &user = "", const std::string &password = "", const UrlOptions &options = UrlOptions() );
121 
122  virtual size_t readDataAvailable( void *dest, size_t maxSize ) { return mImpl->readDataAvailable( dest, maxSize ); }
123  virtual void seekAbsolute( off_t absoluteOffset ) { return mImpl->seekAbsolute( absoluteOffset ); }
124  virtual void seekRelative( off_t relativeOffset ) { return mImpl->seekRelative( relativeOffset ); }
125  virtual off_t tell() const { return mImpl->tell(); }
126  virtual off_t size() const { return mImpl->size(); }
127 
128  virtual bool isEof() const { return mImpl->isEof(); }
129 
130  std::string getUser() const { return mImpl->getUser(); }
131  std::string getPassword() const { return mImpl->getPassword(); }
132 
133  protected:
134  IStreamUrl( const std::string &url, const std::string &user, const std::string &password, const UrlOptions &options );
135 
136  virtual void IORead( void *t, size_t size ) { mImpl->IORead( t, size ); }
138  virtual void IOWrite( const void *t, size_t size ) { throw std::exception(); }
139 
140  std::shared_ptr<IStreamUrlImpl> mImpl;
141 };
142 
143 IStreamUrlRef loadUrlStream( const Url &url, const UrlOptions &options = UrlOptions() );
144 IStreamUrlRef loadUrlStream( const std::string &url, const std::string &user = "", const std::string &password = "", const UrlOptions &options = UrlOptions() );
145 
147 class UrlLoadExc : public Exception {
148  public:
149  UrlLoadExc( int statusCode, const std::string &message );
150 
151  virtual const char * what() const throw() { return mMessage.c_str(); }
152  int statusCode() const { return mStatusCode; }
153 
154  private:
155  std::string mMessage;
156  int mStatusCode;
157 };
158 
159 } // namespace cinder
Definition: Url.h:117
UrlOptions & ignoreCache(bool ignore=true)
Definition: Url.h:70
bool getIgnoreCache() const
Definition: Url.h:71
std::string getUser() const
Definition: Url.h:130
GLsizei const GLchar ** string
Definition: GLee.h:2427
UrlOptions(bool ignoreCache=false, float timeoutSeconds=30.0f)
Definition: Url.h:66
int statusCode() const
Definition: Url.h:152
virtual off_t size() const
Definition: Url.h:126
static std::string encode(const std::string &unescaped)
Replaces illegal URL characters as defined by RFC 2396 with their escaped equivalents and returns a c...
Definition: Url.cpp:59
Url()
Definition: Url.h:33
virtual void IOWrite(const void *t, size_t size)
IStreamURL does not yet support writing.
Definition: Url.h:138
void setTimeout(float seconds)
Definition: Url.h:76
virtual void seekRelative(off_t relativeOffset)
Moves the current position of the stream by relativeOffset bytes.
Definition: Url.h:124
IStreamUrlRef loadUrlStream(const Url &url, const UrlOptions &options=UrlOptions())
Definition: Url.cpp:98
static IStreamUrlRef create(const Url &url, const std::string &user="", const std::string &password="", const UrlOptions &options=UrlOptions())
Creates a new IStreamUrlRef from the Url url with an optional login and password. ...
Definition: Url.cpp:84
std::shared_ptr< IStreamUrlImpl > mImpl
Definition: Url.h:140
UrlLoadExc(int statusCode, const std::string &message)
Definition: Url.cpp:111
std::shared_ptr< class IStreamUrl > IStreamUrlRef
A pointer to an instance of an IStreamUrl. Can be created using IStreamUrl::createRef() ...
Definition: Url.h:114
IStreamUrl(const std::string &url, const std::string &user, const std::string &password, const UrlOptions &options)
Definition: Url.cpp:89
virtual size_t readDataAvailable(void *dest, size_t maxSize)
Definition: Url.h:122
float getTimeout() const
Definition: Url.h:75
std::string str() const
Returns the string representation of the URL as std::string. Encoded as UTF-8.
Definition: Url.h:38
std::ostream & operator<<(std::ostream &lhs, const ColorT< float > &rhs)
Definition: Color.cpp:203
virtual void seekAbsolute(off_t absoluteOffset)
Sets the current position of the stream to byte absoluteOffset. A negative offset is relative to the ...
Definition: Url.h:123
void setIgnoreCache(bool ignore=true)
Definition: Url.h:72
Exception for failed Url loading.
Definition: Url.h:147
std::istream & operator>>(std::istream &is, Url &url)
Definition: Url.h:55
Definition: Exception.h:32
Options for loadUrl() to dictate caching and timeout behavior.
Definition: Url.h:64
std::string getPassword() const
Definition: Url.h:131
UrlOptions & timeout(float seconds)
Definition: Url.h:74
Definition: Stream.h:109
GLdouble GLdouble t
Definition: GLee.h:1426
virtual off_t tell() const
Returns the current position of the stream measured in bytes **/.
Definition: Url.h:125
GLclampf f
Definition: GLee.h:15307
GLsizeiptr size
Definition: GLee.h:2089
virtual void IORead(void *t, size_t size)
Definition: Url.h:136
virtual bool isEof() const
Definition: Url.h:128
const char * c_str() const
Returns the string representation of the URL as char*. Encoded as UTF-8.
Definition: Url.h:40
virtual const char * what() const
Definition: Url.h:151
Definition: Url.h:31