Build.PL100644001750001750 45513440704326 15261 0ustar00vtivti000000000000Protocol-WebSocket-0.26# ========================================================================= # THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. # DO NOT EDIT DIRECTLY. # ========================================================================= use 5.008_001; use strict; use Module::Build::Tiny 0.035; Build_PL(); Changes100644001750001750 1151613440704326 15320 0ustar00vtivti000000000000Protocol-WebSocket-0.26Revision history for perl module Protocol::WebSocket 0.26 2019-03-09T09:35:15Z - Introduce MAX_FRAGMENTS_AMOUNT to change the allowable amount of fragments (Daniel Kamil Kozar) - Update draft-ietf-hybi-00 tests to include the error text in response line (Daniel Kamil Kozar) 0.25 2019-03-09T09:31:45Z - The "Wrong response line" error now includes the actual response line (Max Maischein) 0.24 2018-01-13T09:59:07Z - draft-ietf-hybi-17 uses origin header, not sec-websocket-origin (Eric Wastl) 0.23 2017-12-12T19:16:32Z - revert previous release 0.22 2017-12-08T17:24:15Z - reliable UTF-8 bahaviour (Paul "LeoNerd" Evans #GH-35, #GH-52) - document masked frame attribute 0.21 2017-09-04T17:06:56Z - MAX_PAYLOAD_SIZE global var - MAX_MESSAGE_SIZE global var 0.20 2016-11-04T18:21:37Z - RSV bit (Anton Petrusevich) - do not modify passed headers structure (Graham Ollis) - bypass max payload size (Graham Ollis) 0.19 2015-09-28T16:55:01Z - custom headers in request - fix wrong UTF-8 related documentation (#GH-13) 0.18 2014-09-01T14:45:16Z - Digest::SHA1 -> Digest::SHA (Michal Špaček) 0.17 2014-04-09T08:12:01Z - perl 5.8 unpack fix (Graham Ollis) - doc and test fixes (Michal Špaček) 0.16 2014-01-07T20:33:59Z - just meta files update 0.15 2013-11-03T15:54:47Z - Explicitly stringify $payload in _mask() method (Toshio Ito) 0.14 2013-10-14T08:50:38Z - Fix Changes file (Neil Bowers) - Fix distribution (reported by Géraud CONTINSOUZAS) 0.13 2013-10-13T17:17:03Z - Implemented set cookies for requests (Joao Orui) The following changes made by Toshio Ito (debug-ito): - Add test in which Frame class generates and parses a series of fragments. - Add test for destructiveness of append() method. - Documentation: add remarks about destructiveness of append() method. Sometimes it's useful, the other times it's confusing. - Add documentation of "version" field in new() method. - Add documentation of "masked" field in new() method. - Remove fin() accessor from documentation and test. If a message is fragmented, the return value of fin() is meaningless anyway. Plus, probably current implementation does not handle fin flags if control frames are injected in non-control fragments. If we want fin() accessor, the test for that case is necessary. - Add documentation about "opcode" field in new() method and opcode() accessor method. tests are also added. - Add is_continuation() method. - Now "type" field for new() is immediately resolved to opcode, so that new(type => $type) and is_{type} methods are now consistent. This fixes github issue vti/protocol-websocket#19 ($frame->{type} field is preserved for backward-compatibility, but direct assignment $frame->{type} = "close" followed by $frame->to_bytes() will not work anymore. However, direct assignment is considered rude anyway...) - Add "continuation" frame type. opcode() method is revised so that it can correctly return 0 if $frame->{opcode} == 0. - POD update: ATTRIBUTES - type section is moved under new() method. This is way more straightforward. - Document "fin" field in the constructor and fin() accessor. to_bytes() now encodes "fin" bit according to the fin field of the Frame object. 0.12 2012-10-24 - Improved frame performance (Chuck Bredestege) - Check origin only if origin is present (Matthew Lien) 0.11 2012-08-28 - Added wsconsole - Fixed default version in WebSocket Frame - Fixed default ports in WebSocket URL 0.10 2012-08-21 - Fixed origin issue in hybi-17 - Added query string in resource name (Atomer Ju) - Added possibility to write headers before receiving a nonce (HAProxy) - Updated examples 0.00906 2011-10-30 - Support draft-ietf-hybi-17 0.00905 2011-10-03 - Fixed Firefox 7 Connection issue (reported by Rajstennaj Barrabas) - Fixed random mask generation - Fixed 64bit detection 0.00904 2011-08-15 - Fixed failing tests on perl-5.8.9 0.00903 2011-08-08 - Support for draft-ietf-hybi-10 WebSocket protocol version 0.00902 2011-05-12 - API CHANGE: Frame's to_string now returns a Perl string, use to_bytes to get a UTF-8 encoded octets - Automatically decode when building frame from a Perl string - Pay attention to SCRIPT_NAME in PSGI - If an origin was passed in, don't change it (Jon Gentle) 0.00901 2011-03-15 - Added X-Forwarded-Proto header support - Fixed SSL - Fixed POD - Separated unit tests from author tests 0.00900 2011-03-08 - Added Nginx::Engine example - Added possibility to read directly from IO::Handle - Added PSGI support 0.0004 2010-11-30 - Fixed message parsing - Fixed port - Refactored tests 0.0003 2010-11-28 - Added examples - Added subprotocol parsing and improved tests - Fixed handshake parsing and error passing - Improved docs - Improved tests - Made parser more relax - Made poll example more robust 0.0002 2010-11-26 - Initial release LICENSE100644001750001750 4402613440704326 15034 0ustar00vtivti000000000000Protocol-WebSocket-0.26This software is copyright (c) 2013 by Viacheslav Tykhanovskyi . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is copyright (c) 2013 by Viacheslav Tykhanovskyi . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is copyright (c) 2013 by Viacheslav Tykhanovskyi . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End MANIFEST.SKIP100644001750001750 12413440704326 15654 0ustar00vtivti000000000000Protocol-WebSocket-0.26^blib ^pm_to_blib .*\.old$ ^Makefile$ ^\.git .swp$ README.pod MANIFEST.bak .tar.gz$ README.md100644001750001750 633513440704326 15267 0ustar00vtivti000000000000Protocol-WebSocket-0.26# NAME Protocol::WebSocket - WebSocket protocol # SYNOPSIS # Server side my $hs = Protocol::WebSocket::Handshake::Server->new; $hs->parse('some data from the client'); $hs->is_done; # tells us when handshake is done my $frame = $hs->build_frame; $frame->append('some data from the client'); while (defined(my $message = $frame->next)) { if ($frame->is_close) { # Send close frame back send( $hs->build_frame( type => 'close', version => $version )->to_bytes ); return; } # We got a message! } # DESCRIPTION Client/server WebSocket message and frame parser/constructor. This module does not provide a WebSocket server or client, but is made for using in http servers or clients to provide WebSocket support. [Protocol::WebSocket](https://metacpan.org/pod/Protocol::WebSocket) supports the following WebSocket protocol versions: draft-ietf-hybi-17 (latest) draft-ietf-hybi-10 draft-ietf-hybi-00 (with HAProxy support) draft-hixie-75 By default the latest version is used. The WebSocket version is detected automatically on the server side. On the client side you have set a `version` attribute to an appropriate value. [Protocol::WebSocket](https://metacpan.org/pod/Protocol::WebSocket) itself does not contain any code and cannot be used directly. Instead the following modules should be used: ## High-level modules ### [Protocol::WebSocket::Server](https://metacpan.org/pod/Protocol::WebSocket::Server) Server helper class. ### [Protocol::WebSocket::Client](https://metacpan.org/pod/Protocol::WebSocket::Client) Client helper class. ## Low-level modules ### [Protocol::WebSocket::Handshake::Server](https://metacpan.org/pod/Protocol::WebSocket::Handshake::Server) Server handshake parser and constructor. ### [Protocol::WebSocket::Handshake::Client](https://metacpan.org/pod/Protocol::WebSocket::Handshake::Client) Client handshake parser and constructor. ### [Protocol::WebSocket::Frame](https://metacpan.org/pod/Protocol::WebSocket::Frame) WebSocket frame parser and constructor. ### [Protocol::WebSocket::Request](https://metacpan.org/pod/Protocol::WebSocket::Request) Low level WebSocket request parser and constructor. ### [Protocol::WebSocket::Response](https://metacpan.org/pod/Protocol::WebSocket::Response) Low level WebSocket response parser and constructor. ### [Protocol::WebSocket::URL](https://metacpan.org/pod/Protocol::WebSocket::URL) Low level WebSocket url parser and constructor. # EXAMPLES For examples on how to use [Protocol::WebSocket](https://metacpan.org/pod/Protocol::WebSocket) with various event loops see `examples/` directory in the distribution. # CREDITS In order of appearance: Paul "LeoNerd" Evans Jon Gentle Lee Aylward Chia-liang Kao Atomer Ju Chuck Bredestege Matthew Lien (BlueT) Joao Orui Toshio Ito (debug-ito) Neil Bowers Michal Špaček Graham Ollis Anton Petrusevich Eric Wastl # AUTHOR Viacheslav Tykhanovskyi, `vti@cpan.org`. # COPYRIGHT Copyright (C) 2010-2018, Viacheslav Tykhanovskyi. This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10. cpanfile100644001750001750 17013440704326 15463 0ustar00vtivti000000000000Protocol-WebSocket-0.26requires 'Carp' => 0; requires 'Digest::MD5' => 0; requires 'Digest::SHA' => 0; requires 'Scalar::Util' => 0; anyevent-http-server.pl100644001750001750 74113440704326 22250 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use AnyEvent::Impl::Perl; use AE; use AnyEvent::HTTP::Server; AnyEvent::HTTP::Server->new( host => '0.0.0.0', port => 3000, pid => '/tmp/wsecho.pid', request => sub { my $r = shift; $r->upgrade( websocket => sub { my $ws = shift; $ws->onmessage(sub { $ws->send(@_) }); } ); return 1; } )->start; AE::cv->recv; anyevent.pl100644001750001750 176413440704326 20015 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use AnyEvent::Socket; use AnyEvent::Handle; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; my $cv = AnyEvent->condvar; my $hdl; AnyEvent::Socket::tcp_server undef, 3000, sub { my ($clsock, $host, $port) = @_; my $hs = Protocol::WebSocket::Handshake::Server->new; my $frame = Protocol::WebSocket::Frame->new; $hdl = AnyEvent::Handle->new(fh => $clsock); $hdl->on_read( sub { my $hdl = shift; my $chunk = $hdl->{rbuf}; $hdl->{rbuf} = undef; if (!$hs->is_done) { $hs->parse($chunk); if ($hs->is_done) { $hdl->push_write($hs->to_string); return; } } $frame->append($chunk); while (my $message = $frame->next) { $hdl->push_write($frame->new($message)->to_bytes); } } ); }; $cv->wait; event.pl100644001750001750 123613440704326 17277 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use Event qw(time); require Event::io; use IO::Socket::INET; my $socket = IO::Socket::INET->new( LocalAddr => 'localhost', LocalPort => 3000, Listen => 1, Blocking => 0 ); $socket->blocking(0); Event->io( fd => $socket, timeout => 0.1, poll => "r", repeat => 1, cb => sub { my $e = shift; my $got = $e->got; if ($got eq "r") { sysread(STDIN, my $buf, 80); chop $buf; my $len = length($buf); Event::unloop if !$len; print "read[$len]:$buf:\n"; } } ); Event::loop; event_reactor.pl100644001750001750 167013440704326 21020 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use EventReactor; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; EventReactor->new( address => 'localhost', port => 3000, on_accept => sub { my ($self, $client) = @_; my $hs = Protocol::WebSocket::Handshake::Server->new; my $frame = Protocol::WebSocket::Frame->new; $client->on_read( sub { my ($client, $chunk) = @_; if (!$hs->is_done) { $hs->parse($chunk); if ($hs->is_done) { $client->write($hs->to_string); } return; } $frame->append($chunk); while (my $message = $frame->next) { $client->write($frame->new($message)->to_bytes); } } ); } )->listen->start; io-async.pl100644001750001750 262213440704326 17700 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; use IO::Socket::INET; use IO::Async::Listener; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $listener = IO::Async::Listener->new( on_stream => sub { my ($self, $stream) = @_; my $hs = Protocol::WebSocket::Handshake::Server->new; my $frame = Protocol::WebSocket::Frame->new; $stream->configure( on_read => sub { my ($self, $buffref, $closed) = @_; if (!$hs->is_done) { $hs->parse($$buffref); if ($hs->is_done) { $self->write($hs->to_string); } $$buffref = ""; return 0; } $frame->append($$buffref); while (my $message = $frame->next) { $self->write($frame->new($message)->to_bytes); } $$buffref = ""; return 0; } ); $loop->add($stream); } ); $loop->add($listener); my $socket = IO::Socket::INET->new( LocalAddr => 'localhost', LocalPort => 3000, Listen => 1, ); $listener->listen(handle => $socket); #$listener->listen(addr => ['localhost'], port => 3000, on_listen_error => sub {}); $loop->loop_forever; io-event.pl100644001750001750 172413440704326 17706 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use IO::Event; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; my $ioe = IO::Event::Socket::INET->new( LocalAddr => 'localhost', LocalPort => 3000, Listen => 1, Blocking => 0 ); IO::Event::loop; my $hs; my $frame; sub ie_connection { my ($handler, $ioe) = @_; $hs = Protocol::WebSocket::Handshake::Server->new; $frame = Protocol::WebSocket::Frame->new; $ioe->accept; } sub ie_input { my ($handler, $client, $input_buffer_reference) = @_; if (!$hs->is_done) { $hs->parse($$input_buffer_reference); if ($hs->is_done) { print $client $hs->to_string; } $$input_buffer_reference = ''; return; } $frame->append($$input_buffer_reference); while (my $message = $frame->next) { print $client $frame->new($message)->to_bytes; } $$input_buffer_reference = ''; return; } io-lamda.pl100644001750001750 246713440704326 17650 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use IO::Socket; use IO::Lambda qw(:all); use IO::Lambda::Socket qw(:all); use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; my $conn_timeout = 10; my $server = IO::Socket::INET->new( Listen => 5, LocalPort => 3000, Blocking => 0, ReuseAddr => 1, ) or die $!; my $serv = lambda { context $server; accept { my $conn = shift; again; $conn->blocking(0); my $hs = Protocol::WebSocket::Handshake::Server->new; my $frame = Protocol::WebSocket::Frame->new; my $buf = ''; context readbuf, $conn, \$buf, qr/^(.*)$/s, $conn_timeout; tail { my ($match, $error) = @_; return close($conn) unless defined $match; substr($buf, 0, length($match)) = ''; my $res = ''; if (!$hs->is_done) { $hs->parse($match); if ($hs->is_done) { $res = $hs->to_string; } $match = ''; } $frame->append($match); while (my $message = $frame->next) { $res .= $frame->new($message)->to_bytes; } again; $match = ''; context writebuf, $conn, \$res, length($res), 0, $conn_timeout; &tail(); }} }; $serv->wait; io-multiplex.pl100644001750001750 203113440704326 20600 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use IO::Socket; use IO::Multiplex; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; my $mux = new IO::Multiplex; my $sock = new IO::Socket::INET( Proto => 'tcp', LocalPort => 3000, Listen => 1 ) or die "socket: $@"; $mux->listen($sock); $mux->set_callback_object(__PACKAGE__); $mux->loop; my $hs; my $frame; sub mux_input { my $package = shift; my $mux = shift; my $fh = shift; my $input = shift; $hs ||= Protocol::WebSocket::Handshake::Server->new; $frame ||= Protocol::WebSocket::Frame->new; foreach my $c ($mux->handles) { if (!$hs->is_done) { $hs->parse($$input); if ($hs->is_done) { print $c $hs->to_string; } $$input = ''; return; } $frame->append($$input); while (my $message = $frame->next) { print $c $frame->new($message)->to_bytes; } } $$input = ''; } io-poll.pl100644001750001750 276713440704326 17543 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; BEGIN { use FindBin; use lib "$FindBin::Bin/../lib" } use IO::Socket::INET; use IO::Poll qw/POLLIN POLLOUT/; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; my $poll = IO::Poll->new; my $socket = IO::Socket::INET->new( Blocking => 0, LocalAddr => 'localhost', LocalPort => 3000, Proto => 'tcp', Type => SOCK_STREAM, Listen => 1 ); $socket->blocking(0); $socket->listen; my $client; while (1) { if ($client = $socket->accept) { $poll->mask($client => POLLIN | POLLOUT); last; } } my $hs = Protocol::WebSocket::Handshake::Server->new; my $frame = $hs->build_frame; my $buffer = ''; LOOP: while (1) { $poll->poll(0.1); foreach my $reader ($poll->handles(POLLIN)) { my $rs = $client->sysread(my $chunk, 1024); last LOOP unless $rs; if (!$hs->is_done) { unless (defined $hs->parse($chunk)) { warn "Error: " . $hs->error; last LOOP; } if ($hs->is_done) { $buffer .= $hs->to_string; } next; } $frame->append($chunk); while (defined(my $message = $frame->next)) { $buffer .= $frame->new($message)->to_bytes; } } foreach my $writer ($poll->handles(POLLOUT)) { next unless length $buffer; my $rs = $writer->syswrite($buffer); substr $buffer, 0, $rs, ''; } } net-server-multiplex.pl100644001750001750 172513440704326 22274 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl package SampleChatServer; use strict; use warnings; use base 'Net::Server::Multiplex'; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; __PACKAGE__->run; my $hs; my $frame; sub mux_connection { my $self = shift; my ($mux, $fh) = @_; my $peer = $self->{peeraddr}; $self->{id} = $self->{net_server}->{server}->{requests}; $self->{peerport} = $self->{net_server}->{server}->{peerport}; } sub mux_input { my $self = shift; my ($mux, $fh, $in_ref) = @_; $hs ||= Protocol::WebSocket::Handshake::Server->new; $frame ||= Protocol::WebSocket::Frame->new; if (!$hs->is_done) { $hs->parse($$in_ref); if ($hs->is_done) { print $fh $hs->to_string; } $$in_ref = ""; return 0; } $frame->append($$in_ref); while (my $message = $frame->next) { print $fh $frame->new($message)->to_bytes; } $$in_ref = ""; } nginx-engine.pl100644001750001750 314413440704326 20544 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use Nginx::Engine; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; ngxe_init("", 0, 64); ngxe_server( "*" => 3000 => sub { my $id = shift; my $hs = Protocol::WebSocket::Handshake::Server->new; my $frame = Protocol::WebSocket::Frame->new; ngxe_reader( $id => 0 => 5000 => sub { my ($id, $error, $recv, $send) = @_; return if $error; if (!$hs->is_done) { $hs->parse($recv); if (my $e = $hs->error) { warn "Websocket error '$e'"; return; } if ($hs->is_done) { $send .= $hs->to_string; } } else { $frame->append($recv); while (my $message = $frame->next) { $send .= $frame->new($message)->to_bytes; } } $_[2] = $recv; $_[3] = $send; # switching to writer ngxe_reader_stop($id); ngxe_writer_start($id); } ); ngxe_writer( $id => 0 => 1000 => "" => sub { my ($id, $error) = @_; return if $error; # switching back to reader ngxe_writer_stop($id); ngxe_reader_start($id); } ); ngxe_reader_start($id); } ); ngxe_loop; plack.psgi100644001750001750 161213440704326 17575 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use strict; use warnings; use lib 'lib'; use AnyEvent::Handle; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; my $psgi_app = sub { my $env = shift; my $fh = $env->{'psgix.io'} or return [500, [], []]; my $hs = Protocol::WebSocket::Handshake::Server->new_from_psgi($env); $hs->parse($fh) or return [400, [], [$hs->error]]; return sub { my $respond = shift; my $h = AnyEvent::Handle->new(fh => $fh); my $frame = Protocol::WebSocket::Frame->new; $h->push_write($hs->to_string); $h->on_eof(sub {}); $h->on_read( sub { $frame->append($_[0]->rbuf); while (my $message = $frame->next) { $h->push_write(Protocol::WebSocket::Frame->new($message)->to_bytes); } } ); }; }; $psgi_app; poe.pl100644001750001750 144413440704326 16742 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl use warnings; use strict; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; use POE qw(Component::Server::TCP); my $hs = Protocol::WebSocket::Handshake::Server->new; my $frame = Protocol::WebSocket::Frame->new; POE::Component::Server::TCP->new( Port => 3000, ClientFilter => 'POE::Filter::Stream', ClientInput => sub { my $chunk = $_[ARG0]; if (!$hs->is_done) { $hs->parse($chunk); if ($hs->is_done) { $_[HEAP]{client}->put($hs->to_string); } return; } $frame->append($chunk); while (my $message = $frame->next) { $_[HEAP]{client}->put($frame->new($message)->to_bytes); } } ); POE::Kernel->run; reflex.pl100644001750001750 270713440704326 17447 0ustar00vtivti000000000000Protocol-WebSocket-0.26/examples#!/usr/bin/env perl { package EchoStream; use Moose; extends 'Reflex::Stream'; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::Frame; has hs => ( is => 'ro', isa => 'Protocol::WebSocket::Handshake::Server', default => sub { Protocol::WebSocket::Handshake::Server->new() }, ); has frame => ( is => 'ro', isa => 'Protocol::WebSocket::Frame', default => sub { Protocol::WebSocket::Frame->new() }, ); sub on_data { my ($self, $args) = @_; my $hs = $self->hs; unless ($hs->is_done) { $hs->parse($args->{data}); $self->put($hs->to_string) if $hs->is_done; return; } my $frame = $self->frame; $frame->append($args->{data}); while (my $message = $frame->next) { $self->put($frame->new($message)->to_bytes); } } } { package TcpEchoServer; use Moose; extends 'Reflex::Acceptor'; use Reflex::Collection; has_many clients => (handles => {remember_client => "remember"}); sub on_accept { my ($self, $args) = @_; $self->remember_client( EchoStream->new(handle => $args->{socket}, rd => 1)); } } TcpEchoServer->new( listener => IO::Socket::INET->new( LocalAddr => '127.0.0.1', LocalPort => 3000, Listen => 5, Reuse => 1, ) )->run_all; WebSocket.pm100644001750001750 575213440704326 20605 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocolpackage Protocol::WebSocket; use strict; use warnings; our $VERSION = '0.26'; use Protocol::WebSocket::Frame; use Protocol::WebSocket::Handshake::Client; use Protocol::WebSocket::Handshake::Server; use Protocol::WebSocket::URL; 1; __END__ =encoding UTF-8 =head1 NAME Protocol::WebSocket - WebSocket protocol =head1 SYNOPSIS # Server side my $hs = Protocol::WebSocket::Handshake::Server->new; $hs->parse('some data from the client'); $hs->is_done; # tells us when handshake is done my $frame = $hs->build_frame; $frame->append('some data from the client'); while (defined(my $message = $frame->next)) { if ($frame->is_close) { # Send close frame back send( $hs->build_frame( type => 'close', version => $version )->to_bytes ); return; } # We got a message! } =head1 DESCRIPTION Client/server WebSocket message and frame parser/constructor. This module does not provide a WebSocket server or client, but is made for using in http servers or clients to provide WebSocket support. L supports the following WebSocket protocol versions: draft-ietf-hybi-17 (latest) draft-ietf-hybi-10 draft-ietf-hybi-00 (with HAProxy support) draft-hixie-75 By default the latest version is used. The WebSocket version is detected automatically on the server side. On the client side you have set a C attribute to an appropriate value. L itself does not contain any code and cannot be used directly. Instead the following modules should be used: =head2 High-level modules =head3 L Server helper class. =head3 L Client helper class. =head2 Low-level modules =head3 L Server handshake parser and constructor. =head3 L Client handshake parser and constructor. =head3 L WebSocket frame parser and constructor. =head3 L Low level WebSocket request parser and constructor. =head3 L Low level WebSocket response parser and constructor. =head3 L Low level WebSocket url parser and constructor. =head1 EXAMPLES For examples on how to use L with various event loops see C directory in the distribution. =head1 CREDITS In order of appearance: Paul "LeoNerd" Evans Jon Gentle Lee Aylward Chia-liang Kao Atomer Ju Chuck Bredestege Matthew Lien (BlueT) Joao Orui Toshio Ito (debug-ito) Neil Bowers Michal Špaček Graham Ollis Anton Petrusevich Eric Wastl =head1 AUTHOR Viacheslav Tykhanovskyi, C. =head1 COPYRIGHT Copyright (C) 2010-2018, Viacheslav Tykhanovskyi. This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10. =cut Client.pm100644001750001750 702713440704326 22020 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Client; use strict; use warnings; require Carp; use Protocol::WebSocket::URL; use Protocol::WebSocket::Handshake::Client; use Protocol::WebSocket::Frame; sub new { my $class = shift; $class = ref $class if ref $class; my (%params) = @_; my $self = {}; bless $self, $class; Carp::croak('url is required') unless $params{url}; $self->{url} = Protocol::WebSocket::URL->new->parse($params{url}) or Carp::croak("Can't parse url"); $self->{version} = $params{version}; $self->{on_connect} = $params{on_connect}; $self->{on_write} = $params{on_write}; $self->{on_frame} = $params{on_frame}; $self->{on_eof} = $params{on_eof}; $self->{on_error} = $params{on_error}; $self->{hs} = Protocol::WebSocket::Handshake::Client->new(url => $self->{url}); my %frame_buffer_params = ( max_fragments_amount => $params{max_fragments_amount} ); $frame_buffer_params{max_payload_size} = $params{max_payload_size} if exists $params{max_payload_size}; $self->{frame_buffer} = $self->_build_frame(%frame_buffer_params); return $self; } sub url { shift->{url} } sub version { shift->{version} } sub on { my $self = shift; my ($event, $cb) = @_; $self->{"on_$event"} = $cb; return $self; } sub read { my $self = shift; my ($buffer) = @_; my $hs = $self->{hs}; my $frame_buffer = $self->{frame_buffer}; unless ($hs->is_done) { if (!$hs->parse($buffer)) { $self->{on_error}->($self, $hs->error); return $self; } $self->{on_connect}->($self) if $self->{on_connect} && $hs->is_done; } if ($hs->is_done) { $frame_buffer->append($buffer); while (my $bytes = $frame_buffer->next) { $self->{on_read}->($self, $bytes); #$self->{on_frame}->($self, $bytes); } } return $self; } sub write { my $self = shift; my ($buffer) = @_; my $frame = ref $buffer ? $buffer : $self->_build_frame(masked => 1, buffer => $buffer); $self->{on_write}->($self, $frame->to_bytes); return $self; } sub connect { my $self = shift; my $hs = $self->{hs}; $self->{on_write}->($self, $hs->to_string); return $self; } sub disconnect { my $self = shift; my $frame = $self->_build_frame(type => 'close'); $self->{on_write}->($self, $frame->to_bytes); return $self; } sub _build_frame { my $self = shift; return Protocol::WebSocket::Frame->new(version => $self->{version}, @_); } 1; __END__ =head1 NAME Protocol::WebSocket::Client - WebSocket client =head1 SYNOPSIS my $sock = ...get non-blocking socket...; my $client = Protocol::WebSocket->new(url => 'ws://localhost:3000'); $client->on( write => sub { my $client = shift; my ($buf) = @_; syswrite $sock, $buf; } ); $client->on( read => sub { my $client = shift; my ($buf) = @_; ...do smth with read data... } ); # Sends a correct handshake header $client->connect; # Register on connect handler $client->on( connect => sub { $client->write('hi there'); } ); # Parses incoming data and on every frame calls on_read $client->read(...data from socket...); # Sends correct close header $client->disconnect; =head1 DESCRIPTION L is a convenient class for writing a WebSocket client. =cut Cookie.pm100644001750001750 321613440704326 22007 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Cookie; use strict; use warnings; sub new { my $class = shift; $class = ref $class if ref $class; my $self = {@_}; bless $self, $class; return $self; } sub pairs { @_ > 1 ? $_[0]->{pairs} = $_[1] : $_[0]->{pairs} } my $TOKEN = qr/[^;,\s"]+/; my $NAME = qr/[^;,\s"=]+/; my $QUOTED_STRING = qr/"(?:\\"|[^"])+"/; my $VALUE = qr/(?:$TOKEN|$QUOTED_STRING)/; sub parse { my $self = shift; my $string = shift; $self->{pairs} = []; return unless defined $string && $string ne ''; while ($string =~ m/\s*($NAME)\s*(?:=\s*($VALUE))?;?/g) { my ($attr, $value) = ($1, $2); if (defined $value) { $value =~ s/^"//; $value =~ s/"$//; $value =~ s/\\"/"/g; } push @{$self->{pairs}}, [$attr, $value]; } return $self; } sub to_string { my $self = shift; my $string = ''; my @pairs; foreach my $pair (@{$self->pairs}) { my $string = ''; $string .= $pair->[0]; if (defined $pair->[1]) { $string .= '='; $string .= $pair->[1] !~ m/^$VALUE$/ ? "\"$pair->[1]\"" : $pair->[1]; } push @pairs, $string; } return join '; ' => @pairs; } 1; __END__ =head1 NAME Protocol::WebSocket::Cookie - Base class for WebSocket cookies =head1 DESCRIPTION A base class for L and L. =head1 ATTRIBUTES =head2 C =head1 METHODS =head2 C Create a new L instance. =head2 C =head2 C =cut Request.pm100644001750001750 373213440704326 23442 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocket/Cookiepackage Protocol::WebSocket::Cookie::Request; use strict; use warnings; use base 'Protocol::WebSocket::Cookie'; sub parse { my $self = shift; $self->SUPER::parse(@_); my $cookies = []; my $version = 1; if ($self->pairs->[0] eq '$Version') { my $pair = shift @{$self->pairs}; $version = $pair->[1]; } my $cookie; foreach my $pair (@{$self->pairs}) { next unless defined $pair->[0]; if ($pair->[0] =~ m/^[^\$]/) { push @$cookies, $cookie if defined $cookie; $cookie = $self->_build_cookie( name => $pair->[0], value => $pair->[1], version => $version ); } elsif ($pair->[0] eq '$Path') { $cookie->path($pair->[1]); } elsif ($pair->[0] eq '$Domain') { $cookie->domain($pair->[1]); } } push @$cookies, $cookie if defined $cookie; return $cookies; } sub name { @_ > 1 ? $_[0]->{name} = $_[1] : $_[0]->{name} } sub value { @_ > 1 ? $_[0]->{value} = $_[1] : $_[0]->{value} } sub version { @_ > 1 ? $_[0]->{version} = $_[1] : $_[0]->{version} } sub path { @_ > 1 ? $_[0]->{path} = $_[1] : $_[0]->{path} } sub domain { @_ > 1 ? $_[0]->{domain} = $_[1] : $_[0]->{domain} } sub _build_cookie { shift; Protocol::WebSocket::Cookie::Request->new(@_) } 1; __END__ =head1 NAME Protocol::WebSocket::Cookie::Request - WebSocket Cookie Request =head1 SYNOPSIS # Constructor # Parser my $cookie = Protocol::WebSocket::Cookie::Request->new; $cookies = $cookie->parse( '$Version=1; foo="bar"; $Path=/; bar=baz; $Domain=.example.com'); =head1 DESCRIPTION Construct or parse a WebSocket request cookie. =head1 ATTRIBUTES =head2 C =head2 C =head2 C =head2 C =head2 C =head1 METHODS =head2 C Parse a WebSocket request cookie. =head2 C Construct a WebSocket request cookie. =cut Response.pm100644001750001750 335413440704326 23610 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocket/Cookiepackage Protocol::WebSocket::Cookie::Response; use strict; use warnings; use base 'Protocol::WebSocket::Cookie'; sub parse { my $self = shift; $self->SUPER::parse(@_); } sub to_string { my $self = shift; my $pairs = []; push @$pairs, [$self->{name}, $self->{value}]; push @$pairs, ['Comment', $self->{comment}] if defined $self->{comment}; push @$pairs, ['CommentURL', $self->{comment_url}] if defined $self->{comment_url}; push @$pairs, ['Discard'] if $self->{discard}; push @$pairs, ['Max-Age' => $self->{max_age}] if defined $self->{max_age}; push @$pairs, ['Path' => $self->{path}] if defined $self->{path}; if (defined $self->{portlist}) { $self->{portlist} = [$self->{portlist}] unless ref $self->{portlist} eq 'ARRAY'; my $list = join ' ' => @{$self->{portlist}}; push @$pairs, ['Port' => "\"$list\""]; } push @$pairs, ['Secure'] if $self->{secure}; push @$pairs, ['Version' => '1']; $self->pairs($pairs); return $self->SUPER::to_string; } 1; __END__ =head1 NAME Protocol::WebSocket::Cookie::Response - WebSocket Cookie Response =head1 SYNOPSIS # Constructor my $cookie = Protocol::WebSocket::Cookie::Response->new( name => 'foo', value => 'bar', discard => 1, max_age => 0 ); $cookie->to_string; # foo=bar; Discard; Max-Age=0; Version=1 # Parser my $cookie = Protocol::WebSocket::Cookie::Response->new; $cookie->parse('foo=bar; Discard; Max-Age=0; Version=1'); =head1 DESCRIPTION Construct or parse a WebSocket response cookie. =head1 METHODS =head2 C Parse a WebSocket response cookie. =head2 C Construct a WebSocket response cookie. =cut Frame.pm100644001750001750 2547213440704326 21660 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Frame; use strict; use warnings; use Config; use Encode (); use Scalar::Util 'readonly'; use constant MAX_RAND_INT => 2**32; use constant MATH_RANDOM_SECURE => eval "require Math::Random::Secure;"; our $MAX_PAYLOAD_SIZE = 65536; our $MAX_FRAGMENTS_AMOUNT = 128; our %TYPES = ( continuation => 0x00, text => 0x01, binary => 0x02, ping => 0x09, pong => 0x0a, close => 0x08 ); sub new { my $class = shift; $class = ref $class if ref $class; my $buffer; if (@_ == 1) { $buffer = shift @_; } else { my %args = @_; $buffer = delete $args{buffer}; } my $self = {@_}; bless $self, $class; $buffer = '' unless defined $buffer; if (Encode::is_utf8($buffer)) { $self->{buffer} = Encode::encode('UTF-8', $buffer); } else { $self->{buffer} = $buffer; } if (defined($self->{type}) && defined($TYPES{$self->{type}})) { $self->opcode($TYPES{$self->{type}}); } $self->{version} ||= 'draft-ietf-hybi-17'; $self->{fragments} = []; $self->{max_fragments_amount} ||= $MAX_FRAGMENTS_AMOUNT unless exists $self->{max_fragments_amount}; $self->{max_payload_size} ||= $MAX_PAYLOAD_SIZE unless exists $self->{max_payload_size}; return $self; } sub version { my $self = shift; return $self->{version}; } sub append { my $self = shift; return unless defined $_[0]; $self->{buffer} .= $_[0]; $_[0] = '' unless readonly $_[0]; return $self; } sub next { my $self = shift; my $bytes = $self->next_bytes; return unless defined $bytes; return Encode::decode('UTF-8', $bytes); } sub fin { @_ > 1 ? $_[0]->{fin} = $_[1] : defined($_[0]->{fin}) ? $_[0]->{fin} : 1; } sub rsv { @_ > 1 ? $_[0]->{rsv} = $_[1] : $_[0]->{rsv} } sub opcode { @_ > 1 ? $_[0]->{opcode} = $_[1] : defined($_[0]->{opcode}) ? $_[0]->{opcode} : 1; } sub masked { @_ > 1 ? $_[0]->{masked} = $_[1] : $_[0]->{masked} } sub is_ping { $_[0]->opcode == 9 } sub is_pong { $_[0]->opcode == 10 } sub is_close { $_[0]->opcode == 8 } sub is_continuation { $_[0]->opcode == 0 } sub is_text { $_[0]->opcode == 1 } sub is_binary { $_[0]->opcode == 2 } sub next_bytes { my $self = shift; if ( $self->version eq 'draft-hixie-75' || $self->version eq 'draft-ietf-hybi-00') { if ($self->{buffer} =~ s/^\xff\x00//) { $self->opcode(8); return ''; } return unless $self->{buffer} =~ s/^[^\x00]*\x00(.*?)\xff//s; return $1; } return unless length $self->{buffer} >= 2; while (length $self->{buffer}) { my $hdr = substr($self->{buffer}, 0, 1); my @bits = split //, unpack("B*", $hdr); $self->fin($bits[0]); $self->rsv([@bits[1 .. 3]]); my $opcode = unpack('C', $hdr) & 0b00001111; my $offset = 1; # FIN,RSV[1-3],OPCODE my $payload_len = unpack 'C', substr($self->{buffer}, 1, 1); my $masked = ($payload_len & 0b10000000) >> 7; $self->masked($masked); $offset += 1; # + MASKED,PAYLOAD_LEN $payload_len = $payload_len & 0b01111111; if ($payload_len == 126) { return unless length($self->{buffer}) >= $offset + 2; $payload_len = unpack 'n', substr($self->{buffer}, $offset, 2); $offset += 2; } elsif ($payload_len > 126) { return unless length($self->{buffer}) >= $offset + 4; my $bits = join '', map { unpack 'B*', $_ } split //, substr($self->{buffer}, $offset, 8); # Most significant bit must be 0. # And here is a crazy way of doing it %) $bits =~ s{^.}{0}; # Can we handle 64bit numbers? if ($Config{ivsize} <= 4 || $Config{longsize} < 8 || $] < 5.010) { $bits = substr($bits, 32); $payload_len = unpack 'N', pack 'B*', $bits; } else { $payload_len = unpack 'Q>', pack 'B*', $bits; } $offset += 8; } if ($self->{max_payload_size} && $payload_len > $self->{max_payload_size}) { $self->{buffer} = ''; die "Payload is too big. " . "Deny big message ($payload_len) " . "or increase max_payload_size ($self->{max_payload_size})"; } my $mask; if ($self->masked) { return unless length($self->{buffer}) >= $offset + 4; $mask = substr($self->{buffer}, $offset, 4); $offset += 4; } return if length($self->{buffer}) < $offset + $payload_len; my $payload = substr($self->{buffer}, $offset, $payload_len); if ($self->masked) { $payload = $self->_mask($payload, $mask); } substr($self->{buffer}, 0, $offset + $payload_len, ''); # Injected control frame if (@{$self->{fragments}} && $opcode & 0b1000) { $self->opcode($opcode); return $payload; } if ($self->fin) { if (@{$self->{fragments}}) { $self->opcode(shift @{$self->{fragments}}); } else { $self->opcode($opcode); } $payload = join '', @{$self->{fragments}}, $payload; $self->{fragments} = []; return $payload; } else { # Remember first fragment opcode if (!@{$self->{fragments}}) { push @{$self->{fragments}}, $opcode; } push @{$self->{fragments}}, $payload; die "Too many fragments" if @{$self->{fragments}} > $self->{max_fragments_amount}; } } return; } sub to_bytes { my $self = shift; if ( $self->version eq 'draft-hixie-75' || $self->version eq 'draft-ietf-hybi-00') { if ($self->{type} && $self->{type} eq 'close') { return "\xff\x00"; } return "\x00" . $self->{buffer} . "\xff"; } if ($self->{max_payload_size} && length $self->{buffer} > $self->{max_payload_size}) { die "Payload is too big. " . "Send shorter messages or increase max_payload_size"; } my $rsv_set = 0; if ( $self->{rsv} && ref( $self->{rsv} ) eq 'ARRAY' ) { for my $i ( 0 .. @{ $self->{rsv} } - 1 ) { $rsv_set += $self->{rsv}->[$i] * ( 1 << ( 6 - $i ) ); } } my $string = ''; my $opcode = $self->opcode; $string .= pack 'C', ($opcode | $rsv_set | ($self->fin ? 128 : 0)); my $payload_len = length($self->{buffer}); if ($payload_len <= 125) { $payload_len |= 0b10000000 if $self->masked; $string .= pack 'C', $payload_len; } elsif ($payload_len <= 0xffff) { $string .= pack 'C', 126 + ($self->masked ? 128 : 0); $string .= pack 'n', $payload_len; } else { $string .= pack 'C', 127 + ($self->masked ? 128 : 0); # Shifting by an amount >= to the system wordsize is undefined $string .= pack 'N', $Config{ivsize} <= 4 ? 0 : $payload_len >> 32; $string .= pack 'N', ($payload_len & 0xffffffff); } if ($self->masked) { my $mask = $self->{mask} || ( MATH_RANDOM_SECURE ? Math::Random::Secure::irand(MAX_RAND_INT) : int(rand(MAX_RAND_INT)) ); $mask = pack 'N', $mask; $string .= $mask; $string .= $self->_mask($self->{buffer}, $mask); } else { $string .= $self->{buffer}; } return $string; } sub to_string { my $self = shift; die 'DO NOT USE'; } sub _mask { my $self = shift; my ($payload, $mask) = @_; $mask = $mask x (int(length($payload) / 4) + 1); $mask = substr($mask, 0, length($payload)); $payload = "$payload" ^ $mask; return $payload; } sub max_payload_size { my $self = shift; return $self->{max_payload_size}; } 1; __END__ =head1 NAME Protocol::WebSocket::Frame - WebSocket Frame =head1 SYNOPSIS # Create frame my $frame = Protocol::WebSocket::Frame->new('123'); $frame->to_bytes; # Parse frames my $frame = Protocol::WebSocket::Frame->new; $frame->append(...); $f->next; # get next message $f->next; # get another next message =head1 DESCRIPTION Construct or parse a WebSocket frame. =head1 RANDOM MASK GENERATION By default built-in C is used, this is not secure, so when L is installed it is used instead. =head1 METHODS =head2 C Protocol::WebSocket::Frame->new('data'); # same as (buffer => 'data') Protocol::WebSocket::Frame->new(buffer => 'data', type => 'close'); Create a new L instance. Automatically detect if the passed data is a Perl string (UTF-8 flag) or bytes. When called with more than one arguments, it takes the following named arguments (all of them are optional). =over =item C => STR (default: C<"">) The payload of the frame. =item C => TYPE_STR (default: C<"text">) The type of the frame. Accepted values are: continuation text binary ping pong close =item C => INT (default: 1) The opcode of the frame. If C field is set to a valid string, this field is ignored. =item C => BOOL (default: 1) "fin" flag of the frame. "fin" flag must be 1 in the ending frame of fragments. =item C => BOOL (default: 0) If set to true, the frame will be masked. =item C => VERSION_STR (default: C<'draft-ietf-hybi-17'>) WebSocket protocol version string. See L for valid version strings. =back =head2 C Check if frame is of continuation type. =head2 C Check if frame is of text type. =head2 C Check if frame is of binary type. =head2 C Check if frame is a ping request. =head2 C Check if frame is a pong response. =head2 C Check if frame is of close type. =head2 C $opcode = $frame->opcode; $frame->opcode(8); Get/set opcode of the frame. =head2 C $masked = $frame->masked; $frame->masked(1); Get/set masking of the frame. =head2 C $frame->append($chunk); Append a frame chunk. Beware that this method is B. It makes C<$chunk> empty unless C<$chunk> is read-only. =head2 C $frame->append(...); $frame->next; # next message Return the next message as a Perl string (UTF-8 decoded). =head2 C Return the next message as is. =head2 C Construct a WebSocket message. =head2 C The maximum size of the payload. You may set this to C<0> or C to disable checking the payload size. =cut Handshake.pm100644001750001750 214113440704326 22460 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Handshake; use strict; use warnings; use Protocol::WebSocket::Request; use Protocol::WebSocket::Response; sub new { my $class = shift; $class = ref $class if ref $class; my $self = {@_}; bless $self, $class; return $self; } sub error { @_ > 1 ? $_[0]->{error} = $_[1] : $_[0]->{error} } sub version { $_[0]->req->version } sub req { shift->{req} ||= Protocol::WebSocket::Request->new } sub res { shift->{res} ||= Protocol::WebSocket::Response->new } 1; __END__ =head1 NAME Protocol::WebSocket::Handshake - Base WebSocket Handshake class =head1 DESCRIPTION This is a base class for L and L. =head1 ATTRIBUTES =head2 C $handshake->error; Set or get handshake error. =head2 C $handshake->version; Set or get handshake version. =head1 METHODS =head2 C Create a new L instance. =head2 C $handshake->req; WebSocket request object. =head2 C $handshake->res; WebSocket response object. =cut Client.pm100644001750001750 551013440704326 23701 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocket/Handshakepackage Protocol::WebSocket::Handshake::Client; use strict; use warnings; use base 'Protocol::WebSocket::Handshake'; require Carp; use Protocol::WebSocket::URL; use Protocol::WebSocket::Frame; sub new { my $self = shift->SUPER::new(@_); $self->_set_url($self->{url}) if defined $self->{url}; if (my $version = $self->{version}) { $self->req->version($version); $self->res->version($version); } return $self; } sub url { my $self = shift; my $url = shift; return $self->{url} unless $url; $self->_set_url($url); return $self; } sub parse { my $self = shift; my $req = $self->req; my $res = $self->res; unless ($res->is_done) { unless ($res->parse($_[0])) { $self->error($res->error); return; } if ($res->is_done) { if ( $req->version eq 'draft-ietf-hybi-00' && $req->checksum ne $res->checksum) { $self->error('Checksum is wrong.'); return; } } } return 1; } sub is_done { shift->res->is_done } sub to_string { shift->req->to_string } sub build_frame { my $self = shift; return Protocol::WebSocket::Frame->new(masked => 1, version => $self->version, @_); } sub _build_url { Protocol::WebSocket::URL->new } sub _set_url { my $self = shift; my $url = shift; $url = $self->_build_url->parse($url) unless ref $url; $self->req->secure(1) if $url->secure; my $req = $self->req; my $host = $url->host; $host .= ':' . $url->port if defined $url->port && ($url->secure ? $url->port ne '443' : $url->port ne '80'); $req->host($host); $req->resource_name($url->resource_name); return $self; } 1; __END__ =head1 NAME Protocol::WebSocket::Handshake::Client - WebSocket Client Handshake =head1 SYNOPSIS my $h = Protocol::WebSocket::Handshake::Client->new(url => 'ws://example.com'); # Create request $h->to_string; # Parse server response $h->parse(<<"EOF"); WebSocket HTTP message EOF $h->error; # Check if there were any errors $h->is_done; # Returns 1 =head1 DESCRIPTION Construct or parse a client WebSocket handshake. This module is written for convenience, since using request and response directly requires the same code again and again. =head1 ATTRIBUTES =head2 C $handshake->url('ws://example.com/demo'); Set or get WebSocket url. =head1 METHODS =head2 C Create a new L instance. =head2 C $handshake->parse($buffer); Parse a WebSocket server response. Returns C and sets C attribute on error. Buffer is modified. =head2 C Construct a WebSocket client request. =head2 C Check whether handshake is done. =cut Server.pm100644001750001750 640713440704326 23737 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocket/Handshakepackage Protocol::WebSocket::Handshake::Server; use strict; use warnings; use base 'Protocol::WebSocket::Handshake'; use Protocol::WebSocket::Request; use Protocol::WebSocket::Frame; sub new_from_psgi { my $class = shift; my $req = Protocol::WebSocket::Request->new_from_psgi(@_); my $self = $class->new(req => $req); return $self; } sub parse { my $self = shift; my $req = $self->req; my $res = $self->res; return 1 if $req->is_done; unless ($req->parse($_[0])) { $self->error($req->error); return; } if ($req->is_body || $req->is_done) { $res->version($req->version); $res->host($req->host); $res->secure($req->secure); $res->resource_name($req->resource_name); $res->origin($req->origin); } if ($req->version eq 'draft-ietf-hybi-00') { if ($self->is_done) { $res->checksum(undef); $res->number1($req->number1); $res->number2($req->number2); $res->challenge($req->challenge); } else { $res->checksum(''); } } elsif ($self->is_done && $req->version eq 'draft-ietf-hybi-10' || $req->version eq 'draft-ietf-hybi-17') { $res->key($req->key); } return 1; } sub is_body { shift->req->is_body } sub is_done { shift->req->is_done } sub to_string { my $self = shift; if ($self->is_body) { return $self->{partial} = $self->res->to_string; } elsif ($self->{partial}) { my $to_string = $self->res->to_string; $to_string =~ s/^\Q$self->{partial}\E//; return $to_string; } return $self->res->to_string; } sub build_frame { my $self = shift; return Protocol::WebSocket::Frame->new(version => $self->version, @_); } 1; __END__ =head1 NAME Protocol::WebSocket::Handshake::Server - WebSocket Server Handshake =head1 SYNOPSIS my $h = Protocol::WebSocket::Handshake::Server->new; # Parse client request $h->parse(<<"EOF"); WebSocket HTTP message EOF $h->error; # Check if there were any errors $h->is_done; # Returns 1 # Create response $h->to_string; =head1 DESCRIPTION Construct or parse a server WebSocket handshake. This module is written for convenience, since using request and response directly requires the same code again and again. =head1 METHODS =head2 C Create a new L instance. =head2 C my $env = { HTTP_HOST => 'example.com', HTTP_CONNECTION => 'Upgrade', ... }; my $handshake = Protocol::WebSocket::Handshake::Server->new_from_psgi($env); Create a new L instance from L environment. =head2 C $handshake->parse($buffer); $handshake->parse($handle); Parse a WebSocket client request. Returns C and sets C attribute on error. When buffer is passed it's modified (unless readonly). =head2 C $handshake->build_frame; Builds L with an appropriate version. =head2 C Construct a WebSocket server response. =head2 C Check whether handshake is in body state. =head2 C Check whether handshake is done. =cut Message.pm100644001750001750 1122513440704326 22201 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Message; use strict; use warnings; use base 'Protocol::WebSocket::Stateful'; use Scalar::Util qw(readonly); require Digest::MD5; our $MAX_MESSAGE_SIZE = 10 * 2048; sub new { my $class = shift; $class = ref $class if ref $class; my $self = {@_}; bless $self, $class; $self->{version} ||= ''; $self->{buffer} = ''; $self->{fields} ||= {}; $self->{max_message_size} ||= $MAX_MESSAGE_SIZE; $self->{cookies} ||= []; $self->state('first_line'); return $self; } sub secure { @_ > 1 ? $_[0]->{secure} = $_[1] : $_[0]->{secure} } sub fields { shift->{fields} } sub field { my $self = shift; my $name = lc shift; return $self->fields->{$name} unless @_; $self->fields->{$name} = $_[0]; return $self; } sub error { my $self = shift; return $self->{error} unless @_; my $error = shift; $self->{error} = $error; $self->state('error'); return $self; } sub subprotocol { @_ > 1 ? $_[0]->{subprotocol} = $_[1] : $_[0]->{subprotocol}; } sub host { @_ > 1 ? $_[0]->{host} = $_[1] : $_[0]->{host} } sub origin { @_ > 1 ? $_[0]->{origin} = $_[1] : $_[0]->{origin} } sub version { @_ > 1 ? $_[0]->{version} = $_[1] : $_[0]->{version} } sub number1 { @_ > 1 ? $_[0]->{number1} = $_[1] : $_[0]->{number1} } sub number2 { @_ > 1 ? $_[0]->{number2} = $_[1] : $_[0]->{number2} } sub challenge { @_ > 1 ? $_[0]->{challenge} = $_[1] : $_[0]->{challenge} } sub checksum { my $self = shift; if (@_) { $self->{checksum} = $_[0]; return $self; } return $self->{checksum} if defined $self->{checksum}; Carp::croak(qq/number1 is required/) unless defined $self->number1; Carp::croak(qq/number2 is required/) unless defined $self->number2; Carp::croak(qq/challenge is required/) unless defined $self->challenge; my $checksum = ''; $checksum .= pack 'N' => $self->number1; $checksum .= pack 'N' => $self->number2; $checksum .= $self->challenge; $checksum = Digest::MD5::md5($checksum); return $self->{checksum} ||= $checksum; } sub parse { my $self = shift; return 1 unless defined $_[0]; return if $self->error; return unless $self->_append(@_); while (!$self->is_state('body') && defined(my $line = $self->_get_line)) { if ($self->state eq 'first_line') { return unless defined $self->_parse_first_line($line); $self->state('fields'); } elsif ($line ne '') { return unless defined $self->_parse_field($line); } else { $self->state('body'); last; } } return 1 unless $self->is_state('body'); my $rv = $self->_parse_body; return unless defined $rv; # Need more data return $rv unless ref $rv; $_[0] = $self->{buffer} unless readonly $_[0] || ref $_[0]; return $self->done; } sub _extract_number { my $self = shift; my $key = shift; my $number = join '' => $key =~ m/\d+/g; my $spaces = $key =~ s/ / /g; return if $spaces == 0; return int($number / $spaces); } sub _append { my $self = shift; return if $self->error; if (ref $_[0]) { $_[0]->read(my $buf, $self->{max_message_size}); $self->{buffer} .= $buf; } else { $self->{buffer} .= $_[0]; $_[0] = '' unless readonly $_[0]; } if (length $self->{buffer} > $self->{max_message_size}) { $self->error('Message is too long'); return; } return $self; } sub _get_line { my $self = shift; if ($self->{buffer} =~ s/^(.*?)\x0d?\x0a//) { return $1; } return; } sub _parse_first_line {shift} sub _parse_field { my $self = shift; my $line = shift; my ($name, $value) = split /:\s*/ => $line => 2; unless (defined $name && defined $value) { $self->error('Invalid field'); return; } #$name =~ s/^Sec-WebSocket-Origin$/Origin/i; # FIXME $self->field($name => $value); if ($name =~ m/^x-forwarded-proto$/i) { $self->secure(1); } return $self; } sub _parse_body {shift} 1; __END__ =head1 NAME Protocol::WebSocket::Message - Base class for WebSocket request and response =head1 DESCRIPTION A base class for L and L. =head1 ATTRIBUTES =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head1 METHODS =head2 C Create a new L instance. =head2 C =head2 C =cut Request.pm100644001750001750 3171013440704326 22246 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Request; use strict; use warnings; use base 'Protocol::WebSocket::Message'; require Carp; use MIME::Base64 (); use Protocol::WebSocket::Cookie::Request; sub new { my $self = shift->SUPER::new(@_); my (%params) = @_; $self->{headers} = $params{headers} || []; return $self; } sub new_from_psgi { my $class = shift; my $env = @_ > 1 ? {@_} : shift; Carp::croak('env is required') unless keys %$env; my $version = ''; my $cookies; my $fields = { upgrade => $env->{HTTP_UPGRADE}, connection => $env->{HTTP_CONNECTION}, host => $env->{HTTP_HOST}, }; if ($env->{HTTP_WEBSOCKET_PROTOCOL}) { $fields->{'websocket-protocol'} = $env->{HTTP_WEBSOCKET_PROTOCOL}; } elsif ($env->{HTTP_SEC_WEBSOCKET_PROTOCOL}) { $fields->{'sec-websocket-protocol'} = $env->{HTTP_SEC_WEBSOCKET_PROTOCOL}; } if (exists $env->{HTTP_SEC_WEBSOCKET_VERSION}) { $fields->{'sec-websocket-version'} = $env->{HTTP_SEC_WEBSOCKET_VERSION}; if ($env->{HTTP_SEC_WEBSOCKET_VERSION} eq '13') { $version = 'draft-ietf-hybi-17'; } else { $version = 'draft-ietf-hybi-10'; } } if ($env->{HTTP_SEC_WEBSOCKET_KEY}) { $fields->{'sec-websocket-key'} = $env->{HTTP_SEC_WEBSOCKET_KEY}; } elsif ($env->{HTTP_SEC_WEBSOCKET_KEY1}) { $version = 'draft-ietf-hybi-00'; $fields->{'sec-websocket-key1'} = $env->{HTTP_SEC_WEBSOCKET_KEY1}; $fields->{'sec-websocket-key2'} = $env->{HTTP_SEC_WEBSOCKET_KEY2}; } if ($version eq 'draft-ietf-hybi-10') { $fields->{'sec-websocket-origin'} = $env->{HTTP_SEC_WEBSOCKET_ORIGIN}; } else { $fields->{origin} = $env->{HTTP_ORIGIN}; } if ($env->{HTTP_COOKIE}) { $cookies = Protocol::WebSocket::Cookie->new->parse($env->{HTTP_COOKIE}); } my $self = $class->new( version => $version, fields => $fields, cookies => $cookies, resource_name => "$env->{SCRIPT_NAME}$env->{PATH_INFO}" . ($env->{QUERY_STRING} ? "?$env->{QUERY_STRING}" : "") ); $self->state('body'); if ( $env->{HTTP_X_FORWARDED_PROTO} && $env->{HTTP_X_FORWARDED_PROTO} eq 'https') { $self->secure(1); } return $self; } sub cookies { if(@_ > 1) { my $cookie = Protocol::WebSocket::Cookie->new; return unless $_[1]; if (my $cookies = $cookie->parse($_[1])) { $_[0]->{cookies} = $cookies; } } else { return $_[0]->{cookies}; } } sub resource_name { @_ > 1 ? $_[0]->{resource_name} = $_[1] : $_[0]->{resource_name} || '/'; } sub upgrade { shift->field('Upgrade') } sub connection { shift->field('Connection') } sub number1 { shift->_number('number1', 'key1', @_) } sub number2 { shift->_number('number2', 'key2', @_) } sub key { shift->_key('key' => @_) } sub key1 { shift->_key('key1' => @_) } sub key2 { shift->_key('key2' => @_) } sub to_string { my $self = shift; my $version = $self->version || 'draft-ietf-hybi-17'; my $string = ''; Carp::croak(qq/resource_name is required/) unless defined $self->resource_name; $string .= "GET " . $self->resource_name . " HTTP/1.1\x0d\x0a"; $string .= "Upgrade: WebSocket\x0d\x0a"; $string .= "Connection: Upgrade\x0d\x0a"; Carp::croak(qq/Host is required/) unless defined $self->host; $string .= "Host: " . $self->host . "\x0d\x0a"; if (ref $self->{cookies} eq 'Protocol::WebSocket::Cookie') { my $cookie_string = $self->{cookies}->to_string; $string .= 'Cookie: ' . $cookie_string . "\x0d\x0a" if $cookie_string; } my $origin = $self->origin ? $self->origin : 'http://' . $self->host; $origin =~ s{^http:}{https:} if $self->secure; $string .= ( $version eq 'draft-ietf-hybi-10' ? "Sec-WebSocket-Origin" : "Origin" ) . ': ' . $origin . "\x0d\x0a"; if ($version eq 'draft-ietf-hybi-10' || $version eq 'draft-ietf-hybi-17') { my $key = $self->key; if (!$key) { $key = ''; $key .= chr(int(rand(256))) for 1 .. 16; $key = MIME::Base64::encode_base64($key); $key =~ s{\s+}{}g; } $string .= 'Sec-WebSocket-Protocol: ' . $self->subprotocol . "\x0d\x0a" if defined $self->subprotocol; $string .= 'Sec-WebSocket-Key: ' . $key . "\x0d\x0a"; $string .= 'Sec-WebSocket-Version: ' . ($version eq 'draft-ietf-hybi-17' ? 13 : 8) . "\x0d\x0a"; } elsif ($version eq 'draft-ietf-hybi-00') { $self->_generate_keys; $string .= 'Sec-WebSocket-Protocol: ' . $self->subprotocol . "\x0d\x0a" if defined $self->subprotocol; $string .= 'Sec-WebSocket-Key1: ' . $self->key1 . "\x0d\x0a"; $string .= 'Sec-WebSocket-Key2: ' . $self->key2 . "\x0d\x0a"; $string .= 'Content-Length: ' . length($self->challenge) . "\x0d\x0a"; } elsif ($version eq 'draft-hixie-75') { $string .= 'WebSocket-Protocol: ' . $self->subprotocol . "\x0d\x0a" if defined $self->subprotocol; } else { Carp::croak('Version ' . $self->version . ' is not supported'); } my @headers = @{$self->{headers}}; while (my ($key, $value) = splice @headers, 0, 2) { $key =~ s{[\x0d\x0a]}{}gsm; $value =~ s{[\x0d\x0a]}{}gsm; $string .= "$key: $value\x0d\x0a"; } $string .= "\x0d\x0a"; $string .= $self->challenge if $version eq 'draft-ietf-hybi-00'; return $string; } sub parse { my $self = shift; my $retval = $self->SUPER::parse($_[0]); if (!$self->{finalized} && ($self->is_body || $self->is_done)) { $self->{finalized} = 1; if ($self->key1 && $self->key2) { $self->version('draft-ietf-hybi-00'); } elsif ($self->key) { if ($self->field('sec-websocket-version') eq '13') { $self->version('draft-ietf-hybi-17'); } else { $self->version('draft-ietf-hybi-10'); } } else { $self->version('draft-hixie-75'); } if (!$self->_finalize) { $self->error('Not a valid request'); return; } } return $retval; } sub _parse_first_line { my ($self, $line) = @_; my ($req, $resource_name, $http) = split ' ' => $line; unless ($req && $resource_name && $http) { $self->error('Wrong request line'); return; } unless ($req eq 'GET' && $http eq 'HTTP/1.1') { $self->error('Wrong method or http version'); return; } $self->resource_name($resource_name); return $self; } sub _parse_body { my $self = shift; if ($self->key1 && $self->key2) { return 1 if length $self->{buffer} < 8; my $challenge = substr $self->{buffer}, 0, 8, ''; $self->challenge($challenge); } if (length $self->{buffer}) { $self->error('Leftovers'); return; } return $self; } sub _number { my $self = shift; my ($name, $key, $value) = @_; if (defined $value) { $self->{$name} = $value; return $self; } return $self->{$name} if defined $self->{$name}; return $self->{$name} ||= $self->_extract_number($self->$key); } sub _key { my $self = shift; my $name = shift; my $value = shift; unless (defined $value) { if (my $value = delete $self->{$name}) { $self->field("Sec-WebSocket-" . ucfirst($name) => $value); } return $self->field("Sec-WebSocket-" . ucfirst($name)); } $self->field("Sec-WebSocket-" . ucfirst($name) => $value); return $self; } sub _generate_keys { my $self = shift; unless ($self->key1) { my ($number, $key) = $self->_generate_key; $self->number1($number); $self->key1($key); } unless ($self->key2) { my ($number, $key) = $self->_generate_key; $self->number2($number); $self->key2($key); } $self->challenge($self->_generate_challenge) unless $self->challenge; return $self; } sub _generate_key { my $self = shift; # A random integer from 1 to 12 inclusive my $spaces = int(rand(12)) + 1; # The largest integer not greater than 4,294,967,295 divided by spaces my $max = int(4_294_967_295 / $spaces); # A random integer from 0 to $max inclusive my $number = int(rand($max + 1)); # The result of multiplying $number and $spaces together my $product = $number * $spaces; # A string consisting of $product, expressed in base ten my $key = "$product"; # Insert between one and twelve random characters from the ranges U+0021 # to U+002F and U+003A to U+007E into $key at random positions. my $random_characters = int(rand(12)) + 1; for (1 .. $random_characters) { # From 0 to the last position my $random_position = int(rand(length($key) + 1)); # Random character my $random_character = chr( int(rand(2)) ? int(rand(0x2f - 0x21 + 1)) + 0x21 : int(rand(0x7e - 0x3a + 1)) + 0x3a ); # Insert random character at random position substr $key, $random_position, 0, $random_character; } # Insert $spaces U+0020 SPACE characters into $key at random positions # other than the start or end of the string. for (1 .. $spaces) { # From 1 to the last-1 position my $random_position = int(rand(length($key) - 1)) + 1; # Insert substr $key, $random_position, 0, ' '; } return ($number, $key); } sub _generate_challenge { my $self = shift; # A string consisting of eight random bytes (or equivalently, a random 64 # bit integer encoded in big-endian order). my $challenge = ''; $challenge .= chr(int(rand(256))) for 1 .. 8; return $challenge; } sub _finalize { my $self = shift; return unless $self->upgrade && lc $self->upgrade eq 'websocket'; my $connection = $self->connection; return unless $connection; my @connections = split /\s*,\s*/, $connection; return unless grep { lc $_ eq 'upgrade' } @connections; my $origin = $self->field('Sec-WebSocket-Origin') || $self->field('Origin'); #return unless $origin; $self->origin($origin); if (defined $self->origin) { $self->secure(1) if $self->origin =~ m{^https:}; } my $host = $self->field('Host'); return unless $host; $self->host($host); my $subprotocol = $self->field('Sec-WebSocket-Protocol') || $self->field('WebSocket-Protocol'); $self->subprotocol($subprotocol) if $subprotocol; $self->cookies($self->field('Cookie')); return $self; } sub _build_cookie { Protocol::WebSocket::Cookie::Request->new } 1; __END__ =head1 NAME Protocol::WebSocket::Request - WebSocket Request =head1 SYNOPSIS # Constructor my $req = Protocol::WebSocket::Request->new( host => 'example.com', resource_name => '/demo' ); $req->to_string; # GET /demo HTTP/1.1 # Upgrade: WebSocket # Connection: Upgrade # Host: example.com # Origin: http://example.com # Sec-WebSocket-Key1: 32 0 3lD& 24+< i u4 8! -6/4 # Sec-WebSocket-Key2: 2q 4 2 54 09064 # # x##### # Parser my $req = Protocol::WebSocket::Request->new; $req->parse("GET /demo HTTP/1.1\x0d\x0a"); $req->parse("Upgrade: WebSocket\x0d\x0a"); $req->parse("Connection: Upgrade\x0d\x0a"); $req->parse("Host: example.com\x0d\x0a"); $req->parse("Origin: http://example.com\x0d\x0a"); $req->parse( "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a"); $req->parse( "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a"); $req->parse("\x0d\x0aTm[K T2u"); =head1 DESCRIPTION Construct or parse a WebSocket request. =head1 ATTRIBUTES =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head2 C =head1 METHODS =head2 C Create a new L instance. =head2 C my $env = { HTTP_HOST => 'example.com', HTTP_CONNECTION => 'Upgrade', ... }; my $req = Protocol::WebSocket::Request->new_from_psgi($env); Create a new L instance from L environment. =head2 C $req->parse($buffer); $req->parse($handle); Parse a WebSocket request. Incoming buffer is modified. =head2 C Construct a WebSocket request. =head2 C $self->connection; A shortcut for C<$self->field('Connection')>. =head2 C =head2 C $self->upgrade; A shortcut for C<$self->field('Upgrade')>. =cut Response.pm100644001750001750 2037213440704326 22416 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Response; use strict; use warnings; use base 'Protocol::WebSocket::Message'; require Carp; use MIME::Base64 (); use Digest::SHA (); use Protocol::WebSocket::URL; use Protocol::WebSocket::Cookie::Response; sub location { @_ > 1 ? $_[0]->{location} = $_[1] : $_[0]->{location} } sub resource_name { @_ > 1 ? $_[0]->{resource_name} = $_[1] : $_[0]->{resource_name}; } sub cookies { @_ > 1 ? $_[0]->{cookies} = $_[1] : $_[0]->{cookies} } sub cookie { my $self = shift; push @{$self->{cookies}}, $self->_build_cookie(@_); } sub key { @_ > 1 ? $_[0]->{key} = $_[1] : $_[0]->{key} } sub number1 { shift->_number('number1', 'key1', @_) } sub number2 { shift->_number('number2', 'key2', @_) } sub _number { my $self = shift; my ($name, $key, $value) = @_; my $method = "SUPER::$name"; return $self->$method($value) if defined $value; $value = $self->$method(); $value = $self->_extract_number($self->$key) if not defined $value; return $value; } sub key1 { @_ > 1 ? $_[0]->{key1} = $_[1] : $_[0]->{key1} } sub key2 { @_ > 1 ? $_[0]->{key2} = $_[1] : $_[0]->{key2} } sub status { return '101'; } sub headers { my $self = shift; my $version = $self->version || 'draft-ietf-hybi-10'; my $headers = []; push @$headers, Upgrade => 'WebSocket'; push @$headers, Connection => 'Upgrade'; if ($version eq 'draft-hixie-75' || $version eq 'draft-ietf-hybi-00') { Carp::croak(qq/host is required/) unless defined $self->host; my $location = $self->_build_url( host => $self->host, secure => $self->secure, resource_name => $self->resource_name, ); my $origin = $self->origin ? $self->origin : 'http://' . $location->host; $origin =~ s{^http:}{https:} if !$self->origin && $self->secure; if ($version eq 'draft-hixie-75') { push @$headers, 'WebSocket-Protocol' => $self->subprotocol if defined $self->subprotocol; push @$headers, 'WebSocket-Origin' => $origin; push @$headers, 'WebSocket-Location' => $location->to_string; } elsif ($version eq 'draft-ietf-hybi-00') { push @$headers, 'Sec-WebSocket-Protocol' => $self->subprotocol if defined $self->subprotocol; push @$headers, 'Sec-WebSocket-Origin' => $origin; push @$headers, 'Sec-WebSocket-Location' => $location->to_string; } } elsif ($version eq 'draft-ietf-hybi-10' || $version eq 'draft-ietf-hybi-17') { Carp::croak(qq/key is required/) unless defined $self->key; my $key = $self->key; $key .= '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; # WTF $key = Digest::SHA::sha1($key); $key = MIME::Base64::encode_base64($key); $key =~ s{\s+}{}g; push @$headers, 'Sec-WebSocket-Accept' => $key; push @$headers, 'Sec-WebSocket-Protocol' => $self->subprotocol if defined $self->subprotocol; } else { Carp::croak('Version ' . $version . ' is not supported'); } if (@{$self->cookies}) { my $cookie = join ',' => map { $_->to_string } @{$self->cookies}; push @$headers, 'Set-Cookie' => $cookie; } return $headers; } sub body { my $self = shift; return $self->checksum if $self->version eq 'draft-ietf-hybi-00'; return ''; } sub to_string { my $self = shift; my $status = $self->status; my $string = ''; $string .= "HTTP/1.1 $status WebSocket Protocol Handshake\x0d\x0a"; for (my $i = 0; $i < @{$self->headers}; $i += 2) { my $key = $self->headers->[$i]; my $value = $self->headers->[$i + 1]; $string .= "$key: $value\x0d\x0a"; } $string .= "\x0d\x0a"; $string .= $self->body; return $string; } sub _parse_first_line { my ($self, $line) = @_; my $status = $self->status; unless ($line =~ m{^HTTP/1\.1 $status }) { my $vis = $line; if( length( $vis ) > 80 ) { substr( $vis, 77 )= '...'; } $self->error('Wrong response line. Got [[' . $vis . "]], expected [[HTTP/1.1 $status ]]"); return; } return $self; } sub _parse_body { my $self = shift; if ($self->field('Sec-WebSocket-Accept')) { $self->version('draft-ietf-hybi-10'); } elsif ($self->field('Sec-WebSocket-Origin')) { $self->version('draft-ietf-hybi-00'); return 1 if length $self->{buffer} < 16; my $checksum = substr $self->{buffer}, 0, 16, ''; $self->checksum($checksum); } else { $self->version('draft-hixie-75'); } return $self if $self->_finalize; $self->error('Not a valid response'); return; } sub _finalize { my $self = shift; if ($self->version eq 'draft-hixie-75') { my $location = $self->field('WebSocket-Location'); return unless defined $location; $self->location($location); my $url = $self->_build_url; return unless $url->parse($self->location); $self->secure($url->secure); $self->host($url->host); $self->resource_name($url->resource_name); $self->origin($self->field('WebSocket-Origin')); $self->subprotocol($self->field('WebSocket-Protocol')); } elsif ($self->version eq 'draft-ietf-hybi-00') { my $location = $self->field('Sec-WebSocket-Location'); return unless defined $location; $self->location($location); my $url = $self->_build_url; return unless $url->parse($self->location); $self->secure($url->secure); $self->host($url->host); $self->resource_name($url->resource_name); $self->origin($self->field('Sec-WebSocket-Origin')); $self->subprotocol($self->field('Sec-WebSocket-Protocol')); } else { $self->subprotocol($self->field('Sec-WebSocket-Protocol')); } return 1; } sub _build_url { shift; Protocol::WebSocket::URL->new(@_) } sub _build_cookie { shift; Protocol::WebSocket::Cookie::Response->new(@_) } 1; __END__ =head1 NAME Protocol::WebSocket::Response - WebSocket Response =head1 SYNOPSIS # Constructor $res = Protocol::WebSocket::Response->new( host => 'example.com', resource_name => '/demo', origin => 'file://', number1 => 777_007_543, number2 => 114_997_259, challenge => "\x47\x30\x22\x2D\x5A\x3F\x47\x58" ); $res->to_string; # HTTP/1.1 101 WebSocket Protocol Handshake # Upgrade: WebSocket # Connection: Upgrade # Sec-WebSocket-Origin: file:// # Sec-WebSocket-Location: ws://example.com/demo # # 0st3Rl&q-2ZU^weu # Parser $res = Protocol::WebSocket::Response->new; $res->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"); $res->parse("Upgrade: WebSocket\x0d\x0a"); $res->parse("Connection: Upgrade\x0d\x0a"); $res->parse("Sec-WebSocket-Origin: file://\x0d\x0a"); $res->parse("Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a"); $res->parse("\x0d\x0a"); $res->parse("0st3Rl&q-2ZU^weu"); =head1 DESCRIPTION Construct or parse a WebSocket response. =head1 ATTRIBUTES =head2 C =head2 C =head2 C =head2 C =head2 C =head1 METHODS =head2 C Create a new L instance. =head2 C $res->parse($buffer); Parse a WebSocket response. Incoming buffer is modified. =head2 C Construct a WebSocket response. =head2 C =head2 C =head2 C =head2 C $self->key1; Set or get C field. =head2 C $self->key2; Set or get C field. =head2 C $self->number1; $self->number1(123456); Set or extract from C generated C value. =head2 C $self->number2; $self->number2(123456); Set or extract from C generated C value. =head2 C $self->status; Get response status (101). =head2 C $self->body; Get response body. =head2 C my $arrayref = $self->headers; Get response headers. =cut Stateful.pm100644001750001750 143313440704326 22364 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::Stateful; use strict; use warnings; sub new { my $class = shift; $class = ref $class if ref $class; my $self = {@_}; bless $self, $class; return $self; } sub state { @_ > 1 ? $_[0]->{state} = $_[1] : $_[0]->{state} } sub done { shift->state('done') } sub is_state { shift->state eq shift } sub is_body { shift->is_state('body') } sub is_done { shift->is_state('done') } 1; __END__ =head1 NAME Protocol::WebSocket::Stateful - Base class for all classes with states =head1 DESCRIPTION A base class for all classes with states. =head1 ATTRIBUTES =head2 C =head1 METHODS =head2 C Create a new L instance. =head2 C =head2 C =head2 C =head2 C =cut URL.pm100644001750001750 415213440704326 21240 0ustar00vtivti000000000000Protocol-WebSocket-0.26/lib/Protocol/WebSocketpackage Protocol::WebSocket::URL; use strict; use warnings; sub new { my $class = shift; $class = ref $class if ref $class; my $self = {@_}; bless $self, $class; $self->{secure} ||= 0; return $self; } sub secure { @_ > 1 ? $_[0]->{secure} = $_[1] : $_[0]->{secure} } sub host { @_ > 1 ? $_[0]->{host} = $_[1] : $_[0]->{host} } sub port { @_ > 1 ? $_[0]->{port} = $_[1] : $_[0]->{port} } sub resource_name { @_ > 1 ? $_[0]->{resource_name} = $_[1] : $_[0]->{resource_name}; } sub parse { my $self = shift; my $string = shift; my ($scheme) = $string =~ m{^(wss?)://}; return unless $scheme; $self->secure(1) if $scheme =~ m/ss$/; my ($host, $port) = $string =~ m{^$scheme://([^:\/]+)(?::(\d+))?(?:|\/|$)}; $host = '/' unless defined $host && $host ne ''; $self->host($host); $port ||= $self->secure ? 443 : 80; $self->port($port); # path and query my ($pnq) = $string =~ m{^$scheme://(?:.*?)(/.*)$}; $pnq = '/' unless defined $pnq && $pnq ne ''; $self->resource_name($pnq); return $self; } sub to_string { my $self = shift; my $string = ''; $string .= 'ws'; $string .= 's' if $self->secure; $string .= '://'; $string .= $self->host; $string .= ':' . $self->port if defined $self->port; $string .= $self->resource_name || '/'; return $string; } 1; __END__ =head1 NAME Protocol::WebSocket::URL - WebSocket URL =head1 SYNOPSIS # Construct my $url = Protocol::WebSocket::URL->new; $url->host('example.com'); $url->port('3000'); $url->secure(1); $url->to_string; # wss://example.com:3000 # Parse my $url = Protocol::WebSocket::URL->new->parse('wss://example.com:3000'); $url->host; # example.com $url->port; # 3000 $url->secure; # 1 =head1 DESCRIPTION Construct or parse a WebSocket URL. =head1 ATTRIBUTES =head2 C =head2 C =head2 C =head2 C =head1 METHODS =head2 C Create a new L instance. =head2 C Parse a WebSocket URL. =head2 C Construct a WebSocket URL. =cut minil.toml100644001750001750 3413440704326 15743 0ustar00vtivti000000000000Protocol-WebSocket-0.26name = "Protocol-WebSocket" challenge100644001750001750 1013440704326 16040 0ustar00vtivti000000000000Protocol-WebSocket-0.26/tTm[K T2uclient.t100644001750001750 611413440704326 15711 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t#!/usr/bin/env perl use strict; use warnings; use Test::More; use IO::Handle; use Protocol::WebSocket::Handshake::Server; use_ok 'Protocol::WebSocket::Client'; subtest 'write handshake on connect' => sub { my $client = Protocol::WebSocket::Client->new(url => 'ws://localhost:8080'); my $written = ''; $client->on(write => sub { $written .= $_[1] }); $client->connect; like $written, qr/Upgrade: WebSocket/; }; subtest 'call on_connect on connect' => sub { my $client = Protocol::WebSocket::Client->new(url => 'ws://localhost:8080'); $client->on(write => sub { }); my $connected; $client->on( connect => sub { $connected++; } ); $client->connect; _recv_server_handshake($client); ok $connected; }; subtest 'call on_read on new data' => sub { my $client = Protocol::WebSocket::Client->new(url => 'ws://localhost:8080'); my $read = ''; $client->on(write => sub { }); $client->on(read => sub { $read .= $_[1] }); $client->connect; _recv_server_handshake($client); my $frame = Protocol::WebSocket::Frame->new( version => $client->version, buffer => 'hello' ); $client->read($frame->to_bytes); is $read, 'hello'; }; subtest 'write close frame on disconnect' => sub { my $client = Protocol::WebSocket::Client->new(url => 'ws://localhost:8080'); my $written = ''; $client->on(write => sub { $written .= $_[1] }); $client->disconnect; is $written, "\x88\x00"; }; subtest 'call on_write on write' => sub { my $client = Protocol::WebSocket::Client->new(url => 'ws://localhost:8080'); my $written = ''; $client->on(write => sub { $written .= $_[1] }); $client->write('foobar'); isnt $written, ''; }; subtest 'max_payload_size passed to frame buffer' => sub { is(Protocol::WebSocket::Client->new(url => 'ws://localhost:8080')->{frame_buffer}->max_payload_size, 65536, "default"); is(Protocol::WebSocket::Client->new(url => 'ws://localhost:8080', max_payload_size => 22)->{frame_buffer}->max_payload_size, 22, "set to 22"); is(Protocol::WebSocket::Client->new(url => 'ws://localhost:8080', max_payload_size => 0)->{frame_buffer}->max_payload_size, 0, "set to 0"); is(Protocol::WebSocket::Client->new(url => 'ws://localhost:8080', max_payload_size => undef)->{frame_buffer}->max_payload_size, undef, "set to undef"); }; sub _recv_server_handshake { my ($client) = @_; open my $fh, '<', \'' or die $!; my $io = IO::Handle->new; $io->fdopen(fileno($fh), "r"); my $hs = Protocol::WebSocket::Handshake::Server->new_from_psgi( SCRIPT_NAME => '', PATH_INFO => '/chat', HTTP_UPGRADE => 'websocket', HTTP_CONNECTION => 'Upgrade', HTTP_HOST => 'server.example.com', HTTP_SEC_WEBSOCKET_ORIGIN => 'http://example.com', HTTP_SEC_WEBSOCKET_KEY => 'dGhlIHNhbXBsZSBub25jZQ==', HTTP_SEC_WEBSOCKET_VERSION => 13, ); $hs->parse($io); $client->read($hs->to_string); } done_testing; cookie.t100644001750001750 577613440704326 15721 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t#!/usr/bin/env perl use strict; use warnings; use Test::More; use_ok 'Protocol::WebSocket::Cookie'; use_ok 'Protocol::WebSocket::Cookie::Response'; use_ok 'Protocol::WebSocket::Cookie::Request'; my $cookie; my $cookies; $cookie = Protocol::WebSocket::Cookie->new; $cookie->parse; $cookie->parse(''); $cookie->parse('foo=bar; baz = zab; hello= "the;re"; here'); is_deeply($cookie->pairs, [[foo => 'bar'], [baz => 'zab'], [hello => 'the;re'], ['here', undef]]); is $cookie->to_string => 'foo=bar; baz=zab; hello="the;re"; here'; $cookie = Protocol::WebSocket::Cookie->new; $cookie->parse('$Foo="bar"'); is_deeply($cookie->pairs, [['$Foo' => 'bar']]); $cookie = Protocol::WebSocket::Cookie->new; $cookie->parse('foo=bar=123=xyz'); is_deeply($cookie->pairs, [['foo' => 'bar=123=xyz']]); $cookie = Protocol::WebSocket::Cookie::Response->new(name => 'foo', value => 'bar'); is $cookie->to_string => 'foo=bar; Version=1'; $cookie = Protocol::WebSocket::Cookie::Response->new( name => 'foo', value => 'bar', discard => 1, max_age => 0 ); is $cookie->to_string => 'foo=bar; Discard; Max-Age=0; Version=1'; $cookie = Protocol::WebSocket::Cookie::Response->new( name => 'foo', value => 'bar', portlist => 80 ); is $cookie->to_string => 'foo=bar; Port="80"; Version=1'; $cookie = Protocol::WebSocket::Cookie::Response->new( name => 'foo', value => 'bar', portlist => [80, 443] ); is $cookie->to_string => 'foo=bar; Port="80 443"; Version=1'; $cookie = Protocol::WebSocket::Cookie::Request->new; $cookies = $cookie->parse('$Version=1; foo=bar; $Path=/; $Domain=.example.com'); is $cookies->[0]->name => 'foo'; is $cookies->[0]->value => 'bar'; is $cookies->[0]->version => 1; is $cookies->[0]->path => '/'; is $cookies->[0]->domain => '.example.com'; $cookie = Protocol::WebSocket::Cookie::Request->new; $cookies = $cookie->parse('$Version=1; foo=bar'); is $cookies->[0]->name => 'foo'; is $cookies->[0]->value => 'bar'; is $cookies->[0]->version => 1; ok not defined $cookies->[0]->path; ok not defined $cookies->[0]->domain; $cookie = Protocol::WebSocket::Cookie::Request->new; $cookies = $cookie->parse('$Version=1; foo="hello\"there"'); is $cookies->[0]->name => 'foo'; is $cookies->[0]->value => 'hello"there'; $cookie = Protocol::WebSocket::Cookie::Request->new; $cookies = $cookie->parse( '$Version=1; foo="bar"; $Path=/; bar=baz; $Domain=.example.com'); is $cookies->[0]->name => 'foo'; is $cookies->[0]->value => 'bar'; is $cookies->[0]->path => '/'; is $cookies->[1]->name => 'bar'; is $cookies->[1]->value => 'baz'; is $cookies->[1]->domain => '.example.com'; subtest 'parse when no version is available' => sub { $cookie = Protocol::WebSocket::Cookie::Request->new; $cookies = $cookie->parse('foo=bar; $Path=/; $Domain=.example.com'); is $cookies->[0]->name => 'foo'; is $cookies->[0]->value => 'bar'; is $cookies->[0]->version => 1; is $cookies->[0]->path => '/'; is $cookies->[0]->domain => '.example.com'; }; done_testing; request.t100644001750001750 1712013440704326 20577 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-hixie-75#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 110; use IO::Handle; use_ok 'Protocol::WebSocket::Request'; my $req = Protocol::WebSocket::Request->new; my $message; $req = Protocol::WebSocket::Request->new; ok !$req->is_done; ok $req->parse; ok $req->parse(''); ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Upgrade: WebSocket\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Connection: Upgrade\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Host: example.com\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Cookie: foo=bar;alice=bob\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Origin: http://example.com\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("\x0d\x0a"); is $req->state => 'done'; is $req->version => 'draft-hixie-75'; is $req->resource_name => '/demo'; is $req->host => 'example.com'; is $req->cookies->to_string => 'foo=bar; alice=bob'; is $req->origin => 'http://example.com'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Upgrade\x0d\x0a"); ok $req->parse("Host: example.com:3000\x0d\x0a"); ok $req->parse("Origin: null\x0d\x0a"); ok $req->parse("\x0d\x0a"); is $req->version => 'draft-hixie-75'; is $req->state => 'done'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("UPGRADE: WebSocket\x0d\x0a"); ok $req->parse("CONNECTION: Upgrade\x0d\x0a"); ok $req->parse("HOST: example.com:3000\x0d\x0a"); ok $req->parse("ORIGIN: null\x0d\x0a"); ok $req->parse("\x0d\x0a"); is $req->version => 'draft-hixie-75'; is $req->state => 'done'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Upgrade\x0d\x0a"); ok $req->parse("Host: example.com:3000\x0d\x0a"); ok $req->parse("Origin: null\x0d\x0a"); ok $req->parse("WebSocket-Protocol: sample\x0d\x0a"); ok $req->parse("\x0d\x0a"); is $req->version => 'draft-hixie-75'; is $req->state => 'done'; is $req->subprotocol => 'sample'; $req = Protocol::WebSocket::Request->new; $message = "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a"; ok $req->parse($message); is $message => ''; $message = "Host: example.com:3000\x0d\x0a" . "Origin: null\x0d\x0a" . "\x0d\x0a"; ok $req->parse($message); is $message => ''; is $req->version => 'draft-hixie-75'; ok $req->is_done; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Upgrade\x0d\x0a"); ok $req->parse("Host: example.com\x0d\x0a"); ok $req->parse("Origin: null\x0d\x0a"); ok $req->parse("Cookie: \$Version=1; foo=bar; \$Path=/\x0d\x0a"); ok $req->parse("\x0d\x0a"); ok $req->is_done; is $req->cookies->pairs->[0][0] => '$Version'; is $req->cookies->pairs->[0][1] => '1'; is $req->cookies->pairs->[1][0] => 'foo'; is $req->cookies->pairs->[1][1] => 'bar'; is $req->cookies->pairs->[2][0] => '$Path'; is $req->cookies->pairs->[2][1] => '/'; $req = Protocol::WebSocket::Request->new; $req->parse("GET /demo HTTP/1.1\x0d\x0a"); $req->parse("Upgrade: WebSocket\x0d\x0a"); $req->parse("Connection: Upgrade\x0d\x0a"); $req->parse("Host: example.com\x0d\x0a"); $req->parse("Origin: null\x0d\x0a"); $req->parse("X-Forwarded-Proto: https\x0d\x0a"); $req->parse("\x0d\x0a"); ok $req->is_done; ok $req->secure; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Upgrade\x0d\x0a"); ok $req->parse("Host: example.com\x0d\x0a"); ok $req->parse("Origin: https://example.com\x0d\x0a"); ok $req->parse("\x0d\x0a"); ok $req->is_done; ok $req->secure; $req = Protocol::WebSocket::Request->new( version => 'draft-hixie-75', host => 'example.com', cookies => Protocol::WebSocket::Cookie->new->parse('foo=bar; alice=bob'), resource_name => '/demo' ); is $req->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "\x0d\x0a"; $req = Protocol::WebSocket::Request->new( version => 'draft-hixie-75', host => 'example.com', subprotocol => 'sample', resource_name => '/demo' ); is $req->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "WebSocket-Protocol: sample\x0d\x0a" . "\x0d\x0a"; $req = Protocol::WebSocket::Request->new( version => 'draft-hixie-75', host => 'example.com', resource_name => '/demo' ); is $req->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "\x0d\x0a"; $req = Protocol::WebSocket::Request->new( secure => 1, version => 'draft-hixie-75', host => 'example.com', resource_name => '/demo' ); is $req->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Origin: https://example.com\x0d\x0a" . "\x0d\x0a"; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Bar\x0d\x0a"); ok $req->parse("Host: example.com\x0d\x0a"); ok $req->parse("Origin: http://example.com\x0d\x0a"); ok not defined $req->parse("\x0d\x0a"); ok $req->is_state('error'); is $req->error => 'Not a valid request'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Upgrade\x0d\x0a"); ok $req->parse("Host: example.com\x0d\x0a"); ok $req->parse("Origin: http://example.com\x0d\x0a"); ok not defined $req->parse("\x0d\x0afoo"); ok $req->is_state('error'); is $req->error => 'Leftovers'; eval { Protocol::WebSocket::Request->new_from_psgi() }; ok $@; eval { Protocol::WebSocket::Request->new_from_psgi({}) }; ok $@; open my $fh, '<', 't/empty' or die $!; my $io = IO::Handle->new; $io->fdopen(fileno($fh), "r"); $req = Protocol::WebSocket::Request->new_from_psgi( { SCRIPT_NAME => '', PATH_INFO => '/demo', HTTP_UPGRADE => 'WebSocket', HTTP_CONNECTION => 'Upgrade', HTTP_HOST => 'example.com:3000', HTTP_ORIGIN => 'null', HTTP_WEBSOCKET_PROTOCOL => 'sample' } ); $req->parse($io); is $req->subprotocol => 'sample'; is $req->resource_name => '/demo'; is $req->upgrade => 'WebSocket'; is $req->connection => 'Upgrade'; is $req->host => 'example.com:3000'; is $req->origin => 'null'; ok $req->is_done; is $req->version => 'draft-hixie-75'; $req = Protocol::WebSocket::Request->new_from_psgi( { SCRIPT_NAME => '', PATH_INFO => '/demo', HTTP_UPGRADE => 'WebSocket', HTTP_CONNECTION => 'Upgrade', HTTP_HOST => 'example.com:3000', HTTP_ORIGIN => 'null', HTTP_WEBSOCKET_PROTOCOL => 'sample', HTTP_X_FORWARDED_PROTO => 'https' } ); $req->parse($io); ok $req->secure; response.t100644001750001750 530613440704326 20730 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-hixie-75#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 12; use_ok 'Protocol::WebSocket::Response'; my $res; my $message; $res = Protocol::WebSocket::Response->new; $res->version('draft-hixie-75'); $res->host('example.com'); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "WebSocket-Origin: http://example.com\x0d\x0a" . "WebSocket-Location: ws://example.com/\x0d\x0a" . "\x0d\x0a"; $res = Protocol::WebSocket::Response->new; $res->version('draft-hixie-75'); $res->host('example.com'); $res->subprotocol('sample'); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "WebSocket-Protocol: sample\x0d\x0a" . "WebSocket-Origin: http://example.com\x0d\x0a" . "WebSocket-Location: ws://example.com/\x0d\x0a" . "\x0d\x0a"; $res = Protocol::WebSocket::Response->new; $res->version('draft-hixie-75'); $res->host('example.com'); $res->secure(1); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "WebSocket-Origin: https://example.com\x0d\x0a" . "WebSocket-Location: wss://example.com/\x0d\x0a" . "\x0d\x0a"; $res = Protocol::WebSocket::Response->new; $res->version('draft-hixie-75'); $res->host('example.com'); $res->resource_name('/demo'); $res->origin('file://'); $res->cookie(name => 'foo', value => 'bar', path => '/'); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "WebSocket-Origin: file://\x0d\x0a" . "WebSocket-Location: ws://example.com/demo\x0d\x0a" . "Set-Cookie: foo=bar; Path=/; Version=1\x0d\x0a" . "\x0d\x0a"; $res = Protocol::WebSocket::Response->new; $res->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"); $res->parse("Upgrade: WebSocket\x0d\x0a"); $res->parse("Connection: Upgrade\x0d\x0a"); $res->parse("WebSocket-Protocol: sample\x0d\x0a"); $res->parse("WebSocket-Origin: file://\x0d\x0a"); $res->parse("WebSocket-Location: ws://example.com/demo\x0d\x0a"); $res->parse("\x0d\x0a\x00foo\xff"); ok $res->is_done; is $res->version => 'draft-hixie-75'; is $res->subprotocol => 'sample'; $message = "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "WebSocket-Origin: file://\x0d\x0a" . "WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a\x00foo\xff"; $res = Protocol::WebSocket::Response->new; ok $res->parse($message); ok $res->is_done; is $res->version => 'draft-hixie-75'; is $message => "\x00foo\xff"; client-ssl.t100644001750001750 442213440704326 21703 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-00#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 14; use_ok 'Protocol::WebSocket::Handshake::Client'; my $h = Protocol::WebSocket::Handshake::Client->new( version => 'draft-ietf-hybi-00'); $h->url('wss://example.com/demo'); # Mocking $h->req->key1("18x 6]8vM;54 *(5: { U1]8 z [ 8"); $h->req->key2("1_ tx7X d < nw 334J702) 7]o}` 0"); $h->req->challenge("Tm[K T2u"); $h->req->cookies('foo=bar; alice=bob'); is $h->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Origin: https://example.com\x0d\x0a" . "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" . "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" . "Content-Length: 8\x0d\x0a" . "\x0d\x0aTm[K T2u"; $h = Protocol::WebSocket::Handshake::Client->new( url => 'wss://example.com', version => 'draft-ietf-hybi-00' ); # Mocking $h->req->key1("18x 6]8vM;54 *(5: { U1]8 z [ 8"); $h->req->key2("1_ tx7X d < nw 334J702) 7]o}` 0"); $h->req->challenge("Tm[K T2u"); is $h->to_string => "GET / HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Origin: https://example.com\x0d\x0a" . "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" . "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" . "Content-Length: 8\x0d\x0a" . "\x0d\x0aTm[K T2u"; ok !$h->is_done; ok $h->parse; ok $h->parse(''); ok $h->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Location: wss://example.com/demo\x0d\x0a" . "\x0d\x0a" . "fQJ,fN/4F4!~K~MH"); ok !$h->error; ok $h->is_done; my $message = "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"; $h = Protocol::WebSocket::Handshake::Client->new(url => 'wss://example.com'); ok $h->parse($message); is $message => ''; ok !$h->error; $h = Protocol::WebSocket::Handshake::Client->new; ok !$h->parse("HTTP/1.0 foo bar\x0d\x0a"); is $h->error => 'Wrong response line. Got [[HTTP/1.0 foo bar]], ' . 'expected [[HTTP/1.1 101 ]]'; client.t100644001750001750 441413440704326 21105 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-00#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 14; use_ok 'Protocol::WebSocket::Handshake::Client'; my $h = Protocol::WebSocket::Handshake::Client->new( version => 'draft-ietf-hybi-00'); $h->url('ws://example.com/demo'); # Mocking $h->req->key1("18x 6]8vM;54 *(5: { U1]8 z [ 8"); $h->req->key2("1_ tx7X d < nw 334J702) 7]o}` 0"); $h->req->challenge("Tm[K T2u"); $h->req->cookies('foo=bar; alice=bob'); is $h->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" . "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" . "Content-Length: 8\x0d\x0a" . "\x0d\x0aTm[K T2u"; $h = Protocol::WebSocket::Handshake::Client->new( url => 'ws://example.com', version => 'draft-ietf-hybi-00' ); # Mocking $h->req->key1("18x 6]8vM;54 *(5: { U1]8 z [ 8"); $h->req->key2("1_ tx7X d < nw 334J702) 7]o}` 0"); $h->req->challenge("Tm[K T2u"); is $h->to_string => "GET / HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" . "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" . "Content-Length: 8\x0d\x0a" . "\x0d\x0aTm[K T2u"; ok !$h->is_done; ok $h->parse; ok $h->parse(''); ok $h->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a" . "fQJ,fN/4F4!~K~MH"); ok !$h->error; ok $h->is_done; my $message = "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"; $h = Protocol::WebSocket::Handshake::Client->new(url => 'ws://example.com'); ok $h->parse($message); is $message => ''; ok !$h->error; $h = Protocol::WebSocket::Handshake::Client->new; ok !$h->parse("HTTP/1.0 foo bar\x0d\x0a"); is $h->error => 'Wrong response line. Got [[HTTP/1.0 foo bar]], ' . 'expected [[HTTP/1.1 101 ]]'; frame.t100644001750001750 424413440704326 20722 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-00#!/usr/bin/env perl use strict; use warnings; use utf8; use Test::More tests => 27; use Encode; use_ok 'Protocol::WebSocket::Frame'; my $f = Protocol::WebSocket::Frame->new(version => 'draft-ietf-hybi-00'); $f->append; ok not defined $f->next; $f->append(''); ok not defined $f->next; $f->append('qwe'); ok not defined $f->next; $f->append("\x00foo\xff"); is $f->next => 'foo'; ok not defined $f->next; $f->append("\x00"); ok not defined $f->next; $f->append("\xff"); is $f->next => ''; $f->append("\xff"); $f->append("\x00"); is $f->next => ''; ok $f->is_close; $f->append("\x00"); ok not defined $f->next; $f->append("foo"); $f->append("\xff"); is $f->next => 'foo'; $f->append("\x00foo\xff\x00bar\n\xff"); is $f->next => 'foo'; is $f->next => "bar\n"; ok not defined $f->next; $f->append("123\x00foo\xff56\x00bar\xff789"); is $f->next => 'foo'; is $f->next => 'bar'; ok not defined $f->next; my $frame = "123\x00foo\xff56\x00bar\xff789"; $f->append($frame); is $f->next => 'foo'; is $f->next => 'bar'; ok not defined $f->next; is $frame => ''; # We append bytes, but read characters $f->append("\x00" . Encode::encode_utf8('☺') . "\xff"); is $f->next => '☺'; $f = Protocol::WebSocket::Frame->new(version => 'draft-ietf-hybi-00'); is $f->to_bytes => "\x00\xff"; #$f = Protocol::WebSocket::Frame->new( # buffer => '123', # version => 'draft-ietf-hybi-00' #); #is $f->to_string => "\x00123\xff"; #$f = Protocol::WebSocket::Frame->new( # buffer => '☺', # version => 'draft-ietf-hybi-00' #); #is $f->to_string => "\x00" . "☺" . "\xff"; #$f = Protocol::WebSocket::Frame->new( # buffer => Encode::encode_utf8('☺'), # version => 'draft-ietf-hybi-00' #); #is $f->to_string => "\x00" . "☺" . "\xff"; # We pass characters, but send bytes $f = Protocol::WebSocket::Frame->new( buffer => '☺', version => 'draft-ietf-hybi-00' ); is $f->to_bytes => "\x00" . Encode::encode_utf8("☺") . "\xff"; $f = Protocol::WebSocket::Frame->new( version => 'draft-ietf-hybi-00', type => 'ping' ); is $f->to_bytes => "\x00\xff"; $f = Protocol::WebSocket::Frame->new( version => 'draft-ietf-hybi-00', type => 'close' ); is $f->to_bytes => "\xff\x00"; request.t100644001750001750 1412113440704326 21333 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-00#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 64; use IO::Handle; use_ok 'Protocol::WebSocket::Request'; my $req = Protocol::WebSocket::Request->new; ok !$req->is_done; ok $req->parse; ok $req->parse(''); ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Upgrade: WebSocket\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Connection: Upgrade\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Host: example.com\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Cookie: foo=bar;alice=bob\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Origin: http://example.com\x0d\x0a"); is $req->state => 'fields'; ok $req->parse( "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a"); ok $req->parse( "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("\x0d\x0aTm[K T2u"); is $req->state => 'done'; is $req->number1 => '155712099'; is $req->number2 => '173347027'; is $req->challenge => 'Tm[K T2u'; is $req->version => 'draft-ietf-hybi-00'; is $req->resource_name => '/demo'; is $req->host => 'example.com'; is $req->origin => 'http://example.com'; is $req->checksum => 'fQJ,fN/4F4!~K~MH'; is $req->cookies->to_string => 'foo=bar; alice=bob'; $req = Protocol::WebSocket::Request->new; $req->parse("GET /demo HTTP/1.1\x0d\x0a"); $req->parse("Upgrade: WebSocket\x0d\x0a"); $req->parse("Connection: Upgrade\x0d\x0a"); $req->parse("Host: example.com\x0d\x0a"); $req->parse("Origin: http://example.com\x0d\x0a"); $req->parse("Sec-WebSocket-Protocol: sample\x0d\x0a"); $req->parse( "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a"); $req->parse( "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a"); $req->parse("\x0d\x0a"); is $req->version, 'draft-ietf-hybi-00'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Upgrade\x0d\x0a"); ok $req->parse("Host: example.com\x0d\x0a"); ok $req->parse("Origin: http://example.com\x0d\x0a"); ok $req->parse("Sec-WebSocket-Protocol: sample\x0d\x0a"); ok $req->parse( "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a"); ok $req->parse( "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a"); ok $req->parse("\x0d\x0aTm[K T2u"); ok $req->is_done; is $req->subprotocol => 'sample'; $req = Protocol::WebSocket::Request->new( version => 'draft-ietf-hybi-00', host => 'example.com', cookies => Protocol::WebSocket::Cookie->new->parse('foo=bar; alice=bob'), resource_name => '/demo', key1 => '18x 6]8vM;54 *(5: { U1]8 z [ 8', key2 => '1_ tx7X d < nw 334J702) 7]o}` 0', challenge => 'Tm[K T2u' ); is $req->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" . "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" . "Content-Length: 8\x0d\x0a" . "\x0d\x0a" . "Tm[K T2u"; is $req->checksum => "fQJ,fN/4F4!~K~MH"; $req = Protocol::WebSocket::Request->new( version => 'draft-ietf-hybi-00', host => 'example.com', resource_name => '/demo', subprotocol => 'sample', key1 => '18x 6]8vM;54 *(5: { U1]8 z [ 8', key2 => '1_ tx7X d < nw 334J702) 7]o}` 0', challenge => 'Tm[K T2u' ); is $req->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Protocol: sample\x0d\x0a" . "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" . "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" . "Content-Length: 8\x0d\x0a" . "\x0d\x0a" . "Tm[K T2u"; is $req->checksum => "fQJ,fN/4F4!~K~MH"; $req = Protocol::WebSocket::Request->new( version => 'draft-ietf-hybi-00', host => 'example.com', resource_name => '/demo', key1 => '55 997', key2 => '3 3 64 98', challenge => "\x00\x09\x68\x32\x00\x78\xc7\x10" ); is $req->checksum => "\xc4\x15\xc2\xc8\x29\x5c\x94\x8a\x95\xb9\x4d\xec\x5b\x1d\x33\xce"; $req = Protocol::WebSocket::Request->new( version => 'draft-ietf-hybi-00', host => 'example.com', resource_name => '/demo' ); $req->to_string; ok $req->number1; ok $req->key1; ok $req->number2; ok $req->key2; is length($req->challenge) => 8; is length($req->checksum) => 16; open my $fh, '<', 't/challenge' or die $!; my $io = IO::Handle->new; $io->fdopen(fileno($fh), "r"); $req = Protocol::WebSocket::Request->new_from_psgi( { SCRIPT_NAME => '', PATH_INFO => '/demo', QUERY_STRING => 'foo=bar', HTTP_UPGRADE => 'WebSocket', HTTP_CONNECTION => 'Upgrade', HTTP_HOST => 'example.com', HTTP_COOKIE => 'foo=bar', HTTP_ORIGIN => 'http://example.com', HTTP_SEC_WEBSOCKET_PROTOCOL => 'sample', HTTP_SEC_WEBSOCKET_KEY1 => '18x 6]8vM;54 *(5: { U1]8 z [ 8', HTTP_SEC_WEBSOCKET_KEY2 => '1_ tx7X d < nw 334J702) 7]o}` 0', HTTP_CONTENT_LENGTH => 8 } ); $req->parse($io); is $req->resource_name => '/demo?foo=bar'; is $req->subprotocol => 'sample'; is $req->upgrade => 'WebSocket'; is $req->connection => 'Upgrade'; is $req->host => 'example.com'; is $req->cookies->to_string => 'foo=bar'; is $req->origin => 'http://example.com'; is $req->key1 => '18x 6]8vM;54 *(5: { U1]8 z [ 8'; is $req->key2 => '1_ tx7X d < nw 334J702) 7]o}` 0'; ok $req->is_done; is $req->version => 'draft-ietf-hybi-00'; response.t100644001750001750 1133213440704326 21502 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-00#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 34; use_ok 'Protocol::WebSocket::Response'; my $res; $res = Protocol::WebSocket::Response->new; ok $res->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"); ok $res->parse("Upgrade: WebSocket\x0d\x0a"); ok $res->parse("Connection: Upgrade\x0d\x0a"); ok $res->parse("Sec-WebSocket-Origin: file://\x0d\x0a"); ok $res->parse("Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a"); ok $res->parse("\x0d\x0a"); ok $res->parse("0st3Rl&q-2ZU^weu"); ok $res->is_done; is $res->checksum => '0st3Rl&q-2ZU^weu'; ok !$res->secure; is $res->host => 'example.com'; is $res->resource_name => '/demo'; is $res->origin => 'file://'; $res = Protocol::WebSocket::Response->new; ok $res->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"); ok $res->parse("Upgrade: WebSocket\x0d\x0a"); ok $res->parse("Connection: Upgrade\x0d\x0a"); ok $res->parse("Sec-WebSocket-Protocol: sample\x0d\x0a"); ok $res->parse("Sec-WebSocket-Origin: file://\x0d\x0a"); ok $res->parse("Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a"); ok $res->parse("\x0d\x0a"); ok $res->parse("0st3Rl&q-2ZU^weu"); ok $res->is_done; is $res->subprotocol => 'sample'; $res = Protocol::WebSocket::Response->new; my $message = "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a"; ok $res->parse($message); is $message => ''; $message = "Sec-WebSocket-Origin: file://\x0d\x0a" . "Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a" . "0st3Rl&q-2ZU^weu\x00foo\xff"; ok $res->parse($message); ok $res->is_done; is $message => "\x00foo\xff"; $res = Protocol::WebSocket::Response->new( version => 'draft-ietf-hybi-00', host => 'example.com', resource_name => '/demo', origin => 'file://', number1 => 777_007_543, number2 => 114_997_259, challenge => "\x47\x30\x22\x2D\x5A\x3F\x47\x58" ); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: file://\x0d\x0a" . "Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a" . "0st3Rl&q-2ZU^weu"; $res = Protocol::WebSocket::Response->new( version => 'draft-ietf-hybi-00', host => 'example.com', resource_name => '/demo', origin => 'file://', subprotocol => 'sample', number1 => 777_007_543, number2 => 114_997_259, challenge => "\x47\x30\x22\x2D\x5A\x3F\x47\x58" ); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Protocol: sample\x0d\x0a" . "Sec-WebSocket-Origin: file://\x0d\x0a" . "Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a" . "0st3Rl&q-2ZU^weu"; $res = Protocol::WebSocket::Response->new( version => 'draft-ietf-hybi-00', secure => 1, host => 'example.com', resource_name => '/demo', origin => 'file://', number1 => 777_007_543, number2 => 114_997_259, challenge => "\x47\x30\x22\x2D\x5A\x3F\x47\x58" ); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: file://\x0d\x0a" . "Sec-WebSocket-Location: wss://example.com/demo\x0d\x0a" . "\x0d\x0a" . "0st3Rl&q-2ZU^weu"; $res = Protocol::WebSocket::Response->new( version => 'draft-ietf-hybi-00', secure => 1, host => 'example.com', resource_name => '/demo', origin => 'http://', number1 => 777_007_543, number2 => 114_997_259, challenge => "\x47\x30\x22\x2D\x5A\x3F\x47\x58" ); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: http://\x0d\x0a" . "Sec-WebSocket-Location: wss://example.com/demo\x0d\x0a" . "\x0d\x0a" . "0st3Rl&q-2ZU^weu"; $res = Protocol::WebSocket::Response->new( version => 'draft-ietf-hybi-00', host => 'example.com', resource_name => '/demo', origin => 'file://', key1 => "18x 6]8vM;54 *(5: { U1]8 z [ 8", key2 => "1_ tx7X d < nw 334J702) 7]o}` 0", challenge => "Tm[K T2u" ); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: file://\x0d\x0a" . "Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a" . "fQJ,fN/4F4!~K~MH"; server-ssl.t100644001750001750 165613440704326 21741 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-00#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 12; use_ok 'Protocol::WebSocket::Handshake::Server'; my $h = Protocol::WebSocket::Handshake::Server->new; ok $h->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $h->parse("Upgrade: WebSocket\x0d\x0a"); ok $h->parse("Connection: Upgrade\x0d\x0a"); ok $h->parse("Host: example.com\x0d\x0a"); ok $h->parse("Origin: https://example.com\x0d\x0a"); ok $h->parse( "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a"); ok $h->parse( "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a"); ok $h->parse("\x0d\x0aTm[K T2u"); ok !$h->error; ok $h->is_done; is $h->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: https://example.com\x0d\x0a" . "Sec-WebSocket-Location: wss://example.com/demo\x0d\x0a" . "\x0d\x0a" . "fQJ,fN/4F4!~K~MH"; server.t100644001750001750 410213440704326 21127 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-00#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 33; use_ok 'Protocol::WebSocket::Handshake::Server'; my $h = Protocol::WebSocket::Handshake::Server->new; ok !$h->is_done; ok $h->parse; ok $h->parse(''); ok $h->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $h->parse("Upgrade: WebSocket\x0d\x0a"); ok $h->parse("Connection: Upgrade\x0d\x0a"); ok $h->parse("Host: example.com\x0d\x0a"); ok $h->parse("Origin: http://example.com\x0d\x0a"); ok $h->parse( "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a"); ok $h->parse( "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a"); ok $h->parse("\x0d\x0aTm[K T2u"); ok !$h->error; ok $h->is_done; is $h->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a" . "fQJ,fN/4F4!~K~MH"; $h = Protocol::WebSocket::Handshake::Server->new; ok $h->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $h->parse("Upgrade: WebSocket\x0d\x0a"); ok $h->parse("Connection: Upgrade\x0d\x0a"); ok $h->parse("Host: example.com\x0d\x0a"); ok $h->parse("Origin: http://example.com\x0d\x0a"); ok $h->parse( "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a"); ok $h->parse( "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a"); ok $h->parse("\x0d\x0a"); ok $h->is_body; is $h->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a" . "\x0d\x0a"; ok $h->parse("Tm[K T2u"); ok $h->is_done; is $h->to_string => "fQJ,fN/4F4!~K~MH"; my $message = "GET /demo HTTP/1.1\x0d\x0a"; $h = Protocol::WebSocket::Handshake::Server->new; ok $h->parse($message); is $message => ''; ok !$h->error; $h = Protocol::WebSocket::Handshake::Server->new; ok !$h->parse("GET /demo\x0d\x0a"); is $h->error => 'Wrong request line'; client.t100644001750001750 175013440704326 21106 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-10#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 8; use_ok 'Protocol::WebSocket::Handshake::Client'; my $h = Protocol::WebSocket::Handshake::Client->new(version => 'draft-ietf-hybi-10'); $h->url('ws://example.com/demo'); # Mocking $h->req->key("dGhlIHNhbXBsZSBub25jZQ=="); $h->req->cookies('foo=bar; alice=bob'); is $h->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Sec-WebSocket-Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a" . "Sec-WebSocket-Version: 8\x0d\x0a" . "\x0d\x0a"; ok !$h->is_done; ok $h->parse; ok $h->parse(''); ok $h->parse("HTTP/1.1 101 Switching Protocols\x0d\x0a" . "Upgrade: websocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a" . "\x0d\x0a"); ok !$h->error; ok $h->is_done; request.t100644001750001750 1000213440704326 21326 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-10#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 52; use IO::Handle; use_ok 'Protocol::WebSocket::Request'; my $req; $req = Protocol::WebSocket::Request->new; ok !$req->is_done; ok $req->parse; ok $req->parse(''); ok $req->parse("GET /chat HTTP/1.1\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Host: server.example.com\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Upgrade: websocket\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Connection: Upgrade\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Cookie: foo=bar; alice=bob\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a"); ok $req->parse("Sec-WebSocket-Origin: http://example.com\x0d\x0a"); ok $req->parse("Sec-WebSocket-Protocol: chat, superchat\x0d\x0a"); ok $req->parse("Sec-WebSocket-Version: 8\x0d\x0a\x0d\x0a"); is $req->state => 'done'; is $req->key => 'dGhlIHNhbXBsZSBub25jZQ=='; is $req->version => 'draft-ietf-hybi-10'; is $req->subprotocol => 'chat, superchat'; is $req->resource_name => '/chat'; is $req->host => 'server.example.com'; is $req->origin => 'http://example.com'; is $req->cookies->to_string => 'foo=bar; alice=bob'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /chat HTTP/1.1\x0d\x0a"); ok $req->parse("Host: server.example.com\x0d\x0a"); ok $req->parse("Upgrade: websocket\x0d\x0a"); ok $req->parse("Connection:keep-alive, Upgrade\x0d\x0a"); ok $req->parse("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a"); ok $req->parse("Sec-WebSocket-Origin: http://example.com\x0d\x0a"); ok $req->parse("Sec-WebSocket-Protocol: chat, superchat\x0d\x0a"); ok $req->parse("Sec-WebSocket-Version: 8\x0d\x0a\x0d\x0a"); is $req->state => 'done'; is $req->key => 'dGhlIHNhbXBsZSBub25jZQ=='; is $req->version => 'draft-ietf-hybi-10'; is $req->subprotocol => 'chat, superchat'; is $req->resource_name => '/chat'; is $req->host => 'server.example.com'; is $req->origin => 'http://example.com'; $req = Protocol::WebSocket::Request->new( version => 'draft-ietf-hybi-10', host => 'server.example.com', origin => 'http://example.com', cookies => Protocol::WebSocket::Cookie->new->parse('foo=bar; alice=bob'), subprotocol => 'chat, superchat', resource_name => '/chat', key => 'dGhlIHNhbXBsZSBub25jZQ==' ); is $req->to_string => "GET /chat HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: server.example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Sec-WebSocket-Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Protocol: chat, superchat\x0d\x0a" . "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a" . "Sec-WebSocket-Version: 8\x0d\x0a" . "\x0d\x0a"; open my $fh, '<', 't/empty' or die $!; my $io = IO::Handle->new; $io->fdopen(fileno($fh), "r"); $req = Protocol::WebSocket::Request->new_from_psgi( { SCRIPT_NAME => '', PATH_INFO => '/chat', QUERY_STRING => 'foo=bar', HTTP_UPGRADE => 'websocket', HTTP_CONNECTION => 'Upgrade', HTTP_HOST => 'server.example.com', HTTP_COOKIE => 'foo=bar', HTTP_SEC_WEBSOCKET_ORIGIN => 'http://example.com', HTTP_SEC_WEBSOCKET_PROTOCOL => 'chat, superchat', HTTP_SEC_WEBSOCKET_KEY => 'dGhlIHNhbXBsZSBub25jZQ==', HTTP_SEC_WEBSOCKET_VERSION => 8 } ); $req->parse($io); is $req->resource_name => '/chat?foo=bar'; is $req->subprotocol => 'chat, superchat'; is $req->upgrade => 'websocket'; is $req->connection => 'Upgrade'; is $req->host => 'server.example.com'; is $req->cookies->to_string => 'foo=bar'; is $req->origin => 'http://example.com'; is $req->key => 'dGhlIHNhbXBsZSBub25jZQ=='; ok $req->is_done; is $req->version => 'draft-ietf-hybi-10'; response.t100644001750001750 165313440704326 21470 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-10#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 11; use_ok 'Protocol::WebSocket::Response'; my $res; $res = Protocol::WebSocket::Response->new; ok $res->parse("HTTP/1.1 101 Switching Protocols\x0d\x0a"); ok $res->parse("Upgrade: websocket\x0d\x0a"); ok $res->parse("Connection: Upgrade\x0d\x0a"); ok $res->parse("Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a"); ok $res->parse("Sec-WebSocket-Protocol: chat\x0d\x0a"); ok $res->parse("\x0d\x0a"); ok $res->is_done; ok !$res->secure; is $res->subprotocol => 'chat'; $res = Protocol::WebSocket::Response->new( key => 'dGhlIHNhbXBsZSBub25jZQ==', subprotocol => 'chat' ); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a" . "Sec-WebSocket-Protocol: chat\x0d\x0a" . "\x0d\x0a"; server.t100644001750001750 154613440704326 21141 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-10#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 15; use_ok 'Protocol::WebSocket::Handshake::Server'; my $h = Protocol::WebSocket::Handshake::Server->new; ok !$h->is_done; ok $h->parse; ok $h->parse(''); ok $h->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $h->parse("Upgrade: websocket\x0d\x0a"); ok $h->parse("Connection: Upgrade\x0d\x0a"); ok $h->parse("Host: example.com\x0d\x0a"); ok $h->parse("Sec-WebSocket-Origin: http://example.com\x0d\x0a"); ok $h->parse("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a"); ok $h->parse("Sec-WebSocket-Version: 8\x0d\x0a"); ok $h->parse("\x0d\x0a"); ok !$h->error; ok $h->is_done; is $h->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a" . "\x0d\x0a"; client.t100644001750001750 167213440704326 21120 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-17#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 8; use_ok 'Protocol::WebSocket::Handshake::Client'; my $h = Protocol::WebSocket::Handshake::Client->new; $h->url('ws://example.com/demo'); # Mocking $h->req->key("dGhlIHNhbXBsZSBub25jZQ=="); $h->req->cookies('foo=bar; alice=bob'); is $h->to_string => "GET /demo HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a" . "Sec-WebSocket-Version: 13\x0d\x0a" . "\x0d\x0a"; ok !$h->is_done; ok $h->parse; ok $h->parse(''); ok $h->parse("HTTP/1.1 101 Switching Protocols\x0d\x0a" . "Upgrade: websocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a" . "\x0d\x0a"); ok !$h->error; ok $h->is_done; frame.t100644001750001750 1475613440704326 20763 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-17#!/usr/bin/env perl use strict; use warnings; use utf8; use Test::More; use Encode; use_ok 'Protocol::WebSocket::Frame'; my $f = Protocol::WebSocket::Frame->new; is $f->version, 'draft-ietf-hybi-17'; $f->append; ok not defined $f->next; $f->append(''); ok not defined $f->next; # Not masked $f->append(pack('H*', "810548656c6c6f")); is $f->next_bytes, 'Hello'; is $f->opcode => 1; ok $f->is_text; # Multi $f->append(pack('H*', "810548656c6c6f") . pack('H*', "810548656c6c6f")); is $f->next_bytes, 'Hello'; is $f->next_bytes, 'Hello'; # Masked $f->append(pack('H*', "818537fa213d7f9f4d5158")); is $f->next_bytes, 'Hello'; is $f->opcode => 1; # Fragments $f->append(pack('H*', "010348656c")); ok not defined $f->next_bytes; $f->append(pack('H*', "80026c6f")); is $f->next_bytes, 'Hello'; is $f->opcode => 1; # Multi fragments $f->append(pack('H*', "010348656c") . pack('H*', "80026c6f")); is $f->next_bytes, 'Hello'; is $f->opcode => 1; # Injected control frame (1 fragment, ping, 2 fragment) $f->append(pack('H*', "010348656c")); $f->append(pack('H*', "890548656c6c6f")); $f->append(pack('H*', "80026c6f")); is $f->next_bytes, 'Hello'; is $f->opcode => 9; is $f->next_bytes, 'Hello'; is $f->opcode => 1; # Too many fragments $f->append(pack('H*', "010348656c")) for 1 .. 129; eval { $f->next_bytes }; ok $@; # Ping request $f = Protocol::WebSocket::Frame->new; $f->append(pack('H*', "890548656c6c6f")); is $f->next_bytes => 'Hello'; is $f->opcode => 9; ok $f->is_ping; # Ping response $f->append(pack('H*', "8a0548656c6c6f")); is $f->next_bytes => 'Hello'; is $f->opcode => 10; ok $f->is_pong; # 256 bytes $f->append(pack('H*', "827E0100" . ('05' x 256))); is(length $f->next_bytes, 256); is $f->opcode => 2; ok $f->is_binary; # 64KiB $f->append(pack('H*', "827F0000000000010000" . ('05' x 65536))); is(length $f->next_bytes, 65536); is $f->opcode => 2; # Too big frame $f->append(pack('H*', "827F0000000000100000" . ('05' x (65536 + 1)))); eval { $f->next_bytes }; ok $@; $f = Protocol::WebSocket::Frame->new('Hello'); is $f->to_bytes => pack('H*', "810548656c6c6f"); $f = Protocol::WebSocket::Frame->new( buffer => 'Hello', masked => 1, mask => '939139389' ); is $f->to_bytes, pack('H*', "818537fa213d7f9f4d5158"); # Ping $f = Protocol::WebSocket::Frame->new(buffer => 'Hello', type => 'ping'); is $f->to_bytes => pack('H*', "890548656c6c6f"); # 256 bytes $f = Protocol::WebSocket::Frame->new( buffer => pack('H*', ('05' x 256)), type => 'binary' ); is $f->to_bytes => pack('H*', "827E0100" . ('05' x 256)); # 64KiB bytes $f = Protocol::WebSocket::Frame->new( buffer => pack('H*', ('05' x 65536)), type => 'binary' ); is $f->to_bytes => pack('H*', "827F0000000000010000" . ('05' x 65536)); $f = Protocol::WebSocket::Frame->new; $f->append(Protocol::WebSocket::Frame->new('привет')->to_bytes); is $f->next => 'привет'; # Too big $f = Protocol::WebSocket::Frame->new( buffer => pack('H*', ('05' x (65536 + 1))), type => 'binary' ); eval { $f->to_bytes }; ok $@; # initialize fin flag to zero $f = Protocol::WebSocket::Frame->new(buffer => 'Hello', fin => 0); is $f->to_bytes => pack('H*', "010548656c6c6f"); # continuation frame $f = Protocol::WebSocket::Frame->new(buffer => "Hello", type => "continuation"); is $f->to_bytes => pack('H*', "800548656c6c6f"); # generate fragmented frames $f = Protocol::WebSocket::Frame->new(); $f->append( Protocol::WebSocket::Frame->new( buffer => "Hello", type => "binary", fin => 0 )->to_bytes ); is $f->next_bytes => undef; $f->append( Protocol::WebSocket::Frame->new( buffer => ", ", type => "continuation", fin => 0 )->to_bytes ); is $f->next_bytes => undef; $f->append( Protocol::WebSocket::Frame->new( buffer => "World!", type => "continuation", fin => 1 )->to_bytes ); is $f->next_bytes => "Hello, World!"; is $f->opcode => 2; subtest 'constructor type values and is_$type are consistent' => sub { my @types = qw(continuation text binary ping pong close); foreach my $type (@types) { my $f = Protocol::WebSocket::Frame->new(type => $type); foreach my $test_type (@types) { my $method = "is_$test_type"; if ($type eq $test_type) { ok $f->$method, "type $type $method"; } else { ok !$f->$method, "type $type not $method"; } } } }; subtest 'opcode accessor/mutator' => sub { my $f = Protocol::WebSocket::Frame->new("Hello"); is $f->opcode => 1; is $f->to_bytes => pack('H*', "810548656c6c6f"); $f->opcode(2); is $f->opcode => 2; is $f->to_bytes => pack('H*', "820548656c6c6f"); $f->opcode(0); is $f->opcode => 0; is $f->to_bytes => pack('H*', "800548656c6c6f"); }; subtest 'opcode immediately available' => sub { my $f = Protocol::WebSocket::Frame->new(buffer => "Hello", opcode => 8); is $f->opcode => 8; is $f->to_bytes => pack('H*', "880548656c6c6f"); }; subtest 'if both type and opcode are specified in new(), type wins' => sub { my $f = Protocol::WebSocket::Frame->new( buffer => "Hello", type => "ping", opcode => 2 ); is $f->opcode => 9; is $f->to_bytes => pack('H*', "890548656c6c6f"); }; subtest 'mask frame' => sub { foreach my $test_case ( {label => "foobar", opcode => 9, buffer => "Foobar"}, {label => "empty", opcode => 1, buffer => ""}, {label => "character zero", opcode => 2, buffer => "0"}, {label => "number zero", opcode => 2, buffer => 0}, {label => "number 123", opcode => 1, buffer => 123}, ) { my $f = Protocol::WebSocket::Frame->new( buffer => $test_case->{buffer}, opcode => $test_case->{opcode}, masked => 1 ); my $frame_bytestring = $f->to_bytes; my @frame_bytes = unpack("C*", $frame_bytestring); ok $frame_bytes[1] & 0x80, "$test_case->{label}: MASK bit is set"; my $p = Protocol::WebSocket::Frame->new(); $p->append($frame_bytestring); my $message = $p->next_bytes; is $message => $test_case->{buffer}, "$test_case->{label}: parse buffer OK"; is $p->opcode => $test_case->{opcode}, "$test_case->{label}: parse opcode OK"; } }; subtest 'append is destructive' => sub { my $f = Protocol::WebSocket::Frame->new(); my $chunk = pack('H*', "810548656c6c6f"); $f->append($chunk); is $chunk => "", "append() is destructive"; }; done_testing; request.t100644001750001750 1107413440704326 21347 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-17#!/usr/bin/env perl use strict; use warnings; use Test::More; use_ok 'Protocol::WebSocket::Request'; my $req; $req = Protocol::WebSocket::Request->new; ok !$req->is_done; ok $req->parse; ok $req->parse(''); ok $req->parse("GET /chat HTTP/1.1\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Host: server.example.com\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Cookie: foo=bar;alice=bob\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Upgrade: websocket\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Connection: Upgrade\x0d\x0a"); is $req->state => 'fields'; ok $req->parse("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a"); ok $req->parse("Origin: http://example.com\x0d\x0a"); ok $req->parse("Sec-WebSocket-Protocol: chat, superchat\x0d\x0a"); ok $req->parse("Sec-WebSocket-Version: 13\x0d\x0a\x0d\x0a"); is $req->state => 'done'; is $req->key => 'dGhlIHNhbXBsZSBub25jZQ=='; is $req->version => 'draft-ietf-hybi-17'; is $req->subprotocol => 'chat, superchat'; is $req->resource_name => '/chat'; is $req->host => 'server.example.com'; is $req->origin => 'http://example.com'; is $req->cookies->to_string => 'foo=bar; alice=bob'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /chat HTTP/1.1\x0d\x0a"); ok $req->parse("Host: server.example.com\x0d\x0a"); ok $req->parse("Upgrade: websocket\x0d\x0a"); ok $req->parse("Connection:keep-alive, Upgrade\x0d\x0a"); ok $req->parse("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a"); ok $req->parse("Origin: http://example.com\x0d\x0a"); ok $req->parse("Sec-WebSocket-Protocol: chat, superchat\x0d\x0a"); ok $req->parse("Sec-WebSocket-Version: 13\x0d\x0a\x0d\x0a"); is $req->state => 'done'; is $req->key => 'dGhlIHNhbXBsZSBub25jZQ=='; is $req->version => 'draft-ietf-hybi-17'; is $req->subprotocol => 'chat, superchat'; is $req->resource_name => '/chat'; is $req->host => 'server.example.com'; is $req->origin => 'http://example.com'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /chat HTTP/1.1\x0d\x0a"); ok $req->parse("Host: server.example.com\x0d\x0a"); ok $req->parse("Upgrade: websocket\x0d\x0a"); ok $req->parse("Connection:keep-alive, Upgrade\x0d\x0a"); ok $req->parse("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a"); ok $req->parse("Sec-WebSocket-Origin: http://example.com\x0d\x0a"); ok $req->parse("Sec-WebSocket-Protocol: chat, superchat\x0d\x0a"); ok $req->parse("Sec-WebSocket-Version: 13\x0d\x0a\x0d\x0a"); is $req->state => 'done'; is $req->key => 'dGhlIHNhbXBsZSBub25jZQ=='; is $req->version => 'draft-ietf-hybi-17'; is $req->subprotocol => 'chat, superchat'; is $req->resource_name => '/chat'; is $req->host => 'server.example.com'; is $req->origin => 'http://example.com'; $req = Protocol::WebSocket::Request->new( host => 'server.example.com', origin => 'http://example.com', cookies => Protocol::WebSocket::Cookie->new->parse('foo=bar; alice=bob'), subprotocol => 'chat, superchat', resource_name => '/chat', key => 'dGhlIHNhbXBsZSBub25jZQ==' ); is $req->to_string => "GET /chat HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: server.example.com\x0d\x0a" . "Cookie: foo=bar; alice=bob\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Protocol: chat, superchat\x0d\x0a" . "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a" . "Sec-WebSocket-Version: 13\x0d\x0a" . "\x0d\x0a"; subtest 'add custom headers' => sub { my $req = Protocol::WebSocket::Request->new( host => 'server.example.com', origin => 'http://example.com', resource_name => '/chat', key => 'dGhlIHNhbXBsZSBub25jZQ==', headers => ['X-Foo' => 'bar'] ); is $req->to_string => "GET /chat HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: server.example.com\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a" . "Sec-WebSocket-Version: 13\x0d\x0a" . "X-Foo: bar\x0d\x0a" . "\x0d\x0a"; is $req->to_string => "GET /chat HTTP/1.1\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Host: server.example.com\x0d\x0a" . "Origin: http://example.com\x0d\x0a" . "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a" . "Sec-WebSocket-Version: 13\x0d\x0a" . "X-Foo: bar\x0d\x0a" . "\x0d\x0a"; }; done_testing; request_psgi.t100644001750001750 256513440704326 22356 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-17#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 11; use IO::Handle; use_ok 'Protocol::WebSocket::Request'; my $req; $req = Protocol::WebSocket::Request->new; open my $fh, '<', \'' or die $!; my $io = IO::Handle->new; $io->fdopen( fileno($fh), "r" ); $req = Protocol::WebSocket::Request->new_from_psgi( { SCRIPT_NAME => '', PATH_INFO => '/chat', QUERY_STRING => 'foo=bar', HTTP_UPGRADE => 'websocket', HTTP_CONNECTION => 'Upgrade', HTTP_HOST => 'server.example.com', HTTP_COOKIE => 'foo=bar', HTTP_ORIGIN => 'http://example.com', HTTP_SEC_WEBSOCKET_PROTOCOL => 'chat, superchat', HTTP_SEC_WEBSOCKET_KEY => 'dGhlIHNhbXBsZSBub25jZQ==', HTTP_SEC_WEBSOCKET_VERSION => 13 } ); $req->parse($io); is $req->resource_name => '/chat?foo=bar'; is $req->subprotocol => 'chat, superchat'; is $req->upgrade => 'websocket'; is $req->connection => 'Upgrade'; is $req->host => 'server.example.com'; is $req->cookies->to_string => 'foo=bar'; is $req->origin => 'http://example.com'; is $req->key => 'dGhlIHNhbXBsZSBub25jZQ=='; ok $req->is_done; is $req->version => 'draft-ietf-hybi-17'; response.t100644001750001750 165313440704326 21477 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-17#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 11; use_ok 'Protocol::WebSocket::Response'; my $res; $res = Protocol::WebSocket::Response->new; ok $res->parse("HTTP/1.1 101 Switching Protocols\x0d\x0a"); ok $res->parse("Upgrade: websocket\x0d\x0a"); ok $res->parse("Connection: Upgrade\x0d\x0a"); ok $res->parse("Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a"); ok $res->parse("Sec-WebSocket-Protocol: chat\x0d\x0a"); ok $res->parse("\x0d\x0a"); ok $res->is_done; ok !$res->secure; is $res->subprotocol => 'chat'; $res = Protocol::WebSocket::Response->new( key => 'dGhlIHNhbXBsZSBub25jZQ==', subprotocol => 'chat' ); is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a" . "Sec-WebSocket-Protocol: chat\x0d\x0a" . "\x0d\x0a"; server.t100644001750001750 153113440704326 21142 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t/draft-ietf-hybi-17#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 15; use_ok 'Protocol::WebSocket::Handshake::Server'; my $h = Protocol::WebSocket::Handshake::Server->new; ok !$h->is_done; ok $h->parse; ok $h->parse(''); ok $h->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $h->parse("Upgrade: websocket\x0d\x0a"); ok $h->parse("Connection: Upgrade\x0d\x0a"); ok $h->parse("Host: example.com\x0d\x0a"); ok $h->parse("Origin: http://example.com\x0d\x0a"); ok $h->parse("Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\x0d\x0a"); ok $h->parse("Sec-WebSocket-Version: 13\x0d\x0a"); ok $h->parse("\x0d\x0a"); ok !$h->error; ok $h->is_done; is $h->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" . "Upgrade: WebSocket\x0d\x0a" . "Connection: Upgrade\x0d\x0a" . "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\x0d\x0a" . "\x0d\x0a"; empty100644001750001750 013440704326 15233 0ustar00vtivti000000000000Protocol-WebSocket-0.26/tframe.t100644001750001750 526413440704326 15532 0ustar00vtivti000000000000Protocol-WebSocket-0.26/tuse strict; use warnings; use Test::More; use_ok 'Protocol::WebSocket::Frame'; is(Protocol::WebSocket::Frame->new->max_payload_size, 65536, 'default max_payload_size'); is(Protocol::WebSocket::Frame->new(max_payload_size => 22)->max_payload_size, 22, 'override max_payload_size'); is(Protocol::WebSocket::Frame->new(max_payload_size => 0)->max_payload_size, 0, 'turn off max_payload_size'); is( Protocol::WebSocket::Frame->new(max_payload_size => undef) ->max_payload_size, undef, 'turn off max_payload_size' ); subtest 'payload too large (to_bytes)' => sub { my $frame = Protocol::WebSocket::Frame->new(buffer => 'x' x 65537); eval { $frame->to_bytes }; like $@, qr/Payload is too big\. Send shorter messages or increase max_payload_size/; }; subtest 'payload larger than 65536, but under max (to_bytes)' => sub { my $frame = Protocol::WebSocket::Frame->new( buffer => 'x' x 65537, max_payload_size => 65537 ); eval { $frame->to_bytes }; is $@, ''; }; subtest 'turn off payload size checking (to_bytes)' => sub { my $frame = Protocol::WebSocket::Frame->new( buffer => 'x' x 65537, max_payload_size => 0 ); eval { $frame->to_bytes }; is $@, ''; }; my $large_frame = Protocol::WebSocket::Frame->new(buffer => 'x' x 65537, max_payload_size => 0); subtest 'payload too large (next_bytes)' => sub { my $frame = Protocol::WebSocket::Frame->new; $frame->append($large_frame->to_bytes); eval { $frame->next_bytes }; like $@, qr/Payload is too big\. Deny big message/; }; subtest 'payload larger than 65536, but under max (next_bytes)' => sub { my $frame = Protocol::WebSocket::Frame->new(max_payload_size => 65537); $frame->append($large_frame->to_bytes); eval { $frame->next_bytes }; is $@, ''; }; subtest 'turn off payload size checking (next_bytes)' => sub { my $frame = Protocol::WebSocket::Frame->new(max_payload_size => 0); $frame->append($large_frame->to_bytes); eval { $frame->next_bytes }; is $@, ''; }; my $first_fragment = Protocol::WebSocket::Frame->new(buffer => 'x', type => 'text', fin => 0); my $a_fragment = Protocol::WebSocket::Frame->new(buffer => 'x', type => 'continuation', fin => 0); subtest 'maximum number or fragments exceeded' => sub { local $Protocol::WebSocket::Frame::MAX_FRAGMENTS_AMOUNT = 42; my $frame = Protocol::WebSocket::Frame->new(); is $frame->{max_fragments_amount}, 42; $frame->append($first_fragment->to_bytes); $frame->append($a_fragment->to_bytes) for ( 1 .. $frame->{max_fragments_amount} ); eval { $frame->next_bytes }; like $@, qr/Too many fragments/; }; done_testing; message100644001750001750 42113440704326 15570 0ustar00vtivti000000000000Protocol-WebSocket-0.26/tGET /demo HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: example.com Origin: http://example.com Sec-WebSocket-Protocol: sample Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8 Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0 Content-Length: 8 Tm[K T2u message.t100644001750001750 127313440704326 16060 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 9; use IO::Handle; use_ok 'Protocol::WebSocket::Message'; my $m; $m = Protocol::WebSocket::Message->new; ok $m->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"); ok $m->parse("Upgrade: WebSocket\x0d\x0a"); ok $m->parse("Connection: Upgrade\x0d\x0a"); ok $m->parse("Sec-WebSocket-Origin: file://\x0d\x0a"); ok $m->parse("Sec-WebSocket-Location: ws://example.com/demo\x0d\x0a"); ok $m->parse("\x0d\x0a0st\x0d\x0al&q-2ZU^weu"); ok $m->is_done; open my $fh, '<', 't/message' or die $!; my $io = IO::Handle->new; $io->fdopen(fileno($fh), "r"); $m = Protocol::WebSocket::Message->new; $m->parse($io); ok $m->is_done; request_common.t100644001750001750 153513440704326 17475 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 14; use_ok 'Protocol::WebSocket::Request'; my $req = Protocol::WebSocket::Request->new; $req = Protocol::WebSocket::Request->new; ok !$req->is_done; ok not defined $req->parse("foo\x0d\x0a"); ok $req->is_state('error'); is $req->error => 'Wrong request line'; $req = Protocol::WebSocket::Request->new; ok $req->parse("GET /demo HTTP/1.1\x0d\x0a"); ok $req->parse("Upgrade: WebSocket\x0d\x0a"); ok $req->parse("Connection: Upgrade\x0d\x0a"); ok $req->parse("Origin: http://example.com\x0d\x0a"); ok not defined $req->parse("\x0d\x0a"); ok $req->is_state('error'); local $Protocol::WebSocket::Message::MAX_MESSAGE_SIZE = 1024; $req = Protocol::WebSocket::Request->new; ok not defined $req->parse('x' x (1024 * 10)); ok $req->is_state('error'); is $req->error => 'Message is too long'; response_common.t100644001750001750 145113440704326 17640 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 8; use_ok 'Protocol::WebSocket::Response'; my $res; $res = Protocol::WebSocket::Response->new; $res->parse("foo\x0d\x0a"); ok $res->is_state('error'); is $res->error => 'Wrong response line. Got [[foo]], expected [[HTTP/1.1 101 ]]'; $res = Protocol::WebSocket::Response->new; $res->parse(("1234567890" x 10) . "\x0d\x0a"); ok $res->is_state('error'); is $res->error => 'Wrong response line. Got [[12345678901234567890123456789012345678901234567890123456789012345678901234567...]], expected [[HTTP/1.1 101 ]]'; local $Protocol::WebSocket::Message::MAX_MESSAGE_SIZE = 1024; $res = Protocol::WebSocket::Response->new; ok not defined $res->parse('x' x (1024 * 10)); ok $res->is_state('error'); is $res->error => 'Message is too long'; rsv.t100644001750001750 212413440704326 15242 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t#!/usr/bin/env perl use FindBin qw($Bin); use lib "$Bin/../lib"; use lib "$Bin/../t"; use strict; use warnings; use utf8; use Test::More; use Encode; use_ok 'Protocol::WebSocket::Frame'; my $f = Protocol::WebSocket::Frame->new( buffer => '☺', rsv => [0, 0, 0] ); is substr($f->to_bytes, 0, 1) => "\x81"; $f = Protocol::WebSocket::Frame->new( buffer => '☺', rsv => [0, 0, 1] ); is substr($f->to_bytes, 0, 1) => "\x91"; $f = Protocol::WebSocket::Frame->new( buffer => '☺', rsv => [0, 1, 0] ); is substr($f->to_bytes, 0, 1) => "\xa1"; $f = Protocol::WebSocket::Frame->new( buffer => '☺', rsv => [1, 0, 0] ); is substr($f->to_bytes, 0, 1) => "\xc1"; $f = Protocol::WebSocket::Frame->new( buffer => '☺', rsv => [1, 0, 1] ); is substr($f->to_bytes, 0, 1) => "\xd1"; $f = Protocol::WebSocket::Frame->new( buffer => '☺', rsv => [1, 1, 0] ); is substr($f->to_bytes, 0, 1) => "\xe1"; $f = Protocol::WebSocket::Frame->new( buffer => '☺', rsv => [1, 1, 1] ); is substr($f->to_bytes, 0, 1) => "\xf1"; done_testing(); url.t100644001750001750 343613440704326 15241 0ustar00vtivti000000000000Protocol-WebSocket-0.26/t#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 36; use_ok 'Protocol::WebSocket::URL'; my $url = Protocol::WebSocket::URL->new; ok $url->parse('ws://example.com'); ok !$url->secure; is $url->host => 'example.com'; is $url->port => '80'; is $url->resource_name => '/'; $url = Protocol::WebSocket::URL->new; ok $url->parse('ws://example.com/'); ok !$url->secure; is $url->host => 'example.com'; is $url->resource_name => '/'; $url = Protocol::WebSocket::URL->new; ok $url->parse('ws://example.com/demo'); ok !$url->secure; is $url->host => 'example.com'; is $url->resource_name => '/demo'; $url = Protocol::WebSocket::URL->new; ok $url->parse('ws://example.com:3000'); ok !$url->secure; is $url->host => 'example.com'; is $url->port => '3000'; is $url->resource_name => '/'; $url = Protocol::WebSocket::URL->new; ok $url->parse('ws://example.com/demo?foo=bar'); ok !$url->secure; is $url->host => 'example.com'; is $url->resource_name => '/demo?foo=bar'; $url = Protocol::WebSocket::URL->new; ok $url->parse('wss://example.com'); ok $url->secure; is $url->host => 'example.com'; is $url->port => '443'; is $url->resource_name => '/'; $url = Protocol::WebSocket::URL->new; ok $url->parse('wss://example.com:3000'); ok $url->secure; is $url->host => 'example.com'; is $url->port => '3000'; is $url->resource_name => '/'; $url = Protocol::WebSocket::URL->new(host => 'foo.com', secure => 1); is $url->to_string => 'wss://foo.com/'; $url = Protocol::WebSocket::URL->new( host => 'foo.com', resource_name => '/demo' ); is $url->to_string => 'ws://foo.com/demo'; $url = Protocol::WebSocket::URL->new( host => 'foo.com', port => 3000 ); is $url->to_string => 'ws://foo.com:3000/'; wsconsole100755001750001750 367113440704326 16727 0ustar00vtivti000000000000Protocol-WebSocket-0.26/util#!/usr/bin/env perl use strict; use warnings; use lib 'lib'; use Devel::Hexdump (); use AnyEvent; use AnyEvent::Socket; use AnyEvent::Handle; use Protocol::WebSocket::Client; $|++; sub debug { warn "$_[0]\n" } sub debug_hex { debug('v' x 10); debug(Devel::Hexdump::xd($_[0])); debug('^' x 10); } my $cv = AnyEvent->condvar; my ($url, $version) = @ARGV; die "Usage: $0 " unless $url; my $client = Protocol::WebSocket::Client->new(url => $url, version => $version); my $ws_handle; debug("! Using version '$version'"); debug( "! Connecting to '" . $client->url->host . ':' . $client->url->port . "'"); my ($host, $port) = ($client->url->host, $client->url->port); tcp_connect $host, $port, sub { my ($fh) = @_ or return $cv->send("Connect failed: $!"); debug("! Connected"); $ws_handle = AnyEvent::Handle->new( fh => $fh, on_eof => sub { debug("! Server disconnected"); $cv->send; }, on_error => sub { debug("! Error: " . $_[1]); $cv->send; }, on_read => sub { my ($handle) = @_; my $buf = delete $handle->{rbuf}; debug("< Reading"); debug_hex($buf); $client->read($buf); } ); $client->on( write => sub { my $client = shift; my ($buf) = @_; debug("> Writing"); debug_hex($buf); $ws_handle->push_write($buf); } ); $client->on( read => sub { my $self = shift; my ($buf) = @_; } ); $client->connect; }; my $stdin = AnyEvent::Handle->new( fh => \*STDIN, on_read => sub { my $handle = shift; my $buf = delete $handle->{rbuf}; $client->write($buf); }, on_eof => sub { $client->disconnect; $ws_handle->destroy; $cv->send; } ); $cv->wait; changes.t100644001750001750 24113440704326 16206 0ustar00vtivti000000000000Protocol-WebSocket-0.26/xtuse strict; use warnings; use Test::More; eval 'use Test::CPAN::Changes'; plan skip_all => 'Test::CPAN::Changes required for this test' if $@; changes_ok(); pod-coverage.t100644001750001750 105013440704326 17170 0ustar00vtivti000000000000Protocol-WebSocket-0.26/xtuse strict; use warnings; use Test::More; # Ensure a recent version of Test::Pod::Coverage my $min_tpc = 1.08; eval "use Test::Pod::Coverage $min_tpc"; plan skip_all => "Test::Pod::Coverage $min_tpc required for testing POD coverage" if $@; # Test::Pod::Coverage doesn't require a minimum Pod::Coverage version, # but older versions don't recognize some common documentation styles my $min_pc = 0.18; eval "use Pod::Coverage $min_pc"; plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage" if $@; all_pod_coverage_ok(); pod.t100644001750001750 33613440704326 15365 0ustar00vtivti000000000000Protocol-WebSocket-0.26/xtuse strict; use warnings; use Test::More; # Ensure a recent version of Test::Pod my $min_tp = 1.22; eval "use Test::Pod $min_tp"; plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; all_pod_files_ok(); META.json100644001750001750 760213440704326 15427 0ustar00vtivti000000000000Protocol-WebSocket-0.26{ "abstract" : "WebSocket protocol", "author" : [ "Viacheslav Tykhanovskyi, C." ], "dynamic_config" : 0, "generated_by" : "Minilla/v3.1.3", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Protocol-WebSocket", "no_index" : { "directory" : [ "t", "xt", "inc", "share", "eg", "examples", "author", "builder" ] }, "prereqs" : { "configure" : { "requires" : { "Module::Build::Tiny" : "0.035" } }, "develop" : { "requires" : { "Test::CPAN::Meta" : "0", "Test::MinimumVersion::Fast" : "0.04", "Test::PAUSE::Permissions" : "0.04", "Test::Pod" : "1.41", "Test::Spellunker" : "v0.2.7" } }, "runtime" : { "requires" : { "Carp" : "0", "Digest::MD5" : "0", "Digest::SHA" : "0", "Scalar::Util" : "0" } } }, "provides" : { "Protocol::WebSocket" : { "file" : "lib/Protocol/WebSocket.pm", "version" : "0.26" }, "Protocol::WebSocket::Client" : { "file" : "lib/Protocol/WebSocket/Client.pm" }, "Protocol::WebSocket::Cookie" : { "file" : "lib/Protocol/WebSocket/Cookie.pm" }, "Protocol::WebSocket::Cookie::Request" : { "file" : "lib/Protocol/WebSocket/Cookie/Request.pm" }, "Protocol::WebSocket::Cookie::Response" : { "file" : "lib/Protocol/WebSocket/Cookie/Response.pm" }, "Protocol::WebSocket::Frame" : { "file" : "lib/Protocol/WebSocket/Frame.pm" }, "Protocol::WebSocket::Handshake" : { "file" : "lib/Protocol/WebSocket/Handshake.pm" }, "Protocol::WebSocket::Handshake::Client" : { "file" : "lib/Protocol/WebSocket/Handshake/Client.pm" }, "Protocol::WebSocket::Handshake::Server" : { "file" : "lib/Protocol/WebSocket/Handshake/Server.pm" }, "Protocol::WebSocket::Message" : { "file" : "lib/Protocol/WebSocket/Message.pm" }, "Protocol::WebSocket::Request" : { "file" : "lib/Protocol/WebSocket/Request.pm" }, "Protocol::WebSocket::Response" : { "file" : "lib/Protocol/WebSocket/Response.pm" }, "Protocol::WebSocket::Stateful" : { "file" : "lib/Protocol/WebSocket/Stateful.pm" }, "Protocol::WebSocket::URL" : { "file" : "lib/Protocol/WebSocket/URL.pm" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/vti/protocol-websocket/issues" }, "homepage" : "https://github.com/vti/protocol-websocket", "repository" : { "url" : "git://github.com/vti/protocol-websocket.git", "web" : "https://github.com/vti/protocol-websocket" } }, "version" : "0.26", "x_contributors" : [ "Anton Petrusevich ", "BlueT - Matthew Lien - 練喆明 ", "Chia-liang Kao ", "Daniel Kamil Kozar ", "Eric Wastl ", "Graham Ollis ", "Joao Orui ", "Jon Gentle ", "Jonathan Barber ", "Lee Aylward ", "Max Maischein ", "Michal Špaček ", "Neil Bowers ", "Paul \"LeoNerd\" Evans ", "Toshio Ito ", "vti ", "vti " ], "x_serialization_backend" : "JSON::PP version 2.97001", "x_static_install" : 1 } META.yml100644001750001750 522313440704326 15254 0ustar00vtivti000000000000Protocol-WebSocket-0.26--- abstract: 'WebSocket protocol' author: - 'Viacheslav Tykhanovskyi, C.' build_requires: {} configure_requires: Module::Build::Tiny: '0.035' dynamic_config: 0 generated_by: 'Minilla/v3.1.3, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Protocol-WebSocket no_index: directory: - t - xt - inc - share - eg - examples - author - builder provides: Protocol::WebSocket: file: lib/Protocol/WebSocket.pm version: '0.26' Protocol::WebSocket::Client: file: lib/Protocol/WebSocket/Client.pm Protocol::WebSocket::Cookie: file: lib/Protocol/WebSocket/Cookie.pm Protocol::WebSocket::Cookie::Request: file: lib/Protocol/WebSocket/Cookie/Request.pm Protocol::WebSocket::Cookie::Response: file: lib/Protocol/WebSocket/Cookie/Response.pm Protocol::WebSocket::Frame: file: lib/Protocol/WebSocket/Frame.pm Protocol::WebSocket::Handshake: file: lib/Protocol/WebSocket/Handshake.pm Protocol::WebSocket::Handshake::Client: file: lib/Protocol/WebSocket/Handshake/Client.pm Protocol::WebSocket::Handshake::Server: file: lib/Protocol/WebSocket/Handshake/Server.pm Protocol::WebSocket::Message: file: lib/Protocol/WebSocket/Message.pm Protocol::WebSocket::Request: file: lib/Protocol/WebSocket/Request.pm Protocol::WebSocket::Response: file: lib/Protocol/WebSocket/Response.pm Protocol::WebSocket::Stateful: file: lib/Protocol/WebSocket/Stateful.pm Protocol::WebSocket::URL: file: lib/Protocol/WebSocket/URL.pm requires: Carp: '0' Digest::MD5: '0' Digest::SHA: '0' Scalar::Util: '0' resources: bugtracker: https://github.com/vti/protocol-websocket/issues homepage: https://github.com/vti/protocol-websocket repository: git://github.com/vti/protocol-websocket.git version: '0.26' x_contributors: - 'Anton Petrusevich ' - 'BlueT - Matthew Lien - 練喆明 ' - 'Chia-liang Kao ' - 'Daniel Kamil Kozar ' - 'Eric Wastl ' - 'Graham Ollis ' - 'Joao Orui ' - 'Jon Gentle ' - 'Jonathan Barber ' - 'Lee Aylward ' - 'Max Maischein ' - 'Michal Špaček ' - 'Neil Bowers ' - 'Paul "LeoNerd" Evans ' - 'Toshio Ito ' - 'vti ' - 'vti ' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' x_static_install: 1 MANIFEST100644001750001750 322213440704326 15131 0ustar00vtivti000000000000Protocol-WebSocket-0.26Build.PL Changes LICENSE MANIFEST.SKIP README.md cpanfile examples/anyevent-http-server.pl examples/anyevent.pl examples/event.pl examples/event_reactor.pl examples/io-async.pl examples/io-event.pl examples/io-lamda.pl examples/io-multiplex.pl examples/io-poll.pl examples/net-server-multiplex.pl examples/nginx-engine.pl examples/plack.psgi examples/poe.pl examples/reflex.pl lib/Protocol/WebSocket.pm lib/Protocol/WebSocket/Client.pm lib/Protocol/WebSocket/Cookie.pm lib/Protocol/WebSocket/Cookie/Request.pm lib/Protocol/WebSocket/Cookie/Response.pm lib/Protocol/WebSocket/Frame.pm lib/Protocol/WebSocket/Handshake.pm lib/Protocol/WebSocket/Handshake/Client.pm lib/Protocol/WebSocket/Handshake/Server.pm lib/Protocol/WebSocket/Message.pm lib/Protocol/WebSocket/Request.pm lib/Protocol/WebSocket/Response.pm lib/Protocol/WebSocket/Stateful.pm lib/Protocol/WebSocket/URL.pm minil.toml t/challenge t/client.t t/cookie.t t/draft-hixie-75/request.t t/draft-hixie-75/response.t t/draft-ietf-hybi-00/client-ssl.t t/draft-ietf-hybi-00/client.t t/draft-ietf-hybi-00/frame.t t/draft-ietf-hybi-00/request.t t/draft-ietf-hybi-00/response.t t/draft-ietf-hybi-00/server-ssl.t t/draft-ietf-hybi-00/server.t t/draft-ietf-hybi-10/client.t t/draft-ietf-hybi-10/request.t t/draft-ietf-hybi-10/response.t t/draft-ietf-hybi-10/server.t t/draft-ietf-hybi-17/client.t t/draft-ietf-hybi-17/frame.t t/draft-ietf-hybi-17/request.t t/draft-ietf-hybi-17/request_psgi.t t/draft-ietf-hybi-17/response.t t/draft-ietf-hybi-17/server.t t/empty t/frame.t t/message t/message.t t/request_common.t t/response_common.t t/rsv.t t/url.t util/wsconsole xt/changes.t xt/pod-coverage.t xt/pod.t META.json META.yml MANIFEST