include/cinder/Url.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/Cinder.h"
00026 #include "cinder/Stream.h"
00027 #include <iostream>
00028 
00029 namespace cinder {
00030 
00031 class Url {
00032  public:
00033     Url() {}
00035     explicit Url( const std::string &urlString, bool isEscaped = false );
00036     
00038     std::string str() const { return mStr; }
00040     const char* c_str() const { return mStr.c_str(); }
00041 
00043     static std::string encode( const std::string &unescaped );
00044         
00045  private:
00046     std::string     mStr;
00047 };
00048 
00049 inline std::ostream& operator<<( std::ostream &out, const Url &url )
00050 {
00051     out << url.str();
00052     return out;
00053 }
00054 
00055 inline std::istream& operator>>( std::istream &is, Url &url )
00056 {
00057     std::string temp;
00058     is >> temp;
00059     url = Url( temp );
00060     return is;
00061 }
00062 
00064 class UrlOptions {
00065   public:
00066     UrlOptions( bool ignoreCache = false, float timeoutSeconds = 30.0f )
00067         : mIgnoreCache( ignoreCache ), mTimeout( timeoutSeconds )
00068     {}
00069     
00070     UrlOptions&     ignoreCache( bool ignore = true ) { mIgnoreCache = ignore; return *this; }
00071     bool            getIgnoreCache() const { return mIgnoreCache; }
00072     void            setIgnoreCache( bool ignore = true ) { mIgnoreCache = ignore; }
00073     
00074     UrlOptions&     timeout( float seconds ) { mTimeout = seconds; return *this; }
00075     float           getTimeout() const { return mTimeout; }
00076     void            setTimeout( float seconds ) { mTimeout = seconds; }
00077     
00078   private:
00079     bool            mIgnoreCache;
00080     float           mTimeout;
00081     
00082 };
00083 
00085 // This is an abstract base class for implementing IStreamUrl
00086 class IStreamUrlImpl {
00087   protected:
00088     IStreamUrlImpl( const std::string &user, const std::string &password, const UrlOptions &options )
00089         : mUser( user ), mPassword( password ), mOptions( options ) {}
00090 
00091   public:
00092     virtual ~IStreamUrlImpl() {}
00093   
00094     std::string         getUser() const { return mUser; }
00095     std::string         getPassword() const { return mPassword; }
00096     const UrlOptions&   getOptions() const { return mOptions; }
00097 
00098     virtual size_t      readDataAvailable( void *dest, size_t maxSize ) = 0;
00099     virtual void        seekAbsolute( off_t absoluteOffset ) = 0;
00100     virtual void        seekRelative( off_t relativeOffset ) = 0;
00101     virtual off_t       tell() const = 0;
00102     virtual off_t       size() const = 0;
00103     
00104     virtual bool        isEof() const = 0;
00105     virtual void        IORead( void *t, size_t size ) = 0;
00106     
00107   protected:
00108     const std::string       mUser, mPassword;
00109     const UrlOptions        mOptions;
00110 };
00112 
00114 typedef std::shared_ptr<class IStreamUrl>   IStreamUrlRef;
00115 
00117 class IStreamUrl : public IStream {
00118   public:
00120     static IStreamUrlRef        create( const Url &url, const std::string &user = "", const std::string &password = "", const UrlOptions &options = UrlOptions() );
00121     
00122     virtual size_t      readDataAvailable( void *dest, size_t maxSize ) { return mImpl->readDataAvailable( dest, maxSize ); }
00123     virtual void        seekAbsolute( off_t absoluteOffset ) { return mImpl->seekAbsolute( absoluteOffset ); }
00124     virtual void        seekRelative( off_t relativeOffset ) { return mImpl->seekRelative( relativeOffset ); }
00125     virtual off_t       tell() const { return mImpl->tell(); }
00126     virtual off_t       size() const { return mImpl->size(); }
00127     
00128     virtual bool        isEof() const { return mImpl->isEof(); }
00129 
00130     std::string         getUser() const { return mImpl->getUser(); }
00131     std::string         getPassword() const { return mImpl->getPassword(); }
00132 
00133   protected:
00134     IStreamUrl( const std::string &url, const std::string &user, const std::string &password, const UrlOptions &options );
00135 
00136     virtual void        IORead( void *t, size_t size ) { mImpl->IORead( t, size ); }
00138     virtual void        IOWrite( const void *t, size_t size ) { throw std::exception(); }
00139     
00140     std::shared_ptr<IStreamUrlImpl> mImpl;
00141 };
00142 
00143 IStreamUrlRef       loadUrlStream( const Url &url, const UrlOptions &options = UrlOptions() );
00144 IStreamUrlRef       loadUrlStream( const std::string &url, const std::string &user = "", const std::string &password = "", const UrlOptions &options = UrlOptions() );
00145 
00147 class UrlLoadExc : public Exception {
00148   public:
00149     UrlLoadExc( int statusCode, const std::string &message );
00150 
00151     virtual const char * what() const throw() { return mMessage.c_str(); }
00152     int statusCode() const { return mStatusCode; }
00153 
00154   private:
00155     std::string     mMessage;
00156     int             mStatusCode;
00157 };
00158 
00159 } // namespace cinder