Repository: nicokaiser/php-websocket Branch: master Commit: 7f10c31144d3 Files: 141 Total size: 303.3 KB Directory structure: gitextract_kle3fe4a/ ├── .gitignore ├── .travis ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── TODO.md ├── VERSION ├── composer.json ├── doc/ │ ├── Makefile │ ├── requirements.txt │ └── source/ │ ├── api/ │ │ ├── Application/ │ │ │ ├── Application.rst │ │ │ ├── EchoApplication.rst │ │ │ └── index.rst │ │ ├── BasicServer.rst │ │ ├── Client.rst │ │ ├── Connection.rst │ │ ├── ConnectionManager.rst │ │ ├── Exception/ │ │ │ ├── BadRequestException.rst │ │ │ ├── CloseException.rst │ │ │ ├── ConnectionException.rst │ │ │ ├── Exception.rst │ │ │ ├── FrameException.rst │ │ │ ├── HandshakeException.rst │ │ │ ├── InvalidOriginException.rst │ │ │ ├── PayloadException.rst │ │ │ ├── RateLimiterException.rst │ │ │ ├── SocketException.rst │ │ │ └── index.rst │ │ ├── Frame/ │ │ │ ├── Frame.rst │ │ │ ├── HybiFrame.rst │ │ │ └── index.rst │ │ ├── Listener/ │ │ │ ├── HandshakeRequestListener.rst │ │ │ ├── Listener.rst │ │ │ ├── OriginPolicy.rst │ │ │ ├── RateLimiter.rst │ │ │ └── index.rst │ │ ├── Payload/ │ │ │ ├── HybiPayload.rst │ │ │ ├── Payload.rst │ │ │ └── index.rst │ │ ├── Protocol/ │ │ │ ├── Hybi10Protocol.rst │ │ │ ├── HybiProtocol.rst │ │ │ ├── Protocol.rst │ │ │ ├── Rfc6455Protocol.rst │ │ │ └── index.rst │ │ ├── Resource.rst │ │ ├── Server.rst │ │ ├── Socket/ │ │ │ ├── ClientSocket.rst │ │ │ ├── ServerClientSocket.rst │ │ │ ├── ServerSocket.rst │ │ │ ├── Socket.rst │ │ │ ├── UriSocket.rst │ │ │ └── index.rst │ │ ├── Util/ │ │ │ ├── Configurable.rst │ │ │ ├── Ssl.rst │ │ │ └── index.rst │ │ └── index.rst │ ├── authors.rst │ ├── conf.py │ ├── getting-started.rst │ ├── index.rst │ ├── installing.rst │ ├── introduction.rst │ ├── performance.rst │ └── setup.py ├── examples/ │ ├── StatusApplication.php │ ├── coffeescript/ │ │ ├── coffee/ │ │ │ ├── client.coffee │ │ │ └── status.coffee │ │ ├── css/ │ │ │ ├── client.css │ │ │ └── status.css │ │ ├── index.html │ │ └── status.html │ ├── server.pem │ ├── server.php │ └── server_ssl.php ├── lib/ │ ├── SplClassLoader.php │ └── Wrench/ │ ├── Application/ │ │ ├── Application.php │ │ ├── EchoApplication.php │ │ └── ServerTimeApplication.php │ ├── BasicServer.php │ ├── Client.php │ ├── Connection.php │ ├── ConnectionManager.php │ ├── Exception/ │ │ ├── BadRequestException.php │ │ ├── CloseException.php │ │ ├── ConnectionException.php │ │ ├── Exception.php │ │ ├── FrameException.php │ │ ├── HandshakeException.php │ │ ├── InvalidOriginException.php │ │ ├── PayloadException.php │ │ ├── RateLimiterException.php │ │ └── SocketException.php │ ├── Frame/ │ │ ├── Frame.php │ │ └── HybiFrame.php │ ├── Listener/ │ │ ├── HandshakeRequestListener.php │ │ ├── Listener.php │ │ ├── OriginPolicy.php │ │ └── RateLimiter.php │ ├── Payload/ │ │ ├── HybiPayload.php │ │ ├── Payload.php │ │ └── PayloadHandler.php │ ├── Protocol/ │ │ ├── Hybi10Protocol.php │ │ ├── HybiProtocol.php │ │ ├── Protocol.php │ │ └── Rfc6455Protocol.php │ ├── Resource.php │ ├── Server.php │ ├── Socket/ │ │ ├── ClientSocket.php │ │ ├── ServerClientSocket.php │ │ ├── ServerSocket.php │ │ ├── Socket.php │ │ └── UriSocket.php │ ├── Tests/ │ │ ├── Application/ │ │ │ └── EchoApplicationTest.php │ │ ├── BasicServerTest.php │ │ ├── ClientTest.php │ │ ├── ConnectionManagerTest.php │ │ ├── ConnectionTest.php │ │ ├── Frame/ │ │ │ ├── BaseSubclassFrameTest.php │ │ │ ├── FrameTest.php │ │ │ └── HybiFrameTest.php │ │ ├── Listener/ │ │ │ ├── ListenerTest.php │ │ │ ├── OriginPolicyTest.php │ │ │ └── RateLimiterTest.php │ │ ├── Payload/ │ │ │ ├── HybiPayloadTest.php │ │ │ └── PayloadTest.php │ │ ├── Protocol/ │ │ │ ├── ProtocolTest.php │ │ │ └── Rfc6455ProtocolTest.php │ │ ├── ServerTest.php │ │ ├── ServerTestHelper.php │ │ ├── Socket/ │ │ │ ├── ClientSocketTest.php │ │ │ ├── ServerClientSocketTest.php │ │ │ ├── ServerSocketTest.php │ │ │ ├── SocketTest.php │ │ │ └── UriSocketTest.php │ │ ├── Test.php │ │ ├── bootstrap.php │ │ └── server.php │ └── Util/ │ ├── Configurable.php │ └── Ssl.php └── phpunit.xml ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.sw[m-p] phpunit build doc/build ================================================ FILE: .travis ================================================ #!/bin/bash phpunit return=$? echo "" echo "Server error log" cat build/server.err.log echo "" echo "Server log" cat build/server.log exit $return ================================================ FILE: .travis.yml ================================================ language: php script: ./.travis php: - 5.3 - 5.4 branches: only: - master - devel - 2.0 notifications: email: - dominic@varspool.com ================================================ FILE: CHANGELOG.md ================================================ # Changelog ## 2.0.0 * Name change: php-websocket was renamed to Wrench, along with a top-level namespace change. * Moved to a more traditional project layout. * Added composer.json: wrench/wrench is the new package name. * Added PHPUnit tests, and Travis CI integration * Everything is now much nicer to override and customize. * Extensive changes to the protected API, not much change to the public API * Deprecated: `$server->removeClientOnClose($client)`, `$server->removeClientOnError($client)` (both cases should be managed by overriding the server, or hooking into `$client->onDisconnect()`) * Deprecated: `StatusApplication` and `DemoApplication`, both moved to examples directory. * Split out new classes (and in some cases hierarchies) for protocol, payload frame, connection and event handling. * Added dependency injection patterns everywhere to split logic out into loosely coupled, replacable aggregate classes. * Added the Configurable interface, providing a way to configure most of the primary classes in detail (if you don't feel like extending them). * Refactored the client class to be in the same namespace as the server libraries. * @vincentdieltiens worked on SSL configuration, and added a method to generate a certificate file. ## 1.0.0 * Refactored methods to open up more of the protected API. * @lemmingzshadow switched the server to use streams instead of sockets, and implemented SSL support. * @mazhack added support for the new WebSocket object in Firefox 11. * Plenty of bugfixes ================================================ FILE: LICENSE ================================================ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO. ================================================ FILE: README.md ================================================ # Wrench ## Simple WebSocket Client/Server for PHP ### Formerly known as php-websocket * Version: **2.0.0** * Build Status: [![Build Status](https://secure.travis-ci.org/varspool/Wrench.png?branch=master)](http://travis-ci.org/varspool/Wrench) * Documentation: [wrench.readthedocs.org](http://wrench.readthedocs.org/en/latest/index.html) A simple websocket server and client package for PHP 5.3/5.4, using streams. Protocol support is based around [RFC 6455](http://tools.ietf.org/html/rfc6455), targeting the latest stable versions of Chrome and Firefox. (Suggest other clients [here](https://github.com/varspool/Wrench/wiki)) ### Backward compatibility #### Public API The new vendor namespace is Wrench. This namespace begins in the `/lib` directory, rather than `server/lib`. Apart from the new namespace, the public API of this new major version is fairly compatible with that of php-websocket 1.0.0. #### Protected API The protected API has changed, a lot. Many method have been broken up into simple protected methods. This makes the Server class much easier to extend. In fact, almost all of the classes involved in your typical daemon can now be replaced or extended, including the socket handling and protocol handling. #### What happened to the `client` dir? The client-side libraries are no longer supported: some libraries are included but are packaged only as examples. You're free to use whatever client-side libraries you'd like with the server. If you're still using them, see the 1.0 branch. ## Installation The library is PSR-0 compatible, with a vendor name of **Wrench**. An SplClassLoader is bundled for convenience. ## Usage This creates a server on 127.0.0.1:8000 with one Application that listens for WebSocket requests to `ws://localhost:8000/echo` and `ws://localhost:8000/chat`: ```php $server = new \Wrench\BasicServer('ws://localhost:8000', array( 'allowed_origins' => array( 'mysite.com', 'mysite.dev.localdomain' ) )); $server->registerApplication('echo', new \Wrench\Examples\EchoApplication()); $server->registerApplication('chat', new \My\ChatApplication()); $server->run(); ``` ## Authors The original maintainer and author was [@nicokaiser](https://github.com/nicokaiser). Plentiful improvements were contributed by [@lemmingzshadow](https://github.com/lemmingzshadow) and [@mazhack](https://github.com/mazhack). Parts of the Socket class were written by Moritz Wutz. The server is licensed under the WTFPL, a free software compatible license. ## Bugs/Todos/Hints - Add tests around fragmented payloads (split into many frames). - To report issues, see the [issue tracker](https://github.com/varspool/Wrench/issues). ## Examples - See server.php in the examples directory and Wrench\Application\EchoApplication - [Jitt.li](http://jitt.li), a Twitter API sample project. - For Symfony2, [VarspoolWebsocketBundle](https://github.com/varspool/WebsocketBundle) extends this library for use with the Service Container. ================================================ FILE: TODO.md ================================================ # TODO - Unify the socket handling of `WebSocket\Client` with that of `Websocket\Socket` - Moar tests! ================================================ FILE: VERSION ================================================ 2.0.0 ================================================ FILE: composer.json ================================================ { "name": "wrench/wrench", "type": "library", "description": "PHP WebSocket client/server library", "keywords": ["websocket", "websockets", "hybi"], "homepage": "http://github.com/varspool/Wrench", "license": "WTFPL", "authors": [ { "name": "Dominic Scheirlinck", "email": "dominic@varspool.com", "homepage": "http://www.somethingemporium.com/" }, { "name": "Simon Samtleben", "email": "web@lemmingzshadow.net", "homepage": "http://lemmingzshadow.net/" }, { "name": "Nico Kaiser", "email": "nico@kaiser.me", "homepage": "http://siriux.net/" } ], "require": { "php": ">=5.3" }, "autoload": { "psr-0": { "Wrench": "lib/" } } } ================================================ FILE: doc/Makefile ================================================ # Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = build SPHPDOX = /usr/bin/env php ~/workspace/external/sphpdox/sphpdox.php # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" clean: -rm -rf $(BUILDDIR)/* api: buildapi clean copyapi html singlehtml buildapi: cd ~/workspace/external/sphpdox && ${SPHPDOX} process -t "API Documentation" -x "Wrench\Tests" Wrench ../../wrench/lib copyapi: rm -rf ~/workspace/wrench/doc/source/api cp -r ~/workspace/external/sphpdox/build/Wrench ~/workspace/wrench/doc/source/api html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Wrench.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Wrench.qhc" devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/Wrench" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Wrench" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." ================================================ FILE: doc/requirements.txt ================================================ sphinxcontrib-phpdomain ================================================ FILE: doc/source/api/Application/Application.rst ================================================ -------------------------------- Wrench\\Application\\Application -------------------------------- .. php:namespace: Wrench\\Application .. php:class:: Application Wrench Server Application .. php:method:: onData($payload, $connection) Handle data received from a client :type $payload: Payload :param $payload: A payload object, that supports __toString() :type $connection: Connection :param $connection: ================================================ FILE: doc/source/api/Application/EchoApplication.rst ================================================ ------------------------------------ Wrench\\Application\\EchoApplication ------------------------------------ .. php:namespace: Wrench\\Application .. php:class:: EchoApplication Example application for Wrench: echo server .. php:method:: onData($data, $client) :param $data: :param $client: ================================================ FILE: doc/source/api/Application/index.rst ================================================ ::::::::::::::::::: Wrench\\Application ::::::::::::::::::: .. php:namespace: Wrench\\Application .. toctree:: Application EchoApplication ================================================ FILE: doc/source/api/BasicServer.rst ================================================ ------------------- Wrench\\BasicServer ------------------- .. php:namespace: Wrench .. php:class:: BasicServer .. php:const:: EVENT_SOCKET_CONNECT Events .. php:attr:: rateLimiter protected .. php:attr:: originPolicy protected .. php:attr:: uri protected string The URI of the server .. php:attr:: options protected array Options .. php:attr:: logger protected Closure A logging callback The default callback simply prints to stdout. You can pass your own logger in the options array. It should take a string message and string priority as parameters. .. php:attr:: listeners protected array resource) .. php:method:: getConnectionForClientSocket($socket) Returns the Connection associated with the specified socket resource :type $socket: resource :param $socket: :returns: Connection .. php:method:: selectAndProcess() Select and process an array of resources .. php:method:: processMasterSocket() Process events on the master socket ($this->socket) :returns: void .. php:method:: createConnection($resource) Creates a connection from a socket resource The create connection object is based on the options passed into the constructor ('connection_class', 'connection_options'). This connection instance and its associated socket resource are then stored in the manager. :type $resource: resource :param $resource: A socket resource :returns: Connection .. php:method:: processClientSocket($socket) Process events on a client socket :type $socket: resource :param $socket: .. php:method:: resourceId($resource) This server makes an explicit assumption: PHP resource types may be cast to a integer. Furthermore, we assume this is bijective. Both seem to be true in most circumstances, but may not be guaranteed. This method (and $this->getResourceId()) exist to make this assumption explicit. This is needed on the connection manager as well as on resources :type $resource: resource :param $resource: .. php:method:: getUri() Gets the connection manager's listening URI :returns: string .. php:method:: log($message, $priority = 'info') Logs a message :type $message: string :param $message: :type $priority: string :param $priority: .. php:method:: getServer() :returns: \Wrench\Server .. php:method:: removeConnection(Connection $connection) Removes a connection :type $connection: Connection :param $connection: .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Exception/BadRequestException.rst ================================================ -------------------------------------- Wrench\\Exception\\BadRequestException -------------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: BadRequestException .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __construct($message = null, $code = null, $previous = null) :param $message: :param $code: :type $previous: Exception :param $previous: .. php:method:: __clone() .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/CloseException.rst ================================================ --------------------------------- Wrench\\Exception\\CloseException --------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: CloseException Close connection exception .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __construct($message = null, $code = null, $previous = null) :param $message: :param $code: :type $previous: Exception :param $previous: .. php:method:: __clone() .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/ConnectionException.rst ================================================ -------------------------------------- Wrench\\Exception\\ConnectionException -------------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: ConnectionException .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __clone() .. php:method:: __construct($message, $code, $previous) :param $message: :param $code: :param $previous: .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/Exception.rst ================================================ ---------------------------- Wrench\\Exception\\Exception ---------------------------- .. php:namespace: Wrench\\Exception .. php:class:: Exception .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __clone() .. php:method:: __construct($message, $code, $previous) :param $message: :param $code: :param $previous: .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/FrameException.rst ================================================ --------------------------------- Wrench\\Exception\\FrameException --------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: FrameException .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __clone() .. php:method:: __construct($message, $code, $previous) :param $message: :param $code: :param $previous: .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/HandshakeException.rst ================================================ ------------------------------------- Wrench\\Exception\\HandshakeException ------------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: HandshakeException .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __construct($message = null, $code = null, $previous = null) :param $message: :param $code: :type $previous: Exception :param $previous: .. php:method:: __clone() .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/InvalidOriginException.rst ================================================ ----------------------------------------- Wrench\\Exception\\InvalidOriginException ----------------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: InvalidOriginException Invalid origin exception .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __construct($message = null, $code = null, $previous = null) :param $message: :param $code: :type $previous: Exception :param $previous: .. php:method:: __clone() .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/PayloadException.rst ================================================ ----------------------------------- Wrench\\Exception\\PayloadException ----------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: PayloadException .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __clone() .. php:method:: __construct($message, $code, $previous) :param $message: :param $code: :param $previous: .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/RateLimiterException.rst ================================================ --------------------------------------- Wrench\\Exception\\RateLimiterException --------------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: RateLimiterException .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __construct($message = null, $code = null, $previous = null) :param $message: :param $code: :type $previous: Exception :param $previous: .. php:method:: __clone() .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/SocketException.rst ================================================ ---------------------------------- Wrench\\Exception\\SocketException ---------------------------------- .. php:namespace: Wrench\\Exception .. php:class:: SocketException .. php:attr:: message protected .. php:attr:: code protected .. php:attr:: file protected .. php:attr:: line protected .. php:method:: __clone() .. php:method:: __construct($message, $code, $previous) :param $message: :param $code: :param $previous: .. php:method:: getMessage() .. php:method:: getCode() .. php:method:: getFile() .. php:method:: getLine() .. php:method:: getTrace() .. php:method:: getPrevious() .. php:method:: getTraceAsString() .. php:method:: __toString() ================================================ FILE: doc/source/api/Exception/index.rst ================================================ ::::::::::::::::: Wrench\\Exception ::::::::::::::::: .. php:namespace: Wrench\\Exception .. toctree:: BadRequestException CloseException ConnectionException Exception FrameException HandshakeException InvalidOriginException PayloadException RateLimiterException SocketException ================================================ FILE: doc/source/api/Frame/Frame.rst ================================================ -------------------- Wrench\\Frame\\Frame -------------------- .. php:namespace: Wrench\\Frame .. php:class:: Frame Represents a WebSocket frame .. php:attr:: length protected int The frame data length .. php:attr:: type protected int The type of this payload .. php:attr:: buffer protected string The buffer May not be a complete payload, because this frame may still be receiving data. See .. php:attr:: payload protected string The enclosed frame payload May not be a complete payload, because this frame might indicate a continuation frame. See isFinal() versus isComplete() .. php:method:: getLength() Gets the length of the payload :returns: int .. php:method:: encode($data, $type = Protocol::TYPE_TEXT, $masked = false) Resets the frame and encodes the given data into it :type $data: string :param $data: :type $type: int :param $type: :type $masked: boolean :param $masked: :returns: Frame .. php:method:: isFinal() Whether the frame is the final one in a continuation :returns: boolean .. php:method:: getType() :returns: int .. php:method:: decodeFramePayloadFromBuffer() Decodes a frame payload from the buffer :returns: void .. php:method:: getExpectedBufferLength() Gets the expected length of the buffer once all the data has been receieved :returns: int .. php:method:: isComplete() Whether the frame is complete :returns: boolean .. php:method:: receiveData($data) Receieves data into the frame :param $data: .. php:method:: getRemainingData() Gets the remaining number of bytes before this frame will be complete :returns: number .. php:method:: isWaitingForData() Whether this frame is waiting for more data :returns: boolean .. php:method:: getFramePayload() Gets the contents of the frame payload The frame must be complete to call this method. :returns: string .. php:method:: getFrameBuffer() Gets the contents of the frame buffer This is the encoded value, receieved into the frame with recieveData(). :returns: string binary .. php:method:: getBufferLength() Gets the expected length of the frame payload :returns: int ================================================ FILE: doc/source/api/Frame/HybiFrame.rst ================================================ ------------------------ Wrench\\Frame\\HybiFrame ------------------------ .. php:namespace: Wrench\\Frame .. php:class:: HybiFrame .. php:attr:: masked protected boolean Whether the payload is masked .. php:attr:: mask protected string Masking key .. php:attr:: offset_payload protected int Byte offsets .. php:attr:: offset_mask protected .. php:attr:: length protected int The frame data length .. php:attr:: type protected int The type of this payload .. php:attr:: buffer protected string The buffer May not be a complete payload, because this frame may still be receiving data. See .. php:attr:: payload protected string The enclosed frame payload May not be a complete payload, because this frame might indicate a continuation frame. See isFinal() versus isComplete() .. php:method:: encode($payload, $type = Protocol::TYPE_TEXT, $masked = false) :param $payload: :param $type: :param $masked: .. php:method:: mask($payload) Masks/Unmasks the frame :type $payload: string :param $payload: :returns: string .. php:method:: unmask($payload) Masks a payload :type $payload: string :param $payload: :returns: string .. php:method:: receiveData($data) :param $data: .. php:method:: getMask() Gets the mask :returns: string .. php:method:: generateMask() Generates a suitable masking key :returns: string .. php:method:: isMasked() Whether the frame is masked :returns: boolean .. php:method:: getExpectedBufferLength() .. php:method:: getPayloadOffset() Gets the offset of the payload in the frame :returns: int .. php:method:: getMaskOffset() Gets the offset in the frame to the masking bytes :returns: int .. php:method:: getLength() .. php:method:: getInitialLength() Gets the inital length value, stored in the first length byte This determines how the rest of the length value is parsed out of the frame. :returns: int .. php:method:: getLengthSize() Returns the byte size of the length part of the frame Not including the initial 7 bit part :returns: int .. php:method:: getMaskSize() Returns the byte size of the mask part of the frame :returns: int .. php:method:: decodeFramePayloadFromBuffer() .. php:method:: isFinal() .. php:method:: getType() .. php:method:: isComplete() Whether the frame is complete :returns: boolean .. php:method:: getRemainingData() Gets the remaining number of bytes before this frame will be complete :returns: number .. php:method:: isWaitingForData() Whether this frame is waiting for more data :returns: boolean .. php:method:: getFramePayload() Gets the contents of the frame payload The frame must be complete to call this method. :returns: string .. php:method:: getFrameBuffer() Gets the contents of the frame buffer This is the encoded value, receieved into the frame with recieveData(). :returns: string binary .. php:method:: getBufferLength() Gets the expected length of the frame payload :returns: int ================================================ FILE: doc/source/api/Frame/index.rst ================================================ ::::::::::::: Wrench\\Frame ::::::::::::: .. php:namespace: Wrench\\Frame .. toctree:: Frame HybiFrame ================================================ FILE: doc/source/api/Listener/HandshakeRequestListener.rst ================================================ ------------------------------------------ Wrench\\Listener\\HandshakeRequestListener ------------------------------------------ .. php:namespace: Wrench\\Listener .. php:class:: HandshakeRequestListener .. php:method:: onHandshakeRequest(Connection $connection, $path, $origin, $key, $extensions) Handshake request listener :type $connection: Connection :param $connection: :type $path: string :param $path: :type $origin: string :param $origin: :type $key: string :param $key: :type $extensions: array :param $extensions: ================================================ FILE: doc/source/api/Listener/Listener.rst ================================================ -------------------------- Wrench\\Listener\\Listener -------------------------- .. php:namespace: Wrench\\Listener .. php:class:: Listener .. php:method:: listen(Server $server) :type $server: Server :param $server: ================================================ FILE: doc/source/api/Listener/OriginPolicy.rst ================================================ ------------------------------ Wrench\\Listener\\OriginPolicy ------------------------------ .. php:namespace: Wrench\\Listener .. php:class:: OriginPolicy .. php:attr:: allowed protected .. php:method:: __construct($allowed) :param $allowed: .. php:method:: onHandshakeRequest(Connection $connection, $path, $origin, $key, $extensions) Handshake request listener Closes the connection on handshake from an origin that isn't allowed :type $connection: Connection :param $connection: :type $path: string :param $path: :type $origin: string :param $origin: :type $key: string :param $key: :type $extensions: array :param $extensions: .. php:method:: isAllowed($origin) Whether the specified origin is allowed under this policy :type $origin: string :param $origin: :returns: boolean .. php:method:: listen(Server $server) :type $server: Server :param $server: ================================================ FILE: doc/source/api/Listener/RateLimiter.rst ================================================ ----------------------------- Wrench\\Listener\\RateLimiter ----------------------------- .. php:namespace: Wrench\\Listener .. php:class:: RateLimiter .. php:attr:: server protected Server The server being limited .. php:attr:: ips protected array Connection counts per IP address .. php:attr:: requests protected array> Request tokens per IP address .. php:attr:: options protected array .. php:attr:: protocol protected Protocol .. php:method:: __construct($options = array()) Constructor :type $options: array :param $options: .. php:method:: configure($options) :type $options: array :param $options: .. php:method:: listen(Server $server) :type $server: Server :param $server: .. php:method:: onSocketConnect($socket, $connection) Event listener :type $socket: resource :param $socket: :type $connection: Connection :param $connection: .. php:method:: onSocketDisconnect($socket, $connection) Event listener :type $socket: resource :param $socket: :type $connection: Connection :param $connection: .. php:method:: onClientData($socket, $connection) Event listener :type $socket: resource :param $socket: :type $connection: Connection :param $connection: .. php:method:: checkConnections($connection) Idempotent :type $connection: Connection :param $connection: .. php:method:: checkConnectionsPerIp($connection) NOT idempotent, call once per connection :type $connection: Connection :param $connection: .. php:method:: releaseConnection($connection) NOT idempotent, call once per disconnection :type $connection: Connection :param $connection: .. php:method:: checkRequestsPerMinute($connection) NOT idempotent, call once per data :type $connection: Connection :param $connection: .. php:method:: limit($connection, $limit) Limits the given connection :type $connection: Connection :param $connection: :type $limit: string :param $limit: Reason .. php:method:: log($message, $priority = 'info') Logger :type $message: string :param $message: :type $priority: string :param $priority: .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Listener/index.rst ================================================ :::::::::::::::: Wrench\\Listener :::::::::::::::: .. php:namespace: Wrench\\Listener .. toctree:: HandshakeRequestListener Listener OriginPolicy RateLimiter ================================================ FILE: doc/source/api/Payload/HybiPayload.rst ================================================ ---------------------------- Wrench\\Payload\\HybiPayload ---------------------------- .. php:namespace: Wrench\\Payload .. php:class:: HybiPayload Gets a HyBi payload .. php:attr:: frames protected array A payload may consist of one or more frames .. php:method:: getFrame() .. php:method:: getCurrentFrame() Gets the current frame for the payload :returns: mixed .. php:method:: getReceivingFrame() Gets the frame into which data should be receieved :returns: Frame .. php:method:: isComplete() Whether the payload is complete :returns: boolean .. php:method:: encode($data, $type = Protocol::TYPE_TEXT, $masked = false) Encodes a payload :type $data: string :param $data: :type $type: int :param $type: :type $masked: boolean :param $masked: :returns: Payload .. php:method:: getRemainingData() Gets the number of remaining bytes before this payload will be complete May return 0 (no more bytes required) or null (unknown number of bytes required). :returns: number|NULL .. php:method:: isWaitingForData() Whether this payload is waiting for more data :returns: boolean .. php:method:: sendToSocket(Socket $socket) :type $socket: Socket :param $socket: :returns: boolean .. php:method:: receiveData($data) Receive raw data into the payload :type $data: string :param $data: :returns: void .. php:method:: getPayload() :returns: string .. php:method:: __toString() :returns: string .. php:method:: getType() Gets the type of the payload The type of a payload is taken from its first frame :returns: int ================================================ FILE: doc/source/api/Payload/Payload.rst ================================================ ------------------------ Wrench\\Payload\\Payload ------------------------ .. php:namespace: Wrench\\Payload .. php:class:: Payload Payload class Represents a WebSocket protocol payload, which may be made up of multiple frames. .. php:attr:: frames protected array A payload may consist of one or more frames .. php:method:: getCurrentFrame() Gets the current frame for the payload :returns: mixed .. php:method:: getReceivingFrame() Gets the frame into which data should be receieved :returns: Frame .. php:method:: getFrame() Get a frame object :returns: Frame .. php:method:: isComplete() Whether the payload is complete :returns: boolean .. php:method:: encode($data, $type = Protocol::TYPE_TEXT, $masked = false) Encodes a payload :type $data: string :param $data: :type $type: int :param $type: :type $masked: boolean :param $masked: :returns: Payload .. php:method:: getRemainingData() Gets the number of remaining bytes before this payload will be complete May return 0 (no more bytes required) or null (unknown number of bytes required). :returns: number|NULL .. php:method:: isWaitingForData() Whether this payload is waiting for more data :returns: boolean .. php:method:: sendToSocket(Socket $socket) :type $socket: Socket :param $socket: :returns: boolean .. php:method:: receiveData($data) Receive raw data into the payload :type $data: string :param $data: :returns: void .. php:method:: getPayload() :returns: string .. php:method:: __toString() :returns: string .. php:method:: getType() Gets the type of the payload The type of a payload is taken from its first frame :returns: int ================================================ FILE: doc/source/api/Payload/index.rst ================================================ ::::::::::::::: Wrench\\Payload ::::::::::::::: .. php:namespace: Wrench\\Payload .. toctree:: HybiPayload Payload ================================================ FILE: doc/source/api/Protocol/Hybi10Protocol.rst ================================================ -------------------------------- Wrench\\Protocol\\Hybi10Protocol -------------------------------- .. php:namespace: Wrench\\Protocol .. php:class:: Hybi10Protocol http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10 .. php:const:: SCHEME_WEBSOCKET Relevant schemes .. php:const:: HEADER_HOST HTTP headers .. php:const:: HTTP_SWITCHING_PROTOCOLS HTTP error statuses .. php:const:: CLOSE_NORMAL Close statuses .. php:const:: TYPE_CONTINUATION Frame types %x0 denotes a continuation frame %x1 denotes a text frame %x2 denotes a binary frame %x3-7 are reserved for further non-control frames %x8 denotes a connection close %x9 denotes a ping %xA denotes a pong %xB-F are reserved for further control frames .. php:const:: MAGIC_GUID Magic GUID Used in the WebSocket accept header .. php:const:: UPGRADE_VALUE The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword. .. php:const:: CONNECTION_VALUE The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token. .. php:const:: REQUEST_LINE_FORMAT Request line format .. php:const:: REQUEST_LINE_REGEX Request line regex Used for parsing requested path .. php:const:: RESPONSE_LINE_FORMAT Response line format .. php:const:: HEADER_LINE_FORMAT Header line format .. php:attr:: schemes protected array Valid schemes .. php:attr:: closeReasons array> The request line, and an array of headers .. php:method:: validateScheme($scheme) Validates a scheme :type $scheme: string :param $scheme: :returns: string Underlying scheme .. php:method:: getDefaultRequestHeaders($host, $key, $origin) Gets the default request headers :type $host: string :param $host: :type $key: string :param $key: :type $origin: string :param $origin: :returns: multitype:unknown string NULL .. php:method:: getSuccessResponseHeaders($key) Gets the default response headers :type $key: string :param $key: .. php:method:: getPort($scheme) Gets the default port for a scheme By default, the WebSocket Protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over Transport Layer Security :param $scheme: :returns: int ================================================ FILE: doc/source/api/Protocol/HybiProtocol.rst ================================================ ------------------------------ Wrench\\Protocol\\HybiProtocol ------------------------------ .. php:namespace: Wrench\\Protocol .. php:class:: HybiProtocol .. php:const:: SCHEME_WEBSOCKET Relevant schemes .. php:const:: HEADER_HOST HTTP headers .. php:const:: HTTP_SWITCHING_PROTOCOLS HTTP error statuses .. php:const:: CLOSE_NORMAL Close statuses .. php:const:: TYPE_CONTINUATION Frame types %x0 denotes a continuation frame %x1 denotes a text frame %x2 denotes a binary frame %x3-7 are reserved for further non-control frames %x8 denotes a connection close %x9 denotes a ping %xA denotes a pong %xB-F are reserved for further control frames .. php:const:: MAGIC_GUID Magic GUID Used in the WebSocket accept header .. php:const:: UPGRADE_VALUE The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword. .. php:const:: CONNECTION_VALUE The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token. .. php:const:: REQUEST_LINE_FORMAT Request line format .. php:const:: REQUEST_LINE_REGEX Request line regex Used for parsing requested path .. php:const:: RESPONSE_LINE_FORMAT Response line format .. php:const:: HEADER_LINE_FORMAT Header line format .. php:attr:: schemes protected array Valid schemes .. php:attr:: closeReasons array> The request line, and an array of headers .. php:method:: validateScheme($scheme) Validates a scheme :type $scheme: string :param $scheme: :returns: string Underlying scheme .. php:method:: getDefaultRequestHeaders($host, $key, $origin) Gets the default request headers :type $host: string :param $host: :type $key: string :param $key: :type $origin: string :param $origin: :returns: multitype:unknown string NULL .. php:method:: getSuccessResponseHeaders($key) Gets the default response headers :type $key: string :param $key: .. php:method:: getPort($scheme) Gets the default port for a scheme By default, the WebSocket Protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over Transport Layer Security :param $scheme: :returns: int ================================================ FILE: doc/source/api/Protocol/Protocol.rst ================================================ -------------------------- Wrench\\Protocol\\Protocol -------------------------- .. php:namespace: Wrench\\Protocol .. php:class:: Protocol Definitions and implementation helpers for the Wrenchs protocol Based on RFC 6455: http://tools.ietf.org/html/rfc6455 .. php:const:: SCHEME_WEBSOCKET Relevant schemes .. php:const:: HEADER_HOST HTTP headers .. php:const:: HTTP_SWITCHING_PROTOCOLS HTTP error statuses .. php:const:: CLOSE_NORMAL Close statuses .. php:const:: TYPE_CONTINUATION Frame types %x0 denotes a continuation frame %x1 denotes a text frame %x2 denotes a binary frame %x3-7 are reserved for further non-control frames %x8 denotes a connection close %x9 denotes a ping %xA denotes a pong %xB-F are reserved for further control frames .. php:const:: MAGIC_GUID Magic GUID Used in the WebSocket accept header .. php:const:: UPGRADE_VALUE The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword. .. php:const:: CONNECTION_VALUE The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token. .. php:const:: REQUEST_LINE_FORMAT Request line format .. php:const:: REQUEST_LINE_REGEX Request line regex Used for parsing requested path .. php:const:: RESPONSE_LINE_FORMAT Response line format .. php:const:: HEADER_LINE_FORMAT Header line format .. php:attr:: schemes protected array Valid schemes .. php:attr:: closeReasons array> The request line, and an array of headers .. php:method:: validateScheme($scheme) Validates a scheme :type $scheme: string :param $scheme: :returns: string Underlying scheme .. php:method:: getDefaultRequestHeaders($host, $key, $origin) Gets the default request headers :type $host: string :param $host: :type $key: string :param $key: :type $origin: string :param $origin: :returns: multitype:unknown string NULL .. php:method:: getSuccessResponseHeaders($key) Gets the default response headers :type $key: string :param $key: .. php:method:: getPort($scheme) Gets the default port for a scheme By default, the WebSocket Protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over Transport Layer Security :param $scheme: :returns: int ================================================ FILE: doc/source/api/Protocol/Rfc6455Protocol.rst ================================================ --------------------------------- Wrench\\Protocol\\Rfc6455Protocol --------------------------------- .. php:namespace: Wrench\\Protocol .. php:class:: Rfc6455Protocol This is the version of websockets used by Chrome versions 17 through 19. .. php:const:: SCHEME_WEBSOCKET Relevant schemes .. php:const:: HEADER_HOST HTTP headers .. php:const:: HTTP_SWITCHING_PROTOCOLS HTTP error statuses .. php:const:: CLOSE_NORMAL Close statuses .. php:const:: TYPE_CONTINUATION Frame types %x0 denotes a continuation frame %x1 denotes a text frame %x2 denotes a binary frame %x3-7 are reserved for further non-control frames %x8 denotes a connection close %x9 denotes a ping %xA denotes a pong %xB-F are reserved for further control frames .. php:const:: MAGIC_GUID Magic GUID Used in the WebSocket accept header .. php:const:: UPGRADE_VALUE The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword. .. php:const:: CONNECTION_VALUE The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token. .. php:const:: REQUEST_LINE_FORMAT Request line format .. php:const:: REQUEST_LINE_REGEX Request line regex Used for parsing requested path .. php:const:: RESPONSE_LINE_FORMAT Response line format .. php:const:: HEADER_LINE_FORMAT Header line format .. php:attr:: schemes protected array Valid schemes .. php:attr:: closeReasons array> The request line, and an array of headers .. php:method:: validateScheme($scheme) Validates a scheme :type $scheme: string :param $scheme: :returns: string Underlying scheme .. php:method:: getDefaultRequestHeaders($host, $key, $origin) Gets the default request headers :type $host: string :param $host: :type $key: string :param $key: :type $origin: string :param $origin: :returns: multitype:unknown string NULL .. php:method:: getSuccessResponseHeaders($key) Gets the default response headers :type $key: string :param $key: .. php:method:: getPort($scheme) Gets the default port for a scheme By default, the WebSocket Protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over Transport Layer Security :param $scheme: :returns: int ================================================ FILE: doc/source/api/Protocol/index.rst ================================================ :::::::::::::::: Wrench\\Protocol :::::::::::::::: .. php:namespace: Wrench\\Protocol .. toctree:: Hybi10Protocol HybiProtocol Protocol Rfc6455Protocol ================================================ FILE: doc/source/api/Resource.rst ================================================ ---------------- Wrench\\Resource ---------------- .. php:namespace: Wrench .. php:class:: Resource Resource interface .. php:method:: getResourceId() .. php:method:: getResource() ================================================ FILE: doc/source/api/Server.rst ================================================ -------------- Wrench\\Server -------------- .. php:namespace: Wrench .. php:class:: Server WebSocket server The server extends socket, which provides the master socket resource. This resource is listened to, and an array of clients managed. .. php:const:: EVENT_SOCKET_CONNECT Events .. php:attr:: uri protected string The URI of the server .. php:attr:: options protected array Options .. php:attr:: logger protected Closure A logging callback The default callback simply prints to stdout. You can pass your own logger in the options array. It should take a string message and string priority as parameters. .. php:attr:: listeners protected array The socket class to use, defaults to ServerSocket - socket_options => An array of socket options - logger => Closure($message, $priority = 'info'), used for logging :type $options: array :param $options: :returns: void .. php:method:: configureLogger() Configures the logger :returns: void .. php:method:: configureConnectionManager() Configures the connection manager :returns: void .. php:method:: getConnectionManager() Gets the connection manager :returns: \Wrench\ConnectionManager .. php:method:: getUri() :returns: string .. php:method:: setLogger($logger) Sets a logger :type $logger: Closure :param $logger: :returns: void .. php:method:: run() Main server loop :returns: void This method does not return! .. php:method:: log($message, $priority = 'info') Logs a message to the server log The default logger simply prints the message to stdout. You can provide a logging closure. This is useful, for instance, if you've daemonized and closed STDOUT. :type $message: string :param $message: Message to display. :param $priority: :returns: void .. php:method:: notify($event, $arguments = array()) Notifies listeners of an event :type $event: string :param $event: :type $arguments: array :param $arguments: Event arguments :returns: void .. php:method:: addListener($event, $callback) Adds a listener Provide an event (see the Server::EVENT_* constants) and a callback closure. Some arguments may be provided to your callback, such as the connection the caused the event. :type $event: string :param $event: :type $callback: Closure :param $callback: :returns: void .. php:method:: getApplication($key) Returns a server application. :type $key: string :param $key: Name of application. :returns: Application The application object. .. php:method:: registerApplication($key, $application) Adds a new application object to the application storage. :type $key: string :param $key: Name of application. :type $application: object :param $application: The application object :returns: void .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Socket/ClientSocket.rst ================================================ ---------------------------- Wrench\\Socket\\ClientSocket ---------------------------- .. php:namespace: Wrench\\Socket .. php:class:: ClientSocket Options: - timeout_connect => int, seconds, default 2 .. php:const:: TIMEOUT_CONNECT Default connection timeout .. php:const:: TIMEOUT_SOCKET Default timeout for socket operations (reads, writes) .. php:const:: DEFAULT_RECEIVE_LENGTH .. php:const:: NAME_PART_IP Socket name parts .. php:attr:: scheme protected .. php:attr:: host protected .. php:attr:: port protected .. php:attr:: socket protected resource .. php:attr:: context protected Stream context .. php:attr:: connected protected boolean Whether the socket is connected to a server Note, the connection may not be ready to use, but the socket is connected at least. See $handshaked, and other properties in subclasses. .. php:attr:: firstRead protected boolean Whether the current read is the first one to the socket .. php:attr:: name protected string The socket name according to stream_socket_get_name .. php:attr:: options protected array .. php:attr:: protocol protected Protocol .. php:method:: configure($options) :param $options: .. php:method:: connect() Connects to the given socket .. php:method:: reconnect() .. php:method:: getSocketStreamContextOptions() .. php:method:: getSslStreamContextOptions() .. php:method:: __construct($uri, $options = array()) URI Socket constructor :type $uri: string :param $uri: WebSocket URI, e.g. ws://example.org:8000/chat :param $options: .. php:method:: getUri() Gets the canonical/normalized URI for this socket :returns: string .. php:method:: getName() .. php:method:: getHost() Gets the host name .. php:method:: getPort() .. php:method:: getStreamContext($listen = false) Gets a stream context :param $listen: .. php:method:: getNamePart($name, $part) Gets part of the name of the socket PHP seems to return IPV6 address/port combos like this: ::1:1234, where ::1 is the address and 1234 the port So, the part number here is either the last : delimited section (the port) or all the other sections (the whole initial part, the address). :type $name: string :param $name: (from $this->getName() usually) :param $part: :returns: string .. php:method:: getIp() Gets the IP address of the socket :returns: string .. php:method:: getLastError() Get the last error that occurred on the socket :returns: int|string .. php:method:: isConnected() Whether the socket is currently connected :returns: boolean .. php:method:: disconnect() Disconnect the socket :returns: void .. php:method:: getResource() .. php:method:: getResourceId() .. php:method:: send($data) :type $data: unknown_type :param $data: :returns: boolean|int The number of bytes sent or false on error .. php:method:: receive($length = self::DEFAULT_RECEIVE_LENGTH) Recieve data from the socket :type $length: int :param $length: :returns: string .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Socket/ServerClientSocket.rst ================================================ ---------------------------------- Wrench\\Socket\\ServerClientSocket ---------------------------------- .. php:namespace: Wrench\\Socket .. php:class:: ServerClientSocket .. php:const:: TIMEOUT_SOCKET Default timeout for socket operations (reads, writes) .. php:const:: DEFAULT_RECEIVE_LENGTH .. php:const:: NAME_PART_IP Socket name parts .. php:attr:: socket protected resource .. php:attr:: context protected Stream context .. php:attr:: connected protected boolean Whether the socket is connected to a server Note, the connection may not be ready to use, but the socket is connected at least. See $handshaked, and other properties in subclasses. .. php:attr:: firstRead protected boolean Whether the current read is the first one to the socket .. php:attr:: name protected string The socket name according to stream_socket_get_name .. php:attr:: options protected array .. php:attr:: protocol protected Protocol .. php:method:: __construct($accepted_socket, $options = array()) Constructor A server client socket is accepted from a listening socket, so there's no need to call ->connect() or whatnot. :type $accepted_socket: resource :param $accepted_socket: :type $options: array :param $options: .. php:method:: configure($options) Configure options Options include - timeout_connect => int, seconds, default 2 - timeout_socket => int, seconds, default 5 :type $options: array :param $options: :returns: void .. php:method:: getName() Gets the name of the socket .. php:method:: getNamePart($name, $part) Gets part of the name of the socket PHP seems to return IPV6 address/port combos like this: ::1:1234, where ::1 is the address and 1234 the port So, the part number here is either the last : delimited section (the port) or all the other sections (the whole initial part, the address). :type $name: string :param $name: (from $this->getName() usually) :param $part: :returns: string .. php:method:: getIp() Gets the IP address of the socket :returns: string .. php:method:: getPort() Gets the port of the socket :returns: int .. php:method:: getLastError() Get the last error that occurred on the socket :returns: int|string .. php:method:: isConnected() Whether the socket is currently connected :returns: boolean .. php:method:: disconnect() Disconnect the socket :returns: void .. php:method:: getResource() .. php:method:: getResourceId() .. php:method:: send($data) :type $data: unknown_type :param $data: :returns: boolean|int The number of bytes sent or false on error .. php:method:: receive($length = self::DEFAULT_RECEIVE_LENGTH) Recieve data from the socket :type $length: int :param $length: :returns: string .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Socket/ServerSocket.rst ================================================ ---------------------------- Wrench\\Socket\\ServerSocket ---------------------------- .. php:namespace: Wrench\\Socket .. php:class:: ServerSocket Server socket Used for a server's "master" socket that binds to the configured interface and listens .. php:const:: TIMEOUT_SOCKET Default timeout for socket operations (reads, writes) .. php:const:: DEFAULT_RECEIVE_LENGTH .. php:const:: NAME_PART_IP Socket name parts .. php:attr:: listening protected boolean Whether the socket is listening .. php:attr:: scheme protected .. php:attr:: host protected .. php:attr:: port protected .. php:attr:: socket protected resource .. php:attr:: context protected Stream context .. php:attr:: connected protected boolean Whether the socket is connected to a server Note, the connection may not be ready to use, but the socket is connected at least. See $handshaked, and other properties in subclasses. .. php:attr:: firstRead protected boolean Whether the current read is the first one to the socket .. php:attr:: name protected string The socket name according to stream_socket_get_name .. php:attr:: options protected array .. php:attr:: protocol protected Protocol .. php:method:: configure($options) :param $options: .. php:method:: listen() Listens .. php:method:: accept() Accepts a new connection on the socket :returns: resource .. php:method:: getSocketStreamContextOptions() .. php:method:: getSslStreamContextOptions() .. php:method:: __construct($uri, $options = array()) URI Socket constructor :type $uri: string :param $uri: WebSocket URI, e.g. ws://example.org:8000/chat :param $options: .. php:method:: getUri() Gets the canonical/normalized URI for this socket :returns: string .. php:method:: getName() .. php:method:: getHost() Gets the host name .. php:method:: getPort() .. php:method:: getStreamContext($listen = false) Gets a stream context :param $listen: .. php:method:: getNamePart($name, $part) Gets part of the name of the socket PHP seems to return IPV6 address/port combos like this: ::1:1234, where ::1 is the address and 1234 the port So, the part number here is either the last : delimited section (the port) or all the other sections (the whole initial part, the address). :type $name: string :param $name: (from $this->getName() usually) :param $part: :returns: string .. php:method:: getIp() Gets the IP address of the socket :returns: string .. php:method:: getLastError() Get the last error that occurred on the socket :returns: int|string .. php:method:: isConnected() Whether the socket is currently connected :returns: boolean .. php:method:: disconnect() Disconnect the socket :returns: void .. php:method:: getResource() .. php:method:: getResourceId() .. php:method:: send($data) :type $data: unknown_type :param $data: :returns: boolean|int The number of bytes sent or false on error .. php:method:: receive($length = self::DEFAULT_RECEIVE_LENGTH) Recieve data from the socket :type $length: int :param $length: :returns: string .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Socket/Socket.rst ================================================ ---------------------- Wrench\\Socket\\Socket ---------------------- .. php:namespace: Wrench\\Socket .. php:class:: Socket Socket class Implements low level logic for connecting, serving, reading to, and writing from WebSocket connections using PHP's streams. Unlike in previous versions of this library, a Socket instance now represents a single underlying socket resource. It's designed to be used by aggregation, rather than inheritence. .. php:const:: TIMEOUT_SOCKET Default timeout for socket operations (reads, writes) .. php:const:: DEFAULT_RECEIVE_LENGTH .. php:const:: NAME_PART_IP Socket name parts .. php:attr:: socket protected resource .. php:attr:: context protected Stream context .. php:attr:: connected protected boolean Whether the socket is connected to a server Note, the connection may not be ready to use, but the socket is connected at least. See $handshaked, and other properties in subclasses. .. php:attr:: firstRead protected boolean Whether the current read is the first one to the socket .. php:attr:: name protected string The socket name according to stream_socket_get_name .. php:attr:: options protected array .. php:attr:: protocol protected Protocol .. php:method:: configure($options) Configure options Options include - timeout_connect => int, seconds, default 2 - timeout_socket => int, seconds, default 5 :type $options: array :param $options: :returns: void .. php:method:: getName() Gets the name of the socket .. php:method:: getNamePart($name, $part) Gets part of the name of the socket PHP seems to return IPV6 address/port combos like this: ::1:1234, where ::1 is the address and 1234 the port So, the part number here is either the last : delimited section (the port) or all the other sections (the whole initial part, the address). :type $name: string :param $name: (from $this->getName() usually) :param $part: :returns: string .. php:method:: getIp() Gets the IP address of the socket :returns: string .. php:method:: getPort() Gets the port of the socket :returns: int .. php:method:: getLastError() Get the last error that occurred on the socket :returns: int|string .. php:method:: isConnected() Whether the socket is currently connected :returns: boolean .. php:method:: disconnect() Disconnect the socket :returns: void .. php:method:: getResource() .. php:method:: getResourceId() .. php:method:: send($data) :type $data: unknown_type :param $data: :returns: boolean|int The number of bytes sent or false on error .. php:method:: receive($length = self::DEFAULT_RECEIVE_LENGTH) Recieve data from the socket :type $length: int :param $length: :returns: string .. php:method:: __construct($options = array()) Configurable constructor :param $options: .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Socket/UriSocket.rst ================================================ ------------------------- Wrench\\Socket\\UriSocket ------------------------- .. php:namespace: Wrench\\Socket .. php:class:: UriSocket .. php:const:: TIMEOUT_SOCKET Default timeout for socket operations (reads, writes) .. php:const:: DEFAULT_RECEIVE_LENGTH .. php:const:: NAME_PART_IP Socket name parts .. php:attr:: scheme protected .. php:attr:: host protected .. php:attr:: port protected .. php:attr:: socket protected resource .. php:attr:: context protected Stream context .. php:attr:: connected protected boolean Whether the socket is connected to a server Note, the connection may not be ready to use, but the socket is connected at least. See $handshaked, and other properties in subclasses. .. php:attr:: firstRead protected boolean Whether the current read is the first one to the socket .. php:attr:: name protected string The socket name according to stream_socket_get_name .. php:attr:: options protected array .. php:attr:: protocol protected Protocol .. php:method:: __construct($uri, $options = array()) URI Socket constructor :type $uri: string :param $uri: WebSocket URI, e.g. ws://example.org:8000/chat :param $options: .. php:method:: getUri() Gets the canonical/normalized URI for this socket :returns: string .. php:method:: getName() .. php:method:: getHost() Gets the host name .. php:method:: getPort() .. php:method:: getStreamContext($listen = false) Gets a stream context :param $listen: .. php:method:: getSocketStreamContextOptions() Returns an array of socket stream context options See http://php.net/manual/en/context.socket.php :returns: array .. php:method:: getSslStreamContextOptions() Returns an array of ssl stream context options See http://php.net/manual/en/context.ssl.php :returns: array .. php:method:: configure($options) Configure options Options include - timeout_connect => int, seconds, default 2 - timeout_socket => int, seconds, default 5 :type $options: array :param $options: :returns: void .. php:method:: getNamePart($name, $part) Gets part of the name of the socket PHP seems to return IPV6 address/port combos like this: ::1:1234, where ::1 is the address and 1234 the port So, the part number here is either the last : delimited section (the port) or all the other sections (the whole initial part, the address). :type $name: string :param $name: (from $this->getName() usually) :param $part: :returns: string .. php:method:: getIp() Gets the IP address of the socket :returns: string .. php:method:: getLastError() Get the last error that occurred on the socket :returns: int|string .. php:method:: isConnected() Whether the socket is currently connected :returns: boolean .. php:method:: disconnect() Disconnect the socket :returns: void .. php:method:: getResource() .. php:method:: getResourceId() .. php:method:: send($data) :type $data: unknown_type :param $data: :returns: boolean|int The number of bytes sent or false on error .. php:method:: receive($length = self::DEFAULT_RECEIVE_LENGTH) Recieve data from the socket :type $length: int :param $length: :returns: string .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Socket/index.rst ================================================ :::::::::::::: Wrench\\Socket :::::::::::::: .. php:namespace: Wrench\\Socket .. toctree:: ClientSocket ServerClientSocket ServerSocket Socket UriSocket ================================================ FILE: doc/source/api/Util/Configurable.rst ================================================ -------------------------- Wrench\\Util\\Configurable -------------------------- .. php:namespace: Wrench\\Util .. php:class:: Configurable Configurable base class .. php:attr:: options protected array .. php:attr:: protocol protected Protocol .. php:method:: __construct($options = array()) Configurable constructor :param $options: .. php:method:: configure($options) Configures the options :type $options: array :param $options: .. php:method:: configureProtocol() Configures the protocol option ================================================ FILE: doc/source/api/Util/Ssl.rst ================================================ ----------------- Wrench\\Util\\Ssl ----------------- .. php:namespace: Wrench\\Util .. php:class:: Ssl .. php:method:: generatePemFile($pem_file, $pem_passphrase, $country_name, $state_or_province_name, $locality_name, $organization_name, $organizational_unit_name, $common_name, $email_address) Generates a new PEM File given the informations :type $pem_file: string :param $pem_file: the path of the PEM file to create :type $pem_passphrase: string :param $pem_passphrase: the passphrase to protect the PEM file or if you don't want to use a passphrase :type $country_name: string :param $country_name: the country code of the new PEM file. e.g.: EN :type $state_or_province_name: string :param $state_or_province_name: the state or province name of the new PEM file :type $locality_name: string :param $locality_name: the name of the locality :param $organization_name: :param $organizational_unit_name: :param $common_name: :type $email_address: string :param $email_address: the email address ================================================ FILE: doc/source/api/Util/index.rst ================================================ :::::::::::: Wrench\\Util :::::::::::: .. php:namespace: Wrench\\Util .. toctree:: Configurable Ssl ================================================ FILE: doc/source/api/index.rst ================================================ ````````````````` API Documentation ````````````````` .. php:namespace: Wrench .. toctree:: Application/index BasicServer Client Connection ConnectionManager Exception/index Frame/index Listener/index Payload/index Protocol/index Resource Server Socket/index Util/index ================================================ FILE: doc/source/authors.rst ================================================ ------- Authors ------- The original maintainer and author was `@nicokaiser `_. Plentiful improvements were contributed by `@lemmingzshadow `_ and `@mazhack `_. Parts of the Socket class were written by Moritz Wutz. The server is licensed under the WTFPL, a free software compatible license. ================================================ FILE: doc/source/conf.py ================================================ # -*- coding: utf-8 -*- # # Wrench documentation build configuration file, created by # sphinx-quickstart on Thu Jul 26 13:09:51 2012. # # This file is execfile()d with the current directory set to its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys, os import sphinx.highlighting, pygments.lexers # Highlight PHP code without v documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'Wrench' # -- Options for LaTeX output -------------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ ('index', 'Wrench.tex', u'Wrench Documentation', u'Dominic Scheirlinck and Contributors', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'wrench', u'Wrench Documentation', [u'Dominic Scheirlinck and Contributors'], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------------ # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'Wrench', u'Wrench Documentation', u'Dominic Scheirlinck and Contributors', 'Wrench', 'PHP WebSockets library.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' ================================================ FILE: doc/source/getting-started.rst ================================================ .. vim: set tw=78 sw=4 ts=4 : *************** Getting Started *************** ----------------- Starting a Server ----------------- The first thing you'll want to do to serve WebSockets from PHP is start a WebSockets server. Wrench provides a simple Server class that implements the most recent version of the WebSockts protocol. Subclassing the Server class is encouraged: see WebSocket\BasicServer for an example. When you're ready for your server to start responding to requests, call $server->run():: use Wrench\BasicServer; $server = new BasicServer('ws://localhost:8000', array( 'allowed_origins' => array( 'mysite.com', 'mysite.dev.localdomain' ) )); // Register your applications $server->run(); -------------------------- Registering an Application -------------------------- The server on its own doesn't do anything until you write an Application for it. The server calls methods on your applications once they are registered:: $server->registerApplication('echo', new \Wrench\Examples\EchoApplication()); $server->registerApplication('chat', new \My\ChatApplication()); ================================================ FILE: doc/source/index.rst ================================================ .. vim: set tw=78 sw=2 ts=2 : ====== Wrench ====== Wrench is a WebSockets library for PHP 5.3+ .. toctree:: :maxdepth: 3 introduction installing getting-started performance api/index authors ================================================ FILE: doc/source/installing.rst ================================================ .. vim: set tw=78 sw=4 ts=4 : ***************** Installing Wrench ***************** The library is PSR-0 compatible, with a vendor name of **Wrench**. An SplClassLoader is bundled for convenience. The simplest possible bootstrap looks like this:: require_once 'SplClassLoader.php'; $classLoader = new \SplClassLoader('Wrench', __DIR__ . '/../path/to/wrench/lib'); $classLoader->register(); -------- composer -------- Wrench is available on Packagist as `wrench/wrench `_. Here's what it looks like in your :file:`composer.json` .. code-block:: json { ... "require": { "wrench/wrench": "dev-master" } } --------- deps file --------- Using Symfony2 with a traditional style deps file? You can configure Wrench like this: .. code-block:: ini [wrench] git=git://github.com/varspool/Wrench.git version=origin/master ================================================ FILE: doc/source/introduction.rst ================================================ .. vim: set tw=78 sw=4 ts=4 : ************ Introduction ************ Wrench is a simple websocket server and client package for PHP 5.3/5.4, using streams. ------------- php-websocket ------------- Wrench was previously known as php-websocket. Why the name change? See `Frequently Asked Questions about the PHP License `_. Also, the namespace WebSocket is too generic; it denotes a common functionality, and may already be in use by application code. The BC break of a new `major version `_ was a good time to introduce this move to best practices. ================================================ FILE: doc/source/performance.rst ================================================ .. vim: set tw=78 sw=4 ts=4 : *********** Performance *********** Wrench uses a single-process server, without threads, and blocks while processing data from any client. This means it has little hope of scaling in production. You might like to use some middleware between your PHP application code and WebSocket clients in production. For example, you might use something like `RabbitMQ's STOMP + WebSockets Plugin `_. In any case, if you're hoping to serve large numbers of clients, you should probably look into one of the evented IO based servers. ================================================ FILE: doc/source/setup.py ================================================ #!/usr/bin/env python from distutils.core import setup setup( name='wrench-documentation', version='2.0.0', requires=[ "sphinxcontrib.phpdomain" ] ) ================================================ FILE: examples/StatusApplication.php ================================================ */ class StatusApplication extends Application { private $_clients = array(); private $_serverClients = array(); private $_serverInfo = array(); private $_serverClientCount = 0; public function onConnect($client) { $id = $client->getClientId(); $this->_clients[$id] = $client; $this->_sendServerinfo($client); } public function onDisconnect($client) { $id = $client->getClientId(); unset($this->_clients[$id]); } public function onData($data, $client) { // currently not in use... } public function setServerInfo($serverInfo) { if (is_array($serverInfo)) { $this->_serverInfo = $serverInfo; return true; } return false; } public function clientConnected($ip, $port) { $this->_serverClients[$port] = $ip; $this->_serverClientCount++; $this->statusMsg('Client connected: ' . $ip . ':' . $port); $data = array( 'ip' => $ip, 'port' => $port, 'clientCount' => $this->_serverClientCount, ); $encodedData = $this->_encodeData('clientConnected', $data); $this->_sendAll($encodedData); } public function clientDisconnected($ip, $port) { if (!isset($this->_serverClients[$port])) { return false; } unset($this->_serverClients[$port]); $this->_serverClientCount--; $this->statusMsg('Client disconnected: ' . $ip . ':' . $port); $data = array( 'port' => $port, 'clientCount' => $this->_serverClientCount, ); $encodedData = $this->_encodeData('clientDisconnected', $data); $this->_sendAll($encodedData); } public function clientActivity($port) { $encodedData = $this->_encodeData('clientActivity', $port); $this->_sendAll($encodedData); } public function statusMsg($text, $type = 'info') { $data = array( 'type' => $type, 'text' => '[' . strftime('%m-%d %H:%M', time()) . '] ' . $text, ); $encodedData = $this->_encodeData('statusMsg', $data); $this->_sendAll($encodedData); } private function _sendServerinfo($client) { if (count($this->_clients) < 1) { return false; } $currentServerInfo = $this->_serverInfo; $currentServerInfo['clientCount'] = count($this->_serverClients); $currentServerInfo['clients'] = $this->_serverClients; $encodedData = $this->_encodeData('serverInfo', $currentServerInfo); $client->send($encodedData); } private function _sendAll($encodedData) { if (count($this->_clients) < 1) { return false; } foreach ($this->_clients as $sendto) { $sendto->send($encodedData); } } } ================================================ FILE: examples/coffeescript/coffee/client.coffee ================================================ $(document).ready -> log = (msg) -> $('#log').append("#{msg}
") serverUrl = 'ws://127.0.0.1:8000/demo' if window.MozWebSocket socket = new MozWebSocket serverUrl else if window.WebSocket socket = new WebSocket serverUrl socket.binaryType = 'blob' socket.onopen = (msg) -> $('#status').removeClass().addClass('online').html('connected') socket.onmessage = (msg) -> response = JSON.parse(msg.data) log("Action: #{response.action}") log("Data: #{response.data}") socket.onclose = (msg) -> $('#status').removeClass().addClass('offline').html('disconnected') $('#status').click -> socket.close() $('#send').click -> payload = new Object() payload.action = $('#action').val() payload.data = $('#data').val() socket.send(JSON.stringify(payload)) $('#sendfile').click -> data = document.binaryFrame.file.files[0] if data payload = new Object() payload.action = 'setFilename' payload.data = $('#file').val() socket.send JSON.stringify payload socket.send(data) return false ================================================ FILE: examples/coffeescript/coffee/status.coffee ================================================ $(document).ready -> log = (msg) -> $('#log').prepend("#{msg}
") serverUrl = 'ws://localhost:8000/status' if window.MozWebSocket socket = new MozWebSocket serverUrl else if window.WebSocket socket = new WebSocket serverUrl socket.onopen = (msg) -> $('#status').removeClass().addClass('online').html('connected') socket.onmessage = (msg) -> response = JSON.parse(msg.data) switch response.action when "statusMsg" then statusMsg response.data when "clientConnected" then clientConnected response.data when "clientDisconnected" then clientDisconnected response.data when "clientActivity" then clientActivity response.data when "serverInfo" then refreshServerinfo response.data socket.onclose = (msg) -> $('#status').removeClass().addClass('offline').html('disconnected') $('#status').click -> socket.close() statusMsg = (msgData) -> switch msgData.type when "info" then log msgData.text when "warning" then log "#{msgData.text}" clientConnected = (data) -> $('#clientListSelect').append(new Option("#{data.ip}:#{data.port}", data.port)) $('#clientCount').text(data.clientCount) clientDisconnected = (data) -> $("#clientListSelect option[value='#{data.port}']").remove() $('#clientCount').text(data.clientCount) refreshServerinfo = (serverinfo) -> $('#clientCount').text(serverinfo.clientCount) $('#maxClients').text(serverinfo.maxClients) $('#maxConnections').text(serverinfo.maxConnectionsPerIp) $('#maxRequetsPerMinute').text(serverinfo.maxRequetsPerMinute) for port, ip of serverinfo.clients $('#clientListSelect').append(new Option(ip + ':' + port, port)); clientActivity = (port) -> $("#clientListSelect option[value='#{port}']").css("color", "red").animate({opacity: 100}, 600, -> $(this).css("color", "black") ) ================================================ FILE: examples/coffeescript/css/client.css ================================================ body { background: #f1f1f1; padding-top: 65px; text-align: center; font-family: Arial, Helvetica, sans-serif; text-align: center; font-size: 16px; line-height: 25px; color: #444; } a, a:hover { color: #169; } p { margin: 0; padding: 0 0 21px 0; } #container { text-align: left; width: 580px; background: #fff; position: relative; margin: 0 auto; padding: 40px; border: 1px solid #ddd; border-radius: 10px; -moz-border-radius: 10px; } h1 { font-size: 30px; color: #333; font-weight: normal; margin: 0 0 20px 0; padding: 0; display: inline-block; } h2 { font-size: 16px; font-weight: bold; margin: 8px 0 0 0; } #log { margin: 6px 0 0 0; padding: 5px; border: 1px solid #ccc; height: 200px; overflow: auto; } #send { margin: 0 0 10px 0; } #status { float: right; padding: 0 10px; cursor: pointer; border-radius: 5px; -moz-border-radius: 5px; } .offline { background: #ddd; color: #000; } .online { background: #093; color: #fff; } .error { background: #930; color: #fff; } .connecting { background: #fc0; color: #000; } #action, #data { display: inline-block; width: 200px; margin: 0 0 5px 0; } #file { display: inline-block; width: 410px; margin: 0 0 5px 0; } .bold { font-weight: bold; } ================================================ FILE: examples/coffeescript/css/status.css ================================================ body { background: #f1f1f1; padding-top: 65px; text-align: center; font-family: Arial, Helvetica, sans-serif; text-align: center; font-size: 16px; line-height: 25px; color: #444; } a, a:hover { color: #169; } p { margin: 0; padding: 0; } #container { text-align: left; width: 900px; background: #fff; position: relative; margin: 0 auto; padding: 40px; border: 1px solid #ddd; border-radius: 10px; -moz-border-radius: 10px; } h1 { font-size: 30px; color: #333; font-weight: normal; margin: 0 0 20px 0; padding: 0; display: inline-block; } h2 { font-size: 16px; font-weight: bold; margin: 0; } #log { margin: 6px 0 0 0; padding: 5px; border: 1px solid #ccc; height: 200px; overflow: auto; } #send { margin: 0 0 10px 0; } #status { float: right; padding: 0 10px; cursor: pointer; border-radius: 5px; -moz-border-radius: 5px; } .offline { background: #ddd; color: #000; } .online { background: #093; color: #fff; } .error { background: #930; color: #fff; } .connecting { background: #fc0; color: #000; } #action, #data { display: block; width: 400px; margin: 0 0 5px 0; } .bold { font-weight: bold; } #clientList { float: left; width: 300px; } #clientListSelect { width: 200px; height: 120px; } #serverInfo { float: left; } #console { margin-top: 20px; } #console .warning { color: #f00; } .clearer { clear: both; } ================================================ FILE: examples/coffeescript/index.html ================================================ Shiny WSS Demo Application

