├── .gitignore ├── .gitmodules ├── README ├── samples ├── FtpClient │ ├── include │ │ ├── Resources.h │ │ ├── TcpClientConnection.h │ │ ├── TcpClientEventHandler.h │ │ └── TcpSessionEventHandler.h │ ├── resources │ │ ├── CinderApp.icns │ │ └── cinder_app_icon.ico │ ├── src │ │ ├── FtpClientApp.cpp │ │ ├── TcpClientConnection.cpp │ │ ├── TcpClientEventHandler.cpp │ │ └── TcpSessionEventHandler.cpp │ ├── vc2013 │ │ ├── FtpClientApp.sln │ │ ├── FtpClientApp.vcxproj │ │ ├── FtpClientApp.vcxproj.filters │ │ └── Resources.rc │ ├── vc2015 │ │ ├── FtpClientApp.sln │ │ ├── FtpClientApp.vcxproj │ │ ├── FtpClientApp.vcxproj.filters │ │ └── Resources.rc │ └── xcode │ │ ├── FtpClient.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── contents.xcworkspacedata │ │ ├── FtpClient_Prefix.pch │ │ └── Info.plist ├── HttpClient │ ├── include │ │ └── Resources.h │ ├── resources │ │ ├── CinderApp.icns │ │ └── cinder_app_icon.ico │ ├── src │ │ └── HttpClientApp.cpp │ ├── vc2013 │ │ ├── HttpClientApp.sln │ │ ├── HttpClientApp.vcxproj │ │ ├── HttpClientApp.vcxproj.filters │ │ └── Resources.rc │ ├── vc2015 │ │ ├── HttpClientApp.sln │ │ ├── HttpClientApp.vcxproj │ │ ├── HttpClientApp.vcxproj.filters │ │ └── Resources.rc │ └── xcode │ │ ├── HttpClient.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── contents.xcworkspacedata │ │ ├── HttpClient_Prefix.pch │ │ └── Info.plist ├── HttpServer │ ├── assets │ │ ├── 0.jpg │ │ ├── 1.png │ │ └── 2.mp3 │ ├── include │ │ └── Resources.h │ ├── resources │ │ ├── CinderApp.icns │ │ └── cinder_app_icon.ico │ ├── src │ │ └── HttpServerApp.cpp │ ├── vc2013 │ │ ├── HttpServerApp.sln │ │ ├── HttpServerApp.vcxproj │ │ ├── HttpServerApp.vcxproj.filters │ │ └── Resources.rc │ ├── vc2015 │ │ ├── HttpServerApp.sln │ │ ├── HttpServerApp.vcxproj │ │ ├── HttpServerApp.vcxproj.filters │ │ └── Resources.rc │ └── xcode │ │ ├── HttpServer.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── contents.xcworkspacedata │ │ ├── HttpServer_Prefix.pch │ │ └── Info.plist └── WebClient │ ├── include │ └── Resources.h │ ├── resources │ ├── CinderApp.icns │ └── cinder_app_icon.ico │ ├── src │ └── WebClientApp.cpp │ ├── vc2013 │ ├── Resources.rc │ ├── WebClientApp.sln │ ├── WebClientApp.vcxproj │ └── WebClientApp.vcxproj.filters │ ├── vc2015 │ ├── Resources.rc │ ├── WebClientApp.sln │ ├── WebClientApp.vcxproj │ └── WebClientApp.vcxproj.filters │ └── xcode │ ├── Info.plist │ ├── WebClient.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata │ └── WebClient_Prefix.pch └── src ├── BodyInterface.cpp ├── BodyInterface.h ├── FtpInterface.cpp ├── FtpInterface.h ├── FtpRequest.cpp ├── FtpRequest.h ├── FtpResponse.cpp ├── FtpResponse.h ├── HeaderInterface.cpp ├── HeaderInterface.h ├── HttpInterface.cpp ├── HttpInterface.h ├── HttpRequest.cpp ├── HttpRequest.h ├── HttpResponse.cpp ├── HttpResponse.h ├── KeyValuePairInterface.cpp ├── KeyValuePairInterface.h ├── ProtocolInterface.cpp └── ProtocolInterface.h /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | xcshareddata 3 | xcuserdata 4 | bin 5 | build 6 | DerivedData 7 | ipch 8 | x64 9 | Win32 10 | Debug 11 | Release 12 | *.opensdf 13 | *.sdf 14 | *.suo 15 | *.user -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "blocks/Cinder-Asio"] 2 | path = blocks/Cinder-Asio 3 | url = https://github.com/BanTheRewind/Cinder-Asio.git 4 | branch = master 5 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Cinder-Protocol 2 | =============== 3 | 4 | Implementation of various network protocols for Cinder 5 | 6 | This is being built in tandem with Cinder-Asio, but 7 | does not require it. 8 | 9 | https://github.com/BanTheRewind/Cinder-Asio -------------------------------------------------------------------------------- /samples/FtpClient/include/Resources.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cinder/CinderResources.h" 3 | 4 | //#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE ) 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /samples/FtpClient/include/TcpClientConnection.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "TcpClient.h" 41 | #include "TcpClientEventHandler.h" 42 | #include "TcpSessionEventHandler.h" 43 | 44 | typedef std::shared_ptr TcpClientConnectionRef; 45 | 46 | class TcpClientConnection 47 | { 48 | public: 49 | static TcpClientConnectionRef create( asio::io_service& io ); 50 | 51 | void close(); 52 | void connect( const std::string& host, int16_t port ); 53 | void write( const ci::Buffer& buffer ); 54 | 55 | const ci::Buffer& getBuffer() const; 56 | bool isConnected() const; 57 | protected: 58 | TcpClientConnection( asio::io_service& io ); 59 | 60 | void update(); 61 | 62 | bool mConnected; 63 | 64 | TcpClientRef mClient; 65 | TcpClientEventHandler mClientEventHandler; 66 | TcpSessionEventHandler mSessionEventHandler; 67 | }; -------------------------------------------------------------------------------- /samples/FtpClient/include/TcpClientEventHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "TcpClientEventHandlerInterface.h" 41 | 42 | class TcpClientEventHandler : public TcpClientEventHandlerInterface 43 | { 44 | public: 45 | TcpClientEventHandler(); 46 | 47 | void onConnect( TcpSessionRef session ); 48 | void onError( std::string err, size_t bytesTransferred ); 49 | void onResolve(); 50 | 51 | const std::string& getError() const; 52 | const TcpSessionRef& getSession() const; 53 | protected: 54 | std::string mError; 55 | TcpSessionRef mSession; 56 | }; 57 | -------------------------------------------------------------------------------- /samples/FtpClient/include/TcpSessionEventHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "TcpSessionEventHandlerInterface.h" 41 | 42 | class TcpSessionEventHandler : public TcpSessionEventHandlerInterface 43 | { 44 | public: 45 | TcpSessionEventHandler(); 46 | 47 | const ci::Buffer& getBuffer() const; 48 | const std::string& getError() const; 49 | bool isReadComplete() const; 50 | bool isWriteComplete() const; 51 | 52 | void onClose(); 53 | void onError( std::string err, size_t bytesTransferred ); 54 | void onRead( ci::Buffer buffer ); 55 | void onReadComplete(); 56 | void onWrite( size_t bytesTransferred ); 57 | protected: 58 | ci::Buffer mBuffer; 59 | std::string mError; 60 | bool mReadComplete; 61 | bool mWriteComplete; 62 | }; 63 | -------------------------------------------------------------------------------- /samples/FtpClient/resources/CinderApp.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/FtpClient/resources/CinderApp.icns -------------------------------------------------------------------------------- /samples/FtpClient/resources/cinder_app_icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/FtpClient/resources/cinder_app_icon.ico -------------------------------------------------------------------------------- /samples/FtpClient/src/FtpClientApp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "CinderAsio.h" 39 | 40 | #include "cinder/app/App.h" 41 | #include "cinder/gl/gl.h" 42 | #include "cinder/params/Params.h" 43 | #include "cinder/Text.h" 44 | 45 | #include "boost/algorithm/string.hpp" 46 | 47 | #include "FtpRequest.h" 48 | #include "FtpResponse.h" 49 | 50 | #include "TcpClientConnection.h" 51 | 52 | class FtpClientApp : public ci::app::App 53 | { 54 | public: 55 | void draw(); 56 | void setup(); 57 | void update(); 58 | private: 59 | TcpClientConnectionRef mConnectionControl; 60 | std::string mHost; 61 | 62 | FtpRequest mFtpRequest; 63 | FtpResponse mFtpResponse; 64 | 65 | ci::Font mFont; 66 | std::vector mText; 67 | ci::gl::TextureRef mTexture; 68 | 69 | float mFrameRate; 70 | bool mFullScreen; 71 | ci::params::InterfaceGlRef mParams; 72 | }; 73 | 74 | #include "cinder/app/RendererGl.h" 75 | #include "cinder/Utilities.h" 76 | #include 77 | #include 78 | 79 | using namespace ci; 80 | using namespace ci::app; 81 | using namespace std; 82 | 83 | void FtpClientApp::draw() 84 | { 85 | gl::clear( Colorf::black() ); 86 | gl::setMatricesWindow( getWindowSize() ); 87 | 88 | if ( mTexture ) { 89 | gl::draw( mTexture, ivec2( 250, 20 ) ); 90 | } 91 | 92 | mParams->draw(); 93 | } 94 | 95 | void FtpClientApp::setup() 96 | { 97 | gl::enable( GL_TEXTURE_2D ); 98 | 99 | mFont = Font( "Georgia", 24 ); 100 | mFrameRate = 0.0f; 101 | mFullScreen = false; 102 | 103 | mParams = params::InterfaceGl::create( "Params", ivec2( 200, 120 ) ); 104 | mParams->addParam( "Frame rate", &mFrameRate, "", true ); 105 | mParams->addParam( "Full screen", &mFullScreen ).key( "f" ); 106 | mParams->addButton( "Quit", [ & ]() { quit(); }, "key=q" ); 107 | 108 | mConnectionControl = TcpClientConnection::create( io_service() ); 109 | 110 | mConnectionControl->connect( "www.bantherewind.com", 21 ); 111 | } 112 | 113 | void FtpClientApp::update() 114 | { 115 | mFrameRate = getFrameRate(); 116 | 117 | if ( mFullScreen != isFullScreen() ) { 118 | setFullScreen( mFullScreen ); 119 | mFullScreen = isFullScreen(); 120 | } 121 | 122 | if ( !mText.empty() ) { 123 | TextBox tbox = TextBox() 124 | .alignment( TextBox::LEFT ) 125 | .font( mFont ) 126 | .size( ivec2( getWindowWidth() - 250, TextBox::GROW ) ) 127 | .text( "" ); 128 | for ( vector::const_reverse_iterator iter = mText.rbegin(); iter != mText.rend(); ++iter ) { 129 | tbox.appendText( "> " + *iter + "\n" ); 130 | } 131 | tbox.setColor( ColorAf( 1.0f, 0.8f, 0.75f, 1.0f ) ); 132 | tbox.setBackgroundColor( ColorAf::black() ); 133 | tbox.setPremultiplied( false ); 134 | mTexture = gl::Texture::create( tbox.render() ); 135 | while ( mText.size() > 75 ) { 136 | mText.erase( mText.begin() ); 137 | } 138 | } 139 | 140 | if ( mConnectionControl->isConnected() && 141 | mConnectionControl->getBuffer() && 142 | mConnectionControl->getBuffer().getDataSize() > 0 ) { 143 | const Buffer& buffer = mConnectionControl->getBuffer(); 144 | 145 | string s = ProtocolInterface::bufferToString( buffer ); 146 | 147 | mFtpResponse.parse( buffer ); 148 | mText.push_back( mFtpResponse.toString() ); 149 | 150 | switch ( mFtpResponse.getReplyCode() ) { 151 | case FtpReplyCode_220_SERVICE_READY_FOR_NEW_USER: 152 | mFtpRequest.set( FtpCommand_USER, "****" ); 153 | mText.push_back( mFtpRequest.toString() ); 154 | // TODO get toBuffer to work here 155 | mConnectionControl->write( mFtpRequest.toBuffer() ); 156 | break; 157 | case FtpReplyCode_230_USER_LOGGED_IN_PROCEED: 158 | // TODO open data connection here 159 | break; 160 | case FtpReplyCode_331_USER_NAME_OKAY_NEED_PASSWORD: 161 | mFtpRequest.set( FtpCommand_PASS, "****" ); 162 | // TODO get toBuffer to work here 163 | mText.push_back( mFtpRequest.toString() ); 164 | mConnectionControl->write( mFtpRequest.toBuffer() ); 165 | break; 166 | default: 167 | mConnectionControl->close(); 168 | break; 169 | } 170 | } 171 | } 172 | 173 | CINDER_APP( FtpClientApp, RendererGl ) 174 | -------------------------------------------------------------------------------- /samples/FtpClient/src/TcpClientConnection.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "TcpClientConnection.h" 39 | 40 | #include "cinder/app/App.h" 41 | 42 | using namespace ci; 43 | using namespace ci::app; 44 | using namespace std; 45 | 46 | TcpClientConnectionRef TcpClientConnection::create( asio::io_service& io ) 47 | { 48 | return TcpClientConnectionRef( new TcpClientConnection( io ) ); 49 | } 50 | 51 | TcpClientConnection::TcpClientConnection( asio::io_service& io ) 52 | : mConnected( false ) 53 | { 54 | mClient = TcpClient::create( io ); 55 | 56 | mClient->connectConnectEventHandler( &TcpClientEventHandler::onConnect, &mClientEventHandler ); 57 | mClient->connectErrorEventHandler( &TcpClientEventHandler::onError, &mClientEventHandler ); 58 | mClient->connectResolveEventHandler( &TcpClientEventHandler::onResolve, &mClientEventHandler ); 59 | 60 | app::App::get()->getSignalUpdate().connect( bind( &TcpClientConnection::update, this ) ); 61 | } 62 | 63 | void TcpClientConnection::close() 64 | { 65 | if ( mClientEventHandler.getSession() ) { 66 | mClientEventHandler.getSession()->close(); 67 | } 68 | } 69 | 70 | void TcpClientConnection::connect( const string& host, int16_t port ) 71 | { 72 | if ( isConnected() ) { 73 | mClientEventHandler.getSession()->close(); 74 | } 75 | mClient->connect( host, port ); 76 | } 77 | 78 | void TcpClientConnection::write( const Buffer& buffer ) 79 | { 80 | if ( isConnected() ) { 81 | mClientEventHandler.getSession()->write( buffer ); 82 | } 83 | } 84 | 85 | const Buffer& TcpClientConnection::getBuffer() const 86 | { 87 | return mSessionEventHandler.getBuffer(); 88 | } 89 | 90 | bool TcpClientConnection::isConnected() const 91 | { 92 | return mClientEventHandler.getSession() && 93 | mClientEventHandler.getSession()->getSocket() && 94 | mClientEventHandler.getSession()->getSocket()->is_open(); 95 | } 96 | 97 | void TcpClientConnection::update() 98 | { 99 | bool connected = isConnected(); 100 | if ( mConnected != connected ) { 101 | mConnected = connected; 102 | if ( mConnected ) { 103 | mClientEventHandler.getSession()->connectCloseEventHandler( &TcpSessionEventHandler::onClose, &mSessionEventHandler ); 104 | mClientEventHandler.getSession()->connectErrorEventHandler( &TcpSessionEventHandler::onError, &mSessionEventHandler ); 105 | mClientEventHandler.getSession()->connectReadCompleteEventHandler( &TcpSessionEventHandler::onReadComplete, &mSessionEventHandler ); 106 | mClientEventHandler.getSession()->connectReadEventHandler( &TcpSessionEventHandler::onRead, &mSessionEventHandler ); 107 | mClientEventHandler.getSession()->connectWriteEventHandler( &TcpSessionEventHandler::onWrite, &mSessionEventHandler ); 108 | mClientEventHandler.getSession()->read(); 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /samples/FtpClient/src/TcpClientEventHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "TcpClientEventHandler.h" 39 | 40 | using namespace std; 41 | 42 | TcpClientEventHandler::TcpClientEventHandler() 43 | : mError( "" ) 44 | { 45 | } 46 | 47 | void TcpClientEventHandler::onConnect( TcpSessionRef session ) 48 | { 49 | mError = ""; 50 | mSession = session; 51 | } 52 | 53 | void TcpClientEventHandler::onError( string err, size_t bytesTransferred ) 54 | { 55 | mError = err; 56 | } 57 | 58 | void TcpClientEventHandler::onResolve() 59 | { 60 | mError = ""; 61 | } 62 | 63 | const string& TcpClientEventHandler::getError() const 64 | { 65 | return mError; 66 | } 67 | 68 | const TcpSessionRef& TcpClientEventHandler::getSession() const 69 | { 70 | return mSession; 71 | } 72 | -------------------------------------------------------------------------------- /samples/FtpClient/src/TcpSessionEventHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "TcpSessionEventHandler.h" 39 | 40 | using namespace ci; 41 | using namespace std; 42 | 43 | TcpSessionEventHandler::TcpSessionEventHandler() 44 | : mError( "" ), mReadComplete( false ), mWriteComplete( false ) 45 | { 46 | } 47 | 48 | const Buffer& TcpSessionEventHandler::getBuffer() const 49 | { 50 | return mBuffer; 51 | } 52 | 53 | const string& TcpSessionEventHandler::getError() const 54 | { 55 | return mError; 56 | } 57 | 58 | bool TcpSessionEventHandler::isReadComplete() const 59 | { 60 | return mReadComplete; 61 | } 62 | 63 | bool TcpSessionEventHandler::isWriteComplete() const 64 | { 65 | return mWriteComplete; 66 | } 67 | 68 | void TcpSessionEventHandler::onClose() 69 | { 70 | mError = ""; 71 | } 72 | 73 | void TcpSessionEventHandler::onError( string err, size_t bytesTransferred ) 74 | { 75 | mError = err; 76 | } 77 | 78 | void TcpSessionEventHandler::onRead( Buffer buffer ) 79 | { 80 | size_t len = buffer.getDataSize(); 81 | if ( !mBuffer ) { 82 | mBuffer = Buffer( len ); 83 | } else { 84 | mBuffer.resize( mBuffer.getDataSize() + len ); 85 | } 86 | mBuffer.copyFrom( buffer.getData(), len ); 87 | mError = ""; 88 | } 89 | 90 | void TcpSessionEventHandler::onReadComplete() 91 | { 92 | mError = ""; 93 | mReadComplete = true; 94 | } 95 | 96 | void TcpSessionEventHandler::onWrite( size_t bytesTransferred ) 97 | { 98 | mError = ""; 99 | mWriteComplete = true; 100 | } 101 | -------------------------------------------------------------------------------- /samples/FtpClient/vc2013/FtpClientApp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FtpClientApp", "FtpClientApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Debug|x64 = Debug|x64 12 | Release|Win32 = Release|Win32 13 | Release|x64 = Release|x64 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 23 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /samples/FtpClient/vc2013/FtpClientApp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 11 | 12 | 13 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 14 | h;hpp;hxx;hm;inl;inc;xsd 15 | 16 | 17 | {04862b34-f4de-4713-b6a9-05c481111dd5} 18 | 19 | 20 | {efa5588e-22c4-46b6-ab94-84395aaff046} 21 | 22 | 23 | {c339eb6d-075a-451f-81fb-01925a552431} 24 | 25 | 26 | 27 | 28 | Source Files 29 | 30 | 31 | Blocks\Cinder-Asio 32 | 33 | 34 | Blocks\Cinder-Asio 35 | 36 | 37 | Blocks\Cinder-Asio 38 | 39 | 40 | Blocks\Cinder-Asio 41 | 42 | 43 | Blocks\Cinder-Asio 44 | 45 | 46 | Blocks\Cinder-Asio 47 | 48 | 49 | Blocks\Cinder-Asio 50 | 51 | 52 | Blocks\Cinder-Asio 53 | 54 | 55 | Blocks\Cinder-Asio 56 | 57 | 58 | Blocks\Cinder-Asio 59 | 60 | 61 | Blocks\Cinder-Asio 62 | 63 | 64 | Blocks\Cinder-Protocol 65 | 66 | 67 | Blocks\Cinder-Protocol 68 | 69 | 70 | Blocks\Cinder-Protocol 71 | 72 | 73 | Blocks\Cinder-Protocol 74 | 75 | 76 | Blocks\Cinder-Protocol 77 | 78 | 79 | Blocks\Cinder-Protocol 80 | 81 | 82 | Blocks\Cinder-Protocol 83 | 84 | 85 | Blocks\Cinder-Protocol 86 | 87 | 88 | Blocks\Cinder-Protocol 89 | 90 | 91 | Blocks\Cinder-Protocol 92 | 93 | 94 | Source Files 95 | 96 | 97 | Source Files 98 | 99 | 100 | Source Files 101 | 102 | 103 | 104 | 105 | Resource Files 106 | 107 | 108 | 109 | 110 | Resource Files 111 | 112 | 113 | 114 | 115 | Blocks\Cinder-Asio 116 | 117 | 118 | Blocks\Cinder-Asio 119 | 120 | 121 | Blocks\Cinder-Asio 122 | 123 | 124 | Blocks\Cinder-Asio 125 | 126 | 127 | Blocks\Cinder-Asio 128 | 129 | 130 | Blocks\Cinder-Asio 131 | 132 | 133 | Blocks\Cinder-Asio 134 | 135 | 136 | Blocks\Cinder-Asio 137 | 138 | 139 | Blocks\Cinder-Asio 140 | 141 | 142 | Blocks\Cinder-Asio 143 | 144 | 145 | Blocks\Cinder-Asio 146 | 147 | 148 | Blocks\Cinder-Asio 149 | 150 | 151 | Blocks\Cinder-Asio 152 | 153 | 154 | Blocks\Cinder-Asio 155 | 156 | 157 | Blocks\Cinder-Asio 158 | 159 | 160 | Blocks\Cinder-Asio 161 | 162 | 163 | Blocks\Cinder-Asio 164 | 165 | 166 | Blocks\Cinder-Asio 167 | 168 | 169 | Blocks\Cinder-Asio 170 | 171 | 172 | Blocks\Cinder-Asio 173 | 174 | 175 | Blocks\Cinder-Asio 176 | 177 | 178 | Blocks\Cinder-Asio 179 | 180 | 181 | Blocks\Cinder-Protocol 182 | 183 | 184 | Blocks\Cinder-Protocol 185 | 186 | 187 | Blocks\Cinder-Protocol 188 | 189 | 190 | Blocks\Cinder-Protocol 191 | 192 | 193 | Blocks\Cinder-Protocol 194 | 195 | 196 | Blocks\Cinder-Protocol 197 | 198 | 199 | Blocks\Cinder-Protocol 200 | 201 | 202 | Blocks\Cinder-Protocol 203 | 204 | 205 | Blocks\Cinder-Protocol 206 | 207 | 208 | Blocks\Cinder-Protocol 209 | 210 | 211 | Header Files 212 | 213 | 214 | Header Files 215 | 216 | 217 | Header Files 218 | 219 | 220 | Blocks\Cinder-Asio 221 | 222 | 223 | -------------------------------------------------------------------------------- /samples/FtpClient/vc2013/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/FtpClient/vc2013/Resources.rc -------------------------------------------------------------------------------- /samples/FtpClient/vc2015/FtpClientApp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FtpClientApp", "FtpClientApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Debug|x64 = Debug|x64 12 | Release|Win32 = Release|Win32 13 | Release|x64 = Release|x64 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 23 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /samples/FtpClient/vc2015/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/FtpClient/vc2015/Resources.rc -------------------------------------------------------------------------------- /samples/FtpClient/xcode/FtpClient.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /samples/FtpClient/xcode/FtpClient_Prefix.pch: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #endif 4 | 5 | #if defined( __cplusplus ) 6 | #include "cinder/Cinder.h" 7 | 8 | #include "cinder/app/AppBasic.h" 9 | 10 | #include "cinder/gl/gl.h" 11 | 12 | #include "cinder/CinderMath.h" 13 | #include "cinder/Matrix.h" 14 | #include "cinder/Vector.h" 15 | #include "cinder/Quaternion.h" 16 | #endif 17 | -------------------------------------------------------------------------------- /samples/FtpClient/xcode/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | CinderApp.icns 11 | CFBundleIdentifier 12 | org.libcinder.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2013 __MyCompanyName__. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /samples/HttpClient/include/Resources.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cinder/CinderResources.h" 3 | 4 | //#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE ) 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /samples/HttpClient/resources/CinderApp.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpClient/resources/CinderApp.icns -------------------------------------------------------------------------------- /samples/HttpClient/resources/cinder_app_icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpClient/resources/cinder_app_icon.ico -------------------------------------------------------------------------------- /samples/HttpClient/src/HttpClientApp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "CinderAsio.h" 39 | 40 | #include "HttpRequest.h" 41 | #include "HttpResponse.h" 42 | #include "TcpClient.h" 43 | 44 | #include "cinder/app/App.h" 45 | #include "cinder/gl/gl.h" 46 | #include "cinder/params/Params.h" 47 | #include "cinder/Text.h" 48 | 49 | #include "boost/algorithm/string.hpp" 50 | 51 | class HttpClientApp : public ci::app::App 52 | { 53 | public: 54 | void draw(); 55 | void setup(); 56 | void update(); 57 | private: 58 | TcpClientRef mClient; 59 | TcpSessionRef mSession; 60 | std::string mHost; 61 | 62 | size_t mBytesRead; 63 | size_t mContentLength; 64 | std::string mFilename; 65 | int32_t mIndex; 66 | HttpRequest mHttpRequest; 67 | HttpResponse mHttpResponse; 68 | 69 | void write(); 70 | 71 | void onClose(); 72 | void onConnect( TcpSessionRef session ); 73 | void onError( std::string err, size_t bytesTransferred ); 74 | void onRead( ci::BufferRef buffer ); 75 | void onResolve(); 76 | void onWrite( size_t bytesTransferred ); 77 | 78 | ci::Font mFont; 79 | std::vector mText; 80 | ci::gl::TextureRef mTexture; 81 | 82 | float mFrameRate; 83 | bool mFullScreen; 84 | ci::params::InterfaceGlRef mParams; 85 | }; 86 | 87 | #include "cinder/app/RendererGl.h" 88 | #include "cinder/Utilities.h" 89 | #include 90 | #include 91 | 92 | using namespace ci; 93 | using namespace ci::app; 94 | using namespace std; 95 | 96 | void HttpClientApp::draw() 97 | { 98 | gl::clear( Colorf::black() ); 99 | gl::setMatricesWindow( getWindowSize() ); 100 | 101 | if ( mTexture ) { 102 | gl::draw( mTexture, ivec2( 250, 20 ) ); 103 | } 104 | 105 | mParams->draw(); 106 | } 107 | 108 | void HttpClientApp::onClose() 109 | { 110 | mText.push_back( "Disconnected" ); 111 | } 112 | 113 | void HttpClientApp::onConnect( TcpSessionRef session ) 114 | { 115 | mHttpResponse = HttpResponse(); 116 | mSession = session; 117 | mText.push_back( "Connected" ); 118 | 119 | mSession->connectCloseEventHandler( &HttpClientApp::onClose, this ); 120 | mSession->connectErrorEventHandler( &HttpClientApp::onError, this ); 121 | mSession->connectReadEventHandler( &HttpClientApp::onRead, this ); 122 | mSession->connectWriteEventHandler( &HttpClientApp::onWrite, this ); 123 | 124 | console() << mHttpRequest << endl; 125 | 126 | mSession->write( mHttpRequest.toBuffer() ); 127 | } 128 | 129 | void HttpClientApp::onError( string err, size_t bytesTransferred ) 130 | { 131 | string text = "Error"; 132 | if ( !err.empty() ) { 133 | text += ": " + err; 134 | } 135 | mText.push_back( text ); 136 | } 137 | 138 | void HttpClientApp::onRead( ci::BufferRef buffer ) 139 | { 140 | size_t sz = buffer->getSize(); 141 | mBytesRead += sz; 142 | mText.push_back( toString( sz ) + " bytes read" ); 143 | 144 | if ( !mHttpResponse.hasHeader() ) { 145 | 146 | // Parse header 147 | mHttpResponse.parseHeader( HttpResponse::bufferToString( buffer ) ); 148 | buffer = HttpResponse::removeHeader( buffer ); 149 | 150 | // Get content-length 151 | for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) { 152 | if ( kvp.first == "Content-Length" ) { 153 | mContentLength = fromString( kvp.second ); 154 | break; 155 | } 156 | } 157 | } 158 | 159 | // Append buffer to body 160 | mHttpResponse.append( buffer ); 161 | 162 | if ( mBytesRead < mContentLength ) { 163 | 164 | // Keep reading until we hit the content length 165 | mSession->read(); 166 | } else { 167 | 168 | mText.push_back( "Read complete" ); 169 | mText.push_back( toString( mHttpResponse.getStatusCode() ) + " " + mHttpResponse.getReason() ); 170 | 171 | if ( mHttpResponse.getStatusCode() == 200 ) { 172 | for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) { 173 | 174 | // Choose file extension based on MIME type 175 | if ( kvp.first == "Content-Type" ) { 176 | string mime = kvp.second; 177 | 178 | if ( mime == "audio/mp3" ) { 179 | mFilename += ".mp3"; 180 | } else if ( mime == "image/jpeg" ) { 181 | mFilename += ".jpg"; 182 | } else if ( mime == "image/png" ) { 183 | mFilename += ".png"; 184 | } 185 | } else if ( kvp.first == "Connection" ) { 186 | 187 | // Close connection if requested by server 188 | if ( kvp.second == "close" ) { 189 | mSession->close(); 190 | } 191 | } 192 | } 193 | 194 | // Save the file 195 | fs::path path = getAppPath(); 196 | #if !defined ( CINDER_MSW ) 197 | path = path.parent_path(); 198 | #endif 199 | path = path / mFilename; 200 | OStreamFileRef file = writeFileStream( path ); 201 | file->write( mHttpResponse.getBody() ); 202 | 203 | mText.push_back( mFilename + " downloaded" ); 204 | } else { 205 | 206 | // Write error 207 | mText.push_back( "Response: " + HttpResponse::bufferToString( mHttpResponse.getBody() ) ); 208 | 209 | mSession->close(); 210 | } 211 | } 212 | } 213 | 214 | void HttpClientApp::onResolve() 215 | { 216 | mText.push_back( "Endpoint resolved" ); 217 | } 218 | 219 | void HttpClientApp::onWrite( size_t bytesTransferred ) 220 | { 221 | mText.push_back(toString( bytesTransferred ) + " bytes written" ); 222 | mSession->read(); 223 | } 224 | 225 | void HttpClientApp::setup() 226 | { 227 | gl::enable( GL_TEXTURE_2D ); 228 | 229 | mBytesRead = 0; 230 | mContentLength = 0; 231 | mFont = Font( "Georgia", 24 ); 232 | mFrameRate = 0.0f; 233 | mFullScreen = false; 234 | mHost = "127.0.0.1"; 235 | mIndex = 0; 236 | 237 | mHttpRequest = HttpRequest( "GET", "/", HttpVersion::HTTP_1_1 ); 238 | mHttpRequest.setHeader( "Host", mHost ); 239 | mHttpRequest.setHeader( "Accept", "*/*" ); 240 | 241 | mParams = params::InterfaceGl::create( "Params", ivec2( 200, 150 ) ); 242 | mParams->addParam( "Frame rate", &mFrameRate, "", true ); 243 | mParams->addParam( "Full screen", &mFullScreen ).key( "f" ); 244 | mParams->addParam( "Image index", &mIndex, "min=0 max=3 step=1 keyDecr=i keyIncr=I" ); 245 | mParams->addParam( "Host", &mHost ); 246 | mParams->addButton( "Write", [ & ]() { write(); }, "key=w" ); 247 | mParams->addButton( "Quit", [ & ]() { quit(); }, "key=q" ); 248 | 249 | mClient = TcpClient::create( io_service() ); 250 | mClient->connectConnectEventHandler( &HttpClientApp::onConnect, this ); 251 | mClient->connectErrorEventHandler( &HttpClientApp::onError, this ); 252 | mClient->connectResolveEventHandler( &HttpClientApp::onResolve, this ); 253 | } 254 | 255 | void HttpClientApp::update() 256 | { 257 | mFrameRate = getFrameRate(); 258 | 259 | if ( mFullScreen != isFullScreen() ) { 260 | setFullScreen( mFullScreen ); 261 | mFullScreen = isFullScreen(); 262 | } 263 | 264 | if ( !mText.empty() ) { 265 | TextBox tbox = TextBox() 266 | .alignment( TextBox::LEFT ) 267 | .font( mFont ) 268 | .size( ivec2( getWindowWidth() - 250, TextBox::GROW ) ) 269 | .text( "" ); 270 | for ( vector::const_reverse_iterator iter = mText.rbegin(); iter != mText.rend(); ++iter ) { 271 | tbox.appendText( "> " + *iter + "\n" ); 272 | } 273 | tbox.setColor( ColorAf( 1.0f, 0.8f, 0.75f, 1.0f ) ); 274 | tbox.setBackgroundColor( ColorAf::black() ); 275 | tbox.setPremultiplied( false ); 276 | mTexture = gl::Texture::create( tbox.render() ); 277 | while ( mText.size() > 75 ) { 278 | mText.erase( mText.begin() ); 279 | } 280 | } 281 | } 282 | 283 | void HttpClientApp::write() 284 | { 285 | if ( mSession && mSession->getSocket()->is_open() ) { 286 | return; 287 | } 288 | 289 | // Reset download stats 290 | mBytesRead = 0; 291 | mContentLength = 0; 292 | 293 | // Update request body 294 | string index = toString( mIndex ); 295 | mFilename = index; 296 | mHttpRequest.setBody( HttpRequest::stringToBuffer( index ) ); 297 | 298 | mText.push_back( "Connecting to:\n" + mHost + ":2000" ); 299 | 300 | // Ports <1024 are restricted to root 301 | mClient->connect( mHost, 2000 ); 302 | } 303 | 304 | CINDER_APP( HttpClientApp, RendererGl ) 305 | -------------------------------------------------------------------------------- /samples/HttpClient/vc2013/HttpClientApp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HttpClientApp", "HttpClientApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Debug|x64 = Debug|x64 12 | Release|Win32 = Release|Win32 13 | Release|x64 = Release|x64 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 23 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /samples/HttpClient/vc2013/HttpClientApp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 11 | 12 | 13 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 14 | h;hpp;hxx;hm;inl;inc;xsd 15 | 16 | 17 | {04862b34-f4de-4713-b6a9-05c481111dd5} 18 | 19 | 20 | {efa5588e-22c4-46b6-ab94-84395aaff046} 21 | 22 | 23 | {c339eb6d-075a-451f-81fb-01925a552431} 24 | 25 | 26 | 27 | 28 | Source Files 29 | 30 | 31 | Blocks\Cinder-Asio 32 | 33 | 34 | Blocks\Cinder-Asio 35 | 36 | 37 | Blocks\Cinder-Asio 38 | 39 | 40 | Blocks\Cinder-Asio 41 | 42 | 43 | Blocks\Cinder-Asio 44 | 45 | 46 | Blocks\Cinder-Asio 47 | 48 | 49 | Blocks\Cinder-Asio 50 | 51 | 52 | Blocks\Cinder-Asio 53 | 54 | 55 | Blocks\Cinder-Asio 56 | 57 | 58 | Blocks\Cinder-Asio 59 | 60 | 61 | Blocks\Cinder-Asio 62 | 63 | 64 | Blocks\Cinder-Protocol 65 | 66 | 67 | Blocks\Cinder-Protocol 68 | 69 | 70 | Blocks\Cinder-Protocol 71 | 72 | 73 | Blocks\Cinder-Protocol 74 | 75 | 76 | Blocks\Cinder-Protocol 77 | 78 | 79 | Blocks\Cinder-Protocol 80 | 81 | 82 | Blocks\Cinder-Protocol 83 | 84 | 85 | 86 | 87 | Resource Files 88 | 89 | 90 | 91 | 92 | Resource Files 93 | 94 | 95 | 96 | 97 | Blocks\Cinder-Asio 98 | 99 | 100 | Blocks\Cinder-Asio 101 | 102 | 103 | Blocks\Cinder-Asio 104 | 105 | 106 | Blocks\Cinder-Asio 107 | 108 | 109 | Blocks\Cinder-Asio 110 | 111 | 112 | Blocks\Cinder-Asio 113 | 114 | 115 | Blocks\Cinder-Asio 116 | 117 | 118 | Blocks\Cinder-Asio 119 | 120 | 121 | Blocks\Cinder-Asio 122 | 123 | 124 | Blocks\Cinder-Asio 125 | 126 | 127 | Blocks\Cinder-Asio 128 | 129 | 130 | Blocks\Cinder-Asio 131 | 132 | 133 | Blocks\Cinder-Asio 134 | 135 | 136 | Blocks\Cinder-Asio 137 | 138 | 139 | Blocks\Cinder-Asio 140 | 141 | 142 | Blocks\Cinder-Asio 143 | 144 | 145 | Blocks\Cinder-Asio 146 | 147 | 148 | Blocks\Cinder-Asio 149 | 150 | 151 | Blocks\Cinder-Asio 152 | 153 | 154 | Blocks\Cinder-Asio 155 | 156 | 157 | Blocks\Cinder-Asio 158 | 159 | 160 | Blocks\Cinder-Asio 161 | 162 | 163 | Blocks\Cinder-Protocol 164 | 165 | 166 | Blocks\Cinder-Protocol 167 | 168 | 169 | Blocks\Cinder-Protocol 170 | 171 | 172 | Blocks\Cinder-Protocol 173 | 174 | 175 | Blocks\Cinder-Protocol 176 | 177 | 178 | Blocks\Cinder-Protocol 179 | 180 | 181 | Blocks\Cinder-Protocol 182 | 183 | 184 | Blocks\Cinder-Asio 185 | 186 | 187 | -------------------------------------------------------------------------------- /samples/HttpClient/vc2013/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpClient/vc2013/Resources.rc -------------------------------------------------------------------------------- /samples/HttpClient/vc2015/HttpClientApp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HttpClientApp", "HttpClientApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Debug|x64 = Debug|x64 12 | Release|Win32 = Release|Win32 13 | Release|x64 = Release|x64 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 23 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /samples/HttpClient/vc2015/HttpClientApp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 11 | 12 | 13 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 14 | h;hpp;hxx;hm;inl;inc;xsd 15 | 16 | 17 | {04862b34-f4de-4713-b6a9-05c481111dd5} 18 | 19 | 20 | {efa5588e-22c4-46b6-ab94-84395aaff046} 21 | 22 | 23 | {c339eb6d-075a-451f-81fb-01925a552431} 24 | 25 | 26 | 27 | 28 | Source Files 29 | 30 | 31 | Blocks\Cinder-Asio 32 | 33 | 34 | Blocks\Cinder-Asio 35 | 36 | 37 | Blocks\Cinder-Asio 38 | 39 | 40 | Blocks\Cinder-Asio 41 | 42 | 43 | Blocks\Cinder-Asio 44 | 45 | 46 | Blocks\Cinder-Asio 47 | 48 | 49 | Blocks\Cinder-Asio 50 | 51 | 52 | Blocks\Cinder-Asio 53 | 54 | 55 | Blocks\Cinder-Asio 56 | 57 | 58 | Blocks\Cinder-Asio 59 | 60 | 61 | Blocks\Cinder-Asio 62 | 63 | 64 | Blocks\Cinder-Protocol 65 | 66 | 67 | Blocks\Cinder-Protocol 68 | 69 | 70 | Blocks\Cinder-Protocol 71 | 72 | 73 | Blocks\Cinder-Protocol 74 | 75 | 76 | Blocks\Cinder-Protocol 77 | 78 | 79 | Blocks\Cinder-Protocol 80 | 81 | 82 | Blocks\Cinder-Protocol 83 | 84 | 85 | 86 | 87 | Resource Files 88 | 89 | 90 | 91 | 92 | Resource Files 93 | 94 | 95 | 96 | 97 | Blocks\Cinder-Asio 98 | 99 | 100 | Blocks\Cinder-Asio 101 | 102 | 103 | Blocks\Cinder-Asio 104 | 105 | 106 | Blocks\Cinder-Asio 107 | 108 | 109 | Blocks\Cinder-Asio 110 | 111 | 112 | Blocks\Cinder-Asio 113 | 114 | 115 | Blocks\Cinder-Asio 116 | 117 | 118 | Blocks\Cinder-Asio 119 | 120 | 121 | Blocks\Cinder-Asio 122 | 123 | 124 | Blocks\Cinder-Asio 125 | 126 | 127 | Blocks\Cinder-Asio 128 | 129 | 130 | Blocks\Cinder-Asio 131 | 132 | 133 | Blocks\Cinder-Asio 134 | 135 | 136 | Blocks\Cinder-Asio 137 | 138 | 139 | Blocks\Cinder-Asio 140 | 141 | 142 | Blocks\Cinder-Asio 143 | 144 | 145 | Blocks\Cinder-Asio 146 | 147 | 148 | Blocks\Cinder-Asio 149 | 150 | 151 | Blocks\Cinder-Asio 152 | 153 | 154 | Blocks\Cinder-Asio 155 | 156 | 157 | Blocks\Cinder-Asio 158 | 159 | 160 | Blocks\Cinder-Asio 161 | 162 | 163 | Blocks\Cinder-Protocol 164 | 165 | 166 | Blocks\Cinder-Protocol 167 | 168 | 169 | Blocks\Cinder-Protocol 170 | 171 | 172 | Blocks\Cinder-Protocol 173 | 174 | 175 | Blocks\Cinder-Protocol 176 | 177 | 178 | Blocks\Cinder-Protocol 179 | 180 | 181 | Blocks\Cinder-Protocol 182 | 183 | 184 | Blocks\Cinder-Asio 185 | 186 | 187 | -------------------------------------------------------------------------------- /samples/HttpClient/vc2015/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpClient/vc2015/Resources.rc -------------------------------------------------------------------------------- /samples/HttpClient/xcode/HttpClient.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /samples/HttpClient/xcode/HttpClient_Prefix.pch: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #endif 4 | 5 | #if defined( __cplusplus ) 6 | #include "cinder/Cinder.h" 7 | 8 | #include "cinder/app/AppBasic.h" 9 | 10 | #include "cinder/gl/gl.h" 11 | 12 | #include "cinder/CinderMath.h" 13 | #include "cinder/Matrix.h" 14 | #include "cinder/Vector.h" 15 | #include "cinder/Quaternion.h" 16 | #endif 17 | -------------------------------------------------------------------------------- /samples/HttpClient/xcode/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | CinderApp.icns 11 | CFBundleIdentifier 12 | org.libcinder.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2013 __MyCompanyName__. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /samples/HttpServer/assets/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpServer/assets/0.jpg -------------------------------------------------------------------------------- /samples/HttpServer/assets/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpServer/assets/1.png -------------------------------------------------------------------------------- /samples/HttpServer/assets/2.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpServer/assets/2.mp3 -------------------------------------------------------------------------------- /samples/HttpServer/include/Resources.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cinder/CinderResources.h" 3 | 4 | //#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE ) 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /samples/HttpServer/resources/CinderApp.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpServer/resources/CinderApp.icns -------------------------------------------------------------------------------- /samples/HttpServer/resources/cinder_app_icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpServer/resources/cinder_app_icon.ico -------------------------------------------------------------------------------- /samples/HttpServer/src/HttpServerApp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "CinderAsio.h" 39 | 40 | #include "HttpRequest.h" 41 | #include "HttpResponse.h" 42 | #include "TcpServer.h" 43 | 44 | #include "cinder/app/App.h" 45 | #include "cinder/gl/gl.h" 46 | #include "cinder/params/Params.h" 47 | #include "cinder/Text.h" 48 | 49 | #include "boost/algorithm/string.hpp" 50 | 51 | class HttpServerApp : public ci::app::App 52 | { 53 | public: 54 | void draw(); 55 | void setup(); 56 | void update(); 57 | private: 58 | void accept(); 59 | TcpServerRef mServer; 60 | TcpSessionRef mSession; 61 | 62 | HttpRequest mHttpRequest; 63 | HttpResponse mHttpResponse; 64 | 65 | void onAccept( TcpSessionRef session ); 66 | void onCancel(); 67 | void onClose(); 68 | void onError( std::string err, size_t bytesTransferred ); 69 | void onRead( ci::BufferRef buffer ); 70 | void onWrite( size_t bytesTransferred ); 71 | 72 | ci::Font mFont; 73 | std::vector mText; 74 | ci::gl::TextureRef mTexture; 75 | 76 | float mFrameRate; 77 | bool mFullScreen; 78 | ci::params::InterfaceGlRef mParams; 79 | }; 80 | 81 | #include "cinder/app/RendererGl.h" 82 | #include "cinder/Utilities.h" 83 | 84 | using namespace ci; 85 | using namespace ci::app; 86 | using namespace std; 87 | 88 | void HttpServerApp::accept() 89 | { 90 | 91 | // Ports <1024 are restricted to root 92 | if ( mServer ) { 93 | mServer->accept( 2000 ); 94 | mText.push_back( "Listening on port: 2000" ); 95 | } 96 | } 97 | 98 | void HttpServerApp::draw() 99 | { 100 | gl::clear( Colorf::black() ); 101 | gl::setMatricesWindow( getWindowSize() ); 102 | 103 | if ( mTexture ) { 104 | gl::draw( mTexture, ivec2( 250, 20 ) ); 105 | } 106 | 107 | mParams->draw(); 108 | } 109 | 110 | void HttpServerApp::onAccept( TcpSessionRef session ) 111 | { 112 | mHttpRequest = HttpRequest(); 113 | mSession = session; 114 | mText.push_back( "Connected" ); 115 | 116 | mSession->connectCloseEventHandler( [ & ]() { 117 | mText.push_back( "Session closed" ); 118 | } ); 119 | mSession->connectErrorEventHandler( &HttpServerApp::onError, this ); 120 | mSession->connectReadEventHandler( &HttpServerApp::onRead, this ); 121 | mSession->connectWriteEventHandler( &HttpServerApp::onWrite, this ); 122 | 123 | mSession->read(); 124 | } 125 | 126 | void HttpServerApp::onCancel() 127 | { 128 | mText.push_back( "Canceled" ); 129 | 130 | accept(); 131 | } 132 | 133 | void HttpServerApp::onClose() 134 | { 135 | mText.push_back( "Disconnected" ); 136 | 137 | accept(); 138 | } 139 | 140 | void HttpServerApp::onError( string err, size_t bytesTransferred ) 141 | { 142 | string text = "Error"; 143 | if ( !err.empty() ) { 144 | text += ": " + err; 145 | } 146 | mText.push_back( text ); 147 | } 148 | 149 | void HttpServerApp::onRead( ci::BufferRef buffer ) 150 | { 151 | mText.push_back( toString( buffer->getSize() ) + " bytes read" ); 152 | 153 | // Parse the request buffer into headers and body 154 | mHttpRequest.parse( buffer ); 155 | 156 | console() << mHttpRequest << endl; 157 | 158 | // We're expecting a number between 0 and 2 from the client 159 | string request = HttpRequest::bufferToString( mHttpRequest.getBody() ); 160 | int32_t index = -1; 161 | try { 162 | index = fromString( request ); 163 | } catch ( boost::bad_lexical_cast ) { 164 | console() << "Unable to cast to int32_t" << endl; 165 | } 166 | 167 | // Start a new response 168 | mHttpResponse = HttpResponse(); 169 | mHttpResponse.setHeader( "Server", "Cinder" ); 170 | mHttpResponse.setHttpVersion( HttpVersion::HTTP_1_1 ); 171 | 172 | // Declare an empty response body 173 | BufferRef body; 174 | 175 | if ( index < 0 || index > 2 ) { 176 | 177 | // The request was invalid, so let's create an error 178 | mHttpResponse.setHeader( "Content-Type", "text/html; charset=uhf-8" ); 179 | mHttpResponse.setReason( "Internal Server Error" ); 180 | mHttpResponse.setStatusCode( 500 ); 181 | 182 | // Set the message body 183 | string msg = "\"" + request + "\" is invalid"; 184 | body = HttpResponse::stringToBuffer( msg ); 185 | mHttpResponse.setBody( body ); 186 | } else { 187 | 188 | // Request was OK 189 | mHttpResponse.setReason( "OK" ); 190 | mHttpResponse.setStatusCode( 200 ); 191 | 192 | // Let's pick out a mime type and file based on the requested index 193 | fs::path path; 194 | string mime = ""; 195 | switch ( index ) { 196 | case 0: 197 | mime = "image/jpeg"; 198 | path = getAssetPath( request + ".jpg" ); 199 | break; 200 | case 1: 201 | mime = "image/png"; 202 | path = getAssetPath( request + ".png" ); 203 | break; 204 | case 2: 205 | mime = "audio/mp3"; 206 | path = getAssetPath( request + ".mp3" ); 207 | break; 208 | } 209 | mHttpResponse.setHeader( "Content-Type", mime ); 210 | 211 | // Load the file from assets into a buffer 212 | DataSourceRef file = loadFile( path ); 213 | body = file->getBuffer(); 214 | } 215 | 216 | // Set the content length using the body size 217 | mHttpResponse.setBody( body ); 218 | size_t sz = body->getSize(); 219 | mHttpResponse.setHeader( "Content-Length", toString( sz ) ); 220 | 221 | // Tell the client to close the connection when they're finished 222 | mHttpResponse.setHeader( "Connection", "close" ); 223 | 224 | // Send the response back to the client 225 | mSession->write( mHttpResponse.toBuffer() ); 226 | } 227 | 228 | void HttpServerApp::onWrite( size_t bytesTransferred ) 229 | { 230 | mText.push_back( toString( bytesTransferred ) + " bytes written" ); 231 | 232 | mSession->close(); 233 | accept(); 234 | } 235 | 236 | void HttpServerApp::setup() 237 | { 238 | gl::enable( GL_TEXTURE_2D ); 239 | 240 | mFont = Font( "Georgia", 24 ); 241 | mFrameRate = 0.0f; 242 | mFullScreen = false; 243 | 244 | mParams = params::InterfaceGl::create( "Params", ivec2( 200, 150 ) ); 245 | mParams->addParam( "Frame rate", &mFrameRate, "", true ); 246 | mParams->addParam( "Full screen", &mFullScreen ).key( "f" ); 247 | mParams->addButton( "Quit", bind( &HttpServerApp::quit, this ), "key=q" ); 248 | 249 | mServer = TcpServer::create( io_service() ); 250 | 251 | mServer->connectAcceptEventHandler( &HttpServerApp::onAccept, this ); 252 | mServer->connectCancelEventHandler( &HttpServerApp::onCancel, this ); 253 | mServer->connectErrorEventHandler( &HttpServerApp::onError, this ); 254 | 255 | accept(); 256 | } 257 | 258 | void HttpServerApp::update() 259 | { 260 | mFrameRate = getFrameRate(); 261 | 262 | if ( mFullScreen != isFullScreen() ) { 263 | setFullScreen( mFullScreen ); 264 | mFullScreen = isFullScreen(); 265 | } 266 | 267 | if ( !mText.empty() ) { 268 | TextBox tbox = TextBox() 269 | .alignment( TextBox::LEFT ) 270 | .font( mFont ) 271 | .size( ivec2( getWindowWidth() - 250, TextBox::GROW ) ) 272 | .text( "" ); 273 | for ( vector::const_reverse_iterator iter = mText.rbegin(); iter != mText.rend(); ++iter ) { 274 | tbox.appendText( "> " + *iter + "\n" ); 275 | } 276 | tbox.setColor( ColorAf( 1.0f, 0.8f, 0.75f, 1.0f ) ); 277 | tbox.setBackgroundColor( ColorAf::black() ); 278 | tbox.setPremultiplied( false ); 279 | mTexture = gl::Texture::create( tbox.render() ); 280 | while ( mText.size() > 75 ) { 281 | mText.erase( mText.begin() ); 282 | } 283 | } 284 | } 285 | 286 | CINDER_APP( HttpServerApp, RendererGl ) 287 | -------------------------------------------------------------------------------- /samples/HttpServer/vc2013/HttpServerApp.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio 2013 3 | VisualStudioVersion = 12.0.21005.1 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HttpServerApp", "HttpServerApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 6 | EndProject 7 | Global 8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 9 | Debug|Win32 = Debug|Win32 10 | Debug|x64 = Debug|x64 11 | Release|Win32 = Release|Win32 12 | Release|x64 = Release|x64 13 | EndGlobalSection 14 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 15 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | EndGlobal 28 | -------------------------------------------------------------------------------- /samples/HttpServer/vc2013/HttpServerApp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 11 | 12 | 13 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 14 | h;hpp;hxx;hm;inl;inc;xsd 15 | 16 | 17 | {04862b34-f4de-4713-b6a9-05c481111dd5} 18 | 19 | 20 | {efa5588e-22c4-46b6-ab94-84395aaff046} 21 | 22 | 23 | {c339eb6d-075a-451f-81fb-01925a552431} 24 | 25 | 26 | 27 | 28 | Source Files 29 | 30 | 31 | Blocks\Cinder-Asio 32 | 33 | 34 | Blocks\Cinder-Asio 35 | 36 | 37 | Blocks\Cinder-Asio 38 | 39 | 40 | Blocks\Cinder-Asio 41 | 42 | 43 | Blocks\Cinder-Asio 44 | 45 | 46 | Blocks\Cinder-Asio 47 | 48 | 49 | Blocks\Cinder-Asio 50 | 51 | 52 | Blocks\Cinder-Asio 53 | 54 | 55 | Blocks\Cinder-Asio 56 | 57 | 58 | Blocks\Cinder-Asio 59 | 60 | 61 | Blocks\Cinder-Asio 62 | 63 | 64 | Blocks\Cinder-Protocol 65 | 66 | 67 | Blocks\Cinder-Protocol 68 | 69 | 70 | Blocks\Cinder-Protocol 71 | 72 | 73 | Blocks\Cinder-Protocol 74 | 75 | 76 | Blocks\Cinder-Protocol 77 | 78 | 79 | Blocks\Cinder-Protocol 80 | 81 | 82 | Blocks\Cinder-Protocol 83 | 84 | 85 | Blocks\Cinder-Protocol 86 | 87 | 88 | Blocks\Cinder-Protocol 89 | 90 | 91 | Blocks\Cinder-Protocol 92 | 93 | 94 | 95 | 96 | Resource Files 97 | 98 | 99 | 100 | 101 | Resource Files 102 | 103 | 104 | 105 | 106 | Blocks\Cinder-Asio 107 | 108 | 109 | Blocks\Cinder-Asio 110 | 111 | 112 | Blocks\Cinder-Asio 113 | 114 | 115 | Blocks\Cinder-Asio 116 | 117 | 118 | Blocks\Cinder-Asio 119 | 120 | 121 | Blocks\Cinder-Asio 122 | 123 | 124 | Blocks\Cinder-Asio 125 | 126 | 127 | Blocks\Cinder-Asio 128 | 129 | 130 | Blocks\Cinder-Asio 131 | 132 | 133 | Blocks\Cinder-Asio 134 | 135 | 136 | Blocks\Cinder-Asio 137 | 138 | 139 | Blocks\Cinder-Asio 140 | 141 | 142 | Blocks\Cinder-Asio 143 | 144 | 145 | Blocks\Cinder-Asio 146 | 147 | 148 | Blocks\Cinder-Asio 149 | 150 | 151 | Blocks\Cinder-Asio 152 | 153 | 154 | Blocks\Cinder-Asio 155 | 156 | 157 | Blocks\Cinder-Asio 158 | 159 | 160 | Blocks\Cinder-Asio 161 | 162 | 163 | Blocks\Cinder-Asio 164 | 165 | 166 | Blocks\Cinder-Asio 167 | 168 | 169 | Blocks\Cinder-Asio 170 | 171 | 172 | Blocks\Cinder-Asio 173 | 174 | 175 | Blocks\Cinder-Protocol 176 | 177 | 178 | Blocks\Cinder-Protocol 179 | 180 | 181 | Blocks\Cinder-Protocol 182 | 183 | 184 | Blocks\Cinder-Protocol 185 | 186 | 187 | Blocks\Cinder-Protocol 188 | 189 | 190 | Blocks\Cinder-Protocol 191 | 192 | 193 | Blocks\Cinder-Protocol 194 | 195 | 196 | Blocks\Cinder-Protocol 197 | 198 | 199 | Blocks\Cinder-Protocol 200 | 201 | 202 | Blocks\Cinder-Protocol 203 | 204 | 205 | -------------------------------------------------------------------------------- /samples/HttpServer/vc2013/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpServer/vc2013/Resources.rc -------------------------------------------------------------------------------- /samples/HttpServer/vc2015/HttpServerApp.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio 2013 3 | VisualStudioVersion = 12.0.21005.1 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HttpServerApp", "HttpServerApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 6 | EndProject 7 | Global 8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 9 | Debug|Win32 = Debug|Win32 10 | Debug|x64 = Debug|x64 11 | Release|Win32 = Release|Win32 12 | Release|x64 = Release|x64 13 | EndGlobalSection 14 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 15 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | EndGlobal 28 | -------------------------------------------------------------------------------- /samples/HttpServer/vc2015/HttpServerApp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 11 | 12 | 13 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 14 | h;hpp;hxx;hm;inl;inc;xsd 15 | 16 | 17 | {04862b34-f4de-4713-b6a9-05c481111dd5} 18 | 19 | 20 | {efa5588e-22c4-46b6-ab94-84395aaff046} 21 | 22 | 23 | {c339eb6d-075a-451f-81fb-01925a552431} 24 | 25 | 26 | 27 | 28 | Source Files 29 | 30 | 31 | Blocks\Cinder-Asio 32 | 33 | 34 | Blocks\Cinder-Asio 35 | 36 | 37 | Blocks\Cinder-Asio 38 | 39 | 40 | Blocks\Cinder-Asio 41 | 42 | 43 | Blocks\Cinder-Asio 44 | 45 | 46 | Blocks\Cinder-Asio 47 | 48 | 49 | Blocks\Cinder-Asio 50 | 51 | 52 | Blocks\Cinder-Asio 53 | 54 | 55 | Blocks\Cinder-Asio 56 | 57 | 58 | Blocks\Cinder-Asio 59 | 60 | 61 | Blocks\Cinder-Asio 62 | 63 | 64 | Blocks\Cinder-Protocol 65 | 66 | 67 | Blocks\Cinder-Protocol 68 | 69 | 70 | Blocks\Cinder-Protocol 71 | 72 | 73 | Blocks\Cinder-Protocol 74 | 75 | 76 | Blocks\Cinder-Protocol 77 | 78 | 79 | Blocks\Cinder-Protocol 80 | 81 | 82 | Blocks\Cinder-Protocol 83 | 84 | 85 | Blocks\Cinder-Protocol 86 | 87 | 88 | Blocks\Cinder-Protocol 89 | 90 | 91 | Blocks\Cinder-Protocol 92 | 93 | 94 | 95 | 96 | Resource Files 97 | 98 | 99 | 100 | 101 | Resource Files 102 | 103 | 104 | 105 | 106 | Blocks\Cinder-Asio 107 | 108 | 109 | Blocks\Cinder-Asio 110 | 111 | 112 | Blocks\Cinder-Asio 113 | 114 | 115 | Blocks\Cinder-Asio 116 | 117 | 118 | Blocks\Cinder-Asio 119 | 120 | 121 | Blocks\Cinder-Asio 122 | 123 | 124 | Blocks\Cinder-Asio 125 | 126 | 127 | Blocks\Cinder-Asio 128 | 129 | 130 | Blocks\Cinder-Asio 131 | 132 | 133 | Blocks\Cinder-Asio 134 | 135 | 136 | Blocks\Cinder-Asio 137 | 138 | 139 | Blocks\Cinder-Asio 140 | 141 | 142 | Blocks\Cinder-Asio 143 | 144 | 145 | Blocks\Cinder-Asio 146 | 147 | 148 | Blocks\Cinder-Asio 149 | 150 | 151 | Blocks\Cinder-Asio 152 | 153 | 154 | Blocks\Cinder-Asio 155 | 156 | 157 | Blocks\Cinder-Asio 158 | 159 | 160 | Blocks\Cinder-Asio 161 | 162 | 163 | Blocks\Cinder-Asio 164 | 165 | 166 | Blocks\Cinder-Asio 167 | 168 | 169 | Blocks\Cinder-Asio 170 | 171 | 172 | Blocks\Cinder-Asio 173 | 174 | 175 | Blocks\Cinder-Protocol 176 | 177 | 178 | Blocks\Cinder-Protocol 179 | 180 | 181 | Blocks\Cinder-Protocol 182 | 183 | 184 | Blocks\Cinder-Protocol 185 | 186 | 187 | Blocks\Cinder-Protocol 188 | 189 | 190 | Blocks\Cinder-Protocol 191 | 192 | 193 | Blocks\Cinder-Protocol 194 | 195 | 196 | Blocks\Cinder-Protocol 197 | 198 | 199 | Blocks\Cinder-Protocol 200 | 201 | 202 | Blocks\Cinder-Protocol 203 | 204 | 205 | -------------------------------------------------------------------------------- /samples/HttpServer/vc2015/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/HttpServer/vc2015/Resources.rc -------------------------------------------------------------------------------- /samples/HttpServer/xcode/HttpServer.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /samples/HttpServer/xcode/HttpServer_Prefix.pch: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #endif 4 | 5 | #if defined( __cplusplus ) 6 | #include "cinder/Cinder.h" 7 | 8 | #include "cinder/app/AppBasic.h" 9 | 10 | #include "cinder/gl/gl.h" 11 | 12 | #include "cinder/CinderMath.h" 13 | #include "cinder/Matrix.h" 14 | #include "cinder/Vector.h" 15 | #include "cinder/Quaternion.h" 16 | #endif 17 | -------------------------------------------------------------------------------- /samples/HttpServer/xcode/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | CinderApp.icns 11 | CFBundleIdentifier 12 | org.libcinder.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2013 __MyCompanyName__. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /samples/WebClient/include/Resources.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cinder/CinderResources.h" 3 | 4 | //#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE ) 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /samples/WebClient/resources/CinderApp.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/WebClient/resources/CinderApp.icns -------------------------------------------------------------------------------- /samples/WebClient/resources/cinder_app_icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/WebClient/resources/cinder_app_icon.ico -------------------------------------------------------------------------------- /samples/WebClient/src/WebClientApp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "CinderAsio.h" 39 | 40 | #include "cinder/app/App.h" 41 | #include "cinder/gl/gl.h" 42 | #include "cinder/params/Params.h" 43 | #include "cinder/Text.h" 44 | 45 | #include "boost/algorithm/string.hpp" 46 | 47 | #include "HttpRequest.h" 48 | #include "HttpResponse.h" 49 | #include "TcpClient.h" 50 | 51 | class WebClientApp : public ci::app::App 52 | { 53 | public: 54 | void draw() override; 55 | void setup() override; 56 | void update() override; 57 | private: 58 | TcpClientRef mClient; 59 | TcpSessionRef mSession; 60 | std::string mHost; 61 | int32_t mPort; 62 | 63 | HttpRequest mHttpRequest; 64 | HttpResponse mHttpResponse; 65 | 66 | void write(); 67 | 68 | void onClose(); 69 | void onConnect( TcpSessionRef session ); 70 | void onError( std::string err, size_t bytesTransferred ); 71 | void onRead( ci::BufferRef buffer ); 72 | void onReadComplete(); 73 | void onResolve(); 74 | void onWrite( size_t bytesTransferred ); 75 | 76 | ci::Font mFont; 77 | std::vector mText; 78 | ci::gl::TextureRef mTexture; 79 | 80 | float mFrameRate; 81 | bool mFullScreen; 82 | ci::params::InterfaceGlRef mParams; 83 | }; 84 | 85 | #include "cinder/app/RendererGl.h" 86 | #include "cinder/Utilities.h" 87 | 88 | using namespace ci; 89 | using namespace ci::app; 90 | using namespace std; 91 | 92 | void WebClientApp::draw() 93 | { 94 | gl::clear( Colorf::black() ); 95 | gl::setMatricesWindow( getWindowSize() ); 96 | 97 | if ( mTexture ) { 98 | gl::draw( mTexture, ivec2( 250, 20 ) ); 99 | } 100 | 101 | mParams->draw(); 102 | } 103 | 104 | void WebClientApp::onClose() 105 | { 106 | mText.push_back( "Disconnected" ); 107 | } 108 | 109 | void WebClientApp::onConnect( TcpSessionRef session ) 110 | { 111 | mHttpResponse = HttpResponse(); 112 | mSession = session; 113 | mText.push_back( "Connected" ); 114 | 115 | mSession->connectCloseEventHandler( &WebClientApp::onClose, this ); 116 | mSession->connectErrorEventHandler( &WebClientApp::onError, this ); 117 | mSession->connectReadCompleteEventHandler( &WebClientApp::onReadComplete, this ); 118 | mSession->connectReadEventHandler( &WebClientApp::onRead, this ); 119 | mSession->connectWriteEventHandler( &WebClientApp::onWrite, this ); 120 | 121 | mSession->write( mHttpRequest.toBuffer() ); 122 | } 123 | 124 | void WebClientApp::onError( string err, size_t bytesTransferred ) 125 | { 126 | string text = "Error"; 127 | if ( !err.empty() ) { 128 | text += ": " + err; 129 | } 130 | mText.push_back( text ); 131 | } 132 | 133 | void WebClientApp::onRead( ci::BufferRef buffer ) 134 | { 135 | mText.push_back(toString( buffer->getSize() ) + " bytes read" ); 136 | 137 | if ( !mHttpResponse.hasHeader() ) { 138 | mHttpResponse.parseHeader( HttpResponse::bufferToString( buffer ) ); 139 | buffer = HttpResponse::removeHeader( buffer ); 140 | } 141 | mHttpResponse.append( buffer ); 142 | 143 | mSession->read(); 144 | } 145 | 146 | void WebClientApp::onReadComplete() 147 | { 148 | mText.push_back( "Read complete" ); 149 | 150 | console() << "HTTP version: "; 151 | switch ( mHttpResponse.getHttpVersion() ) { 152 | case HttpVersion::HTTP_0_9: 153 | console() << "0.9"; 154 | break; 155 | case HttpVersion::HTTP_1_0: 156 | console() << "1.0"; 157 | break; 158 | case HttpVersion::HTTP_1_1: 159 | console() << "1.1"; 160 | break; 161 | case HttpVersion::HTTP_2_0: 162 | console() << "2.0"; 163 | break; 164 | } 165 | console() << endl; 166 | console() << "Status code: " << mHttpResponse.getStatusCode() << endl; 167 | console() << "Reason: " << mHttpResponse.getReason() << endl; 168 | 169 | console() << "Header fields: " << endl; 170 | for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) { 171 | console() << ">> " << kvp.first << ": " << kvp.second << endl; 172 | } 173 | console() << endl; 174 | 175 | console() << "Body:" << endl; 176 | console() << HttpResponse::bufferToString( mHttpResponse.getBody() ) << endl; 177 | 178 | mSession->close(); 179 | } 180 | 181 | void WebClientApp::onResolve() 182 | { 183 | mText.push_back( "Endpoint resolved" ); 184 | } 185 | 186 | void WebClientApp::onWrite( size_t bytesTransferred ) 187 | { 188 | mText.push_back(toString( bytesTransferred ) + " bytes written" ); 189 | mSession->read(); 190 | } 191 | 192 | void WebClientApp::setup() 193 | { 194 | gl::enable( GL_TEXTURE_2D ); 195 | 196 | mFont = Font( "Georgia", 24 ); 197 | mFrameRate = 0.0f; 198 | mFullScreen = false; 199 | mHost = "libcinder.org"; 200 | mPort = 80; 201 | 202 | mHttpRequest = HttpRequest( "GET", "/", HttpVersion::HTTP_1_0 ); 203 | mHttpRequest.setHeader( "Host", mHost ); 204 | mHttpRequest.setHeader( "Accept", "*/*" ); 205 | mHttpRequest.setHeader( "Connection", "close" ); 206 | 207 | mParams = params::InterfaceGl::create( "Params", ivec2( 200, 150 ) ); 208 | mParams->addParam( "Frame rate", &mFrameRate, "", true ); 209 | mParams->addParam( "Full screen", &mFullScreen ).key( "f" ); 210 | mParams->addParam( "Host", &mHost ); 211 | mParams->addParam( "Port", &mPort, "min=0 max=65535 step=1 keyDecr=p keyIncr=P" ); 212 | mParams->addButton( "Write", bind( &WebClientApp::write, this ), "key=w" ); 213 | mParams->addButton( "Quit", bind( &WebClientApp::quit, this ), "key=q" ); 214 | 215 | mClient = TcpClient::create( io_service() ); 216 | mClient->connectConnectEventHandler( &WebClientApp::onConnect, this ); 217 | mClient->connectErrorEventHandler( &WebClientApp::onError, this ); 218 | mClient->connectResolveEventHandler( &WebClientApp::onResolve, this ); 219 | } 220 | 221 | void WebClientApp::update() 222 | { 223 | mFrameRate = getFrameRate(); 224 | 225 | if ( mFullScreen != isFullScreen() ) { 226 | setFullScreen( mFullScreen ); 227 | mFullScreen = isFullScreen(); 228 | } 229 | 230 | if ( !mText.empty() ) { 231 | TextBox tbox = TextBox().alignment( TextBox::LEFT ).font( mFont ).size( ivec2( getWindowWidth() - 250, TextBox::GROW ) ).text( "" ); 232 | for ( vector::const_reverse_iterator iter = mText.rbegin(); iter != mText.rend(); ++iter ) { 233 | tbox.appendText( "> " + *iter + "\n" ); 234 | } 235 | tbox.setColor( ColorAf( 1.0f, 0.8f, 0.75f, 1.0f ) ); 236 | tbox.setBackgroundColor( ColorAf::black() ); 237 | tbox.setPremultiplied( false ); 238 | mTexture = gl::Texture::create( tbox.render() ); 239 | while ( mText.size() > 75 ) { 240 | mText.erase( mText.begin() ); 241 | } 242 | } 243 | } 244 | 245 | void WebClientApp::write() 246 | { 247 | // This sample is meant to work with only one session at a time 248 | if ( mSession && mSession->getSocket()->is_open() ) { 249 | return; 250 | } 251 | 252 | mText.push_back( "Connecting to:\n" + mHost + ":" + toString( mPort ) ); 253 | 254 | mClient->connect( mHost, (uint16_t)mPort ); 255 | } 256 | 257 | CINDER_APP( WebClientApp, RendererGl ) 258 | -------------------------------------------------------------------------------- /samples/WebClient/vc2013/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/WebClient/vc2013/Resources.rc -------------------------------------------------------------------------------- /samples/WebClient/vc2013/WebClientApp.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio 2013 3 | VisualStudioVersion = 12.0.21005.1 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WebClientApp", "WebClientApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 6 | EndProject 7 | Global 8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 9 | Debug|Win32 = Debug|Win32 10 | Debug|x64 = Debug|x64 11 | Release|Win32 = Release|Win32 12 | Release|x64 = Release|x64 13 | EndGlobalSection 14 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 15 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | EndGlobal 28 | -------------------------------------------------------------------------------- /samples/WebClient/vc2013/WebClientApp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 11 | 12 | 13 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 14 | h;hpp;hxx;hm;inl;inc;xsd 15 | 16 | 17 | {04862b34-f4de-4713-b6a9-05c481111dd5} 18 | 19 | 20 | {efa5588e-22c4-46b6-ab94-84395aaff046} 21 | 22 | 23 | {c339eb6d-075a-451f-81fb-01925a552431} 24 | 25 | 26 | 27 | 28 | Source Files 29 | 30 | 31 | Blocks\Cinder-Asio 32 | 33 | 34 | Blocks\Cinder-Asio 35 | 36 | 37 | Blocks\Cinder-Asio 38 | 39 | 40 | Blocks\Cinder-Asio 41 | 42 | 43 | Blocks\Cinder-Asio 44 | 45 | 46 | Blocks\Cinder-Asio 47 | 48 | 49 | Blocks\Cinder-Asio 50 | 51 | 52 | Blocks\Cinder-Asio 53 | 54 | 55 | Blocks\Cinder-Asio 56 | 57 | 58 | Blocks\Cinder-Asio 59 | 60 | 61 | Blocks\Cinder-Asio 62 | 63 | 64 | Blocks\Cinder-Protocol 65 | 66 | 67 | Blocks\Cinder-Protocol 68 | 69 | 70 | Blocks\Cinder-Protocol 71 | 72 | 73 | Blocks\Cinder-Protocol 74 | 75 | 76 | Blocks\Cinder-Protocol 77 | 78 | 79 | Blocks\Cinder-Protocol 80 | 81 | 82 | Blocks\Cinder-Protocol 83 | 84 | 85 | Blocks\Cinder-Protocol 86 | 87 | 88 | Blocks\Cinder-Protocol 89 | 90 | 91 | Blocks\Cinder-Protocol 92 | 93 | 94 | 95 | 96 | Resource Files 97 | 98 | 99 | 100 | 101 | Resource Files 102 | 103 | 104 | 105 | 106 | Blocks\Cinder-Asio 107 | 108 | 109 | Blocks\Cinder-Asio 110 | 111 | 112 | Blocks\Cinder-Asio 113 | 114 | 115 | Blocks\Cinder-Asio 116 | 117 | 118 | Blocks\Cinder-Asio 119 | 120 | 121 | Blocks\Cinder-Asio 122 | 123 | 124 | Blocks\Cinder-Asio 125 | 126 | 127 | Blocks\Cinder-Asio 128 | 129 | 130 | Blocks\Cinder-Asio 131 | 132 | 133 | Blocks\Cinder-Asio 134 | 135 | 136 | Blocks\Cinder-Asio 137 | 138 | 139 | Blocks\Cinder-Asio 140 | 141 | 142 | Blocks\Cinder-Asio 143 | 144 | 145 | Blocks\Cinder-Asio 146 | 147 | 148 | Blocks\Cinder-Asio 149 | 150 | 151 | Blocks\Cinder-Asio 152 | 153 | 154 | Blocks\Cinder-Asio 155 | 156 | 157 | Blocks\Cinder-Asio 158 | 159 | 160 | Blocks\Cinder-Asio 161 | 162 | 163 | Blocks\Cinder-Asio 164 | 165 | 166 | Blocks\Cinder-Asio 167 | 168 | 169 | Blocks\Cinder-Asio 170 | 171 | 172 | Blocks\Cinder-Protocol 173 | 174 | 175 | Blocks\Cinder-Protocol 176 | 177 | 178 | Blocks\Cinder-Protocol 179 | 180 | 181 | Blocks\Cinder-Protocol 182 | 183 | 184 | Blocks\Cinder-Protocol 185 | 186 | 187 | Blocks\Cinder-Protocol 188 | 189 | 190 | Blocks\Cinder-Protocol 191 | 192 | 193 | Blocks\Cinder-Protocol 194 | 195 | 196 | Blocks\Cinder-Protocol 197 | 198 | 199 | Blocks\Cinder-Protocol 200 | 201 | 202 | Blocks\Cinder-Asio 203 | 204 | 205 | -------------------------------------------------------------------------------- /samples/WebClient/vc2015/Resources.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BanTheRewind/Cinder-Protocol/1ecefd54ef9ff61f1285bfb0c984e42a765b0f8e/samples/WebClient/vc2015/Resources.rc -------------------------------------------------------------------------------- /samples/WebClient/vc2015/WebClientApp.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio 2013 3 | VisualStudioVersion = 12.0.21005.1 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WebClientApp", "WebClientApp.vcxproj", "{74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}" 6 | EndProject 7 | Global 8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 9 | Debug|Win32 = Debug|Win32 10 | Debug|x64 = Debug|x64 11 | Release|Win32 = Release|Win32 12 | Release|x64 = Release|x64 13 | EndGlobalSection 14 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 15 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.ActiveCfg = Debug|Win32 16 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|Win32.Build.0 = Debug|Win32 17 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.ActiveCfg = Debug|x64 18 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Debug|x64.Build.0 = Debug|x64 19 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.ActiveCfg = Release|Win32 20 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|Win32.Build.0 = Release|Win32 21 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.ActiveCfg = Release|x64 22 | {74202EDD-91D2-4D2A-B0B6-355CEB16E6BE}.Release|x64.Build.0 = Release|x64 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | EndGlobal 28 | -------------------------------------------------------------------------------- /samples/WebClient/vc2015/WebClientApp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 10 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 11 | 12 | 13 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 14 | h;hpp;hxx;hm;inl;inc;xsd 15 | 16 | 17 | {04862b34-f4de-4713-b6a9-05c481111dd5} 18 | 19 | 20 | {efa5588e-22c4-46b6-ab94-84395aaff046} 21 | 22 | 23 | {c339eb6d-075a-451f-81fb-01925a552431} 24 | 25 | 26 | 27 | 28 | Source Files 29 | 30 | 31 | Blocks\Cinder-Asio 32 | 33 | 34 | Blocks\Cinder-Asio 35 | 36 | 37 | Blocks\Cinder-Asio 38 | 39 | 40 | Blocks\Cinder-Asio 41 | 42 | 43 | Blocks\Cinder-Asio 44 | 45 | 46 | Blocks\Cinder-Asio 47 | 48 | 49 | Blocks\Cinder-Asio 50 | 51 | 52 | Blocks\Cinder-Asio 53 | 54 | 55 | Blocks\Cinder-Asio 56 | 57 | 58 | Blocks\Cinder-Asio 59 | 60 | 61 | Blocks\Cinder-Asio 62 | 63 | 64 | Blocks\Cinder-Protocol 65 | 66 | 67 | Blocks\Cinder-Protocol 68 | 69 | 70 | Blocks\Cinder-Protocol 71 | 72 | 73 | Blocks\Cinder-Protocol 74 | 75 | 76 | Blocks\Cinder-Protocol 77 | 78 | 79 | Blocks\Cinder-Protocol 80 | 81 | 82 | Blocks\Cinder-Protocol 83 | 84 | 85 | Blocks\Cinder-Protocol 86 | 87 | 88 | Blocks\Cinder-Protocol 89 | 90 | 91 | Blocks\Cinder-Protocol 92 | 93 | 94 | 95 | 96 | Resource Files 97 | 98 | 99 | 100 | 101 | Resource Files 102 | 103 | 104 | 105 | 106 | Blocks\Cinder-Asio 107 | 108 | 109 | Blocks\Cinder-Asio 110 | 111 | 112 | Blocks\Cinder-Asio 113 | 114 | 115 | Blocks\Cinder-Asio 116 | 117 | 118 | Blocks\Cinder-Asio 119 | 120 | 121 | Blocks\Cinder-Asio 122 | 123 | 124 | Blocks\Cinder-Asio 125 | 126 | 127 | Blocks\Cinder-Asio 128 | 129 | 130 | Blocks\Cinder-Asio 131 | 132 | 133 | Blocks\Cinder-Asio 134 | 135 | 136 | Blocks\Cinder-Asio 137 | 138 | 139 | Blocks\Cinder-Asio 140 | 141 | 142 | Blocks\Cinder-Asio 143 | 144 | 145 | Blocks\Cinder-Asio 146 | 147 | 148 | Blocks\Cinder-Asio 149 | 150 | 151 | Blocks\Cinder-Asio 152 | 153 | 154 | Blocks\Cinder-Asio 155 | 156 | 157 | Blocks\Cinder-Asio 158 | 159 | 160 | Blocks\Cinder-Asio 161 | 162 | 163 | Blocks\Cinder-Asio 164 | 165 | 166 | Blocks\Cinder-Asio 167 | 168 | 169 | Blocks\Cinder-Asio 170 | 171 | 172 | Blocks\Cinder-Protocol 173 | 174 | 175 | Blocks\Cinder-Protocol 176 | 177 | 178 | Blocks\Cinder-Protocol 179 | 180 | 181 | Blocks\Cinder-Protocol 182 | 183 | 184 | Blocks\Cinder-Protocol 185 | 186 | 187 | Blocks\Cinder-Protocol 188 | 189 | 190 | Blocks\Cinder-Protocol 191 | 192 | 193 | Blocks\Cinder-Protocol 194 | 195 | 196 | Blocks\Cinder-Protocol 197 | 198 | 199 | Blocks\Cinder-Protocol 200 | 201 | 202 | Blocks\Cinder-Asio 203 | 204 | 205 | -------------------------------------------------------------------------------- /samples/WebClient/xcode/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | CinderApp.icns 11 | CFBundleIdentifier 12 | org.libcinder.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2013 __MyCompanyName__. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /samples/WebClient/xcode/WebClient.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /samples/WebClient/xcode/WebClient_Prefix.pch: -------------------------------------------------------------------------------- 1 | #ifdef __OBJC__ 2 | #import 3 | #endif 4 | 5 | #if defined( __cplusplus ) 6 | #include "cinder/Cinder.h" 7 | 8 | #include "cinder/app/AppBasic.h" 9 | 10 | #include "cinder/gl/gl.h" 11 | 12 | #include "cinder/CinderMath.h" 13 | #include "cinder/Matrix.h" 14 | #include "cinder/Vector.h" 15 | #include "cinder/Quaternion.h" 16 | #endif 17 | -------------------------------------------------------------------------------- /src/BodyInterface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "BodyInterface.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include "cinder/Utilities.h" 42 | #include 43 | 44 | using namespace ci; 45 | using namespace std; 46 | 47 | BodyInterface::BodyInterface() 48 | : ProtocolInterface() 49 | { 50 | } 51 | 52 | void BodyInterface::append( const ci::BufferRef& buffer ) 53 | { 54 | size_t sz = 0; 55 | size_t len = buffer->getSize(); 56 | if ( mBody ) { 57 | sz = mBody->getSize(); 58 | mBody->resize( sz + len ); 59 | } else { 60 | mBody = Buffer::create( len ); 61 | } 62 | char_traits::copy( (char*)mBody->getData() + sz, (char*)buffer->getData(), len ); 63 | } 64 | 65 | const BufferRef& BodyInterface::getBody() const 66 | { 67 | return mBody; 68 | } 69 | 70 | void BodyInterface::setBody( const BufferRef& body ) 71 | { 72 | size_t sz = body->getSize(); 73 | if ( mBody && sz > 0 ) { 74 | mBody->resize( sz ); 75 | } else { 76 | mBody = Buffer::create( sz ); 77 | } 78 | char_traits::copy( (char*)mBody->getData(), (char*)body->getData(), sz ); 79 | } 80 | 81 | BufferRef BodyInterface::toBuffer() const 82 | { 83 | return mBody; 84 | } 85 | 86 | string BodyInterface::toString() const 87 | { 88 | return bufferToString( mBody ); 89 | } 90 | -------------------------------------------------------------------------------- /src/BodyInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "ProtocolInterface.h" 41 | 42 | class BodyInterface : public ProtocolInterface 43 | { 44 | public: 45 | //! Appends \a buffer to body. 46 | void append( const ci::BufferRef& buffer ); 47 | //! Returns message body. 48 | const ci::BufferRef& getBody() const; 49 | //! Sets or replaces message body with \a body. 50 | void setBody( const ci::BufferRef& body ); 51 | 52 | //! Converts entire message to ci::Buffer. 53 | virtual ci::BufferRef toBuffer() const; 54 | //! Converts entire message to std::string. 55 | virtual std::string toString() const; 56 | protected: 57 | BodyInterface(); 58 | 59 | ci::BufferRef mBody; 60 | }; 61 | -------------------------------------------------------------------------------- /src/FtpInterface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "FtpInterface.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include "cinder/Utilities.h" 42 | #include 43 | 44 | using namespace ci; 45 | using namespace std; 46 | 47 | FtpInterface::FtpInterface( const string& command, const string& value ) 48 | : KeyValuePairInterface( command, value ) 49 | { 50 | } 51 | 52 | string FtpInterface::toString() const 53 | { 54 | return keyValuePairToString( mKeyValuePair ) + "\r\n"; 55 | } 56 | -------------------------------------------------------------------------------- /src/FtpInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "KeyValuePairInterface.h" 41 | 42 | class FtpInterface : public KeyValuePairInterface 43 | { 44 | public: 45 | //! Converts message to string. 46 | std::string toString() const; 47 | protected: 48 | FtpInterface( const std::string& key = "", const std::string& value = "" ); 49 | }; 50 | -------------------------------------------------------------------------------- /src/FtpRequest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "FtpRequest.h" 39 | 40 | #include"cinder/Utilities.h" 41 | 42 | using namespace ci; 43 | using namespace std; 44 | 45 | FtpRequest::FtpRequest( const string& command, const string& value ) 46 | : FtpInterface( command, value ) 47 | { 48 | } 49 | 50 | string FtpRequest::getCommandString( FtpCommand c ) 51 | { 52 | static vector commands; 53 | if ( commands.empty() ) { 54 | commands.push_back( "ABOR" ); 55 | commands.push_back( "ACCT" ); 56 | commands.push_back( "ADAT" ); 57 | commands.push_back( "ALLO" ); 58 | commands.push_back( "APPE" ); 59 | commands.push_back( "AUTH" ); 60 | commands.push_back( "CCC" ); 61 | commands.push_back( "CDUP" ); 62 | commands.push_back( "CONF" ); 63 | commands.push_back( "CWD" ); 64 | commands.push_back( "DELE" ); 65 | commands.push_back( "ENC" ); 66 | commands.push_back( "EPRT" ); 67 | commands.push_back( "EPSV" ); 68 | commands.push_back( "FEAT" ); 69 | commands.push_back( "HELP" ); 70 | commands.push_back( "LANG" ); 71 | commands.push_back( "LIST" ); 72 | commands.push_back( "LPRT" ); 73 | commands.push_back( "LPSV" ); 74 | commands.push_back( "MDTM" ); 75 | commands.push_back( "MIC" ); 76 | commands.push_back( "MKD" ); 77 | commands.push_back( "MLSD" ); 78 | commands.push_back( "MLST" ); 79 | commands.push_back( "MODE" ); 80 | commands.push_back( "NLST" ); 81 | commands.push_back( "NOOP" ); 82 | commands.push_back( "OPTS" ); 83 | commands.push_back( "PASS" ); 84 | commands.push_back( "PASV" ); 85 | commands.push_back( "PBSZ" ); 86 | commands.push_back( "PORT" ); 87 | commands.push_back( "PROT" ); 88 | commands.push_back( "PWD" ); 89 | commands.push_back( "QUIT" ); 90 | commands.push_back( "REIN" ); 91 | commands.push_back( "REST" ); 92 | commands.push_back( "RETR" ); 93 | commands.push_back( "RMD" ); 94 | commands.push_back( "RNFR" ); 95 | commands.push_back( "RNTO" ); 96 | commands.push_back( "SITE" ); 97 | commands.push_back( "SIZE" ); 98 | commands.push_back( "SMNT" ); 99 | commands.push_back( "STAT" ); 100 | commands.push_back( "STOR" ); 101 | commands.push_back( "STOU" ); 102 | commands.push_back( "STRU" ); 103 | commands.push_back( "SYST" ); 104 | commands.push_back( "TYPE" ); 105 | commands.push_back( "USER" ); 106 | commands.push_back( "XCUP" ); 107 | commands.push_back( "XMKD" ); 108 | commands.push_back( "XPWD" ); 109 | commands.push_back( "XRCP" ); 110 | commands.push_back( "XRMD" ); 111 | commands.push_back( "XRSQ" ); 112 | commands.push_back( "XSEM" ); 113 | commands.push_back( "XSEN" ); 114 | } 115 | if ( (size_t)c > commands.size() ) { 116 | throw ExcCommandNotFound( "FTP command ID " + ci::toString( c ) +" not found." ); 117 | } 118 | return commands.at( c ); 119 | } 120 | 121 | string FtpRequest::getCommandDescription( FtpCommand c ) 122 | { 123 | static vector commands; 124 | if ( commands.empty() ) { 125 | commands.push_back( "Abort" ); 126 | commands.push_back( "Account" ); 127 | commands.push_back( "Authentication data" ); 128 | commands.push_back( "Allocate" ); 129 | commands.push_back( "Append" ); 130 | commands.push_back( "Authentication" ); 131 | commands.push_back( "Clear command channel" ); 132 | commands.push_back( "Change to parent directory" ); 133 | commands.push_back( "Confidentiality protected command" ); 134 | commands.push_back( "Change working directory" ); 135 | commands.push_back( "Delete" ); 136 | commands.push_back( "Privacy protected command" ); 137 | commands.push_back( "Extended data port" ); 138 | commands.push_back( "Extended passive" ); 139 | commands.push_back( "Feature" ); 140 | commands.push_back( "Help" ); 141 | commands.push_back( "Language negotiation" ); 142 | commands.push_back( "List" ); 143 | commands.push_back( "Long data port" ); 144 | commands.push_back( "Long passive" ); 145 | commands.push_back( "File modification time" ); 146 | commands.push_back( "Integrity protected command" ); 147 | commands.push_back( "Make directory" ); 148 | commands.push_back( "List" ); 149 | commands.push_back( "List" ); 150 | commands.push_back( "Transfer mode" ); 151 | commands.push_back( "Name list" ); 152 | commands.push_back( "No operation" ); 153 | commands.push_back( "Options" ); 154 | commands.push_back( "Password" ); 155 | commands.push_back( "Passive mode" ); 156 | commands.push_back( "Protection buffer size" ); 157 | commands.push_back( "Data port" ); 158 | commands.push_back( "Data channel protection level" ); 159 | commands.push_back( "Print working directory" ); 160 | commands.push_back( "Logout" ); 161 | commands.push_back( "Reinitialize" ); 162 | commands.push_back( "Restart of interrupted transfer" ); 163 | commands.push_back( "Retrieve" ); 164 | commands.push_back( "Remove directory" ); 165 | commands.push_back( "Rename from" ); 166 | commands.push_back( "Rename to" ); 167 | commands.push_back( "Site parameters" ); 168 | commands.push_back( "File size" ); 169 | commands.push_back( "Structure mount" ); 170 | commands.push_back( "Status" ); 171 | commands.push_back( "Store" ); 172 | commands.push_back( "Store unique" ); 173 | commands.push_back( "File structure" ); 174 | commands.push_back( "System" ); 175 | commands.push_back( "Representation type" ); 176 | commands.push_back( "User name" ); 177 | commands.push_back( "Change to the parent of the current working directory" ); 178 | commands.push_back( "Make a directory" ); 179 | commands.push_back( "Print the current working directory" ); 180 | commands.push_back( "Specify recipients after scheme selection" ); 181 | commands.push_back( "Remove the directory" ); 182 | commands.push_back( "Scheme selection" ); 183 | commands.push_back( "Send, mail if cannot" ); 184 | commands.push_back( "Send to terminal" ); 185 | } 186 | if ( (size_t)c > commands.size() ) { 187 | throw ExcCommandNotFound( "FTP command ID " + ci::toString( c ) + " not found." ); 188 | } 189 | return commands.at( c ); 190 | } 191 | 192 | FtpCommand FtpRequest::getCommand() const 193 | { 194 | size_t c = fromString( mKeyValuePair.first ); 195 | if ( c > FtpCommand_XSEN ) { 196 | throw ExcCommandNotFound( "FTP command ID " + ci::toString( c ) + " not found." ); 197 | } 198 | return (FtpCommand)c; 199 | } 200 | 201 | const string& FtpRequest::getCommandString() const 202 | { 203 | return mKeyValuePair.first; 204 | } 205 | 206 | void FtpRequest::setCommand( const string& c ) 207 | { 208 | mKeyValuePair.first = c; 209 | } 210 | 211 | void FtpRequest::setCommand( FtpCommand c ) 212 | { 213 | mKeyValuePair.first = getCommandString( c ); 214 | } 215 | 216 | void FtpRequest::set( const string& c, const string& v ) 217 | { 218 | mKeyValuePair = make_pair( c, v ); 219 | } 220 | 221 | void FtpRequest::set( FtpCommand c, const string& v ) 222 | { 223 | mKeyValuePair = make_pair( getCommandString( c ), v ); 224 | } 225 | 226 | FtpRequest::ExcCommandNotFound::ExcCommandNotFound( const string& msg ) throw() 227 | { 228 | sprintf( mMessage, msg.c_str() ); 229 | } 230 | -------------------------------------------------------------------------------- /src/FtpRequest.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "FtpInterface.h" 41 | 42 | enum : size_t 43 | { 44 | FtpCommand_ABOR, 45 | FtpCommand_ACCT, 46 | FtpCommand_ADAT, 47 | FtpCommand_ALLO, 48 | FtpCommand_APPE, 49 | FtpCommand_AUTH, 50 | FtpCommand_CCC, 51 | FtpCommand_CDUP, 52 | FtpCommand_CONF, 53 | FtpCommand_CWD, 54 | FtpCommand_DELE, 55 | FtpCommand_ENC, 56 | FtpCommand_EPRT, 57 | FtpCommand_EPSV, 58 | FtpCommand_FEAT, 59 | FtpCommand_HELP, 60 | FtpCommand_LANG, 61 | FtpCommand_LIST, 62 | FtpCommand_LPRT, 63 | FtpCommand_LPSV, 64 | FtpCommand_MDTM, 65 | FtpCommand_MIC, 66 | FtpCommand_MKD, 67 | FtpCommand_MLSD, 68 | FtpCommand_MLST, 69 | FtpCommand_MODE, 70 | FtpCommand_NLST, 71 | FtpCommand_NOOP, 72 | FtpCommand_OPTS, 73 | FtpCommand_PASS, 74 | FtpCommand_PASV, 75 | FtpCommand_PBSZ, 76 | FtpCommand_PORT, 77 | FtpCommand_PROT, 78 | FtpCommand_PWD, 79 | FtpCommand_QUIT, 80 | FtpCommand_REIN, 81 | FtpCommand_REST, 82 | FtpCommand_RETR, 83 | FtpCommand_RMD, 84 | FtpCommand_RNFR, 85 | FtpCommand_RNTO, 86 | FtpCommand_SITE, 87 | FtpCommand_SIZE, 88 | FtpCommand_SMNT, 89 | FtpCommand_STAT, 90 | FtpCommand_STOR, 91 | FtpCommand_STOU, 92 | FtpCommand_STRU, 93 | FtpCommand_SYST, 94 | FtpCommand_TYPE, 95 | FtpCommand_USER, 96 | FtpCommand_XCUP, 97 | FtpCommand_XMKD, 98 | FtpCommand_XPWD, 99 | FtpCommand_XRCP, 100 | FtpCommand_XRMD, 101 | FtpCommand_XRSQ, 102 | FtpCommand_XSEM, 103 | FtpCommand_XSEN 104 | } typedef FtpCommand; 105 | 106 | class FtpRequest : public FtpInterface 107 | { 108 | public: 109 | //! Creates a FTP request with command \a command and value \a value. 110 | FtpRequest( const std::string& command = "", const std::string& value = "" ); 111 | 112 | //! Get string representing command \c. 113 | static std::string getCommandString( FtpCommand c ); 114 | //! Get string representing description of command \c. 115 | static std::string getCommandDescription( FtpCommand c ); 116 | 117 | //! Returns FTP command representing command. 118 | FtpCommand getCommand() const; 119 | //! Returns string representing command. 120 | const std::string& getCommandString() const; 121 | //! Sets command to string \a c. 122 | void setCommand( const std::string& c ); 123 | //! Sets command to FtpCommand \a c. 124 | void setCommand( FtpCommand c ); 125 | //! Sets command to string \a c and value to \a v. 126 | void set( const std::string& c, const std::string& v ); 127 | //! Sets command to FtpCommand \a c and value to \a v. 128 | void set( FtpCommand c, const std::string& v ); 129 | 130 | //! Exception representing missing or inavlid FTP command. 131 | class ExcCommandNotFound : public ci::Exception 132 | { 133 | public: 134 | ExcCommandNotFound( const std::string& msg ) throw(); 135 | virtual const char* what() const throw() 136 | { 137 | return mMessage; 138 | } 139 | private: 140 | char mMessage[ 2048 ]; 141 | }; 142 | }; 143 | -------------------------------------------------------------------------------- /src/FtpResponse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "FtpResponse.h" 39 | 40 | #include"cinder/Utilities.h" 41 | 42 | using namespace ci; 43 | using namespace std; 44 | 45 | FtpResponse::FtpResponse( FtpReplyCode replyCode, const string& value ) 46 | : FtpInterface( ci::toString( (size_t)replyCode ), value ) 47 | { 48 | } 49 | 50 | FtpResponse::FtpResponse( size_t replyCode, const string& value ) 51 | : FtpInterface( ci::toString( replyCode ), value ) 52 | { 53 | } 54 | 55 | FtpResponse::FtpResponse( const FtpResponse& rhs ) 56 | { 57 | *this = rhs; 58 | } 59 | 60 | FtpResponse& FtpResponse::operator=( const FtpResponse& rhs ) 61 | { 62 | mKeyValuePair = rhs.mKeyValuePair; 63 | return *this; 64 | } 65 | 66 | FtpResponse& FtpResponse::replyCode( FtpReplyCode c ) 67 | { 68 | setReplyCode( c ); 69 | return *this; 70 | } 71 | 72 | FtpResponse& FtpResponse::replyCode( size_t c ) 73 | { 74 | setReplyCode( c ); 75 | return *this; 76 | } 77 | 78 | string FtpResponse::getReplyCodeDescription( FtpReplyCode c ) 79 | { 80 | static vector replyCodes; 81 | if ( replyCodes.empty() ) { 82 | replyCodes.push_back( "Restart marker reply" ); 83 | replyCodes.push_back( "Service ready in nnn minutes" ); 84 | replyCodes.push_back( "Data connection already open; transfer starting" ); 85 | replyCodes.push_back( "File status okay; about to open data connection" ); 86 | replyCodes.push_back( "Command okay" ); 87 | replyCodes.push_back( "Command not implemented, superfluous at this site" ); 88 | replyCodes.push_back( "System status, or system help reply" ); 89 | replyCodes.push_back( "Directory status" ); 90 | replyCodes.push_back( "File status" ); 91 | replyCodes.push_back( "Help message" ); 92 | replyCodes.push_back( "NAME system type" ); 93 | replyCodes.push_back( "Service ready for new user" ); 94 | replyCodes.push_back( "Service closing control connection" ); 95 | replyCodes.push_back( "Data connection open; no transfer in progress" ); 96 | replyCodes.push_back( "Closing data connection" ); 97 | replyCodes.push_back( "Entering passive mode..." ); 98 | replyCodes.push_back( "Entering long passive mode" ); 99 | replyCodes.push_back( "Extended passive mode entered" ); 100 | replyCodes.push_back( "User logged in, proceed" ); 101 | replyCodes.push_back( "Requested file action okay, completed" ); 102 | replyCodes.push_back( "Pathname created" ); 103 | replyCodes.push_back( "User name okay, need password" ); 104 | replyCodes.push_back( "Need account for login" ); 105 | replyCodes.push_back( "Requested file action pending further information" ); 106 | replyCodes.push_back( "Service not available, closing control connection" ); 107 | replyCodes.push_back( "Cannot open data connection" ); 108 | replyCodes.push_back( "Connection closed; transfer aborted" ); 109 | replyCodes.push_back( "Requested file action not taken" ); 110 | replyCodes.push_back( "Requested action aborted: local error in processing" ); 111 | replyCodes.push_back( "Requested action not taken" ); 112 | replyCodes.push_back( "Syntax error, command unrecognised" ); 113 | replyCodes.push_back( "Syntax error in parameters or arguments" ); 114 | replyCodes.push_back( "Command not implemented" ); 115 | replyCodes.push_back( "Bad sequence of commands" ); 116 | replyCodes.push_back( "Command not implemented for that parameter" ); 117 | replyCodes.push_back( "Supported address families are..." ); 118 | replyCodes.push_back( "Protocol not supported" ); 119 | replyCodes.push_back( "Not logged in" ); 120 | replyCodes.push_back( "Need account for storing files" ); 121 | replyCodes.push_back( "Requested action not taken" ); 122 | replyCodes.push_back( "Requested action aborted: page type unknown" ); 123 | replyCodes.push_back( "Requested file action aborted" ); 124 | replyCodes.push_back( "Requested action not taken" ); 125 | replyCodes.push_back( "Requested action not taken: invalid REST parameter" ); 126 | replyCodes.push_back( "Requested action not taken: TYPE or STRU mismatch" ); 127 | } 128 | if ( (size_t)c > replyCodes.size() ) { 129 | throw ExcReplyCodeNotFound( (size_t)c ); 130 | } 131 | return replyCodes.at( c ); 132 | } 133 | 134 | size_t FtpResponse::getReplyCode() const 135 | { 136 | return fromString( mKeyValuePair.first ); 137 | } 138 | 139 | void FtpResponse::setReplyCode( FtpReplyCode c ) 140 | { 141 | mKeyValuePair.first = ci::toString( (size_t)c ); 142 | } 143 | 144 | void FtpResponse::setReplyCode( size_t c ) 145 | { 146 | mKeyValuePair.first = ci::toString( c ); 147 | } 148 | 149 | FtpResponse::ExcReplyCodeNotFound::ExcReplyCodeNotFound( size_t replyCode ) throw() 150 | { 151 | sprintf( mMessage, "Reply code \"%zu\" not found", replyCode ); 152 | } 153 | -------------------------------------------------------------------------------- /src/FtpResponse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "FtpInterface.h" 41 | 42 | enum : size_t 43 | { 44 | FtpReplyCode_110_RESTART_MARKER_REPLY = 110, 45 | FtpReplyCode_120_SERVICE_READY_IN_NNN_MINUTES = 120, 46 | FtpReplyCode_125_DATA_CONNECTION_ALREADY_OPEN_TRANSFER_STARTING = 125, 47 | FtpReplyCode_150_FILE_STATUS_OKAY_ABOUT_TO_OPEN_DATA_CONNECTION = 150, 48 | FtpReplyCode_200_COMMAND_OKAY = 200, 49 | FtpReplyCode_202_COMMAND_NOT_IMPLEMENTED_SUPERFLUOUS_AT_THIS_SITE = 202, 50 | FtpReplyCode_211_SYSTEM_STATUS_OR_SYSTEM_HELP_REPLY = 211, 51 | FtpReplyCode_212_DIRECTORY_STATUS = 212, 52 | FtpReplyCode_213_FILE_STATUS = 213, 53 | FtpReplyCode_214_HELP_MESSAGE = 214, 54 | FtpReplyCode_215_NAME_SYSTEM_TYPE = 215, 55 | FtpReplyCode_220_SERVICE_READY_FOR_NEW_USER = 220, 56 | FtpReplyCode_221_SERVICE_CLOSING_CONTROL_CONNECTION = 221, 57 | FtpReplyCode_225_DATA_CONNECTION_OPEN_NO_TRANSFER_IN_PROGRESS = 225, 58 | FtpReplyCode_226_CLOSING_DATA_CONNECTION = 226, 59 | FtpReplyCode_227_ENTERING_PASSIVE_MODE = 227, 60 | FtpReplyCode_228_ENTERING_LONG_PASSIVE_MODE = 228, 61 | FtpReplyCode_229_EXTENDED_PASSIVE_MODE_ENTERED = 229, 62 | FtpReplyCode_230_USER_LOGGED_IN_PROCEED = 230, 63 | FtpReplyCode_250_REQUESTED_FILE_ACTION_OKAY_COMPLETED = 250, 64 | FtpReplyCode_257_PATHNAME_CREATED = 257, 65 | FtpReplyCode_331_USER_NAME_OKAY_NEED_PASSWORD = 331, 66 | FtpReplyCode_332_NEED_ACCOUNT_FOR_LOGIN = 332, 67 | FtpReplyCode_350_REQUESTED_FILE_ACTION_PENDING_FURTHER_INFORMATION = 350, 68 | FtpReplyCode_421_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION = 421, 69 | FtpReplyCode_425_CANNOT_OPEN_DATA_CONNECTION = 425, 70 | FtpReplyCode_426_CONNECTION_CLOSED_TRANSFER_ABORTED = 426, 71 | FtpReplyCode_450_REQUESTED_FILE_ACTION_NOT_TAKEN = 450, 72 | FtpReplyCode_451_REQUESTED_ACTION_ABORTED_LOCAL_ERROR_IN_PROCESSING = 451, 73 | FtpReplyCode_452_REQUESTED_ACTION_NOT_TAKEN = 452, 74 | FtpReplyCode_500_SYNTAX_ERROR_COMMAND_UNRECOGNIZED = 500, 75 | FtpReplyCode_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS = 501, 76 | FtpReplyCode_502_COMMAND_NOT_IMPLEMENTED = 502, 77 | FtpReplyCode_503_BAD_SEQUENCE_OF_COMMANDS = 503, 78 | FtpReplyCode_504_COMMAND_NOT_IMPLEMENTED_FOR_THAT_PARAMETER = 504, 79 | FtpReplyCode_521_SUPPORTED_ADDRESS_FAMILIES_ARE = 521, 80 | FtpReplyCode_522_PROTOCOL_NOT_SUPPORTED = 522, 81 | FtpReplyCode_530_NOT_LOGGED_IN = 530, 82 | FtpReplyCode_532_NEED_ACCOUNT_FOR_STORING_FILES = 532, 83 | FtpReplyCode_550_REQUESTED_ACTION_NOT_TAKEN = 550, 84 | FtpReplyCode_551_REQUESTED_ACTION_ABORTED_PAGE_TYPE_UNKNOWN = 551, 85 | FtpReplyCode_552_REQUESTED_FILE_ACTION_ABORTED = 552, 86 | FtpReplyCode_553_REQUESTED_ACTION_NOT_TAKEN = 553, 87 | FtpReplyCode_554_REQUESTED_ACTION_NOT_TAKEN_INVALID_REST_PARAMETER = 554, 88 | FtpReplyCode_555_REQUESTED_ACTION_NOT_TAKEN_TYPE_OR_STRU_MISMATCH = 555 89 | } typedef FtpReplyCode; 90 | 91 | class FtpResponse : public FtpInterface 92 | { 93 | public: 94 | //! Creates a FTP response with reply code \a replyCode and value \a value. 95 | explicit FtpResponse( FtpReplyCode replyCode = FtpReplyCode_200_COMMAND_OKAY, 96 | const std::string& value = "" ); 97 | //! Creates a FTP response with reply code \a replyCode and value \a value. 98 | explicit FtpResponse( size_t replyCode, const std::string& value = "" ); 99 | 100 | //! Returns string representing description of FtpReplyCode \a c. 101 | static std::string getReplyCodeDescription( FtpReplyCode c ); 102 | 103 | FtpResponse( const FtpResponse& rhs ); 104 | FtpResponse& operator=( const FtpResponse& rhs ); 105 | 106 | //! Sets reply code to \a c. 107 | FtpResponse& replyCode( FtpReplyCode c ); 108 | //! Sets reply code to \a c. 109 | FtpResponse& replyCode( size_t c ); 110 | 111 | //! Returns reply code as size_t. 112 | size_t getReplyCode() const; 113 | 114 | //! Sets reply code to \a c. 115 | void setReplyCode( FtpReplyCode c ); 116 | //! Sets reply code to \a c. 117 | void setReplyCode( size_t c ); 118 | 119 | //! Exception representing missing reply code. 120 | class ExcReplyCodeNotFound : public ci::Exception 121 | { 122 | public: 123 | ExcReplyCodeNotFound( size_t replyCode ) throw(); 124 | virtual const char* what() const throw() 125 | { 126 | return mMessage; 127 | } 128 | private: 129 | char mMessage[ 2048 ]; 130 | }; 131 | }; 132 | -------------------------------------------------------------------------------- /src/HeaderInterface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "HeaderInterface.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include "cinder/Utilities.h" 42 | #include 43 | 44 | using namespace ci; 45 | using namespace std; 46 | 47 | HeaderInterface::HeaderInterface() 48 | : ProtocolInterface(), mHasHeader( false ) 49 | { 50 | } 51 | 52 | HeaderMap HeaderInterface::stringToHeaderMap( const string& h ) 53 | { 54 | vector tokens; 55 | boost::split( tokens, h, boost::is_any_of( sCrLf ) ); 56 | HeaderMap headerMap; 57 | for ( vector::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter ) { 58 | if ( !iter->empty() ) { 59 | KeyValuePair kvp = stringToKeyValuePair( *iter, ":" ); 60 | headerMap.insert( kvp ); 61 | } 62 | } 63 | return headerMap; 64 | } 65 | 66 | string HeaderInterface::headerMapToString( const HeaderMap& h ) 67 | { 68 | string headerMap = ""; 69 | for ( HeaderMap::const_iterator iter = h.begin(); iter != h.end(); ++iter ) { 70 | headerMap += keyValuePairToString( *iter, ":" ) + sCrLf; 71 | } 72 | return headerMap; 73 | } 74 | 75 | BufferRef HeaderInterface::removeHeader( const BufferRef& buffer ) 76 | { 77 | string msg = bufferToString( buffer ); 78 | size_t offset = msg.find( sCrLf + sCrLf ); 79 | size_t sz = buffer->getSize(); 80 | if ( offset < sz ) { 81 | size_t len = ( sz - offset ) - 4; 82 | BufferRef body = Buffer::create( len ); 83 | char_traits::copy( (char*)body->getData(), (char*)buffer->getData() + ( offset + 4 ), len ); 84 | return body; 85 | } else { 86 | return Buffer::create( 0 ); 87 | } 88 | } 89 | 90 | void HeaderInterface::eraseHeader( const string& field ) 91 | { 92 | if ( mHeaderMap.find( field ) == mHeaderMap.end() ) { 93 | throw ExcHeaderNotFound( field ); 94 | } else { 95 | mHeaderMap.erase( field ); 96 | } 97 | } 98 | 99 | const string& HeaderInterface::getHeader( const string& field ) 100 | { 101 | if ( mHeaderMap.find( field ) == mHeaderMap.end() ) { 102 | throw ExcHeaderNotFound( field ); 103 | } else { 104 | return mHeaderMap[ field ]; 105 | } 106 | } 107 | 108 | HeaderMap& HeaderInterface::getHeaders() 109 | { 110 | return mHeaderMap; 111 | } 112 | 113 | const HeaderMap& HeaderInterface::getHeaders() const 114 | { 115 | return mHeaderMap; 116 | } 117 | 118 | bool HeaderInterface::hasHeader() const 119 | { 120 | return mHasHeader; 121 | } 122 | 123 | void HeaderInterface::setHeader( const string& field, const string& value ) 124 | { 125 | mHeaderMap[ field ] = value; 126 | } 127 | 128 | void HeaderInterface::setHeader( const KeyValuePair& kvp ) 129 | { 130 | setHeader( kvp.first, kvp.second ); 131 | } 132 | 133 | void HeaderInterface::setHeaders( const HeaderMap& headerMap ) 134 | { 135 | mHeaderMap = headerMap; 136 | } 137 | 138 | void HeaderInterface::parse( const BufferRef& buffer ) 139 | { 140 | string msg = bufferToString( buffer ); 141 | parseHeader( msg ); 142 | } 143 | 144 | BufferRef HeaderInterface::toBuffer() const 145 | { 146 | string header = headerToString(); 147 | size_t sz = header.size(); 148 | 149 | BufferRef buffer = Buffer::create( sz ); 150 | char_traits::copy( (char*)buffer->getData(), (char*)&header[ 0 ], sz ); 151 | 152 | return buffer; 153 | } 154 | 155 | string HeaderInterface::toString() const 156 | { 157 | return headerToString(); 158 | } 159 | 160 | HeaderInterface::ExcHeaderNotFound::ExcHeaderNotFound( const string& field ) throw() 161 | { 162 | sprintf( mMessage, "Header field \"%s\" not found", field.c_str() ); 163 | } 164 | -------------------------------------------------------------------------------- /src/HeaderInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "ProtocolInterface.h" 41 | 42 | typedef std::map HeaderMap; 43 | 44 | class HeaderInterface : public ProtocolInterface 45 | { 46 | public: 47 | //! Parses \a headerMap into a header map object. 48 | static HeaderMap stringToHeaderMap( const std::string& headerMap ); 49 | //! Return string representation of \a headerMap. 50 | static std::string headerMapToString( const HeaderMap& headerMap ); 51 | //! Returns copy of buffer with header removed. 52 | static ci::BufferRef removeHeader( const ci::BufferRef& buffer ); 53 | 54 | //! Erases header named \a field. 55 | void eraseHeader( const std::string& field ); 56 | //! Returns header value for \a field. 57 | const std::string& getHeader( const std::string& field ); 58 | //! Return header map. 59 | HeaderMap& getHeaders(); 60 | //! Return header map. 61 | const HeaderMap& getHeaders() const; 62 | //! Returns true if a valid header exists. 63 | bool hasHeader() const; 64 | //! Set header \a value for \a field. Overwrites existing value with same key. 65 | void setHeader( const std::string& field, const std::string& value ); 66 | //! Set header field from \a kvp. Overwrites existing value with same key. 67 | void setHeader( const KeyValuePair& kvp ); 68 | // Sets all header fields from \a headerMap. Overwrites existing values. 69 | void setHeaders( const HeaderMap& headerMap ); 70 | //! Parses \a buffer into headers. 71 | virtual void parse( const ci::BufferRef& buffer ); 72 | virtual void parseHeader( const std::string& header ) = 0; 73 | 74 | //! Converts entire message to ci::Buffer. 75 | virtual ci::BufferRef toBuffer() const; 76 | //! Converts entire message to std::string. 77 | virtual std::string toString() const; 78 | 79 | //! Exception representing missing header 80 | class ExcHeaderNotFound : public ci::Exception 81 | { 82 | public: 83 | ExcHeaderNotFound( const std::string& field ) throw(); 84 | virtual const char* what() const throw() 85 | { 86 | return mMessage; 87 | } 88 | private: 89 | char mMessage[ 2048 ]; 90 | }; 91 | protected: 92 | HeaderInterface(); 93 | 94 | virtual std::string headerToString() const = 0; 95 | 96 | bool mHasHeader; 97 | HeaderMap mHeaderMap; 98 | }; 99 | 100 | -------------------------------------------------------------------------------- /src/HttpInterface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "HttpInterface.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include "cinder/Utilities.h" 42 | #include 43 | 44 | using namespace ci; 45 | using namespace std; 46 | 47 | HttpInterface::HttpInterface( HttpVersion v ) 48 | : BodyInterface(), HeaderInterface(), mHttpVersion( v ) 49 | { 50 | } 51 | 52 | HttpVersion HttpInterface::stringToHttpVersion( const string& v ) 53 | { 54 | vector tokens; 55 | boost::split( tokens, v, boost::is_any_of( "/" ) ); 56 | if ( tokens.size() == 2 ) { 57 | string ver = tokens.at( 1 ); 58 | if ( ver == "0.9" ) { 59 | return HTTP_0_9; 60 | } else if ( ver == "1.0" ) { 61 | return HTTP_1_0; 62 | } else if ( ver == "1.1" ) { 63 | return HTTP_1_1; 64 | } else if ( ver == "2.0" ) { 65 | return HTTP_2_0; 66 | } else { 67 | throw ExcHttpVersionInvalid( v ); 68 | } 69 | } else { 70 | throw ExcHttpVersionInvalid( v ); 71 | } 72 | } 73 | 74 | string HttpInterface::httpVersionToString( HttpVersion v ) 75 | { 76 | static const string prefix = "HTTP/"; 77 | switch ( v ) { 78 | case HTTP_0_9: 79 | return prefix + "0.9"; 80 | case HTTP_1_0: 81 | return prefix + "1.0"; 82 | case HTTP_1_1: 83 | return prefix + "1.1"; 84 | case HTTP_2_0: 85 | return prefix + "2.0"; 86 | default: 87 | throw ExcHttpVersionInvalid( ci::toString( v ) ); 88 | break; 89 | } 90 | } 91 | 92 | HttpVersion HttpInterface::getHttpVersion() const 93 | { 94 | return mHttpVersion; 95 | } 96 | 97 | void HttpInterface::setHttpVersion( HttpVersion v ) 98 | { 99 | mHttpVersion = v; 100 | } 101 | 102 | void HttpInterface::parse( const BufferRef& buffer ) 103 | { 104 | string msg = bufferToString( buffer ); 105 | size_t offset = msg.find( sCrLf + sCrLf ); 106 | size_t sz = buffer->getSize(); 107 | if ( offset < sz ) { 108 | msg = msg.substr( 0, offset ); 109 | parseHeader( msg ); 110 | 111 | size_t len = sz - offset; 112 | BufferRef body = Buffer::create( len ); 113 | char_traits::copy( (char*)body->getData(), (char*)buffer->getData() + ( offset + 4 ), len ); 114 | mBody = body; 115 | } 116 | } 117 | 118 | BufferRef HttpInterface::toBuffer() const 119 | { 120 | string header = headerToString(); 121 | size_t headerLength = header.size(); 122 | size_t bodyLength = 0; 123 | if ( mBody ) { 124 | bodyLength = mBody->getSize(); 125 | } 126 | BufferRef buffer = Buffer::create( headerLength + bodyLength ); 127 | char_traits::copy( (char*)buffer->getData(), (char*)&header[ 0 ], headerLength ); 128 | if ( bodyLength > 0 ) { 129 | char_traits::copy( (char*)buffer->getData() + headerLength, (char*)mBody->getData(), bodyLength ); 130 | } 131 | 132 | return buffer; 133 | } 134 | 135 | string HttpInterface::toString() const 136 | { 137 | string body = ""; 138 | string header = headerToString(); 139 | if ( mBody ) { 140 | body = bufferToString( mBody ); 141 | } 142 | return header + body; 143 | } 144 | 145 | 146 | HttpInterface::ExcHttpVersionInvalid::ExcHttpVersionInvalid( const string& ver ) throw() 147 | { 148 | sprintf( mMessage, "\"%s\" is not a valid HTTP version", ver.c_str() ); 149 | } 150 | 151 | ostream& operator<<( ostream& out, const HttpInterface& p ) 152 | { 153 | out << p.toString(); 154 | return out; 155 | } 156 | -------------------------------------------------------------------------------- /src/HttpInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "BodyInterface.h" 41 | #include "HeaderInterface.h" 42 | 43 | enum : size_t 44 | { 45 | HTTP_0_9, HTTP_1_0, HTTP_1_1, HTTP_2_0 46 | } typedef HttpVersion; 47 | 48 | class HttpInterface : public BodyInterface, public HeaderInterface 49 | { 50 | public: 51 | //! Parses HTTP version from \a v. 52 | static HttpVersion stringToHttpVersion( const std::string& v ); 53 | //! Returns string representing HTTP version \a v. 54 | static std::string httpVersionToString( HttpVersion v ); 55 | 56 | //! Returns HTTP version. 57 | HttpVersion getHttpVersion() const; 58 | //! Sets HTTP version to \a v. 59 | void setHttpVersion( HttpVersion v ); 60 | 61 | //! Parses \a buffer to header and body. Throws exception for invalid header. 62 | void parse( const ci::BufferRef& buffer ); 63 | 64 | //! Converts entire message to ci::Buffer. 65 | ci::BufferRef toBuffer() const; 66 | //! Converts entire message to std::string. 67 | std::string toString() const; 68 | 69 | //! Exception representing invalid HTTP version. 70 | class ExcHttpVersionInvalid : public ci::Exception 71 | { 72 | public: 73 | ExcHttpVersionInvalid( const std::string &ver ) throw(); 74 | virtual const char* what() const throw() 75 | { 76 | return mMessage; 77 | } 78 | private: 79 | char mMessage[ 2048 ]; 80 | }; 81 | protected: 82 | HttpInterface( HttpVersion v ); 83 | 84 | HttpVersion mHttpVersion; 85 | 86 | friend std::ostream& operator<<( std::ostream& out, const HttpInterface& h ); 87 | }; 88 | 89 | std::ostream& operator<<( std::ostream& out, const HttpInterface& h ); 90 | -------------------------------------------------------------------------------- /src/HttpRequest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "HttpRequest.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include 42 | 43 | using namespace ci; 44 | using namespace std; 45 | 46 | HttpRequest::HttpRequest() 47 | : HttpInterface( HttpVersion::HTTP_1_1 ), mOptions( "GET" ), mUri( "*" ) 48 | { 49 | } 50 | 51 | HttpRequest::HttpRequest( const string& options, const string& uri, HttpVersion httpVersion ) 52 | : HttpInterface( httpVersion ), mOptions( options ), mUri( uri ) 53 | { 54 | } 55 | 56 | HttpRequest::HttpRequest( const HttpRequest& rhs ) 57 | : HttpInterface( rhs.mHttpVersion ) 58 | { 59 | *this = rhs; 60 | } 61 | 62 | HttpRequest& HttpRequest::operator=( const HttpRequest& rhs ) 63 | { 64 | mBody = rhs.mBody; 65 | mHasHeader = rhs.mHasHeader; 66 | mHeaderMap = rhs.mHeaderMap; 67 | mHttpVersion = rhs.mHttpVersion; 68 | mOptions = rhs.mOptions; 69 | mUri = rhs.mUri; 70 | return *this; 71 | } 72 | 73 | HttpRequest& HttpRequest::options( const string& options ) 74 | { 75 | setOptions( options ); 76 | return *this; 77 | } 78 | 79 | HttpRequest& HttpRequest::uri( const string& uri ) 80 | { 81 | setUri( uri ); 82 | return *this; 83 | } 84 | 85 | const string& HttpRequest::getOptions() const 86 | { 87 | return mOptions; 88 | } 89 | 90 | const string& HttpRequest::getUri() const 91 | { 92 | return mUri; 93 | } 94 | 95 | void HttpRequest::setOptions( const string& options ) 96 | { 97 | mOptions = options; 98 | } 99 | 100 | void HttpRequest::setUri( const string& uri ) 101 | { 102 | mUri = uri; 103 | } 104 | 105 | void HttpRequest::parseHeader( const string& header ) 106 | { 107 | if ( header.empty() ) { 108 | return; 109 | } 110 | size_t d = header.find( sCrLf + sCrLf ); 111 | size_t len = header.length(); 112 | string h = header; 113 | if ( d < len ) { 114 | h = h.substr( 0, d ); 115 | } 116 | 117 | vector lines; 118 | boost::split( lines, h, boost::is_any_of( sCrLf ) ); 119 | if ( !lines.empty() ) { 120 | string request = boost::trim_copy( lines.at( 0 ) ); 121 | vector tokens; 122 | boost::split( tokens, request, boost::is_any_of( " " ) ); 123 | if ( tokens.size() < 2 ) { 124 | throw ExcRequestLineInvalid( request ); 125 | } else { 126 | mOptions = tokens.at( 0 ); 127 | mUri = tokens.at( 1 ); 128 | mHttpVersion = stringToHttpVersion( tokens.at( 2 ) ); 129 | } 130 | lines.erase( lines.begin() ); 131 | } 132 | 133 | string headerMap = boost::join( lines, sCrLf ); 134 | mHeaderMap = stringToHeaderMap( headerMap ); 135 | 136 | mHasHeader = true; 137 | } 138 | 139 | string HttpRequest::headerToString() const 140 | { 141 | string header = mOptions + " " + mUri + " "; 142 | header += httpVersionToString( mHttpVersion ); 143 | header += sCrLf; 144 | if ( !mHeaderMap.empty() ) { 145 | header += headerMapToString( mHeaderMap ); 146 | } 147 | header += sCrLf; 148 | return header; 149 | } 150 | 151 | HttpRequest::ExcRequestLineInvalid::ExcRequestLineInvalid( const string& requestLine ) throw() 152 | { 153 | sprintf( mMessage, "\"%s\" is not a valid request line", requestLine.c_str() ); 154 | } 155 | -------------------------------------------------------------------------------- /src/HttpRequest.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "HttpInterface.h" 41 | 42 | class HttpRequest : public HttpInterface 43 | { 44 | public: 45 | //! Creates empty HTTP request. 46 | HttpRequest(); 47 | //! Creates a HTTP request. Populates request line with arguments. 48 | HttpRequest( const std::string& options, const std::string& uri, HttpVersion httpVersion ); 49 | 50 | HttpRequest( const HttpRequest& rhs ); 51 | HttpRequest& operator=( const HttpRequest& rhs ); 52 | 53 | //! Sets HTTP options to \a options. 54 | HttpRequest& options( const std::string& options ); 55 | //! Sets URI to \a uri. 56 | HttpRequest& uri( const std::string& uri ); 57 | 58 | //! Returns HTTP options as string. 59 | const std::string& getOptions() const; 60 | //! Return URI as string. 61 | const std::string& getUri() const; 62 | 63 | //! Sets HTTP options to \a options. 64 | void setOptions( const std::string& options ); 65 | //! Sets URI to \a uri. 66 | void setUri( const std::string& uri ); 67 | 68 | /*! Parses header from \a header. Throws exceptions for incomplete 69 | or invalid headers. */ 70 | virtual void parseHeader( const std::string& header ) override; 71 | 72 | //! Exception representing invalid request line. 73 | class ExcRequestLineInvalid : public ci::Exception 74 | { 75 | public: 76 | ExcRequestLineInvalid( const std::string &requestLine ) throw(); 77 | virtual const char* what() const throw() 78 | { 79 | return mMessage; 80 | } 81 | private: 82 | char mMessage[ 2048 ]; 83 | }; 84 | protected: 85 | std::string headerToString() const; 86 | 87 | std::string mOptions; 88 | std::string mUri; 89 | }; 90 | -------------------------------------------------------------------------------- /src/HttpResponse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "HttpResponse.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include "cinder/Utilities.h" 42 | 43 | using namespace ci; 44 | using namespace std; 45 | 46 | HttpResponse::HttpResponse() 47 | : HttpInterface( HttpVersion::HTTP_1_1 ), mReason( "" ), mStatusCode( 0 ) 48 | { 49 | } 50 | 51 | HttpResponse::HttpResponse( HttpVersion httpVersion, size_t statusCode, const string& reason ) 52 | : HttpInterface( HttpVersion::HTTP_1_1 ), mReason( reason ), mStatusCode( statusCode ) 53 | { 54 | } 55 | 56 | HttpResponse::HttpResponse( const HttpResponse& rhs ) 57 | : HttpInterface( rhs.mHttpVersion ) 58 | { 59 | *this = rhs; 60 | } 61 | 62 | HttpResponse& HttpResponse::operator=( const HttpResponse& rhs ) 63 | { 64 | mBody = rhs.mBody; 65 | mHasHeader = rhs.mHasHeader; 66 | mHeaderMap = rhs.mHeaderMap; 67 | mHttpVersion = rhs.mHttpVersion; 68 | mReason = rhs.mReason; 69 | mStatusCode = rhs.mStatusCode; 70 | return *this; 71 | } 72 | 73 | HttpResponse& HttpResponse::reason( const string& reason ) 74 | { 75 | setReason( reason ); 76 | return *this; 77 | } 78 | 79 | HttpResponse& HttpResponse::statusCode( size_t code ) 80 | { 81 | setStatusCode( code ); 82 | return *this; 83 | } 84 | 85 | const string& HttpResponse::getReason() const 86 | { 87 | return mReason; 88 | } 89 | 90 | size_t HttpResponse::getStatusCode() const 91 | { 92 | return mStatusCode; 93 | } 94 | 95 | void HttpResponse::setReason( const string& reason ) 96 | { 97 | mReason = reason; 98 | } 99 | 100 | void HttpResponse::setStatusCode( size_t code ) 101 | { 102 | mStatusCode = code; 103 | } 104 | 105 | void HttpResponse::parseHeader( const string& header ) 106 | { 107 | if ( header.empty() ) { 108 | return; 109 | } 110 | size_t d = header.find( sCrLf + sCrLf ); 111 | size_t l = header.length(); 112 | string h = header; 113 | if ( d < l ) { 114 | h = h.substr( 0, d ); 115 | } 116 | 117 | vector lines; 118 | boost::split( lines, h, boost::is_any_of( sCrLf ) ); 119 | if ( !lines.empty() ) { 120 | string status = boost::trim_copy( lines.at( 0 ) ); 121 | vector tokens; 122 | boost::split( tokens, status, boost::is_any_of( " " ) ); 123 | if ( tokens.size() < 2 ) { 124 | throw ExcStatusLineInvalid( status ); 125 | } else { 126 | mHttpVersion = stringToHttpVersion( tokens.at( 0 ) ); 127 | mStatusCode = fromString( tokens.at( 1 ) ); 128 | mReason = ""; 129 | for ( size_t i = 0; i < 2; ++i ) { 130 | tokens.erase( tokens.begin() ); 131 | } 132 | if ( !tokens.empty() ) { 133 | mReason = boost::join( tokens, " " ); 134 | } 135 | } 136 | lines.erase( lines.begin() ); 137 | } 138 | 139 | string headerMap = boost::join( lines, sCrLf ); 140 | mHeaderMap = stringToHeaderMap( headerMap ); 141 | mHasHeader = true; 142 | } 143 | 144 | string HttpResponse::headerToString() const 145 | { 146 | string header = httpVersionToString( mHttpVersion );; 147 | header += " " + ci::toString( mStatusCode ) + " " + mReason; 148 | header += sCrLf; 149 | if ( !mHeaderMap.empty() ) { 150 | header += headerMapToString( mHeaderMap ); 151 | } 152 | header += sCrLf; 153 | return header; 154 | } 155 | 156 | HttpResponse::ExcStatusLineInvalid::ExcStatusLineInvalid( const string& statusLine ) throw() 157 | { 158 | sprintf( mMessage, "\"%s\" is not a valid status line", statusLine.c_str() ); 159 | } 160 | -------------------------------------------------------------------------------- /src/HttpResponse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "HttpInterface.h" 41 | 42 | class HttpResponse : public HttpInterface 43 | { 44 | public: 45 | //! Creates empty HTTP response. 46 | HttpResponse(); 47 | //! Creates a HTTP response. Populates status line with arguments. 48 | HttpResponse( HttpVersion httpVersion, size_t statusCode, const std::string& reason ); 49 | 50 | HttpResponse( const HttpResponse& rhs ); 51 | HttpResponse& operator=( const HttpResponse& rhs ); 52 | 53 | //! Sets reason to \a reason. 54 | HttpResponse& reason( const std::string& reason ); 55 | //! Sets status code to \a code. 56 | HttpResponse& statusCode( size_t code ); 57 | 58 | //! Returns reason. 59 | const std::string& getReason() const; 60 | //! Returns status code. 61 | size_t getStatusCode() const; 62 | 63 | //! Sets reason to \a reason. 64 | void setReason( const std::string& reason ); 65 | //! Sets status code to \a code. 66 | void setStatusCode( size_t code ); 67 | 68 | /*! Parses header from \a header. Throws exceptions for incomplete 69 | or invalid headers. */ 70 | void parseHeader( const std::string& header ); 71 | 72 | //! Exception representing invalid status line. 73 | class ExcStatusLineInvalid : public ci::Exception 74 | { 75 | public: 76 | ExcStatusLineInvalid( const std::string &statusLine ) throw(); 77 | virtual const char* what() const throw() 78 | { 79 | return mMessage; 80 | } 81 | private: 82 | char mMessage[ 2048 ]; 83 | }; 84 | protected: 85 | std::string headerToString() const; 86 | 87 | std::string mReason; 88 | size_t mStatusCode; 89 | }; 90 | -------------------------------------------------------------------------------- /src/KeyValuePairInterface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "KeyValuePairInterface.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include "cinder/Utilities.h" 42 | #include 43 | 44 | using namespace ci; 45 | using namespace std; 46 | 47 | KeyValuePairInterface::KeyValuePairInterface( const string& key, const string& value ) 48 | : ProtocolInterface(), mKeyValuePair( make_pair( key, value ) ) 49 | { 50 | } 51 | 52 | const string& KeyValuePairInterface::getValue() const 53 | { 54 | return mKeyValuePair.second; 55 | } 56 | 57 | void KeyValuePairInterface::setValue( const string& v ) 58 | { 59 | mKeyValuePair.second = v; 60 | } 61 | 62 | void KeyValuePairInterface::parse( const BufferRef& buffer ) 63 | { 64 | string s = bufferToString( buffer ); 65 | vector lines; 66 | boost::split( lines, s, boost::is_any_of( sCrLf ) ); 67 | if ( !lines.empty() ) { 68 | vector pair; 69 | boost::split( pair, lines.at( 0 ), boost::is_any_of( " " ) ); 70 | if ( pair.size() > 1 ) { 71 | string key = pair.at( 0 ); 72 | pair.erase( pair.begin() ); 73 | string value = boost::join( pair, " " ); 74 | mKeyValuePair = make_pair( key, value ); 75 | } 76 | } 77 | } 78 | 79 | BufferRef KeyValuePairInterface::toBuffer() const 80 | { 81 | return stringToBuffer( toString() ); 82 | } 83 | 84 | string KeyValuePairInterface::toString() const 85 | { 86 | return keyValuePairToString( mKeyValuePair ); 87 | } 88 | 89 | ostream& operator<<( ostream& out, const KeyValuePairInterface& p ) 90 | { 91 | out << p.toString(); 92 | return out; 93 | } 94 | -------------------------------------------------------------------------------- /src/KeyValuePairInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "ProtocolInterface.h" 41 | 42 | class KeyValuePairInterface : public ProtocolInterface 43 | { 44 | public: 45 | //! Returns string representing value. 46 | const std::string& getValue() const; 47 | //! Sets value to \a v. 48 | void setValue( const std::string& v ); 49 | 50 | //! Parses \a buffer to key-value pair. 51 | void parse( const ci::BufferRef& buffer ); 52 | 53 | //! Converts message to ci::Buffer. 54 | ci::BufferRef toBuffer() const; 55 | //! Converts message to string. 56 | virtual std::string toString() const; 57 | protected: 58 | KeyValuePairInterface( const std::string& key = "", const std::string& value = "" ); 59 | 60 | KeyValuePair mKeyValuePair; 61 | 62 | friend std::ostream& operator<<( std::ostream& out, const KeyValuePairInterface& h ); 63 | }; 64 | 65 | std::ostream& operator<<( std::ostream& out, const KeyValuePairInterface& h ); 66 | -------------------------------------------------------------------------------- /src/ProtocolInterface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #include "ProtocolInterface.h" 39 | 40 | #include "boost/algorithm/string.hpp" 41 | #include "cinder/Utilities.h" 42 | #include 43 | 44 | using namespace ci; 45 | using namespace std; 46 | 47 | string ProtocolInterface::sCrLf = "\r\n"; 48 | 49 | ProtocolInterface::ProtocolInterface() 50 | { 51 | } 52 | 53 | KeyValuePair ProtocolInterface::stringToKeyValuePair( const string& kvp, const string& delim ) 54 | { 55 | vector tokens; 56 | boost::split( tokens, kvp, boost::is_any_of( delim ) ); 57 | if ( tokens.size() < 2 ) { 58 | throw ExcKeyValuePairInvalid( kvp ); 59 | } else { 60 | string key = boost::trim_copy( tokens.at( 0 ) ); 61 | tokens.erase( tokens.begin() ); 62 | string value = boost::join( tokens, delim ); 63 | value = boost::trim_copy( value ); 64 | 65 | KeyValuePair kvp( key, value ); 66 | return kvp; 67 | } 68 | } 69 | 70 | string ProtocolInterface::keyValuePairToString( const KeyValuePair& kvp, const string& delim ) 71 | { 72 | return kvp.first + delim + kvp.second; 73 | } 74 | 75 | BufferRef ProtocolInterface::stringToBuffer( const string& value ) 76 | { 77 | BufferRef buffer = Buffer::create( value.size() ); 78 | buffer->copyFrom( (char*)&value[ 0 ], value.size() ); 79 | return buffer; 80 | } 81 | 82 | string ProtocolInterface::bufferToString( const BufferRef& buffer ) 83 | { 84 | string s( static_cast( buffer->getData() ) ); 85 | if ( s.length() > buffer->getSize() ) { 86 | s.resize( buffer->getSize() ); 87 | } 88 | return s; 89 | } 90 | 91 | void ProtocolInterface::parse( const string& msg ) 92 | { 93 | if ( !msg.empty() ) { 94 | parse( Buffer::create( (char*)&msg[ 0 ], msg.length() ) ); 95 | } 96 | } 97 | 98 | ProtocolInterface::ExcKeyValuePairInvalid::ExcKeyValuePairInvalid( const string& kvp ) throw() 99 | { 100 | sprintf( mMessage, "\"%s\" is not a valid key:value pair", kvp.c_str() ); 101 | } 102 | -------------------------------------------------------------------------------- /src/ProtocolInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, Wieden+Kennedy, 4 | * Stephen Schieberl 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or 8 | * without modification, are permitted provided that the following 9 | * conditions are met: 10 | * 11 | * Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * Neither the name of the Ban the Rewind nor the names of its 19 | * contributors may be used to endorse or promote products 20 | * derived from this software without specific prior written 21 | * permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | * 36 | */ 37 | 38 | #pragma once 39 | 40 | #include "cinder/Buffer.h" 41 | #include "cinder/Exception.h" 42 | #include 43 | #include 44 | #include 45 | 46 | typedef std::pair KeyValuePair; 47 | 48 | class ProtocolInterface 49 | { 50 | public: 51 | //! Parses \a kvp into a key-value pair object. 52 | static KeyValuePair stringToKeyValuePair( const std::string& kvp, const std::string& delim = " " ); 53 | //! Returns string representation of key-value pair \a kvp. 54 | static std::string keyValuePairToString( const KeyValuePair& kvp, const std::string& delim = " " ); 55 | 56 | //! Return string \a value as Buffer. 57 | static ci::BufferRef stringToBuffer( const std::string& value ); 58 | //! Returns string representation of \a buffer. 59 | static std::string bufferToString( const ci::BufferRef& buffer ); 60 | 61 | virtual ci::BufferRef toBuffer() const = 0; 62 | virtual std::string toString() const = 0; 63 | 64 | //! Parses \a msg into relevant commands, field, body, etc. 65 | void parse( const std::string& msg ); 66 | virtual void parse( const ci::BufferRef& buffer ) = 0; 67 | 68 | //! Exception representing invalid key-value pair 69 | class ExcKeyValuePairInvalid : public ci::Exception 70 | { 71 | public: 72 | ExcKeyValuePairInvalid( const std::string& kvp ) throw(); 73 | virtual const char* what() const throw() 74 | { 75 | return mMessage; 76 | } 77 | private: 78 | char mMessage[ 2048 ]; 79 | }; 80 | protected: 81 | ProtocolInterface(); 82 | 83 | static std::string sCrLf; 84 | }; 85 | --------------------------------------------------------------------------------