Shiny WSS Demo Application

offline

Send Text Frame

Send Binary Frame

Server-Response

​ ================================================ FILE: examples/coffeescript/status.html ================================================ Shiny WSS Status

Shiny WSS Status

disconnected

Clients:

Server Info:

Connected Clients:

Limit Clients:

Limit Connections/IP:

Limit Requetes/Min:

Server Messages:

​ ================================================ FILE: examples/server.pem ================================================ -----BEGIN CERTIFICATE----- MIICsDCCAhmgAwIBAgIBADANBgkqhkiG9w0BAQQFADB1MQswCQYDVQQGEwJERTEN MAsGA1UECAwEbm9uZTENMAsGA1UEBwwEbm9uZTENMAsGA1UECgwEbm9uZTENMAsG A1UECwwEbm9uZTEPMA0GA1UEAwwGZm9vLmxoMRkwFwYJKoZIhvcNAQkBFgpiYXpA Zm9vLmxoMB4XDTEyMDQwNzA5MzEwNVoXDTEzMDQwNzA5MzEwNVowdTELMAkGA1UE BhMCREUxDTALBgNVBAgMBG5vbmUxDTALBgNVBAcMBG5vbmUxDTALBgNVBAoMBG5v bmUxDTALBgNVBAsMBG5vbmUxDzANBgNVBAMMBmZvby5saDEZMBcGCSqGSIb3DQEJ ARYKYmF6QGZvby5saDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA7CDQtHvJ RE+sy2f3zKyGrMpffcf7fvbaEwT3tEpFpOJOp5qaq6JRc961O6v72++tr8iRVlfO MGYV5JavKBe0PLzR+tHa/+eigcvjujFsPZqTP+8zkmkOKQIsKjmtpQBKYGgqcDDR Jv7xYASVBl3/6LuIaD1hjk+r6DH7uqmcA2cCAwEAAaNQME4wHQYDVR0OBBYEFNH0 dPlR4RSosYd26yWaIvCeGN4kMB8GA1UdIwQYMBaAFNH0dPlR4RSosYd26yWaIvCe GN4kMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAGeqrcS9tZ7kTYrEe sT+iPtnqky8Sxu3znxkyjWKymDvELtc4gO17JaoFsQ+7PQBf22EzSgzuqlpk2gGa pNaMwoS2o/q/Htm7KuGVP0yNEVCYAKNwutFynrn+pd87Pr/+Oq4ighvM8wEzLPl1 OKSgTOSRdr80EdoSzgBKDD8gyuw= -----END CERTIFICATE----- -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,DE4E623117398BE5 2FRyRzTeOSijgSCcvnBChbJJHMHkhAlB8Sxbo4fxeAIKyTGx9lyVZTtGN/XVWviK XDh/7rL5qZz0zJcHshMg1pypNsJ+dSfF3KigpXxHm3Fpb5GnCDj3UKVFxOWPwu2K ro9RyUdEdOwou2o1TnYnimgEmF7pb1CJFtR/sN1lZ+J/XUh8wfbn2Obce9GCtXGB P+nVJECELVqX9KgquT49PKpIvf9gLq0Npns5P1lMenR4KGUlCGhFqgnuevPBaHaQ yRDQfl/qnCehiA59KpCBPyNBPn2hsfpsN+AcnTkt6zd8wWjQA4SlyI1koSxG6FE7 OjPYsAhpQRi5qVWDyp0jkZqnU42aCGe7CqXXsdOfZv1m0wWACAvxJzV5uWljk/aD 7+k/WcTHHsNF05FYD2eQOReQ75UnOvKF7EmDO0iSJ6NaH9kHg7bvCxPD2oQGMqU8 ISeHhnI7URcyUBJZnEBCC2eJW6R2m/mI78P1KpOoCg4YbdB/ByGCyqDl134cv243 Knja0TV5mmTkdXWnUdSAQX0RJpohfOGomYFeeN7TqoqZxQUfFGHNgQP3oNoEzYay mMfvBk80TRuRD1UpEsg8f+4xw+F6L82rRnfLgNr7FKJYDSTpVsJ3L+xsmyP0hjNw b10iH/iOd2RprzOZ4YS+LdLLhOBJ97BciBTm71j1h0yLQuGWVLdsrisiyb22luZp jU5mF0+wLIxCVl+k/Ibk3UYbBlvGblJzVEOnR2BOf3P3C6Skvr2JvDP/74Si/ybI idm+Igm+TnUQ2PIntjV0wkiJIEQY5XyDwt58TR99w2Y= -----END RSA PRIVATE KEY----- ================================================ FILE: examples/server.php ================================================ #!/usr/bin/env php register(); $server = new \Wrench\Server('ws://localhost:8000/', array( 'allowed_origins' => array( 'mysite.localhost' ), // Optional defaults: // 'check_origin' => true, // 'connection_manager_class' => 'Wrench\ConnectionManager', // 'connection_manager_options' => array( // 'timeout_select' => 0, // 'timeout_select_microsec' => 200000, // 'socket_master_class' => 'Wrench\Socket\ServerSocket', // 'socket_master_options' => array( // 'backlog' => 50, // 'ssl_cert_file' => null, // 'ssl_passphrase' => null, // 'ssl_allow_self_signed' => false, // 'timeout_accept' => 5, // 'timeout_socket' => 5, // ), // 'connection_class' => 'Wrench\Connection', // 'connection_options' => array( // 'socket_class' => 'Wrench\Socket\ServerClientSocket', // 'socket_options' => array(), // 'connection_id_secret' => 'asu5gj656h64Da(0crt8pud%^WAYWW$u76dwb', // 'connection_id_algo' => 'sha512' // ) // ) )); $server->registerApplication('echo', new \Wrench\Application\EchoApplication()); $server->run(); ================================================ FILE: examples/server_ssl.php ================================================ register(); // Generate PEM file $pemFile = dirname(__FILE__) . '/generated.pem'; $pemPassphrase = null; $countryName = "DE"; $stateOrProvinceName = "none"; $localityName = "none"; $organizationName = "none"; $organizationalUnitName = "none"; $commonName = "foo.lh"; $emailAddress = "baz@foo.lh"; \Wrench\Socket::generatePEMFile( $pemFile, $pemPassphrase, $countryName, $stateOrProvinceName, $localityName, $organizationName, $organizationalUnitName, $commonName, $emailAddress ); // User can use tls in place of ssl $server = new \Wrench\Server('127.0.0.1', 8000, 'ssl', $pemFile, $pemPassphrase); // server settings: $server->setMaxClients(100); $server->setCheckOrigin(true); $server->setAllowedOrigin('foo.lh'); $server->setMaxConnectionsPerIp(100); $server->setMaxRequestsPerMinute(2000); // Hint: Status application should not be removed as it displays usefull server informations: $server->registerApplication('status', \Wrench\Application\StatusApplication::getInstance()); $server->registerApplication('demo', \Wrench\Application\DemoApplication::getInstance()); $server->run(); ================================================ FILE: lib/SplClassLoader.php ================================================ register(); * * @author Jonathan H. Wage * @author Roman S. Borschel * @author Matthew Weier O'Phinney * @author Kris Wallsmith * @author Fabien Potencier */ class SplClassLoader { private $_fileExtension = '.php'; private $_namespace; private $_includePath; private $_namespaceSeparator = '\\'; /** * Creates a new SplClassLoader that loads classes of the * specified namespace. * * @param string $ns The namespace to use. */ public function __construct($ns = null, $includePath = null) { $this->_namespace = $ns; $this->_includePath = $includePath; } /** * Sets the namespace separator used by classes in the namespace of this class loader. * * @param string $sep The separator to use. */ public function setNamespaceSeparator($sep) { $this->_namespaceSeparator = $sep; } /** * Gets the namespace seperator used by classes in the namespace of this class loader. * * @return void */ public function getNamespaceSeparator() { return $this->_namespaceSeparator; } /** * Sets the base include path for all class files in the namespace of this class loader. * * @param string $includePath */ public function setIncludePath($includePath) { $this->_includePath = $includePath; } /** * Gets the base include path for all class files in the namespace of this class loader. * * @return string $includePath */ public function getIncludePath() { return $this->_includePath; } /** * Sets the file extension of class files in the namespace of this class loader. * * @param string $fileExtension */ public function setFileExtension($fileExtension) { $this->_fileExtension = $fileExtension; } /** * Gets the file extension of class files in the namespace of this class loader. * * @return string $fileExtension */ public function getFileExtension() { return $this->_fileExtension; } /** * Installs this class loader on the SPL autoload stack. */ public function register() { spl_autoload_register(array($this, 'loadClass')); } /** * Uninstalls this class loader from the SPL autoloader stack. */ public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } /** * Loads the given class or interface. * * @param string $className The name of the class to load. * @return void */ public function loadClass($className) { if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { $fileName = ''; $namespace = ''; if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; } } } ================================================ FILE: lib/Wrench/Application/Application.php ================================================ send($data); } } ================================================ FILE: lib/Wrench/Application/ServerTimeApplication.php ================================================ clients[] = $client; } /** * @see Wrench\Application.Application::onUpdate() */ public function onUpdate() { // limit updates to once per second if(time() > $this->lastTimestamp) { $this->lastTimestamp = time(); foreach ($this->clients as $sendto) { $sendto->send(date('d-m-Y H:i:s')); } } } } ================================================ FILE: lib/Wrench/BasicServer.php ================================================ configureRateLimiter(); $this->configureOriginPolicy(); } /** * @see Wrench.Server::configure() */ protected function configure(array $options) { $options = array_merge(array( 'check_origin' => true, 'allowed_origins' => array(), 'origin_policy_class' => 'Wrench\Listener\OriginPolicy', 'rate_limiter_class' => 'Wrench\Listener\RateLimiter' ), $options); parent::configure($options); } protected function configureRateLimiter() { $class = $this->options['rate_limiter_class']; $this->rateLimiter = new $class(); $this->rateLimiter->listen($this); } /** * Configures the origin policy */ protected function configureOriginPolicy() { $class = $this->options['origin_policy_class']; $this->originPolicy = new $class($this->options['allowed_origins']); if ($this->options['check_origin']) { $this->originPolicy->listen($this); } } /** * Adds an allowed origin * * @param array $origin */ public function addAllowedOrigin($origin) { $this->originPolicy->addAllowedOrigin($origin); } } ================================================ FILE: lib/Wrench/Client.php ================================================ */ protected $received = array(); /** * Constructor * * @param string $uri * @param string $origin The origin to include in the handshake (required * in later versions of the protocol) * @param array $options (optional) Array of options * - socket => Socket instance (otherwise created) * - protocol => Protocol */ public function __construct($uri, $origin, array $options = array()) { parent::__construct($options); $uri = (string)$uri; if (!$uri) { throw new InvalidArgumentException('No URI specified'); } $this->uri = $uri; $origin = (string)$origin; if (!$origin) { throw new InvalidArgumentException('No origin specified'); } $this->origin = $origin; $this->protocol->validateUri($this->uri); $this->protocol->validateOriginUri($this->origin); $this->configureSocket(); $this->configurePayloadHandler(); } /** * Configure options * * @param array $options * @return void */ protected function configure(array $options) { $options = array_merge(array( 'socket_class' => 'Wrench\\Socket\\ClientSocket', 'on_data_callback' => null ), $options); parent::configure($options); } /** * Configures the client socket */ protected function configureSocket() { $class = $this->options['socket_class']; $this->socket = new $class($this->uri); } /** * Configures the payload handler */ protected function configurePayloadHandler() { $this->payloadHandler = new PayloadHandler(array($this, 'onData'), $this->options); } /** * Payload receiver * * Public because called from our PayloadHandler. Don't call us, we'll call * you (via the on_data_callback option). * * @param Payload $payload */ public function onData(Payload $payload) { $this->received[] = $payload; if (($callback = $this->options['on_data_callback'])) { call_user_func($callback, $payload); } } /** * Adds a request header to be included in the initial handshake * * For example, to include a Cookie header * * @param string $name * @param string $value * @return void */ public function addRequestHeader($name, $value) { $this->headers[$name] = $value; } /** * Sends data to the socket * * @param string $data * @param string $type Payload type * @param boolean $masked * @return boolean Success */ public function sendData($data, $type = Protocol::TYPE_TEXT, $masked = true) { if (is_string($type) && isset(Protocol::$frameTypes[$type])) { $type = Protocol::$frameTypes[$type]; } $payload = $this->protocol->getPayload(); $payload->encode( $data, $type, $masked ); return $payload->sendToSocket($this->socket); } /** * Receives data sent by the server * * @param callable $callback * @return array Payload received since the last call to receive() */ public function receive() { if (!$this->isConnected()) { return false; } $data = $this->socket->receive(); if (!$data) { return $data; } $old = $this->received; $this->payloadHandler->handle($data); return array_diff($this->received, $old); } /** * Connect to the Wrench server * * @return boolean Whether a new connection was made */ public function connect() { if ($this->isConnected()) { return false; } $this->socket->connect(); $key = $this->protocol->generateKey(); $handshake = $this->protocol->getRequestHandshake( $this->uri, $key, $this->origin, $this->headers ); $this->socket->send($handshake); $response = $this->socket->receive(self::MAX_HANDSHAKE_RESPONSE); return ($this->connected = $this->protocol->validateResponseHandshake($response, $key)); } /** * Whether the client is currently connected * * @return boolean */ public function isConnected() { return $this->connected; } /** * @todo Bug: what if connect has been called twice. The first socket never * gets closed. */ public function disconnect() { if ($this->socket) { $this->socket->disconnect(); } $this->connected = false; } } ================================================ FILE: lib/Wrench/Connection.php ================================================ manager = $manager; $this->socket = $socket; parent::__construct($options); $this->configureClientInformation(); $this->configurePayloadHandler(); $this->log('Connected'); } /** * Gets the connection manager of this connection * * @return \Wrench\ConnectionManager */ public function getConnectionManager() { return $this->manager; } /** * @see Wrench\Util.Configurable::configure() */ protected function configure(array $options) { $options = array_merge(array( 'connection_id_secret' => 'asu5gj656h64Da(0crt8pud%^WAYWW$u76dwb', 'connection_id_algo' => 'sha512', ), $options); parent::configure($options); } protected function configurePayloadHandler() { $this->payloadHandler = new PayloadHandler( array($this, 'handlePayload'), $this->options ); } /** * @throws RuntimeException */ protected function configureClientInformation() { $this->ip = $this->socket->getIp(); $this->port = $this->socket->getPort(); $this->configureClientId(); } /** * Configures the client ID * * We hash the client ID to prevent leakage of information if another client * happens to get a hold of an ID. The secret *must* be lengthy, and must * be kept secret for this to work: otherwise it's trivial to search the space * of possible IP addresses/ports (well, if not trivial, at least very fast). */ protected function configureClientId() { $message = sprintf( '%s:uri=%s&ip=%s&port=%s', $this->options['connection_id_secret'], rawurlencode($this->manager->getUri()), rawurlencode($this->ip), rawurlencode($this->port) ); $algo = $this->options['connection_id_algo']; if (extension_loaded('gmp')) { $hash = hash($algo, $message, true); $hash = gmp_strval(gmp_init($hash, 16), 62); } else { // @codeCoverageIgnoreStart $hash = hash($algo, $message); // @codeCoverageIgnoreEnd } $this->id = $hash; } /** * Data receiver * * Called by the connection manager when the connection has received data * * @param string $data */ public function onData($data) { if (!$this->handshaked) { return $this->handshake($data); } return $this->handle($data); } /** * Performs a websocket handshake * * @param string $data * @throws BadRequestException * @throws HandshakeException * @throws WrenchException */ public function handshake($data) { try { list($path, $origin, $key, $extensions) = $this->protocol->validateRequestHandshake($data); $this->application = $this->manager->getApplicationForPath($path); if (!$this->application) { throw new BadRequestException('Invalid application'); } $this->manager->getServer()->notify( Server::EVENT_HANDSHAKE_REQUEST, array($this, $path, $origin, $key, $extensions) ); $response = $this->protocol->getResponseHandshake($key); if (!$this->socket->isConnected()) { throw new HandshakeException('Socket is not connected'); } if ($this->socket->send($response) === false) { throw new HandshakeException('Could not send handshake response'); } $this->handshaked = true; $this->log(sprintf( 'Handshake successful: %s:%d (%s) connected to %s', $this->getIp(), $this->getPort(), $this->getId(), $path ), 'info'); $this->manager->getServer()->notify( Server::EVENT_HANDSHAKE_SUCCESSFUL, array($this) ); if (method_exists($this->application, 'onConnect')) { $this->application->onConnect($this); } } catch (WrenchException $e) { $this->log('Handshake failed: ' . $e, 'err'); $this->close($e); } } /** * Returns a string export of the given binary data * * @param string $data * @return string */ protected function export($data) { $export = ''; foreach (str_split($data) as $chr) { $export .= '\\x' . ord($chr); } } /** * Handle data received from the client * * The data passed in may belong to several different frames across one or * more protocols. It may not even contain a single complete frame. This method * manages slotting the data into separate payload objects. * * @todo An endpoint MUST be capable of handling control frames in the * middle of a fragmented message. * @param string $data * @return void */ public function handle($data) { $this->payloadHandler->handle($data); } /** * Handle a complete payload received from the client * * Public because called from our PayloadHandler * * @param string $payload */ public function handlePayload(Payload $payload) { $app = $this->getClientApplication(); $this->log('Handling payload: ' . $payload->getPayload(), 'debug'); switch ($type = $payload->getType()) { case Protocol::TYPE_TEXT: if (method_exists($app, 'onData')) { $app->onData($payload, $this); } return; case Protocol::TYPE_BINARY: if(method_exists($app, 'onBinaryData')) { $app->onBinaryData($payload, $this); } else { $this->close(1003); } break; case Protocol::TYPE_PING: $this->log('Ping received', 'notice'); $this->send($payload->getPayload(), Protocol::TYPE_PONG); $this->log('Pong!', 'debug'); break; /** * A Pong frame MAY be sent unsolicited. This serves as a * unidirectional heartbeat. A response to an unsolicited Pong * frame is not expected. */ case Protocol::TYPE_PONG: $this->log('Received unsolicited pong', 'info'); break; case Protocol::TYPE_CLOSE: $this->log('Close frame received', 'notice'); $this->close(); $this->log('Disconnected', 'info'); break; default: throw new ConnectionException('Unhandled payload type'); } } /** * Sends the payload to the connection * * @param string $payload * @param string $type * @throws HandshakeException * @throws ConnectionException * @return boolean */ public function send($data, $type = Protocol::TYPE_TEXT) { if (!$this->handshaked) { throw new HandshakeException('Connection is not handshaked'); } $payload = $this->protocol->getPayload(); // Servers don't send masked payloads $payload->encode($data, $type, false); if (!$payload->sendToSocket($this->socket)) { $this->log('Could not send payload to client', 'warn'); throw new ConnectionException('Could not send data to connection: ' . $this->socket->getLastError()); } return true; } /** * Processes data on the socket * * @throws CloseException */ public function process() { $data = $this->socket->receive(); $bytes = strlen($data); if ($bytes === 0 || $data === false) { throw new CloseException('Error reading data from socket: ' . $this->socket->getLastError()); } $this->onData($data); } /** * Closes the connection according to the WebSocket protocol * * If an endpoint receives a Close frame and that endpoint did not * previously send a Close frame, the endpoint MUST send a Close frame * in response. It SHOULD do so as soon as is practical. An endpoint * MAY delay sending a close frame until its current message is sent * (for instance, if the majority of a fragmented message is already * sent, an endpoint MAY send the remaining fragments before sending a * Close frame). However, there is no guarantee that the endpoint which * has already sent a Close frame will continue to process data. * After both sending and receiving a close message, an endpoint * considers the WebSocket connection closed, and MUST close the * underlying TCP connection. The server MUST close the underlying TCP * connection immediately; the client SHOULD wait for the server to * close the connection but MAY close the connection at any time after * sending and receiving a close message, e.g. if it has not received a * TCP close from the server in a reasonable time period. * * @param int|Exception $statusCode * @return boolean */ public function close($code = Protocol::CLOSE_NORMAL) { try { if (!$this->handshaked) { $response = $this->protocol->getResponseError($code); $this->socket->send($response); } else { $response = $this->protocol->getCloseFrame($code); $this->socket->send($response); } } catch (Exception $e) { $this->log('Unable to send close message', 'warning'); } if ($this->application && method_exists($this->application, 'onDisconnect')) { $this->application->onDisconnect($this); } $this->socket->disconnect(); $this->manager->removeConnection($this); } /** * Logs a message * * @param string $message * @param string $priority */ public function log($message, $priority = 'info') { $this->manager->log(sprintf( '%s: %s:%d (%s): %s', __CLASS__, $this->getIp(), $this->getPort(), $this->getId(), $message ), $priority); } /** * Gets the IP address of the connection * * @return string Usually dotted quad notation */ public function getIp() { return $this->ip; } /** * Gets the port of the connection * * @return int */ public function getPort() { return $this->port; } /** * Gets the connection ID * * @return string */ public function getId() { return $this->id; } /** * Gets the socket object * * @return Socket\ServerClientSocket */ public function getSocket() { return $this->socket; } /** * Gets the client application * * @return Application */ public function getClientApplication() { return (isset($this->application)) ? $this->application : false; } } ================================================ FILE: lib/Wrench/ConnectionManager.php ================================================ Connection> */ protected $connections = array(); /** * An array of raw socket resources, corresponding to connections, roughly * * @var array resource> */ protected $resources = array(); /** * Constructor * * @param Server $server * @param array $options */ public function __construct(Server $server, array $options = array()) { $this->server = $server; parent::__construct($options); } /** * @see Countable::count() */ public function count() { return count($this->connections); } /** * @see Wrench\Socket.Socket::configure() * Options include: * - timeout_select => int, seconds, default 0 * - timeout_select_microsec => int, microseconds (NB: not milli), default: 200000 */ protected function configure(array $options) { $options = array_merge(array( 'socket_master_class' => 'Wrench\Socket\ServerSocket', 'socket_master_options' => array(), 'socket_client_class' => 'Wrench\Socket\ServerClientSocket', 'socket_client_options' => array(), 'connection_class' => 'Wrench\Connection', 'connection_options' => array(), 'timeout_select' => self::TIMEOUT_SELECT, 'timeout_select_microsec' => self::TIMEOUT_SELECT_MICROSEC ), $options); parent::configure($options); $this->configureMasterSocket(); } /** * Gets the application associated with the given path * * @param string $path */ public function getApplicationForPath($path) { $path = ltrim($path, '/'); return $this->server->getApplication($path); } /** * Configures the main server socket * * @param string $uri */ protected function configureMasterSocket() { $class = $this->options['socket_master_class']; $options = $this->options['socket_master_options']; $this->socket = new $class($this->server->getUri(), $options); } /** * Listens on the main socket * * @return void */ public function listen() { $this->socket->listen(); $this->resources[$this->socket->getResourceId()] = $this->socket->getResource(); } /** * Gets all resources * * @return array resource) */ protected function getAllResources() { return array_merge($this->resources, array( $this->socket->getResourceId() => $this->socket->getResource() )); } /** * Returns the Connection associated with the specified socket resource * * @param resource $socket * @return Connection */ protected function getConnectionForClientSocket($socket) { if (!isset($this->connections[$this->resourceId($socket)])) { return false; } return $this->connections[$this->resourceId($socket)]; } /** * Select and process an array of resources * * @param array $resources */ public function selectAndProcess() { $read = $this->resources; $unused_write = null; $unsued_exception = null; stream_select( $read, $unused_write, $unused_exception, $this->options['timeout_select'], $this->options['timeout_select_microsec'] ); foreach ($read as $socket) { if ($socket == $this->socket->getResource()) { $this->processMasterSocket(); } else { $this->processClientSocket($socket); } } } /** * Process events on the master socket ($this->socket) * * @return void */ protected function processMasterSocket() { $new = null; try { $new = $this->socket->accept(); } catch (Exception $e) { $this->server->log('Socket error: ' . $e, 'err'); return; } $connection = $this->createConnection($new); $this->server->notify(Server::EVENT_SOCKET_CONNECT, array($new, $connection)); } /** * Creates a connection from a socket resource * * The create connection object is based on the options passed into the * constructor ('connection_class', 'connection_options'). This connection * instance and its associated socket resource are then stored in the * manager. * * @param resource $resource A socket resource * @return Connection */ protected function createConnection($resource) { if (!$resource || !is_resource($resource)) { throw new InvalidArgumentException('Invalid connection resource'); } $socket_class = $this->options['socket_client_class']; $socket_options = $this->options['socket_client_options']; $connection_class = $this->options['connection_class']; $connection_options = $this->options['connection_options']; $socket = new $socket_class($resource, $socket_options); $connection = new $connection_class($this, $socket, $connection_options); $id = $this->resourceId($resource); $this->resources[$id] = $resource; $this->connections[$id] = $connection; return $connection; } /** * Process events on a client socket * * @param resource $socket */ protected function processClientSocket($socket) { $connection = $this->getConnectionForClientSocket($socket); if (!$connection) { $this->log('No connection for client socket', 'warning'); return; } try { $connection->process(); } catch (CloseException $e) { $this->log('Client connection closed: ' . $e, 'notice'); $connection->close($e); } catch (WrenchException $e) { $this->log('Error on client socket: ' . $e, 'warning'); $connection->close($e); } } /** * This server makes an explicit assumption: PHP resource types may be cast * to a integer. Furthermore, we assume this is bijective. Both seem to be * true in most circumstances, but may not be guaranteed. * * This method (and $this->getResourceId()) exist to make this assumption * explicit. * * This is needed on the connection manager as well as on resources * * @param resource $resource */ protected function resourceId($resource) { return (int)$resource; } /** * Gets the connection manager's listening URI * * @return string */ public function getUri() { return $this->server->getUri(); } /** * Logs a message * * @param string $message * @param string $priority */ public function log($message, $priority = 'info') { $this->server->log(sprintf( '%s: %s', __CLASS__, $message ), $priority); } /** * @return \Wrench\Server */ public function getServer() { return $this->server; } /** * Removes a connection * * @param Connection $connection */ public function removeConnection(Connection $connection) { $socket = $connection->getSocket(); if ($socket->getResource()) { $index = $socket->getResourceId(); } else { $index = array_search($connection, $this->connections); } if (!$index) { $this->log('Could not remove connection: not found', 'warning'); } unset($this->connections[$index]); unset($this->resources[$index]); $this->server->notify( Server::EVENT_SOCKET_DISCONNECT, array($connection->getSocket(), $connection) ); } } ================================================ FILE: lib/Wrench/Exception/BadRequestException.php ================================================ buffer) { return false; } try { return $this->getBufferLength() >= $this->getExpectedBufferLength(); } catch (FrameException $e) { return false; } } /** * Receieves data into the frame * * @param string $buffer */ public function receiveData($data) { $this->buffer .= $data; } /** * Gets the remaining number of bytes before this frame will be complete * * @return number */ public function getRemainingData() { try { return $this->getExpectedBufferLength() - $this->getBufferLength(); } catch (FrameException $e) { return null; } } /** * Whether this frame is waiting for more data * * @return boolean */ public function isWaitingForData() { return $this->getRemainingData() > 0; } /** * Gets the contents of the frame payload * * The frame must be complete to call this method. * * @return string */ public function getFramePayload() { if (!$this->isComplete()) { throw new FrameException('Cannot get payload: frame is not complete'); } if (!$this->payload && $this->buffer) { $this->decodeFramePayloadFromBuffer(); } return $this->payload; } /** * Gets the contents of the frame buffer * * This is the encoded value, receieved into the frame with recieveData(). * * @throws FrameException * @return string binary */ public function getFrameBuffer() { if (!$this->buffer && $this->payload) { throw new FrameException('Cannot get frame buffer'); } return $this->buffer; } /** * Gets the expected length of the frame payload * * @return int */ protected function getBufferLength() { return strlen($this->buffer); } } ================================================ FILE: lib/Wrench/Frame/HybiFrame.php ================================================ = 0 */ public function encode($payload, $type = Protocol::TYPE_TEXT, $masked = false) { if (!is_int($type) || !in_array($type, Protocol::$frameTypes)) { throw new InvalidArgumentException('Invalid frame type'); } $this->type = $type; $this->masked = $masked; $this->payload = $payload; $this->length = strlen($this->payload); $this->offset_mask = null; $this->offset_payload = null; $this->buffer = "\x00\x00"; $this->buffer[self::BYTE_HEADER] = chr( (self::BITFIELD_TYPE & $this->type) | (self::BITFIELD_FINAL & PHP_INT_MAX) ); $masked_bit = (self::BITFIELD_MASKED & ($this->masked ? PHP_INT_MAX : 0)); if ($this->length <= 125) { $this->buffer[self::BYTE_INITIAL_LENGTH] = chr( (self::BITFIELD_INITIAL_LENGTH & $this->length) | $masked_bit ); } elseif ($this->length <= 65536) { $this->buffer[self::BYTE_INITIAL_LENGTH] = chr( (self::BITFIELD_INITIAL_LENGTH & 126) | $masked_bit ); $this->buffer .= pack('n', $this->length); } else { $this->buffer[self::BYTE_INITIAL_LENGTH] = chr( (self::BITFIELD_INITIAL_LENGTH & 127) | $masked_bit ); if (PHP_INT_MAX > 2147483647) { $this->buffer .= pack('NN', $this->length >> 32, $this->length); // $this->buffer .= pack('I', $this->length); } else { $this->buffer .= pack('NN', 0, $this->length); } } if ($this->masked) { $this->mask = $this->generateMask(); $this->buffer .= $this->mask; $this->buffer .= $this->mask($this->payload); } else { $this->buffer .= $this->payload; } $this->offset_mask = $this->getMaskOffset(); $this->offset_payload = $this->getPayloadOffset(); return $this; } /** * Masks/Unmasks the frame * * @param string $payload * @return string */ protected function mask($payload) { $length = strlen($payload); $mask = $this->getMask(); $unmasked = ''; for ($i = 0; $i < $length; $i++) { $unmasked .= $payload[$i] ^ $mask[$i % 4]; } return $unmasked; } /** * Masks a payload * * @param string $payload * @return string */ protected function unmask($payload) { return $this->mask($payload); } public function receiveData($data) { if ($this->getBufferLength() <= self::BYTE_INITIAL_LENGTH) { $this->length = null; $this->offset_payload = null; } parent::receiveData($data); } /** * Gets the mask * * @throws FrameException * @return string */ protected function getMask() { if (!isset($this->mask)) { if (!$this->isMasked()) { throw new FrameException('Cannot get mask: frame is not masked'); } $this->mask = substr($this->buffer, $this->getMaskOffset(), $this->getMaskSize()); } return $this->mask; } /** * Generates a suitable masking key * * @return string */ protected function generateMask() { if (extension_loaded('openssl')) { return openssl_random_pseudo_bytes(4); } else { // SHA1 is 128 bit (= 16 bytes) // So we pack it into 32 bits return pack('N', sha1(spl_object_hash($this) . mt_rand(0, PHP_INT_MAX) . uniqid('', true), true)); } } /** * Whether the frame is masked * * @return boolean */ public function isMasked() { if (!isset($this->masked)) { if (!isset($this->buffer[1])) { throw new FrameException('Cannot tell if frame is masked: not enough frame data recieved'); } $this->masked = (boolean)(ord($this->buffer[1]) & self::BITFIELD_MASKED); } return $this->masked; } /** * @see Wrench\Frame.Frame::getExpectedDataLength() */ protected function getExpectedBufferLength() { return $this->getLength() + $this->getPayloadOffset(); } /** * Gets the offset of the payload in the frame * * @return int */ protected function getPayloadOffset() { if (!isset($this->offset_payload)) { $offset = $this->getMaskOffset(); $offset += $this->getMaskSize(); $this->offset_payload = $offset; } return $this->offset_payload; } /** * Gets the offset in the frame to the masking bytes * * @return int */ protected function getMaskOffset() { if (!isset($this->offset_mask)) { $offset = self::BYTE_INITIAL_LENGTH + 1; $offset += $this->getLengthSize(); $this->offset_mask = $offset; } return $this->offset_mask; } /** * @see Wrench\Frame.Frame::getLength() */ public function getLength() { if (!$this->length) { $initial = $this->getInitialLength(); if ($initial < 126) { $this->length = $initial; } elseif ($initial >= 126) { // Extended payload length: 2 or 8 bytes $start = self::BYTE_INITIAL_LENGTH + 1; $end = self::BYTE_INITIAL_LENGTH + $this->getLengthSize(); if ($end > $this->getBufferLength()) { throw new FrameException('Cannot get extended length: need more data'); } $length = 0; for ($i = $start; $i <= $end; $i++) { $length <<= 8; $length += ord($this->buffer[$i]); } $this->length = $length; } } return $this->length; } /** * Gets the inital length value, stored in the first length byte * * This determines how the rest of the length value is parsed out of the * frame. * * @return int */ protected function getInitialLength() { if (!isset($this->buffer[self::BYTE_INITIAL_LENGTH])) { throw new FrameException('Cannot yet tell expected length'); } $a = (int)(ord($this->buffer[self::BYTE_INITIAL_LENGTH]) & self::BITFIELD_INITIAL_LENGTH); return (int)(ord($this->buffer[self::BYTE_INITIAL_LENGTH]) & self::BITFIELD_INITIAL_LENGTH); } /** * Returns the byte size of the length part of the frame * * Not including the initial 7 bit part * * @return int */ protected function getLengthSize() { $initial = $this->getInitialLength(); if ($initial < 126) { return 0; } elseif ($initial === 126) { return 2; } elseif ($initial === 127) { return 8; } } /** * Returns the byte size of the mask part of the frame * * @return int */ protected function getMaskSize() { if ($this->isMasked()) { return 4; } return 0; } /** * @see Wrench\Frame.Frame::decodeFramePayloadFromBuffer() */ protected function decodeFramePayloadFromBuffer() { $payload = substr($this->buffer, $this->getPayloadOffset()); if ($this->isMasked()) { $payload = $this->unmask($payload); } $this->payload = $payload; } /** * @see Wrench\Frame.Frame::isFinal() */ public function isFinal() { if (!isset($this->buffer[self::BYTE_HEADER])) { throw new FrameException('Cannot yet tell if frame is final'); } return (boolean)(ord($this->buffer[self::BYTE_HEADER]) & self::BITFIELD_FINAL); } /** * @throws FrameException * @see Wrench\Frame.Frame::getType() */ public function getType() { if (!isset($this->buffer[self::BYTE_HEADER])) { throw new FrameException('Cannot yet tell type of frame'); } $type = (int)(ord($this->buffer[self::BYTE_HEADER]) & self::BITFIELD_TYPE); if (!in_array($type, Protocol::$frameTypes)) { throw new FrameException('Invalid payload type'); } return $type; } } ================================================ FILE: lib/Wrench/Listener/HandshakeRequestListener.php ================================================ allowed = $allowed; } /** * Handshake request listener * * Closes the connection on handshake from an origin that isn't allowed * * @param Connection $connection * @param string $path * @param string $origin * @param string $key * @param array $extensions */ public function onHandshakeRequest(Connection $connection, $path, $origin, $key, $extensions) { if (!$this->isAllowed($origin)) { $connection->close(new InvalidOriginException('Origin not allowed')); } } /** * Whether the specified origin is allowed under this policy * * @param string $origin * @return boolean */ public function isAllowed($origin) { $scheme = parse_url($origin, PHP_URL_SCHEME); $host = parse_url($origin, PHP_URL_HOST) ?: $origin; foreach ($this->allowed as $allowed) { $allowed_scheme = parse_url($allowed, PHP_URL_SCHEME); if ($allowed_scheme && $scheme != $allowed_scheme) { continue; } $allowed_host = parse_url($allowed, PHP_URL_HOST) ?: $allowed; if ($host != $allowed_host) { continue; } return true; } return false; } /** * @param Server $server */ public function listen(Server $server) { $server->addListener( Server::EVENT_HANDSHAKE_REQUEST, array($this, 'onHandshakeRequest') ); } } ================================================ FILE: lib/Wrench/Listener/RateLimiter.php ================================================ */ protected $ips = array(); /** * Request tokens per IP address * * @var array> */ protected $requests = array(); /** * Constructor * * @param array $options */ public function __construct(array $options = array()) { parent::__construct($options); } /** * @param array $options */ protected function configure(array $options) { $options = array_merge(array( 'connections' => 200, // Total 'connections_per_ip' => 5, // At once 'requests_per_minute' => 200 // Per connection ), $options); parent::configure($options); } /** * @see Wrench\Listener.Listener::listen() */ public function listen(Server $server) { $this->server = $server; $server->addListener( Server::EVENT_SOCKET_CONNECT, array($this, 'onSocketConnect') ); $server->addListener( Server::EVENT_SOCKET_DISCONNECT, array($this, 'onSocketDisconnect') ); $server->addListener( Server::EVENT_CLIENT_DATA, array($this, 'onClientData') ); } /** * Event listener * * @param resource $socket * @param Connection $connection */ public function onSocketConnect($socket, $connection) { $this->checkConnections($connection); $this->checkConnectionsPerIp($connection); } /** * Event listener * * @param resource $socket * @param Connection $connection */ public function onSocketDisconnect($socket, $connection) { $this->releaseConnection($connection); } /** * Event listener * * @param resource $socket * @param Connection $connection */ public function onClientData($socket, $connection) { $this->checkRequestsPerMinute($connection); } /** * Idempotent * * @param Connection $connection */ protected function checkConnections($connection) { $connections = $connection->getConnectionManager()->count(); if ($connections > $this->options['connections']) { $this->limit($connection, 'Max connections'); } } /** * NOT idempotent, call once per connection * * @param Connection $connection */ protected function checkConnectionsPerIp($connection) { $ip = $connection->getIp(); if (!$ip) { $this->log('Cannot check connections per IP', 'warning'); return; } if (!isset($this->ips[$ip])) { $this->ips[$ip] = 1; } else { $this->ips[$ip] = min( $this->options['connections_per_ip'], $this->ips[$ip] + 1 ); } if ($this->ips[$ip] > $this->options['connections_per_ip']) { $this->limit($connection, 'Connections per IP'); } } /** * NOT idempotent, call once per disconnection * * @param Connection $connection */ protected function releaseConnection($connection) { $ip = $connection->getIp(); if (!$ip) { $this->log('Cannot release connection', 'warning'); return; } if (!isset($this->ips[$ip])) { $this->ips[$ip] = 0; } else { $this->ips[$ip] = max(0, $this->ips[$ip] - 1); } unset($this->requests[$connection->getId()]); } /** * NOT idempotent, call once per data * * @param Connection $connection */ protected function checkRequestsPerMinute($connection) { $id = $connection->getId(); if (!isset($this->requests[$id])) { $this->requests[$id] = array(); } // Add current token $this->requests[$id][] = time(); // Expire old tokens while (reset($this->requests[$id]) < time() - 60) { array_shift($this->requests[$id]); } if (count($this->requests[$id]) > $this->options['requests_per_minute']) { $this->limit($connection, 'Requests per minute'); } } /** * Limits the given connection * * @param Connection $connection * @param string $limit Reason */ protected function limit($connection, $limit) { $this->log(sprintf( 'Limiting connection %s: %s', $connection->getIp(), $limit ), 'notice'); $connection->close(new RateLimiterException($limit)); } /** * Logger * * @param string $message * @param string $priority */ public function log($message, $priority = 'info') { $this->server->log('RateLimiter: ' . $message, $priority); } } ================================================ FILE: lib/Wrench/Payload/HybiPayload.php ================================================ */ protected $frames = array(); /** * Gets the current frame for the payload * * @return mixed */ protected function getCurrentFrame() { if (empty($this->frames)) { array_push($this->frames, $this->getFrame()); } return end($this->frames); } /** * Gets the frame into which data should be receieved * * @throws PayloadException * @return Frame */ protected function getReceivingFrame() { $current = $this->getCurrentFrame(); if ($current->isComplete()) { if ($current->isFinal()) { throw new PayloadException('Payload cannot receieve data: it is already complete'); } else { $current = array_push($this->frames, $this->getFrame()); } } return $current; } /** * Get a frame object * * @return Frame */ abstract protected function getFrame(); /** * Whether the payload is complete * * @return boolean */ public function isComplete() { return $this->getCurrentFrame()->isComplete() && $this->getCurrentFrame()->isFinal(); } /** * Encodes a payload * * @param string $data * @param int $type * @param boolean $masked * @return Payload * @todo No splitting into multiple frames just yet */ public function encode($data, $type = Protocol::TYPE_TEXT, $masked = false) { $this->frames = array(); $frame = $this->getFrame(); array_push($this->frames, $frame); $frame->encode($data, $type, $masked); return $this; } /** * Gets the number of remaining bytes before this payload will be * complete * * May return 0 (no more bytes required) or null (unknown number of bytes * required). * * @return number|NULL */ public function getRemainingData() { if ($this->isComplete()) { return 0; } try { if ($this->getCurrentFrame()->isFinal()) { return $this->getCurrentFrame()->getRemainingData(); } } catch (FrameException $e) { return null; } return null; } /** * Whether this payload is waiting for more data * * @return boolean */ public function isWaitingForData() { return $this->getRemainingData() > 0; } /** * @param Socket $socket * @return boolean */ public function sendToSocket(Socket $socket) { $success = true; foreach ($this->frames as $frame) { $success = $success && ($socket->send($frame->getFrameBuffer()) !== false); } return $success; } /** * Receive raw data into the payload * * @param string $data * @return void */ public function receiveData($data) { while ($data) { $frame = $this->getReceivingFrame(); $size = strlen($data); $remaining = $frame->getRemainingData(); if ($remaining === null) { $chunk_size = 2; } elseif ($remaining > 0) { $chunk_size = $remaining; } $chunk_size = min(strlen($data), $chunk_size); $chunk = substr($data, 0, $chunk_size); $data = substr($data, $chunk_size); $frame->receiveData($chunk); } } /** * @return string */ public function getPayload() { $this->buffer = ''; foreach ($this->frames as $frame) { $this->buffer .= $frame->getFramePayload(); } return $this->buffer; } /** * @return string */ public function __toString() { try { return $this->getPayload(); } catch (\Exception $e) { // __toString must not throw an exception return ''; } } /** * Gets the type of the payload * * The type of a payload is taken from its first frame * * @throws PayloadException * @return int */ public function getType() { if (!isset($this->frames[0])) { throw new PayloadException('Cannot tell payload type yet'); } return $this->frames[0]->getType(); } } ================================================ FILE: lib/Wrench/Payload/PayloadHandler.php ================================================ callback = $callback; } /** * Handles the raw socket data given * * @param string $data */ public function handle($data) { if (!$this->payload) { $this->payload = $this->protocol->getPayload(); } while ($data) { // Each iteration pulls off a single payload chunk $size = strlen($data); $remaining = $this->payload->getRemainingData(); // If we don't yet know how much data is remaining, read data into // the payload in two byte chunks (the size of a WebSocket frame // header to get the initial length) // // Then re-loop. For extended lengths, this will happen once or four // times extra, as the extended length is read in. if ($remaining === null) { $chunk_size = 2; } elseif ($remaining > 0) { $chunk_size = $remaining; } elseif ($remaining === 0) { $chunk_size = 0; } $chunk_size = min(strlen($data), $chunk_size); $chunk = substr($data, 0, $chunk_size); $data = substr($data, $chunk_size); $this->payload->receiveData($chunk); if ($remaining !== 0 && !$this->payload->isComplete()) { continue; } if ($this->payload->isComplete()) { $this->emit($this->payload); $this->payload = $this->protocol->getPayload(); } else { throw new PayloadException('Payload will not complete'); } } } /** * Get the current payload * * @return Payload */ public function getCurrent() { return $this->getPayloadHandler->getCurrent(); } /** * Emits a complete payload to the callback * * @param Payload $payload */ protected function emit(Payload $payload) { call_user_func($this->callback, $payload); } } ================================================ FILE: lib/Wrench/Protocol/Hybi10Protocol.php ================================================ = 10) { return true; } } } ================================================ FILE: lib/Wrench/Protocol/HybiProtocol.php ================================================ */ protected static $schemes = array( self::SCHEME_WEBSOCKET, self::SCHEME_WEBSOCKET_SECURE, self::SCHEME_UNDERLYING, self::SCHEME_UNDERLYING_SECURE ); /** * Close status codes * * @var array string> */ public static $closeReasons = array( self::CLOSE_NORMAL => 'normal close', self::CLOSE_GOING_AWAY => 'going away', self::CLOSE_PROTOCOL_ERROR => 'protocol error', self::CLOSE_DATA_INVALID => 'data invalid', self::CLOSE_DATA_INCONSISTENT => 'data inconsistent', self::CLOSE_POLICY_VIOLATION => 'policy violation', self::CLOSE_MESSAGE_TOO_BIG => 'message too big', self::CLOSE_EXTENSION_NEEDED => 'extension needed', self::CLOSE_UNEXPECTED => 'unexpected error', self::CLOSE_RESERVED => null, // Don't use these! self::CLOSE_RESERVED_NONE => null, self::CLOSE_RESERVED_ABNORM => null, self::CLOSE_RESERVED_TLS => null ); /** * Frame types * * @todo flip values and keys? * @var array int> */ public static $frameTypes = array( 'continuation' => self::TYPE_CONTINUATION, 'text' => self::TYPE_TEXT, 'binary' => self::TYPE_BINARY, 'close' => self::TYPE_CLOSE, 'ping' => self::TYPE_PING, 'pong' => self::TYPE_PONG ); /** * HTTP errors * * @var array string> */ public static $httpResponses = array( self::HTTP_SWITCHING_PROTOCOLS => 'Switching Protocols', self::HTTP_BAD_REQUEST => 'Bad Request', self::HTTP_UNAUTHORIZED => 'Unauthorized', self::HTTP_FORBIDDEN => 'Forbidden', self::HTTP_NOT_FOUND => 'Not Found', self::HTTP_NOT_IMPLEMENTED => 'Not Implemented', self::HTTP_RATE_LIMITED => 'Enhance Your Calm' ); /** * Gets a version number * * @return */ abstract public function getVersion(); /** * Subclasses should implement this method and return a boolean to the given * version string, as to whether they would like to accept requests from * user agents that specify that version. * * @return boolean */ abstract public function acceptsVersion($version); /** * Gets a payload instance, suitable for use in decoding/encoding protocol * frames * * @return Payload */ abstract public function getPayload(); /** * Generates a key suitable for use in the protocol * * This base implementation returns a 16-byte (128 bit) random key as a * binary string. * * @return string */ public function generateKey() { if (extension_loaded('openssl')) { $key = openssl_random_pseudo_bytes(16); } else { // SHA1 is 128 bit (= 16 bytes) $key = sha1(spl_object_hash($this) . mt_rand(0, PHP_INT_MAX) . uniqid('', true), true); } return base64_encode($key); } /** * Gets request handshake string * * The leading line from the client follows the Request-Line format. * The leading line from the server follows the Status-Line format. The * Request-Line and Status-Line productions are defined in [RFC2616]. * * An unordered set of header fields comes after the leading line in * both cases. The meaning of these header fields is specified in * Section 4 of this document. Additional header fields may also be * present, such as cookies [RFC6265]. The format and parsing of * headers is as defined in [RFC2616]. * * @param string $uri WebSocket URI, e.g. ws://example.org:8000/chat * @param string $key 16 byte binary string key * @param string $origin Origin of the request * @return string */ public function getRequestHandshake( $uri, $key, $origin, array $headers = array() ) { if (!$uri || !$key || !$origin) { throw new InvalidArgumentException('You must supply a URI, key and origin'); } list($scheme, $host, $port, $path) = self::validateUri($uri); $handshake = array( sprintf(self::REQUEST_LINE_FORMAT, $path) ); $headers = array_merge( $this->getDefaultRequestHeaders( $host . ':' . $port, $key, $origin ), $headers ); foreach ($headers as $name => $value) { $handshake[] = sprintf(self::HEADER_LINE_FORMAT, $name, $value); } return implode($handshake, "\r\n") . "\r\n\r\n"; } /** * Gets a handshake response body * * @param string $key * @param array $headers */ public function getResponseHandshake($key, array $headers = array()) { $headers = array_merge( $this->getSuccessResponseHeaders( $key ), $headers ); return $this->getHttpResponse(self::HTTP_SWITCHING_PROTOCOLS, $headers); } /** * Gets a response to an error in the handshake * * @param int|Exception $e Exception or HTTP error * @param array $headers */ public function getResponseError($e, array $headers = array()) { $code = false; if ($e instanceof Exception) { $code = $e->getCode(); } elseif (is_numeric($e)) { $code = (int)$e; } if (!$code || $code < 400 || $code > 599) { $code = self::HTTP_SERVER_ERROR; } return $this->getHttpResponse($code, $headers); } /** * Gets an HTTP response * * @param int $status * @param array $headers */ protected function getHttpResponse($status, array $headers = array()) { if (array_key_exists($status, self::$httpResponses)) { $response = self::$httpResponses[$status]; } else { $response = self::$httpResponses[self::HTTP_NOT_IMPLEMENTED]; } $handshake = array( sprintf(self::RESPONSE_LINE_FORMAT, $status, $response) ); foreach ($headers as $name => $value) { $handshake[] = sprintf(self::HEADER_LINE_FORMAT, $name, $value); } return implode($handshake, "\r\n") . "\r\n\r\n"; } /** * @todo better header handling * @todo throw exception * @param unknown_type $response * @param unknown_type $key * @return boolean */ public function validateResponseHandshake($response, $key) { if (!$response) { return false; } $headers = $this->getHeaders($response); if (!isset($headers[self::HEADER_ACCEPT])) { throw new HandshakeException('No accept header receieved on handshake response'); } $accept = $headers[self::HEADER_ACCEPT]; if (!$accept) { throw new HandshakeException('Invalid accept header'); } $expected = $this->getAcceptValue($key); preg_match('#Sec-WebSocket-Accept:\s(.*)$#mU', $response, $matches); $keyAccept = trim($matches[1]); return ($keyAccept === $this->getEncodedHash($key)) ? true : false; } /** * Gets an encoded hash for a key * * @param string $key * @return string */ public function getEncodedHash($key) { return base64_encode(pack('H*', sha1($key . self::MAGIC_GUID))); } /** * Validates a request handshake * * @param string $request * @throws BadRequestException */ public function validateRequestHandshake( $request ) { if (!$request) { return false; } list($request, $headers) = $this->getRequestHeaders($request); $path = $this->validateRequestLine($request); if (!isset($headers[self::HEADER_ORIGIN]) || !$headers[self::HEADER_ORIGIN]) { throw new BadRequestException('No origin header'); } $origin = $headers[self::HEADER_ORIGIN]; if (!isset($headers[self::HEADER_UPGRADE]) || strtolower($headers[self::HEADER_UPGRADE]) != self::UPGRADE_VALUE ) { throw new BadRequestException('Invalid upgrade header'); } if (!isset($headers[self::HEADER_CONNECTION]) || strpos($headers[self::HEADER_CONNECTION], self::CONNECTION_VALUE) === false ) { throw new BadRequestException('Invalid connection header'); } if (!isset($headers[self::HEADER_HOST])) { // @todo Validate host == listening socket? Or would that break // TCP proxies? throw new BadRequestException('No host header'); } if (!isset($headers[self::HEADER_VERSION])) { throw new BadRequestException('No version header received on handshake request'); } if (!$this->acceptsVersion($headers[self::HEADER_VERSION])) { throw new BadRequestException('Unsupported version: ' . $version); } if (!isset($headers[self::HEADER_KEY])) { throw new BadRequestException('No key header received'); } $key = trim($headers[self::HEADER_KEY]); if (!$key) { throw new BadRequestException('Invalid key'); } // Optional $protocol = isset($headers[self::HEADER_PROTOCOL]) ? $headers[self::HEADER_PROTOCOL] : null; $extensions = array(); if (isset($headers[self::HEADER_EXTENSIONS]) && $headers[self::HEADER_EXTENSIONS]) { $extensions = $headers[self::HEADER_EXTENSIONS]; if (is_scalar($extensions)) { $extensions = array($extensions); } } return array($path, $origin, $key, $extensions, $protocol); } /** * Gets a suitable WebSocket close frame * * @param Exception|int $e */ public function getCloseFrame($e) { $code = false; if ($e instanceof Exception) { $code = $e->getCode(); } elseif (is_numeric($e)) { $code = (int)$e; } if (!$code || !key_exists($code, self::$closeReasons)) { $code = self::CLOSE_UNEXPECTED; } $body = pack('n', $code) . self::$closeReasons[$code]; $payload = $this->getPayload(); return $payload->encode($body, self::TYPE_CLOSE); } /** * Validates a WebSocket URI * * @param string $uri * @return array(string $scheme, string $host, int $port, string $path) */ public function validateUri($uri) { $uri = (string)$uri; if (!$uri) { throw new InvalidArgumentException('Invalid URI'); } $scheme = parse_url($uri, PHP_URL_SCHEME); $this->validateScheme($scheme); $host = parse_url($uri, PHP_URL_HOST); if (!$host) { throw new InvalidArgumentException("Invalid host"); } $port = parse_url($uri, PHP_URL_PORT); if (!$port) { $port = $this->getPort($scheme); } $path = parse_url($uri, PHP_URL_PATH); if (!$path) { throw new InvalidArgumentException('Invalid path'); } return array($scheme, $host, $port, $path); } /** * Validates a socket URI * * @param string $uri * @throws InvalidArgumentException * @return array(string $scheme, string $host, string $port) */ public function validateSocketUri($uri) { $uri = (string)$uri; if (!$uri) { throw new InvalidArgumentException('Invalid URI'); } $scheme = parse_url($uri, PHP_URL_SCHEME); $scheme = $this->validateScheme($scheme); $host = parse_url($uri, PHP_URL_HOST); if (!$host) { throw new InvalidArgumentException("Invalid host"); } $port = parse_url($uri, PHP_URL_PORT); if (!$port) { $port = $this->getPort($scheme); } return array($scheme, $host, $port); } /** * Validates an origin URI * * @param string $origin * @throws InvalidArgumentException * @return string */ public function validateOriginUri($origin) { $origin = (string)$origin; if (!$origin) { throw new InvalidArgumentException('Invalid URI'); } $scheme = parse_url($origin, PHP_URL_SCHEME); if (!$scheme) { throw new InvalidArgumentException('Invalid scheme'); } $host = parse_url($origin, PHP_URL_HOST); if (!$host) { throw new InvalidArgumentException("Invalid host"); } return $origin; } /** * Validates a request line * * @param string $line * @throws BadRequestException */ protected function validateRequestLine($line) { $matches = array(0 => null, 1 => null); if (!preg_match(self::REQUEST_LINE_REGEX, $line, $matches) || !$matches[1]) { throw new BadRequestException('Invalid request line', 400); } return $matches[1]; } /** * Gets the expected accept value for a handshake response * * Note that the protocol calls for the base64 encoded value to be hashed, * not the original 16 byte random key. * * @see http://tools.ietf.org/html/rfc6455#section-4.2.2 * @param string $key */ protected function getAcceptValue($encoded_key) { return base64_encode(sha1($encoded_key . self::MAGIC_GUID, true)); } /** * Gets the headers from a full response * * @param string $response * @return array() * @throws InvalidArgumentException */ protected function getHeaders($response, &$request_line = null) { $parts = explode("\r\n\r\n", $response, 2); if (count($parts) != 2) { $parts = array($parts, ''); } list($headers, $body) = $parts; $return = array(); foreach (explode("\r\n", $headers) as $header) { $parts = explode(': ', $header, 2); if (count($parts) == 2) { list($name, $value) = $parts; if (!isset($return[$name])) { $return[$name] = $value; } else { if (is_array($return[$name])) { $return[$name][] = $value; } else { $return[$name] = array($return[$name], $value); } } } } return $return; } /** * Gets request headers * * @param string $response * @return array> The request line, and an array of * headers * @throws InvalidArgumentException */ protected function getRequestHeaders($response) { $eol = strpos($response, "\r\n"); if ($eol === false) { throw new InvalidArgumentException('Invalid request line'); } $request = substr($response, 0, $eol); $headers = $this->getHeaders(substr($response, $eol + 2)); return array($request, $headers); } /** * Validates a scheme * * @param string $scheme * @return string Underlying scheme * @throws InvalidArgumentException */ protected function validateScheme($scheme) { if (!$scheme) { throw new InvalidArgumentException('No scheme specified'); } if (!in_array($scheme, self::$schemes)) { throw new InvalidArgumentException( 'Unknown socket scheme: ' . $scheme ); } if ($scheme == self::SCHEME_WEBSOCKET_SECURE) { return self::SCHEME_UNDERLYING_SECURE; } return self::SCHEME_UNDERLYING; } /** * Gets the default request headers * * @param string $host * @param string $key * @param string $origin * @param int $version * @return multitype:unknown string NULL */ protected function getDefaultRequestHeaders($host, $key, $origin) { return array( self::HEADER_HOST => $host, self::HEADER_UPGRADE => self::UPGRADE_VALUE, self::HEADER_CONNECTION => self::CONNECTION_VALUE, self::HEADER_KEY => $key, self::HEADER_ORIGIN => $origin, self::HEADER_VERSION => $this->getVersion() ); } /** * Gets the default response headers * * @param string $key */ protected function getSuccessResponseHeaders($key) { return array( self::HEADER_UPGRADE => self::UPGRADE_VALUE, self::HEADER_CONNECTION => self::CONNECTION_VALUE, self::HEADER_ACCEPT => $this->getAcceptValue($key) ); } /** * Gets the default port for a scheme * * By default, the WebSocket Protocol uses port 80 for regular WebSocket * connections and port 443 for WebSocket connections tunneled over * Transport Layer Security * * @param string $uri * @return int */ protected function getPort($scheme) { if ($scheme == self::SCHEME_WEBSOCKET) { return 80; } elseif ($scheme == self::SCHEME_WEBSOCKET_SECURE) { return 443; } elseif ($scheme == self::SCHEME_UNDERLYING) { return 80; } elseif ($scheme == self::SCHEME_UNDERLYING_SECURE) { return 443; } else { throw new InvalidArgumentException('Unknown websocket scheme'); } } } ================================================ FILE: lib/Wrench/Protocol/Rfc6455Protocol.php ================================================ * @author Simon Samtleben * @author Dominic Scheirlinck */ class Server extends Configurable { /**#@+ * Events * * @var string */ const EVENT_SOCKET_CONNECT = 'socket_connect'; const EVENT_SOCKET_DISCONNECT = 'socket_disconnect'; const EVENT_HANDSHAKE_REQUEST = 'handshake_request'; const EVENT_HANDSHAKE_SUCCESSFUL = 'handshake_successful'; const EVENT_CLIENT_DATA = 'client_data'; /**#@-*/ /** * The URI of the server * * @var string */ protected $uri; /** * Options * * @var array */ protected $options = array(); /** * A logging callback * * The default callback simply prints to stdout. You can pass your own logger * in the options array. It should take a string message and string priority * as parameters. * * @var Closure */ protected $logger; /** * Event listeners * * Add listeners using the addListener() method. * * @var array array> */ protected $listeners = array(); /** * Connection manager * * @var ConnectionManager */ protected $connectionManager; /** * Applications * * @var array Application> */ protected $applications = array(); /** * Constructor * * @param string $uri Websocket URI, e.g. ws://localhost:8000/, path will * be ignored * @param array $options (optional) See configure */ public function __construct($uri, array $options = array()) { $this->uri = $uri; parent::__construct($options); $this->log('Server initialized', 'info'); } /** * Configure options * * Options include * - socket_class => The socket class to use, defaults to ServerSocket * - socket_options => An array of socket options * - logger => Closure($message, $priority = 'info'), used * for logging * * @param array $options * @return void */ protected function configure(array $options) { $options = array_merge(array( 'connection_manager_class' => 'Wrench\ConnectionManager', 'connection_manager_options' => array() ), $options); parent::configure($options); $this->configureConnectionManager(); $this->configureLogger(); } /** * Configures the logger * * @return void */ protected function configureLogger() { // Default logger if (!isset($this->options['logger'])) { $this->options['logger'] = function ($message, $priority = 'info') { printf("%s: %s%s", $priority, $message, PHP_EOL); }; } $this->setLogger($this->options['logger']); } /** * Configures the connection manager * * @return void */ protected function configureConnectionManager() { $class = $this->options['connection_manager_class']; $options = $this->options['connection_manager_options']; $this->connectionManager = new $class($this, $options); } /** * Gets the connection manager * * @return \Wrench\ConnectionManager */ public function getConnectionManager() { return $this->connectionManager; } /** * @return string */ public function getUri() { return $this->uri; } /** * Sets a logger * * @param Closure $logger * @return void */ public function setLogger($logger) { if (!is_callable($logger)) { throw new \InvalidArgumentException('Logger must be callable'); } $this->logger = $logger; } /** * Main server loop * * @return void This method does not return! */ public function run() { $this->connectionManager->listen(); while (true) { /* * If there's nothing changed on any of the sockets, the server * will sleep and other processes will have a change to run. Control * this behaviour with the timeout options. */ $this->connectionManager->selectAndProcess(); /* * If the application wants to perform periodic operations or queries and push updates to clients based on the result then that logic can be implemented in the 'onUpdate' method. */ foreach($this->applications as $application) { if(method_exists($application, 'onUpdate')) { $application->onUpdate(); } } } } /** * Logs a message to the server log * * The default logger simply prints the message to stdout. You can provide * a logging closure. This is useful, for instance, if you've daemonized * and closed STDOUT. * * @param string $message Message to display. * @param string $type Type of message. * @return void */ public function log($message, $priority = 'info') { call_user_func($this->logger, $message, $priority); } /** * Notifies listeners of an event * * @param string $event * @param array $arguments Event arguments * @return void */ public function notify($event, array $arguments = array()) { if (!isset($this->listeners[$event])) { return; } foreach ($this->listeners[$event] as $listener) { call_user_func_array($listener, $arguments); } } /** * Adds a listener * * Provide an event (see the Server::EVENT_* constants) and a callback * closure. Some arguments may be provided to your callback, such as the * connection the caused the event. * * @param string $event * @param Closure $callback * @return void * @throws InvalidArgumentException */ public function addListener($event, $callback) { if (!isset($this->listeners[$event])) { $this->listeners[$event] = array(); } if (!is_callable($callback)) { throw new InvalidArgumentException('Invalid listener'); } $this->listeners[$event][] = $callback; } /** * Returns a server application. * * @param string $key Name of application. * @return Application The application object. */ public function getApplication($key) { if (empty($key)) { return false; } if (array_key_exists($key, $this->applications)) { return $this->applications[$key]; } return false; } /** * Adds a new application object to the application storage. * * @param string $key Name of application. * @param object $application The application object * @return void */ public function registerApplication($key, $application) { $this->applications[$key] = $application; } } ================================================ FILE: lib/Wrench/Socket/ClientSocket.php ================================================ int, seconds, default 2 */ class ClientSocket extends UriSocket { /** * Default connection timeout * * @var int seconds */ const TIMEOUT_CONNECT = 2; /** * @see Wrench\Socket.Socket::configure() * Options include: * - ssl_verify_peer => boolean, whether to perform peer verification * of SSL certificate used * - ssl_allow_self_signed => boolean, whether ssl_verify_peer allows * self-signed certs * - timeout_connect => int, seconds, default 2 */ protected function configure(array $options) { $options = array_merge(array( 'timeout_connect' => self::TIMEOUT_CONNECT, 'ssl_verify_peer' => false, 'ssl_allow_self_signed' => true ), $options); parent::configure($options); } /** * Connects to the given socket */ public function connect() { if ($this->isConnected()) { return true; } $errno = null; $errstr = null; $this->socket = stream_socket_client( $this->getUri(), $errno, $errstr, $this->options['timeout_connect'], STREAM_CLIENT_CONNECT, $this->getStreamContext() ); if (!$this->socket) { throw new ConnectionException(sprintf( 'Could not connect to socket: %s (%d)', $errstr, $errno )); } stream_set_timeout($this->socket, $this->options['timeout_socket']); return ($this->connected = true); } public function reconnect() { $this->disconnect(); $this->connect(); } /** * @see Wrench\Socket.UriSocket::getSocketStreamContextOptions() */ protected function getSocketStreamContextOptions() { $options = array(); return $options; } /** * @see Wrench\Socket.UriSocket::getSslStreamContextOptions() */ protected function getSslStreamContextOptions() { $options = array(); if ($this->options['ssl_verify_peer']) { $options['verify_peer'] = true; } if ($this->options['ssl_allow_self_signed']) { $options['allow_self_signed'] = true; } return $options; } } ================================================ FILE: lib/Wrench/Socket/ServerClientSocket.php ================================================ connect() or whatnot. * * @param resource $accepted_socket * @param array $options */ public function __construct($accepted_socket, array $options = array()) { parent::__construct($options); $this->socket = $accepted_socket; $this->connected = (boolean)$accepted_socket; } } ================================================ FILE: lib/Wrench/Socket/ServerSocket.php ================================================ int, used to limit the number of outstanding * connections in the socket's listen queue * - ssl_cert_file => string, server SSL certificate * file location. File should contain * certificate and private key * - ssl_passphrase => string, passphrase for the key * - timeout_accept => int, seconds, default 5 */ protected function configure(array $options) { $options = array_merge(array( 'backlog' => 50, 'ssl_cert_file' => null, 'ssl_passphrase' => null, 'ssl_allow_self_signed' => false, 'timeout_accept' => self::TIMEOUT_ACCEPT ), $options); parent::configure($options); } /** * Listens * * @throws ConnectionException */ public function listen() { $this->socket = stream_socket_server( $this->getUri(), $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN. $this->getStreamContext() ); if (!$this->socket) { throw new ConnectionException(sprintf( 'Could not listen on socket: %s (%d)', $errstr, $errno )); } $this->listening = true; } /** * Accepts a new connection on the socket * * @throws ConnectionException * @return resource */ public function accept() { $new = stream_socket_accept( $this->socket, $this->options['timeout_accept'] ); if (!$new) { throw new ConnectionException(socket_strerror(socket_last_error($new))); } return $new; } /** * @see Wrench\Socket.UriSocket::getSocketStreamContextOptions() */ protected function getSocketStreamContextOptions() { $options = array(); if ($this->options['backlog']) { $options['backlog'] = $this->options['backlog']; } return $options; } /** * @see Wrench\Socket.UriSocket::getSslStreamContextOptions() */ protected function getSslStreamContextOptions() { $options = array(); if ($this->options['server_ssl_cert_file']) { $options['local_cert'] = $this->options['server_ssl_cert_file']; if ($this->options['server_ssl_passphrase']) { $options['passphrase'] = $this->options['server_ssl_passphrase']; } } return $options; } } ================================================ FILE: lib/Wrench/Socket/Socket.php ================================================ int, seconds, default 2 * - timeout_socket => int, seconds, default 5 * * @param array $options * @return void */ protected function configure(array $options) { $options = array_merge(array( 'timeout_socket' => self::TIMEOUT_SOCKET, ), $options); parent::configure($options); } /** * Gets the name of the socket */ protected function getName() { if (!isset($this->name) || !$this->name) { $this->name = @stream_socket_get_name($this->socket, true); } return $this->name; } /** * Gets part of the name of the socket * * PHP seems to return IPV6 address/port combos like this: * ::1:1234, where ::1 is the address and 1234 the port * So, the part number here is either the last : delimited section (the port) * or all the other sections (the whole initial part, the address). * * @param string $name (from $this->getName() usually) * @param int<0, 1> $part * @return string * @throws SocketException */ public static function getNamePart($name, $part) { if (!$name) { throw new InvalidArgumentException('Invalid name'); } $parts = explode(':', $name); if (count($parts) < 2) { throw new SocketException('Could not parse name parts: ' . $name); } if ($part == self::NAME_PART_PORT) { return end($parts); } elseif ($part == self::NAME_PART_IP) { return implode(':', array_slice($parts, 0, -1)); } else { throw new InvalidArgumentException('Invalid name part'); } return null; } /** * Gets the IP address of the socket * * @return string */ public function getIp() { $name = $this->getName(); if ($name) { return self::getNamePart($name, self::NAME_PART_IP); } else { throw new SocketException('Cannot get socket IP address'); } } /** * Gets the port of the socket * * @return int */ public function getPort() { $name = $this->getName(); if ($name) { return self::getNamePart($name, self::NAME_PART_PORT); } else { throw new SocketException('Cannot get socket IP address'); } } /** * Get the last error that occurred on the socket * * @return int|string */ public function getLastError() { if ($this->isConnected() && $this->socket) { $err = @socket_last_error($this->socket); if ($err) { $err = socket_strerror($err); } if (!$err) { $err = 'Unknown error'; } return $err; } else { return 'Not connected'; } } /** * Whether the socket is currently connected * * @return boolean */ public function isConnected() { return $this->connected; } /** * Disconnect the socket * * @return void */ public function disconnect() { if ($this->socket) { stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR); } $this->socket = null; $this->connected = false; } /** * @see Wrench.Resource::getResource() */ public function getResource() { return $this->socket; } /** * @see Wrench.Resource::getResourceId() */ public function getResourceId() { return (int)$this->socket; } /** * @param unknown_type $data * @throws SocketException * @return boolean|int The number of bytes sent or false on error */ public function send($data) { if (!$this->isConnected()) { throw new SocketException('Socket is not connected'); } $length = strlen($data); if ($length == 0) { return 0; } for ($i = $length; $i > 0; $i -= $written) { $written = @fwrite($this->socket, substr($data, -1 * $i)); if ($written === false) { return false; } elseif ($written === 0) { return false; } } return $length; } /** * Recieve data from the socket * * @param int $length * @return string */ public function receive($length = self::DEFAULT_RECEIVE_LENGTH) { $remaining = $length; $buffer = ''; $metadata['unread_bytes'] = 0; do { if (feof($this->socket)) { return $buffer; } $result = fread($this->socket, $length); if ($result === false) { return $buffer; } $buffer .= $result; if (feof($this->socket)) { return $buffer; } $continue = false; if ($this->firstRead == true && strlen($result) == 1) { // Workaround Chrome behavior (still needed?) $continue = true; } $this->firstRead = false; if (strlen($result) == $length) { $continue = true; } // Continue if more data to be read $metadata = stream_get_meta_data($this->socket); if ($metadata && isset($metadata['unread_bytes']) && $metadata['unread_bytes']) { $continue = true; $length = $metadata['unread_bytes']; } } while ($continue); return $buffer; } } ================================================ FILE: lib/Wrench/Socket/UriSocket.php ================================================ Wrench\Protocol object, latest protocol * version used if not specified * - timeout_socket => int, seconds, default 5 * - server_ssl_cert_file => string, server SSL certificate * file location. File should contain * certificate and private key * - server_ssl_passphrase => string, passphrase for the key * - server_ssl_allow_self_signed => boolean, whether to allows self- * signed certs */ public function __construct($uri, array $options = array()) { parent::__construct($options); list($this->scheme, $this->host, $this->port) = $this->protocol->validateSocketUri($uri); } /** * Gets the canonical/normalized URI for this socket * * @return string */ protected function getUri() { return sprintf( '%s://%s:%d', $this->scheme, $this->host, $this->port ); } /** * @todo DNS lookup? Override getIp()? * @see Wrench\Socket.Socket::getName() */ protected function getName() { return sprintf('%s:%s', $this->host, $this->port); } /** * Gets the host name */ public function getHost() { return $this->host; } /** * @see Wrench\Socket.Socket::getPort() */ public function getPort() { return $this->port; } /** * Gets a stream context */ protected function getStreamContext($listen = false) { $options = array(); if ($this->scheme == Protocol::SCHEME_UNDERLYING_SECURE || $this->scheme == Protocol::SCHEME_UNDERLYING) { $options['socket'] = $this->getSocketStreamContextOptions(); } if ($this->scheme == Protocol::SCHEME_UNDERLYING_SECURE) { $options['ssl'] = $this->getSslStreamContextOptions(); } return stream_context_create( $options, array() ); } /** * Returns an array of socket stream context options * * See http://php.net/manual/en/context.socket.php * * @return array */ abstract protected function getSocketStreamContextOptions(); /** * Returns an array of ssl stream context options * * See http://php.net/manual/en/context.ssl.php * * @return array */ abstract protected function getSslStreamContextOptions(); } ================================================ FILE: lib/Wrench/Tests/Application/EchoApplicationTest.php ================================================ assertInstanceOfClass($this->getInstance()); } /** * @param unknown_type $payload * @dataProvider getValidPayloads */ public function testOnData($payload) { $connection = $this->getMockBuilder('Wrench\Connection') ->disableOriginalConstructor() ->getMock(); $connection ->expects($this->once()) ->method('send') ->with($this->equalTo($payload), $this->equalTo(Protocol::TYPE_TEXT)) ->will($this->returnValue(true)); $this->getInstance()->onData($payload, $connection); } /** * Data provider * * @return array> */ public function getValidPayloads() { return array( array('asdkllakdaowidoaw noaoinosdna nwodinado ndsnd aklndiownd'), array(' ') ); } } ================================================ FILE: lib/Wrench/Tests/BasicServerTest.php ================================================ getInstance('ws://localhost:8000', array( 'allowed_origins' => $allowed, 'logger' => array($this, 'log') )); $connection = $this->getMockBuilder('Wrench\Connection') ->disableOriginalConstructor() ->getMock(); $connection ->expects($this->never()) ->method('close') ->will($this->returnValue(true)); $server->notify( Server::EVENT_HANDSHAKE_REQUEST, array($connection, '', $origin, '', array()) ); } /** * @param array $allowed * @param string $origin * @dataProvider getInvalidOrigins */ public function testInvalidOriginPolicy(array $allowed, $origin) { $server = $this->getInstance('ws://localhost:8000', array( 'allowed_origins' => $allowed, 'logger' => array($this, 'log') )); $connection = $this->getMockBuilder('Wrench\Connection') ->disableOriginalConstructor() ->getMock(); $connection ->expects($this->once()) ->method('close') ->will($this->returnValue(true)); $server->notify( Server::EVENT_HANDSHAKE_REQUEST, array($connection, '', $origin, '', array()) ); } /** * @see Wrench\Tests.ServerTest::getValidConstructorArguments() */ public function getValidConstructorArguments() { return array_merge(parent::getValidConstructorArguments(), array( array( 'ws://localhost:8000', array('logger' => function () {}) ) )); } /** * Data provider * * @return array> */ public function getValidOrigins() { return array( array(array('localhost'), 'localhost'), array(array('somewhere.com'), 'somewhere.com'), ); } /** * Data provider * * @return array> */ public function getInvalidOrigins() { return array( array(array('localhost'), 'blah'), array(array('somewhere.com'), 'somewhereelse.com'), array(array('somewhere.com'), 'subdomain.somewhere.com') ); } } ================================================ FILE: lib/Wrench/Tests/ClientTest.php ================================================ assertInstanceOfClass( $client = new Client( 'ws://localhost/test', 'http://example.org/' ), 'ws:// scheme, default socket' ); $this->assertInstanceOfClass( $client = new Client( 'ws://localhost/test', 'http://example.org/', array('socket' => $this->getMockSocket()) ), 'ws:// scheme, socket specified' ); } /** * Gets a mock socket * * @return Socket */ protected function getMockSocket() { return $this->getMock('Wrench\Socket\ClientSocket', array(), array('wss://localhost:8000')); } /** * @expectedException PHPUnit_Framework_Error */ public function testConstructorSocketUnspecified() { $w = new Client(); } /** * @expectedException InvalidArgumentException */ public function testConstructorUriInvalid() { $w = new Client('invalid uri', 'http://www.example.com/'); } /** * @expectedException InvalidArgumentException */ public function testConstructorUriEmpty() { $w = new Client(null, 'http://www.example.com/'); } /** * @expectedException InvalidArgumentException */ public function testConstructorUriPathUnspecified() { $w = new Client('ws://localhost', 'http://www.example.com/'); } /** * @expectedException PHPUnit_Framework_Error */ public function testConstructorOriginUnspecified() { $w = new Client('ws://localhost'); } /** * @expectedException InvalidArgumentException */ public function testConstructorOriginEmpty() { $w = new Client('wss://localhost', null); } /** * @expectedException InvalidArgumentException */ public function testConstructorOriginInvalid() { $w = new Client('ws://localhost:8000', 'NOTAVALIDURI'); } /** * @expectedException InvalidArgumentException */ public function testSendInvalidType() { $client = new Client('ws://localhost/test', 'http://example.org/'); $client->sendData('blah', 9999); } /** * @expectedException InvalidArgumentException */ public function testSendInvalidTypeString() { $client = new Client('ws://localhost/test', 'http://example.org/'); $client->sendData('blah', 'fooey'); } public function testSend() { try { $helper = new ServerTestHelper(); $helper->setUp(); /* @var $instance Wrench\Client */ $instance = $this->getInstance($helper->getEchoConnectionString(), 'http://www.example.com/send'); $instance->addRequestHeader('X-Test', 'Custom Request Header'); $this->assertFalse($instance->receive(), 'Receive before connect'); $success = $instance->connect(); $this->assertTrue($success, 'Client can connect to test server'); $this->assertTrue($instance->isConnected()); $this->assertFalse($instance->connect(), 'Double connect'); $this->assertFalse((boolean)$instance->receive(), 'No data'); $bytes = $instance->sendData('foobar', 'text'); $this->assertTrue($bytes >= 6, 'sent text frame'); sleep(1); $bytes = $instance->sendData('baz', Protocol::TYPE_TEXT); $this->assertTrue($bytes >= 3, 'sent text frame'); sleep(1); $responses = $instance->receive(); $this->assertTrue(is_array($responses)); $this->assertCount(2, $responses); $this->assertInstanceOf('Wrench\\Payload\\Payload', $responses[0]); $this->assertInstanceOf('Wrench\\Payload\\Payload', $responses[1]); $instance->disconnect(); $this->assertFalse($instance->isConnected()); } catch (\Exception $e) { $helper->tearDown(); throw $e; } $helper->tearDown(); } } ================================================ FILE: lib/Wrench/Tests/ConnectionManagerTest.php ================================================ assertInstanceOfClass( $instance = $this->getInstance( $server, $options ), 'Valid constructor arguments' ); } /** * Tests the constructor */ public function testConstructor() { $this->assertInstanceOfClass( $instance = $this->getInstance( $this->getMockServer(), array() ), 'Constructor' ); return $instance; } /** * @depends testConstructor * @param ConnectionManager $instance */ public function testCount($instance) { $this->assertTrue(is_numeric($instance->count())); } /** * Data provider */ public function getValidConstructorArguments() { return array( array($this->getMockServer(), array()) ); } /** * Gets a mock server */ protected function getMockServer() { $server = $this->getMock('Wrench\Server', array(), array(), '', false); $server->registerApplication('/echo', $this->getMockApplication()); $server->expects($this->any()) ->method('getUri') ->will($this->returnValue('ws://localhost:8000/')); return $server; } /** * Gets a mock application * * @return EchoApplication */ protected function getMockApplication() { return new EchoApplication(); } } ================================================ FILE: lib/Wrench/Tests/ConnectionTest.php ================================================ assertInstanceOfClass( $instance = $this->getInstance( $manager, $socket, $options ), 'Valid constructor arguments' ); return $instance; } /** * @dataProvider getValidCloseCodes */ public function testClose($code) { $socket = $this->getMockSocket(); $socket->expects($this->any()) ->method('getIp') ->will($this->returnValue('127.0.0.1')); $socket->expects($this->any()) ->method('getPort') ->will($this->returnValue(mt_rand(1025, 50000))); $manager = $this->getMockConnectionManager(); $connection = $this->getInstance($manager, $socket); $connection->close($code); } /** * @dataProvider getValidHandshakeData */ public function testHandshake($path, $request) { $connection = $this->getConnectionForHandshake( $this->getConnectedSocket(), $path, $request ); $connection->handshake($request); $connection->onData('somedata'); $this->assertTrue($connection->send('someotherdata')); return $connection; } /** * @dataProvider getValidHandshakeData * @expectedException Wrench\Exception\HandshakeException */ public function testHandshakeBadSocket($path, $request) { $connection = $this->getConnectionForHandshake( $this->getNotConnectedSocket(), $path, $request ); $connection->handshake($request); } /** * Because expectation is that only $path application is available * * @dataProvider getWrongPathHandshakeData * @expectedException PHPUnit_Framework_ExpectationFailedException */ public function testWrongPathHandshake($path, $request) { $connection = $this->getConnectionForHandshake( $this->getConnectedSocket(), $path, $request ); $connection->handshake($request); } /** * @dataProvider getValidHandleData */ public function testHandle($path, $request_handshake, array $requests, array $counts) { $connection = $this->getConnectionForHandle( $this->getConnectedSocket(), $path, $request_handshake, $counts ); $connection->handshake($request_handshake); foreach ($requests as $request) { $connection->handle($request); } return $connection; } /** * @return Socket */ protected function getConnectedSocket() { $socket = $this->getMockSocket(); $socket->expects($this->any()) ->method('isConnected') ->will($this->returnValue(true)); return $socket; } /** * @return Socket */ protected function getNotConnectedSocket() { $socket = $this->getMockSocket(); $socket->expects($this->any()) ->method('isConnected') ->will($this->returnValue(false)); return $socket; } protected function getConnectionForHandshake($socket, $path, $request) { $manager = $this->getMockConnectionManager(); $application = $this->getMockApplication(); $server = $this->getMock('Wrench\Server', array(), array(), '', false); $server->registerApplication($path, $application); $manager->expects($this->any()) ->method('getApplicationForPath') ->with($path) ->will($this->returnValue($application)); $manager->expects($this->any()) ->method('getServer') ->will($this->returnValue($server)); $connection = $this->getInstance($manager, $socket); return $connection; } protected function getConnectionForHandle($socket, $path, $handshake, array $counts) { $connection = $this->getConnectionForHandshake($socket, $path, $handshake); $manager = $this->getMockConnectionManager(); $application = $this->getMockApplication(); $application->expects($this->exactly(isset($counts['onData']) ? $counts['onData'] : 0)) ->method('onData') ->will($this->returnValue(true)); $server = $this->getMock('Wrench\Server', array(), array(), '', false); $server->registerApplication($path, $application); $manager->expects($this->any()) ->method('getApplicationForPath') ->with($path) ->will($this->returnValue($application)); $manager->expects($this->exactly(isset($counts['removeConnection']) ? $counts['removeConnection'] : 0)) ->method('removeConnection'); $manager->expects($this->any()) ->method('getServer') ->will($this->returnValue($server)); $connection = $this->getInstance($manager, $socket); return $connection; } /** * @return ConnectionManager */ protected function getMockConnectionManager() { return $this->getMock('Wrench\ConnectionManager', array(), array(), '', false); } /** * Gets a mock socket * * @return Socket */ protected function getMockSocket() { return $this->getMock('Wrench\Socket\ServerClientSocket', array(), array(), '', false); } /** * Gets a mock application * * @return EchoApplication */ protected function getMockApplication() { return $this->getMock('Wrench\Application\EchoApplication'); } /** * Data provider * * @return array> */ public function getValidCloseCodes() { $arguments = array(); foreach (Protocol::$closeReasons as $code => $reason) { $arguments[] = array($code); } return $arguments; } /** * Data provider * * @return array> */ public function getValidConstructorArguments() { $socket = $this->getMockSocket(); $socket->expects($this->any()) ->method('getIp') ->will($this->returnValue('127.0.0.1')); $socket->expects($this->any()) ->method('getPort') ->will($this->returnValue(mt_rand(1025, 50000))); $manager = $this->getMockConnectionManager(); return array( array( $manager, $socket, array('logger' => function() {}) ), array( $manager, $socket, array('logger' => function () {}, 'connection_id_algo' => 'sha512') ) ); } /** * Data provider * * Uses this awkward valid request array so that splitting of payloads * across multiple calls to handle can be tested * * testHandle($path, $request_handshake, array $requests, array $counts) */ public function getValidHandleData() { $valid_requests = array( array( 'data' => array( "\x81\xad\x2e\xab\x82\xac\x6f\xfe\xd6\xe4\x14\x8b\xf9\x8c\x0c" ."\xde\xf1\xc9\x5c\xc5\xe3\xc1\x4b\x89\xb8\x8c\x0c\xcd\xed\xc3" ."\x0c\x87\xa2\x8e\x5e\xca\xf1\xdf\x59\xc4\xf0\xc8\x0c\x91\xa2" ."\x8e\x4c\xca\xf0\x8e\x53\x81\xad\xd4\xfd\x81\xfe\x95\xa8\xd5" ."\xb6\xee\xdd\xfa\xde\xf6\x88\xf2\x9b\xa6\x93\xe0\x93\xb1\xdf" ."\xbb\xde\xf6\x9b\xee\x91\xf6\xd1\xa1\xdc\xa4\x9c\xf2\x8d\xa3" ."\x92\xf3\x9a\xf6\xc7\xa1\xdc\xb6\x9c\xf3\xdc\xa9\x81\x80\x8e" ."\x12\xcd\x8e\x81\x8c\xf6\x8a\xf0\xee\x9a\xeb\x83\x9a\xd6\xe7" ."\x95\x9d\x85\xeb\x97\x8b" // Four text frames ), 'counts' => array( 'onData' => 4 ) ), array( 'data' => array( "\x88\x80\xdc\x8e\xa2\xc5" // Close frame ), 'counts' => array( 'removeConnection' => 1 ) ) ); $data = array(); $handshakes = $this->getValidHandshakeData(); foreach ($handshakes as $handshake) { foreach ($valid_requests as $handle_args) { $arguments = $handshake; $arguments[] = $handle_args['data']; $arguments[] = $handle_args['counts']; $data[] = $arguments; } } return $data; } /** * Data provider */ public function getValidHandshakeData() { return array( array( '/chat', "GET /chat HTTP/1.1\r Host: server.example.com\r Upgrade: websocket\r Connection: Upgrade\r Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r Origin: http://example.com\r Sec-WebSocket-Version: 13\r\n\r\n" ) ); } /** * Data provider */ public function getWrongPathHandshakeData() { return array( array( '/foobar', "GET /chat HTTP/1.1\r Host: server.example.com\r Upgrade: websocket\r Connection: Upgrade\r Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r Origin: http://example.com\r Sec-WebSocket-Version: 13\r\n\r\n" ), ); } } ================================================ FILE: lib/Wrench/Tests/Frame/BaseSubclassFrameTest.php ================================================ getFrameBuffer(); } protected function getClass() { return 'Wrench\Tests\Frame\BadSubclassFrame'; } } ================================================ FILE: lib/Wrench/Tests/Frame/FrameTest.php ================================================ frame = $this->getNewFrame(); } protected function getNewFrame() { $class = $this->getClass(); return new $class(); } /** * @see PHPUnit_Framework_TestCase::tearDown() */ protected function tearDown() { parent::tearDown(); unset($this->frame); } /** * @param string $payload * @dataProvider getValidEncodePayloads */ public function testBijection($type, $payload, $masked) { // Encode the payload $this->frame->encode($payload, $type, $masked); // Get the resulting buffer $buffer = $this->frame->getFrameBuffer(); $this->assertTrue((boolean)$buffer, 'Got raw frame buffer'); // And feed it back into a new frame $frame = $this->getNewFrame(); $frame->receiveData($buffer); // Check the properties of the new frame against the old, all match $this->assertEquals( $this->frame->getType(), $frame->getType(), 'Types match after encode -> receiveData' ); $this->assertEquals( $this->frame->getFramePayload(), $frame->getFramePayload(), 'Payloads match after encode -> receiveData' ); // Masking key should not be different, because we read the buffer in directly $this->assertEquals( $this->frame->getFrameBuffer(), $frame->getFrameBuffer(), 'Raw buffers match too' ); // This time, we create a new frame and read the data in with encode $frame = $this->getNewFrame(); $frame->encode($this->frame->getFramePayload(), $type, $masked); // These still match $this->assertEquals( $this->frame->getType(), $frame->getType(), 'Types match after encode -> receiveData -> encode' ); $this->assertEquals( $this->frame->getFramePayload(), $frame->getFramePayload(), 'Payloads match after encode -> receiveData -> encode' ); // But the masking key should be different, thus, so are the buffers if ($masked) { $this->assertNotEquals( $this->frame->getFrameBuffer(), $frame->getFrameBuffer(), 'Raw buffers don\'t match because of masking' ); } else { $this->assertEquals( $this->frame->getFramePayload(), $frame->getFramePayload(), 'Payloads match after encode -> receiveData -> encode' ); } } /** * @param string $payload * @dataProvider getValidEncodePayloads */ public function testEncodeTypeReflection($type, $payload, $masked) { $this->frame->encode($payload, $type); $this->assertEquals(Protocol::TYPE_TEXT, $this->frame->getType(), 'Encode retains type information'); } /** * @param string $payload * @dataProvider getValidEncodePayloads */ public function testEncodeLengthReflection($type, $payload, $masked) { $this->frame->encode($payload, $type); $this->assertEquals(strlen($payload), $this->frame->getLength(), 'Encode does not alter payload length'); } /** * @param string $payload * @dataProvider getValidEncodePayloads */ public function testEncodePayloadReflection($type, $payload, $masked) { $this->frame->encode($payload, $type, $masked); $this->assertEquals($payload, $this->frame->getFramePayload(), 'Encode retains payload information'); } /** * Data provider * * @return array */ public function getValidEncodePayloads() { return array( array( Protocol::TYPE_TEXT, "123456\x007890!@#$%^&*()qwe\trtyuiopQWERTYUIOPasdfghjklASFGH\n JKLzxcvbnmZXCVBNM,./<>?;[]{}-=_+\|'asdad0x11\aasdassasdasasdsd", true ), array( Protocol::TYPE_TEXT, pack('CCCCCCC', 0x00, 0x01, 0x02, 0x03, 0x04, 0xff, 0xf0), true ), array(Protocol::TYPE_TEXT, ' ', true) ); } } ================================================ FILE: lib/Wrench/Tests/Frame/HybiFrameTest.php ================================================ getMock('Wrench\Server', array(), array(), '', false); $instance->listen($server); } abstract public function testConstructor(); } ================================================ FILE: lib/Wrench/Tests/Listener/OriginPolicyTest.php ================================================ getInstance(array()); $this->assertInstanceOfClass($instance, 'No constructor arguments'); return $instance; } /** * @dataProvider getValidArguments * @param array $allowed * @param string $domain */ public function testValidAllowed($allowed, $domain) { $instance = $this->getInstance($allowed); $this->assertTrue($instance->isAllowed($domain)); } /** * @dataProvider getValidArguments * @param array $allowed * @param string $domain */ public function testValidHandshake($allowed, $domain) { $instance = $this->getInstance($allowed); $connection = $this->getMock('Wrench\Connection', array(), array(), '', false); $connection ->expects($this->never()) ->method('close'); $instance->onHandshakeRequest($connection, '/', $domain, 'abc', array()); } /** * @dataProvider getInvalidArguments * @param array $allowed * @param string $bad_domain */ public function testInvalidAllowed($allowed, $bad_domain) { $instance = $this->getInstance($allowed); $this->assertFalse($instance->isAllowed($bad_domain)); } /** * @dataProvider getInvalidArguments * @param array $allowed * @param string $domain */ public function testInvalidHandshake($allowed, $bad_domain) { $instance = $this->getInstance($allowed); $connection = $this->getMock('Wrench\Connection', array(), array(), '', false); $connection ->expects($this->once()) ->method('close'); $instance->onHandshakeRequest($connection, '/', $bad_domain, 'abc', array()); } /** * Data provider */ public function getValidArguments() { return array( array(array('localhost'), 'http://localhost'), array(array('foobar.com'), 'https://foobar.com'), array(array('https://foobar.com'), 'https://foobar.com') ); } /** * Data provider */ public function getInvalidArguments() { return array( array(array('localhost'), 'localdomain'), array(array('foobar.com'), 'foobar.org'), array(array('https://foobar.com'), 'http://foobar.com'), array(array('http://foobar.com'), 'foobar.com') ); } } ================================================ FILE: lib/Wrench/Tests/Listener/RateLimiterTest.php ================================================ getInstance(); $this->assertInstanceOfClass($instance, 'No constructor arguments'); return $instance; } public function testOnSocketConnect() { $this->getInstance()->onSocketConnect(null, $this->getConnection()); } public function testOnSocketDisconnect() { $this->getInstance()->onSocketDisconnect(null, $this->getConnection()); } public function testOnClientData() { $this->getInstance()->onClientData(null, $this->getConnection()); } protected function getConnection() { $connection = $this->getMock('Wrench\Connection', array(), array(), '', false); $connection ->expects($this->any()) ->method('getIp') ->will($this->returnValue('127.0.0.1')); $connection ->expects($this->any()) ->method('getId') ->will($this->returnValue('abcdef01234567890')); $manager = $this->getMock('Wrench\ConnectionManager', array(), array(), '', false); $manager->expects($this->any())->method('count')->will($this->returnValue(5)); $connection ->expects($this->any()) ->method('getConnectionManager') ->will($this->returnValue($manager)); return $connection; } } ================================================ FILE: lib/Wrench/Tests/Payload/HybiPayloadTest.php ================================================ payload = $this->getInstance(); } /** * Tests the constructor */ public function testConstructor() { $this->assertInstanceOfClass($this->getInstance()); } /** * @param string $payload * @dataProvider getValidEncodePayloads */ public function testBijection($type, $payload) { // Encode the payload $this->payload->encode($payload, $type); // Create a new payload and read the data in with encode $payload = $this->getInstance(); $payload->encode($this->payload->getPayload(), $type); // These still match $this->assertEquals( $this->payload->getType(), $payload->getType(), 'Types match after encode -> receiveData' ); $this->assertEquals( $this->payload->getPayload(), $payload->getPayload(), 'Payloads match after encode -> receiveData' ); } /** * @param string $payload * @dataProvider getValidEncodePayloads */ public function testEncodeTypeReflection($type, $payload) { $this->payload->encode($payload, Protocol::TYPE_TEXT); $this->assertEquals(Protocol::TYPE_TEXT, $this->payload->getType(), 'Encode retains type information'); } /** * @param string $payload * @dataProvider getValidEncodePayloads */ public function testEncodePayloadReflection($type, $payload) { $this->payload->encode($payload, Protocol::TYPE_TEXT); $this->assertEquals($payload, $this->payload->getPayload(), 'Encode retains payload information'); } /** * Tests sending to a socket * @dataProvider getValidEncodePayloads */ public function testSendToSocket($type, $payload) { $successfulSocket = $this->getMock('Wrench\Socket\ClientSocket', array(), array('wss://localhost:8000')); $failedSocket = clone $successfulSocket; $successfulSocket->expects($this->any()) ->method('send') ->will($this->returnValue(true)); $failedSocket->expects($this->any()) ->method('send') ->will($this->returnValue(false)); $this->payload->encode($payload, $type); $this->assertTrue($this->payload->sendToSocket($successfulSocket)); $this->assertFalse($this->payload->sendToSocket($failedSocket)); } /** * Tests receiving data * @dataProvider getValidEncodePayloads */ public function testReceieveData($type, $payload) { $payload = $this->getInstance(); $payload->receiveData($payload); } /** * Data provider * * @return array */ public function getValidEncodePayloads() { return array( array( Protocol::TYPE_TEXT, "123456\x007890!@#$%^&*()qwe\trtyuiopQWERTYUIOPasdfghjklASFGH\n JKLzxcvbnmZXCVBNM,./<>?;[]{}-=_+\|'asdad0x11\aasdassasdasasdsd" ), array( Protocol::TYPE_TEXT, pack('CCCCCCC', 0x00, 0x01, 0x02, 0x03, 0x04, 0xff, 0xf0) ), array(Protocol::TYPE_TEXT, ' ') ); } } ================================================ FILE: lib/Wrench/Tests/Protocol/ProtocolTest.php ================================================ getInstance()->validateRequestHandshake($request); $this->assertEquals('/chat', $path); $this->assertEquals('http://example.com', $origin); $this->assertEquals('dGhlIHNhbXBsZSBub25jZQ==', $key); $this->assertTrue(is_array($extensions), 'Extensions returned as array'); $this->assertEquals(array('x-test', 'x-test2'), $extensions, 'Extensions match'); $this->assertEquals('chat, superchat', $protocol); } catch (Exception $e) { $this->fail($e); } } /** * @dataProvider getValidHandshakeResponses */ public function testValidateHandshakeResponseValid($response, $key) { try { $valid = $this->getInstance()->validateResponseHandshake($response, $key); $this->assertTrue(is_bool($valid), 'Validation return value is boolean'); $this->assertTrue($valid, 'Handshake response validates'); } catch (Exception $e) { $this->fail('Validated valid response handshake as invalid'); } } /** * @dataProvider getValidHandshakeResponses */ public function testGetResponseHandsake($unused, $key) { try { $response = $this->getInstance()->getResponseHandshake($key); $this->assertHttpResponse($response); } catch (Exception $e) { $this->fail('Unable to get handshake response: ' . $e); } } /** * Asserts the string response is an HTTP response * * @param string $response */ protected function assertHttpResponse($response, $message = '') { $this->assertStringStartsWith('HTTP', $response, $message . ' - response starts well'); $this->assertStringEndsWith("\r\n", $response, $message . ' - response ends well'); } public function testGetVersion() { $version = $this->getInstance()->getVersion(); $this->assertTrue(is_int($version)); } public function testGetResponseError() { $response = $this->getInstance()->getResponseError(400); $this->assertHttpResponse($response, 'Code as int'); $response = $this->getInstance()->getResponseError(new Exception('Some message', 500)); $this->assertHttpResponse($response, 'Code in Exception'); $response = $this->getInstance()->getResponseError(888); $this->assertHttpResponse($response, 'Invalid code produces unimplemented response'); } /** * @dataProvider getValidOriginUris */ public function testValidateOriginUriValid($uri) { try { $this->getInstance()->validateOriginUri($uri); } catch (\Exception $e) { $this->fail('Valid URI validated as invalid: ' . $e); } } /** * @dataProvider getInvalidOriginUris * @expectedException InvalidArgumentException */ public function testValidateOriginUriInvalid($uri) { $this->getInstance()->validateOriginUri($uri); } public function getValidOriginUris() { return array( array('http://www.example.org'), array('http://www.example.com/some/page'), array('https://localhost/') ); } public function getInvalidOriginUris() { return array( array(false), array(true), array(''), array('blah') ); } public function getValidHandshakeRequests() { $cases = array(); $cases[] = array("GET /chat HTTP/1.1\r Host: server.example.com\r Upgrade: websocket\r Connection: Upgrade\r Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r Origin: http://example.com\r Sec-WebSocket-Extensions: x-test\r Sec-WebSocket-Extensions: x-test2\r Sec-WebSocket-Protocol: chat, superchat\r Sec-WebSocket-Version: 13\r \r\n"); $cases[] = array("GET /chat HTTP/1.1\r Host: server.example.com\r Upgrade: Websocket\r Connection: Upgrade\r Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r Origin: http://example.com\r Sec-WebSocket-Extensions: x-test\r Sec-WebSocket-Extensions: x-test2\r Sec-WebSocket-Protocol: chat, superchat\r Sec-WebSocket-Version: 13\r \r\n"); return $cases; } public function getValidHandshakeResponses() { $cases = array(); for ($i = 10; $i > 0; $i--) { $key = sha1(time() . uniqid('', true)); $response = "Sec-WebSocket-Accept: " . base64_encode(sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true)) . "\r\n\r\n"; $cases[] = array($response, $key); } return $cases; } } ================================================ FILE: lib/Wrench/Tests/Protocol/Rfc6455ProtocolTest.php ================================================ assertInstanceOfClass( $this->getInstance($url, $options), 'Valid constructor arguments' ); } /** * Tests logging */ public function testLogging() { $test = $this; $logged = false; $server = $this->getInstance('ws://localhost:8000', array( 'logger' => function ($message, $priority) use ($test, &$logged) { $test->assertTrue(is_string($message), 'Log had a string message'); $test->assertTrue(is_string($priority), 'Log had a string priority'); $logged = true; } )); $this->assertTrue($logged, 'The log callback was hit'); } /** * Data provider * * @return array> */ public function getValidConstructorArguments() { return array( array( 'ws://localhost:8000', array('logger' => array($this, 'log')) ), array( 'ws://localhost', array('logger' => array($this, 'log')) ) ); } } ================================================ FILE: lib/Wrench/Tests/ServerTestHelper.php ================================================ tearDown(); } /** * @return string */ public function getConnectionString() { return 'ws://localhost:' . $this->port; } /** * @return string */ public function getEchoConnectionString() { return $this->getConnectionString() . '/echo'; } /** * Sets up the server process and sleeps for a few seconds while * it wakes up */ public function setUp() { $this->port = self::getNextPort(); $this->process = proc_open( $this->getCommand(), array( 0 => array('file', '/dev/null', 'r'), 1 => array('file', __DIR__ . '/../../../build/server.log', 'a+'), 2 => array('file', __DIR__ . '/../../../build/server.err.log', 'a+') ), $this->pipes, __DIR__ . '../' ); sleep(3); } /** * Tears down the server process * * This method *must* be called */ public function tearDown() { if ($this->process) { foreach ($this->pipes as &$pipe) { fclose($pipe); } $this->pipes = null; // Sigh $status = proc_get_status($this->process); if ($status && isset($status['pid']) && $status['pid']) { // More sigh, this is the pid of the parent sh process, we want // to terminate the server directly $this->log('Command: /bin/ps -ao pid,ppid | /usr/bin/col | /usr/bin/tail -n +2 | /bin/grep \' ' . $status['pid'] . "'", 'info'); exec('/bin/ps -ao pid,ppid | /usr/bin/col | /usr/bin/tail -n +2 | /bin/grep \' ' . $status['pid'] . "'", $processes, $return); if ($return === 0) { foreach ($processes as $process) { list($pid, $ppid) = explode(' ', str_replace(' ', ' ', $process)); if ($pid) { $this->log('Killing ' . $pid, 'info'); exec('/bin/kill ' . $pid . ' > /dev/null 2>&1'); } } } else { $this->log('Unable to find child processes', 'warning'); } sleep(1); $this->log('Killing ' . $status['pid'], 'info'); exec('/bin/kill ' . $status['pid'] . ' > /dev/null 2>&1'); sleep(1); } proc_close($this->process); $this->process = null; } } /** * Gets the server command * * @return string */ protected function getCommand() { return sprintf('/usr/bin/env php %s/server.php %d', __DIR__, $this->port); } /** * Logs a message * * @param string $message * @param string $priority */ public function log($message, $priority = 'info') { //echo $message . "\n"; } } ================================================ FILE: lib/Wrench/Tests/Socket/ClientSocketTest.php ================================================ assertInstanceOfClass( new ClientSocket('ws://localhost/'), 'ws:// scheme, default port' ); $this->assertInstanceOfClass( new ClientSocket('ws://localhost/some-arbitrary-path'), 'with path' ); $this->assertInstanceOfClass( new ClientSocket('wss://localhost/test', array()), 'empty options' ); $this->assertInstanceOfClass( new ClientSocket('ws://localhost:8000/foo'), 'specified port' ); return $instance; } public function testOptions() { $socket = null; $this->assertInstanceOfClass( $socket = new ClientSocket( 'ws://localhost:8000/foo', array( 'timeout_connect' => 10 ) ), 'connect timeout' ); $this->assertInstanceOfClass( $socket = new ClientSocket( 'ws://localhost:8000/foo', array( 'timeout_socket' => 10 ) ), 'socket timeout' ); $this->assertInstanceOfClass( $socket = new ClientSocket( 'ws://localhost:8000/foo', array( 'protocol' => new Rfc6455Protocol() ) ), 'protocol' ); } /** * @expectedException InvalidArgumentException */ public function testProtocolTypeError() { $socket = new ClientSocket( 'ws://localhost:8000/foo', array( 'protocol' => new stdClass() ) ); } /** * @expectedException PHPUnit_Framework_Error */ public function testConstructorUriUnspecified() { $w = new ClientSocket(); } /** * @expectedException InvalidArgumentException */ public function testConstructorUriEmpty() { $w = new ClientSocket(null); } /** * @expectedException InvalidArgumentException */ public function testConstructorUriInvalid() { $w = new ClientSocket('Bad argument'); } /** * @depends testConstructor * @expectedException Wrench\Exception\SocketException */ public function testSendTooEarly($instance) { $instance->send('foo'); } /** * Test the connect, send, receive method */ public function testConnect() { try { $helper = new ServerTestHelper(); $helper->setUp(); $instance = $this->getInstance($helper->getConnectionString()); $success = $instance->connect(); $this->assertTrue($success, 'Client socket can connect to test server'); $sent = $instance->send("GET /echo HTTP/1.1\r Host: localhost\r Upgrade: websocket\r Connection: Upgrade\r Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r Origin: http://localhost\r Sec-WebSocket-Version: 13\r\n\r\n"); $this->assertNotEquals(false, $sent, 'Client socket can send to test server'); $response = $instance->receive(); $this->assertStringStartsWith('HTTP', $response, 'Response looks like HTTP handshake response'); } catch (\Exception $e) { $helper->tearDown(); throw $e; } $helper->tearDown(); } } ================================================ FILE: lib/Wrench/Tests/Socket/ServerClientSocketTest.php ================================================ getInstance($resource); $this->assertInstanceOfClass($instance); return $instance; } /** * @expectedException Wrench\Exception\SocketException * @depends testConstructor */ public function testGetIpTooSoon($instance) { $instance->getIp(); } /** * @expectedException Wrench\Exception\SocketException * @depends testConstructor */ public function testGetPortTooSoon($instance) { $instance->getPort(); } } ================================================ FILE: lib/Wrench/Tests/Socket/ServerSocketTest.php ================================================ isConnected(); $this->assertTrue(is_bool($connected), 'isConnected returns boolean'); $this->assertFalse($connected); } /** * @dataProvider getValidNames * @param string $name */ public function testGetNamePart($name, $ip, $port) { $this->assertEquals($ip, Socket::getNamePart($name, Socket::NAME_PART_IP), 'splits ip correctly'); $this->assertEquals($port, Socket::getNamePart($name, Socket::NAME_PART_PORT), 'splits port correctly'); } /** * Data provider */ public function getValidNames() { return array( array('127.0.0.1:52339', '127.0.0.1', '52339'), array('255.255.255.255:1025', '255.255.255.255', '1025'), array('::1:56670', '::1', '56670') ); } } ================================================ FILE: lib/Wrench/Tests/Socket/UriSocketTest.php ================================================ getInstance('ws://localhost:8000'); $this->assertInstanceOfClass($instance); return $instance; } /** * @dataProvider getInvalidConstructorArguments * @expectedException InvalidArgumentException */ public function testInvalidConstructor($uri) { $this->getInstance($uri); } /** * @depends testConstructor */ public function testGetIp($instance) { $this->assertStringStartsWith('localhost', $instance->getIp(), 'Correct host'); } /** * @depends testConstructor */ public function testGetPort($instance) { $this->assertEquals(8000, $instance->getPort(), 'Correct port'); } /** * Data provider */ public function getInvalidConstructorArguments() { return array( array(false), array('http://www.google.com/'), array('ws:///'), array(':::::'), ); } } ================================================ FILE: lib/Wrench/Tests/Test.php ================================================ assertInstanceOf( $this->getClass(), $instance, $message ); } /** * Gets an instance of the class under test * * @param mixed Normal constructor arguments * @magic This method accepts a variable number of arguments * @return object Of type given by getClass() */ public function getInstance(/* ... */) { $reflection = new ReflectionClass($this->getClass()); return $reflection->newInstanceArgs(func_get_args()); } /** * Logging function * * Passed into some classes under test as a callable * * @param string $message * @param string $priority * @return void */ public function log($message, $priority = 'info') { // nothing } } ================================================ FILE: lib/Wrench/Tests/bootstrap.php ================================================ register(); ================================================ FILE: lib/Wrench/Tests/server.php ================================================ register(); $server = new Wrench\Server('ws://localhost:' . $port); $server->registerApplication('echo', new Wrench\Application\EchoApplication()); $server->run(); ================================================ FILE: lib/Wrench/Util/Configurable.php ================================================ Wrench\Protocol object, latest protocol * version used if not specified */ public function __construct( array $options = array() ) { $this->configure($options); $this->configureProtocol(); } /** * Configures the options * * @param array $options */ protected function configure(array $options) { $this->options = array_merge(array( 'protocol' => new Rfc6455Protocol() ), $options); } /** * Configures the protocol option * * @throws InvalidArgumentException */ protected function configureProtocol() { $protocol = $this->options['protocol']; if (!$protocol || !($protocol instanceof Protocol)) { throw new InvalidArgumentException('Invalid protocol option'); } $this->protocol = $protocol; } } ================================================ FILE: lib/Wrench/Util/Ssl.php ================================================ $country_name, 'stateOrProvinceName' => $state_or_province_name, 'localityName' => $locality_name, 'organizationName' => $organization_name, 'organizationalUnitName' => $organizational_unit_name, 'commonName' => $common_name, 'emailAddress' => $email_address ); $privkey = openssl_pkey_new(); $cert = openssl_csr_new($dn, $privkey); $cert = openssl_csr_sign($cert, null, $privkey, 365); $pem = array(); openssl_x509_export($cert, $pem[0]); if ($pem_passphrase !== null) { openssl_pkey_export($privkey, $pem[1], $pem_passphrase); } $pem = implode($pem); file_put_contents($pem_file, $pem); } } ================================================ FILE: phpunit.xml ================================================ lib/Wrench/Tests lib/SplClassLoader.php