Net-Async-WebSocket-0.14000755001750001750 014670354144 13654 5ustar00leoleo000000000000Net-Async-WebSocket-0.14/.editorconfig000444001750001750 5314670354144 16424 0ustar00leoleo000000000000root = true [*.{pm,pl,t}] indent_size = 3 Net-Async-WebSocket-0.14/Build.PL000444001750001750 152614670354144 15311 0ustar00leoleo000000000000use v5; use strict; use warnings; use Module::Build; my $build = Module::Build->new( module_name => 'Net::Async::WebSocket', configure_requires => { 'Module::Build' => '0.4004', }, requires => { 'perl' => '5.014', 'meta' => '0.008', 'IO::Async::Listener' => '0.61', # handle_class 'IO::Async::Notifier' => '0.63', # ->adopt_future 'IO::Async::Stream' => '0.34', 'Protocol::WebSocket' => '0.22', # Frame->masked accessor 'Protocol::WebSocket::Frame' => 0, 'Protocol::WebSocket::Handshake::Client' => 0, 'Protocol::WebSocket::Handshake::Server' => 0, 'URI' => 0, 'URI::wss' => 0, }, test_requires => { 'IO::Async::OS' => 0, 'Test2::V0' => 0, }, license => 'perl', create_license => 1, create_readme => 1, ); $build->create_build_script; Net-Async-WebSocket-0.14/Changes000444001750001750 540314670354144 15306 0ustar00leoleo000000000000Revision history for Net-Async-WebSocket 0.14 2024-09-11 [CHANGES] * Use `meta` v0.008 rather than `no strict 'refs'` and `Sub::Util` hackery * Depend on `URI::wss` rather than locally reïmplementing it * General minor style updates to module code and documentation 0.13 2018-10-11 18:54:29 [CHANGES] * Allow a custom request object at client handshake time (RT124118) 0.12 2017-12-08 17:50:04 [CHANGES] * Fix documentation of ->send_TYPE_frame methods, noting they return Futures * Add ->send_close_frame method and on_close_frame event * Set the SSL_hostname option when connecting over SSL * Update for Protocol::WebSocket 0.22 [BUGFIXES] * Clients must emit masked frames (RT88441, RT122052) * Declare requires perl >= 5.010 because of // operator 0.11 2017-12-08 13:38:45 [CHANGES] * Add frametype-specific methods for sending and events for receving * Updated documentation style [BUGFIXES] * Make sure to actually use SSL when connecting to 'wss' URLs * Fix URI::ws/wss hackery * Ensure NaWebSocket::Server respects subclasses overriding the 'handle_class' parameter 0.10 2015/11/18 16:23:44 [BUGFIXES] * Declare dependency on URI (RT103073) 0.09 2015/03/03 21:54:03 [CHANGES] * Infer the host/service to connect to from the url string * Updated SYNOPSIS examples for modern style * Updated Future-returning method documentation [BUGFIXES] * Make sure not to confuse SUPER::connect() with an on_connected continuation not intended for it 0.08 2014/03/27 16:43:22 [CHANGES] * First-class support for Futures * Subclass IO::Async::Stream directly; avoid IaProtocol::Stream [BUGFIXES] * Only pull out ->configure arguments that actually exist * Handle defined-but-false frames (RT92477) 0.07 CHANGES: * ->socketpair now comes from IO::Async::OS, not the underlying loop 0.06 CHANGES: * Added on_handshake test/continuation for early-aborting connections if required (patch by David Leadbeater) (RT#83436) 0.05 CHANGES: * Pass all arguments from send_frame into Protocol::WebSocket::Frame constructor (RT79202) 0.04 CHANGES: * Updated for Protocol::WebSocket 0.009 - s/to_string/to_bytes/ 0.03 CHANGES: * Use IO::Async::Protocol->connect from 0.34 0.02 CHANGES: * Bugfixes for getaddrinfo that isn't on Linux * Created some example scripts and SYNOPSIS code samples 0.01 First version, released on an unsuspecting world. Net-Async-WebSocket-0.14/LICENSE000444001750001750 4653414670354144 15052 0ustar00leoleo000000000000This software is copyright (c) 2024 by Paul Evans . 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) 2024 by Paul Evans . 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, Fifth Floor, Boston, MA 02110-1301 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 Perl Artistic License 1.0 --- This software is Copyright (c) 2024 by Paul Evans . This is free software, licensed under: The Perl 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 as specified below. "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 uunet.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) give non-standard executables non-standard names, and clearly document 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. You may embed this Package's interpreter within an executable of yours (by linking); this shall be construed as a mere form of aggregation, provided that the complete Standard Version of the interpreter is so embedded. 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 whoever generated them, and may be sold commercially, and may be aggregated with this Package. If such scripts or library files are aggregated with this Package via the so-called "undump" or "unexec" methods of producing a binary executable image, then distribution of such an image shall neither be construed as a distribution of this Package nor shall it fall under the restrictions of Paragraphs 3 and 4, provided that you do not represent such an executable image as a Standard Version of this Package. 7. C subroutines (or comparably compiled subroutines in other languages) supplied by you and linked into this Package in order to emulate subroutines and variables of the language defined by this Package shall not be considered part of this Package, but are the equivalent of input as in Paragraph 6, provided these subroutines do not change the language in any way that would cause it to fail the regression tests for the language. 8. Aggregation of this Package with a commercial distribution is always permitted provided that the use of this Package is embedded; that is, when no overt attempt is made to make this Package's interfaces visible to the end user of the commercial distribution. Such use shall not be construed as a distribution of this Package. 9. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Net-Async-WebSocket-0.14/MANIFEST000444001750001750 51514670354144 15123 0ustar00leoleo000000000000.editorconfig Build.PL Changes examples/echo-server.pl examples/stdio-client.pl lib/Net/Async/WebSocket.pm lib/Net/Async/WebSocket/Client.pm lib/Net/Async/WebSocket/Protocol.pm lib/Net/Async/WebSocket/Server.pm MANIFEST This list of files t/00use.t t/01client.t t/02server.t t/03cross.t t/99pod.t README LICENSE META.yml META.json Net-Async-WebSocket-0.14/META.json000444001750001750 356314670354144 15441 0ustar00leoleo000000000000{ "abstract" : "use WebSockets with C", "author" : [ "Paul Evans " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4234", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Net-Async-WebSocket", "prereqs" : { "configure" : { "requires" : { "Module::Build" : "0.4004" } }, "runtime" : { "requires" : { "IO::Async::Listener" : "0.61", "IO::Async::Notifier" : "0.63", "IO::Async::Stream" : "0.34", "Protocol::WebSocket" : "0.22", "Protocol::WebSocket::Frame" : "0", "Protocol::WebSocket::Handshake::Client" : "0", "Protocol::WebSocket::Handshake::Server" : "0", "URI" : "0", "URI::wss" : "0", "meta" : "0.008", "perl" : "5.014" } }, "test" : { "requires" : { "IO::Async::OS" : "0", "Test2::V0" : "0" } } }, "provides" : { "Net::Async::WebSocket" : { "file" : "lib/Net/Async/WebSocket.pm", "version" : "0.14" }, "Net::Async::WebSocket::Client" : { "file" : "lib/Net/Async/WebSocket/Client.pm", "version" : "0.14" }, "Net::Async::WebSocket::Protocol" : { "file" : "lib/Net/Async/WebSocket/Protocol.pm", "version" : "0.14" }, "Net::Async::WebSocket::Server" : { "file" : "lib/Net/Async/WebSocket/Server.pm", "version" : "0.14" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.14", "x_serialization_backend" : "JSON::PP version 4.16" } Net-Async-WebSocket-0.14/META.yml000444001750001750 235314670354144 15265 0ustar00leoleo000000000000--- abstract: 'use WebSockets with C' author: - 'Paul Evans ' build_requires: IO::Async::OS: '0' Test2::V0: '0' configure_requires: Module::Build: '0.4004' dynamic_config: 1 generated_by: 'Module::Build version 0.4234, 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: Net-Async-WebSocket provides: Net::Async::WebSocket: file: lib/Net/Async/WebSocket.pm version: '0.14' Net::Async::WebSocket::Client: file: lib/Net/Async/WebSocket/Client.pm version: '0.14' Net::Async::WebSocket::Protocol: file: lib/Net/Async/WebSocket/Protocol.pm version: '0.14' Net::Async::WebSocket::Server: file: lib/Net/Async/WebSocket/Server.pm version: '0.14' requires: IO::Async::Listener: '0.61' IO::Async::Notifier: '0.63' IO::Async::Stream: '0.34' Protocol::WebSocket: '0.22' Protocol::WebSocket::Frame: '0' Protocol::WebSocket::Handshake::Client: '0' Protocol::WebSocket::Handshake::Server: '0' URI: '0' URI::wss: '0' meta: '0.008' perl: '5.014' resources: license: http://dev.perl.org/licenses/ version: '0.14' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Net-Async-WebSocket-0.14/README000444001750001750 113214670354144 14666 0ustar00leoleo000000000000NAME Net::Async::WebSocket - use WebSockets with IO::Async DESCRIPTION This distribution provides modules that implement the WebSocket protocol, and allows either servers or clients to be written based on IO::Async. To implement a server, see Net::Async::WebSocket::Server. To implement a client, see Net::Async::WebSocket::Client. This module itself does not provide any code, and exists only to provide the module $VERSION and top-level documentation. SEE ALSO * Protocol::WebSocket - WebSocket protocol AUTHOR Paul Evans Net-Async-WebSocket-0.14/examples000755001750001750 014670354144 15472 5ustar00leoleo000000000000Net-Async-WebSocket-0.14/examples/echo-server.pl000444001750001750 102614670354144 20405 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use IO::Async::Loop; use Net::Async::WebSocket::Server; my $PORT = 3000; my $server = Net::Async::WebSocket::Server->new( on_client => sub { my ( undef, $client ) = @_; $client->configure( on_text_frame => sub { my ( $self, $frame ) = @_; $self->send_text_frame( $frame ); }, ); } ); my $loop = IO::Async::Loop->new; $loop->add( $server ); $server->listen( family => "inet", service => $PORT, )->get; $loop->run; Net-Async-WebSocket-0.14/examples/stdio-client.pl000444001750001750 141314670354144 20561 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use IO::Async::Loop; use IO::Async::Stream; use Net::Async::WebSocket::Client; my $HOST = shift @ARGV or die "Need HOST"; my $PORT = shift @ARGV or die "Need PORT"; my ( $client, $stdio ); $client = Net::Async::WebSocket::Client->new( on_text_frame => sub { my ( $self, $frame ) = @_; $stdio->write( $frame ); }, ); $stdio = IO::Async::Stream->new_for_stdio( on_read => sub { my ( $self, $buffref ) = @_; $client->send_text_frame( $$buffref ); $$buffref = ""; }, ); my $loop = IO::Async::Loop->new; $loop->add( $client ); $loop->add( $stdio ); $client->connect( host => $HOST, service => $PORT, url => "ws://$HOST:$PORT/", )->get; print "Connected; go ahead...\n"; $loop->run; Net-Async-WebSocket-0.14/lib000755001750001750 014670354144 14422 5ustar00leoleo000000000000Net-Async-WebSocket-0.14/lib/Net000755001750001750 014670354144 15150 5ustar00leoleo000000000000Net-Async-WebSocket-0.14/lib/Net/Async000755001750001750 014670354144 16225 5ustar00leoleo000000000000Net-Async-WebSocket-0.14/lib/Net/Async/WebSocket.pm000444001750001750 161714670354144 20613 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk package Net::Async::WebSocket 0.14; use v5.14; use warnings; =head1 NAME C - use WebSockets with C =head1 DESCRIPTION This distribution provides modules that implement the WebSocket protocol, and allows either servers or clients to be written based on L. To implement a server, see L. To implement a client, see L. This module itself does not provide any code, and exists only to provide the module C<$VERSION> and top-level documentation. =cut =head1 SEE ALSO =over 8 =item * L - WebSocket protocol =back =head1 AUTHOR Paul Evans =cut 0x55AA; Net-Async-WebSocket-0.14/lib/Net/Async/WebSocket000755001750001750 014670354144 20113 5ustar00leoleo000000000000Net-Async-WebSocket-0.14/lib/Net/Async/WebSocket/Client.pm000444001750001750 1051114670354144 22042 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk package Net::Async::WebSocket::Client 0.14; use v5.14; use warnings; use base qw( Net::Async::WebSocket::Protocol ); IO::Async::Notifier->VERSION( '0.63' ); # ->adopt_future use Carp; BEGIN { if( $^V ge v5.40 ) { *blessed = \&builtin::blessed; } else { require Scalar::Util; *blessed = \&Scalar::Util::blessed; } } use URI; use URI::wss; BEGIN { # We also need to support ->resource_name, which the CPAN module does not # understand as of 2017-01-01 no warnings 'once'; *URI::wss::resource_name = sub { shift->path_query } unless URI::wss->can( "resource_name" ); } use Protocol::WebSocket::Handshake::Client; =head1 NAME C - connect to a WebSocket server using C =head1 SYNOPSIS use Future::AsyncAwait; use IO::Async::Loop; use Net::Async::WebSocket::Client; my $client = Net::Async::WebSocket::Client->new( on_text_frame => sub { my ( $self, $frame ) = @_; print $frame; }, ); my $loop = IO::Async::Loop->new; $loop->add( $client ); await $client->connect( url => "ws://$HOST:$PORT/" ); await $client->send_text_frame( "Hello, world!\n" ); $loop->run; =head1 DESCRIPTION This subclass of L connects to a WebSocket server to establish a WebSocket connection for passing frames. =cut sub new { my $class = shift; return $class->SUPER::new( masked => 1, @_, ); } =head1 METHODS The following methods documented in an C expression return L instances. =cut sub _do_handshake { my $self = shift; my %params = @_; my $hs = Protocol::WebSocket::Handshake::Client->new( url => $params{url}, req => $params{req}, ); $self->debug_printf( "HANDSHAKE start" ); $self->write( $hs->to_string ); my $f = $self->loop->new_future; $self->SUPER::configure( on_read => sub { my ( undef, $buffref, $closed ) = @_; $hs->parse( $$buffref ); # modifies $$buffref if( $hs->is_done ) { $self->debug_printf( "HANDSHAKE done" ); $self->SUPER::configure( on_read => undef ); $f->done( $self ); } return 0; } ); return $f; } =head2 connect await $self->connect( %params ); Connect to a WebSocket server. Takes the following named parameters: =over 8 =item url => STRING URL to provide to WebSocket handshake. This is also used to infer the host and service name (port number) if not otherwise supplied. =item req => Protocol::WebSocket::Request Optional. If provided, gives the L instance used for performing the handshake. =back The returned L returns the client instance itself, making it useful in chaining constructors. =head2 connect (void) $self->connect( %params ); When not returning a C, the following additional parameters provide continuations: =over 8 =item on_connected => CODE CODE reference to invoke when the handshaking is complete. =back =cut sub connect { my $self = shift; my %params = @_; if( my $url = $params{url} ) { $url = URI->new( $url ) unless blessed $url and $url->isa( "URI" ); $params{host} //= $url->host; $params{service} //= $url->port; if( $url->secure ) { require IO::Async::SSL; push @{ $params{extensions} }, qw( SSL ); $params{SSL_hostname} //= $url->host; } } my $on_connected = delete $params{on_connected}; my $f = $self->SUPER::connect( %params )->then( sub { my ( $self ) = @_; $self->_do_handshake( %params ); }); $f->on_done( $on_connected ) if $on_connected; return $f if defined wantarray; $self->adopt_future( $f ); } =head2 connect_handle await $client->connect_handle( $handle, %params ); Sets the read and write handles to the IO reference given, then performs the initial handshake using the parameters given. These are as for C. =cut sub connect_handle { my $self = shift; my ( $handle, %params ) = @_; $self->set_handle( $handle ); $self->_do_handshake( %params ); } =head1 AUTHOR Paul Evans =cut 0x55AA; Net-Async-WebSocket-0.14/lib/Net/Async/WebSocket/Protocol.pm000444001750001750 1334014670354144 22430 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk package Net::Async::WebSocket::Protocol 0.14; use v5.14; use warnings; use base qw( IO::Async::Stream ); use Carp; use Protocol::WebSocket::Frame; use meta 0.008; no warnings 'meta::experimental'; my %FRAMETYPES = ( 1 => "text", 2 => "binary", 0x8 => "close", 0x9 => "ping", 0xa => "pong", ); my @ON_TYPE_FRAMES = map { "on_${_}_frame" } values %FRAMETYPES; =head1 NAME C - send and receive WebSocket frames =head1 DESCRIPTION This subclass of L implements an established WebSocket connection, that has already completed its setup handshaking and is ready to pass frames. Objects of this type would not normally be constructed directly. For WebSocket clients, see L, which is a subclass of this. For WebSocket servers, see L, which constructs objects in this class when it accepts a new connection and passes it to its event handler. =cut =head1 EVENTS The following events are invoked, either using subclass methods or CODE references in parameters: =head2 on_text_frame $self->on_text_frame( $text ); $on_text_frame->( $self, $text ); Invoked when a text frame is received. It is passed a Unicode character string formed by decoding the received UTF-8 bytes. =head2 on_frame $self->on_frame( $text ); $on_frame->( $self, $text ); A synonym for C, provided for backward compatibility. This may be removed in a later version. =head2 on_binary_frame, on_ping_frame, on_pong_frame, on_close_frame $self->on_..._frame( $bytes ); $on_..._frame->( $self, $bytes ); Invoked when other types of frame are received. These will be passed plain byte strings. =head2 on_raw_frame $self->on_raw_frame( $frame, $bytes ); $on_raw_frame->( $self, $frame, $bytes ); Invoked when a frame is received that does not have a specific handler defined of one of the above types. C<$frame> will be an instance of L. =cut sub _init { my $self = shift; $self->SUPER::_init; $self->{framebuffer} = Protocol::WebSocket::Frame->new; } =head1 PARAMETERS The following named parameters may be passed to C or C: =over 8 =item on_frame => CODE =item on_text_frame => CODE =item on_binary_frame, on_ping_frame, on_pong_frame, on_close_frame => CODE =item on_raw_frame => CODE CODE references for event handlers. =item masked => BOOL Whether frames constructed and sent by this instance will be masked. =back =cut sub configure { my $self = shift; my %params = @_; foreach (qw( on_frame on_raw_frame masked ), @ON_TYPE_FRAMES ) { $self->{$_} = delete $params{$_} if exists $params{$_}; } $self->SUPER::configure( %params ); } sub on_read { my $self = shift; my ( $buffref, $closed ) = @_; my $framebuffer = $self->{framebuffer}; $framebuffer->append( $$buffref ); # modifies $$buffref while( defined( my $bytes = $framebuffer->next_bytes ) ) { my $type = $FRAMETYPES{$framebuffer->opcode}; $self->debug_printf( "FRAME $type" ); my $text = $framebuffer->is_text ? Encode::decode_utf8( $bytes ) : undef; $self->maybe_invoke_event( "on_${type}_frame" => $text // $bytes ) or $self->maybe_invoke_event( on_raw_frame => $framebuffer, $bytes ); $self->maybe_invoke_event( on_frame => $text ) if $framebuffer->is_text; } return 0; } =head1 METHODS The following methods documented in an C expression return L instances. =cut =head2 send_frame await $self->send_frame( @args ); Sends a frame to the peer containing containing the given string. The arguments are passed to L's C method. This method is discouraged in favour of the more specific ones listed below, and is only provided for back-compatibility or for sending new frame types not recognised by the specific methods. =cut sub send_frame { my $self = shift; $self->write( Protocol::WebSocket::Frame->new( @_ )->to_bytes ); } =head2 send_text_frame await $self->send_text_frame( $text, %params ); Sends a text frame to the peer. The given string will be treated as a Unicode character string, and sent as UTF-8 encoded bytes. Any additional arguments will be passed as parameters to the underlying L call. =head2 send_I_frame await $self->send_binary_frame( $bytes, %params ); await $self->send_ping_frame( $bytes, %params ); await $self->send_pong_frame( $bytes, %params ); await $self->send_close_frame( $bytes, %params ); Sends a frame of the given type to the peer. Any additional arguments will be passed as parameters to the underlying L call. =cut sub send_text_frame { my $self = shift; my ( $text, %params ) = @_; # Protocol::WebSocket::Frame will UTF-8 encode this for us $self->write( Protocol::WebSocket::Frame->new( type => "text", buffer => $text, masked => $self->{masked}, )->to_bytes, %params ); } my $metapkg = meta::get_this_package; foreach my $type ( values %FRAMETYPES ) { next if $type eq "text"; my $method = "send_${type}_frame"; my $code = sub { my $self = shift; my ( $bytes, %params ) = @_; $self->write( Protocol::WebSocket::Frame->new( type => $type, buffer => $bytes, masked => $self->{masked}, )->to_bytes, %params ); }; $metapkg->add_named_sub( $method => $code ); } =head1 AUTHOR Paul Evans =cut 0x55AA; Net-Async-WebSocket-0.14/lib/Net/Async/WebSocket/Server.pm000444001750001750 740314670354144 22060 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk package Net::Async::WebSocket::Server 0.14; use v5.14; use warnings; use base qw( IO::Async::Listener ); use Carp; use Net::Async::WebSocket::Protocol; use Protocol::WebSocket::Handshake::Server; =head1 NAME C - serve WebSocket clients using C =head1 SYNOPSIS use Future::AsyncAwait; use IO::Async::Loop; use Net::Async::WebSocket::Server; my $server = Net::Async::WebSocket::Server->new( on_client => sub { my ( undef, $client ) = @_; $client->configure( on_text_frame => sub { my ( $self, $frame ) = @_; $self->send_text_frame( $frame ); }, ); } ); my $loop = IO::Async::Loop->new; $loop->add( $server ); await $server->listen( service => 3000, ); $loop->run; =head1 DESCRIPTION This subclass of L accepts WebSocket connections. When a new connection arrives it will perform an initial handshake, and then pass the connection on to the continuation callback or method. =cut =head1 EVENTS The following events are invoked, either using subclass methods or CODE references in parameters: =head2 on_client $self->on_client( $client ); $on_client->( $self, $client ); Invoked when a new client connects and completes its initial handshake. It will be passed a new instance of a L object, wrapping the client connection. =head2 on_handshake Invoked when a handshake has been requested. $self->on_handshake( $client, $hs, $continue ); $on_handshake->( $self, $client, $hs, $continue ); Calling C<$continue> with a true value will complete the handshake, false will drop the connection. This is useful for filtering on origin, for example: on_handshake => sub { my ( $self, $client, $hs, $continue ) = @_; $continue->( $hs->req->origin eq "http://localhost" ); } =cut sub new { my $class = shift; return $class->SUPER::new( handle_class => "Net::Async::WebSocket::Protocol", @_, ); } sub on_accept { my $self = shift; my ( $client ) = @_; my $hs = Protocol::WebSocket::Handshake::Server->new; $client->configure( on_read => sub { my ( $client, $buffref, $closed ) = @_; $hs->parse( $$buffref ); # modifies $$buffref if( $hs->is_done ) { my $on_handshake = $self->can_event( "on_handshake" ) || sub { $_[3]->( 1 ) }; $on_handshake->( $self, $client, $hs, sub { my ( $ok ) = @_; unless( $ok ) { $self->remove_child( $client ); return; } $client->configure( on_read => undef ); $client->write( $hs->to_string ); $client->debug_printf( "HANDSHAKE done" ); $self->invoke_event( on_client => $client ); } ); } return 0; }, ); $self->add_child( $client ); } =head1 PARAMETERS The following named parameters may be passed to C or C: =over 8 =item on_client => CODE =item on_handshake => CODE CODE references for event handlers. =back =cut sub configure { my $self = shift; my %params = @_; foreach (qw( on_client on_handshake )) { $self->{$_} = delete $params{$_} if exists $params{$_}; } $self->SUPER::configure( %params ); } sub listen { my $self = shift; my %params = @_; $self->SUPER::listen( socktype => 'stream', %params, ); } =head1 AUTHOR Paul Evans =cut 0x55AA; Net-Async-WebSocket-0.14/t000755001750001750 014670354144 14117 5ustar00leoleo000000000000Net-Async-WebSocket-0.14/t/00use.t000444001750001750 37214670354144 15357 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; require Net::Async::WebSocket; require Net::Async::WebSocket::Protocol; require Net::Async::WebSocket::Client; require Net::Async::WebSocket::Server; pass( 'Modules loaded' ); done_testing; Net-Async-WebSocket-0.14/t/01client.t000444001750001750 752214670354144 16066 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; use IO::Async::Test; use IO::Async::Loop; use IO::Async::OS; use IO::Async::Stream; use Net::Async::WebSocket::Client; use Protocol::WebSocket::Handshake::Server; my $loop = IO::Async::Loop->new; testing_loop( $loop ); my ( $serversock, $clientsock ) = IO::Async::OS->socketpair or die "Cannot socketpair - $!"; my @frames; my $client = Net::Async::WebSocket::Client->new( on_frame => sub { my ( $self, $frame ) = @_; push @frames, $frame; }, ); ok( defined $client, '$client defined' ); isa_ok( $client, [ "Net::Async::WebSocket::Client" ], '$client' ); $loop->add( $client ); my $f = $client->connect_handle( $clientsock, url => "ws://localhost/test", ); $f->on_fail( sub { $f->get } ); my $h = Protocol::WebSocket::Handshake::Server->new; my $stream = ""; wait_for_stream { $h->parse( $stream ); $stream = ""; $h->is_done } $serversock => $stream; $serversock->write( $h->to_string ); wait_for { $f->is_ready }; $f->get; # receive { $serversock->write( Protocol::WebSocket::Frame->new( "Here is my message" )->to_bytes ); wait_for { @frames }; is( \@frames, [ "Here is my message" ], 'received @frames' ); undef @frames; } # send { $client->send_frame( "Here is my response" ); my $fb = Protocol::WebSocket::Frame->new; $stream = ""; my $frame; wait_for_stream { $fb->append( $stream ); $stream = ""; $frame = $fb->next } $serversock => $stream; is( $frame, "Here is my response", 'responded $frame' ); } # frames with false values { $serversock->write( Protocol::WebSocket::Frame->new( "" )->to_bytes ); wait_for { @frames }; is( \@frames, [ "" ], 'received frame with false value' ); } # U+2010 = HYPHEN = 0xe2 0x80 0x90 my $UTF_8_char = "UTF\x{2010}8"; my $UTF_8_bytes = "UTF\xe2\x80\x908"; # receiving frame types { my ( $got_text, $got_binary ); $client->configure( on_text_frame => sub { $got_text = $_[1] }, on_binary_frame => sub { $got_binary = $_[1] }, ); $serversock->write( Protocol::WebSocket::Frame->new( type => "text", buffer => $UTF_8_char, # Protocol::WebSocket::Frame will encode this )->to_bytes ); wait_for { defined $got_text }; is( $got_text, $UTF_8_char, 'received text frame' ); # Valid UTF-8 encoding but should not be decoded $serversock->write( Protocol::WebSocket::Frame->new( type => "binary", buffer => $UTF_8_bytes, )->to_bytes ); wait_for { defined $got_binary }; is( $got_binary, $UTF_8_bytes, 'received binary frame' ); } # sending frame types { my $fb = Protocol::WebSocket::Frame->new; my $bytes; $client->send_text_frame( $UTF_8_char ); $stream = ""; wait_for_stream { $fb->append( $stream ); $stream = ""; $bytes = $fb->next_bytes } $serversock => $stream; ok( $fb->is_text, 'sent text frame' ); ok( $fb->masked, 'sent frame was masked' ); is( $bytes, $UTF_8_bytes, 'content of text frame' ); $client->send_binary_frame( $UTF_8_bytes ); $stream = ""; wait_for_stream { $fb->append( $stream ); $stream = ""; $bytes = $fb->next_bytes } $serversock => $stream; ok( $fb->is_binary, 'sent binary frame' ); ok( $fb->masked, 'sent frame was masked' ); is( $bytes, $UTF_8_bytes, 'content of binary frame' ); } # custom req { my $client = Net::Async::WebSocket::Client->new(); $loop->add( $client ); my $f = $client->connect_handle( $clientsock, url => "ws://localhost/test", req => Protocol::WebSocket::Request->new( headers => [ "X-Custom" => "value" ] ), ); $f->on_fail( sub { $f->get } ); my $h = Protocol::WebSocket::Handshake::Server->new; my $stream = ""; wait_for_stream { $h->parse( $stream ); $stream = ""; $h->is_done } $serversock => $stream; is( $h->req->field( "X-Custom" ), "value", 'request header contains custom field' ); } done_testing; Net-Async-WebSocket-0.14/t/02server.t000444001750001750 370614670354144 16117 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; use IO::Async::Test; use IO::Async::Loop; use Net::Async::WebSocket::Server; use Protocol::WebSocket::Frame; use Protocol::WebSocket::Handshake::Client; use IO::Socket::INET; my $loop = IO::Async::Loop->new; testing_loop( $loop ); my $serversock = IO::Socket::INET->new( LocalHost => "127.0.0.1", Listen => 1, ) or die "Cannot allocate listening socket - $@"; my $client; my @frames; my $server = Net::Async::WebSocket::Server->new( handle => $serversock, on_handshake => sub { my ( $self, $stream, $hs, $continue ) = @_; $continue->( $hs->req->origin eq "http://localhost" ); }, on_client => sub { my ( undef, $thisclient ) = @_; $client = $thisclient; $thisclient->configure( on_frame => sub { my ( $self, $frame ) = @_; push @frames, $frame; }, ); }, ); ok( defined $server, '$server defined' ); isa_ok( $server, [ "Net::Async::WebSocket::Server" ], '$server' ); $loop->add( $server ); my $clientsock = IO::Socket::INET->new( PeerHost => $serversock->sockhost, PeerPort => $serversock->sockport, ) or die "Cannot connect to testing server - $@"; my $h = Protocol::WebSocket::Handshake::Client->new( url => "ws://localhost/test", ); $clientsock->write( $h->to_string ); my $stream = ""; wait_for_stream { $h->parse( $stream ); $stream = ""; $h->is_done } $clientsock => $stream; ok( defined $client, '$client is defined' ); $clientsock->write( Protocol::WebSocket::Frame->new( "Here is my message" )->to_bytes ); wait_for { @frames }; is( \@frames, [ "Here is my message" ], 'received @frames' ); undef @frames; $client->send_frame( "Here is my response" ); my $fb = Protocol::WebSocket::Frame->new; $stream = ""; my $frame; wait_for_stream { $fb->append( $stream ); $stream = ""; $frame = $fb->next } $clientsock => $stream; is( $frame, "Here is my response", 'responded $frame' ); done_testing; Net-Async-WebSocket-0.14/t/03cross.t000444001750001750 266714670354144 15750 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; use IO::Async::Test; use IO::Async::Loop; use Net::Async::WebSocket::Client; use Net::Async::WebSocket::Server; use IO::Socket::INET; my $loop = IO::Async::Loop->new; testing_loop( $loop ); my $serversock = IO::Socket::INET->new( LocalHost => "127.0.0.1", Listen => 1, ) or die "Cannot allocate listening socket - $@"; my @serverframes; my $acceptedclient; my $server = Net::Async::WebSocket::Server->new( handle => $serversock, on_client => sub { my ( undef, $thisclient ) = @_; $acceptedclient = $thisclient; $thisclient->configure( on_text_frame => sub { my ( $self, $frame ) = @_; push @serverframes, $frame; }, ); }, ); $loop->add( $server ); my @clientframes; my $client = Net::Async::WebSocket::Client->new( on_text_frame => sub { my ( $self, $frame ) = @_; push @clientframes, $frame; }, ); $loop->add( $client ); $client->connect( host => $serversock->sockhost, service => $serversock->sockport, url => "ws://localhost/test", )->get; $client->send_text_frame( "Here is my message" ); wait_for { @serverframes }; is( \@serverframes, [ "Here is my message" ], 'received @serverframes' ); $acceptedclient->send_text_frame( "Here is my response" ); wait_for { @clientframes }; is( \@clientframes, [ "Here is my response" ], 'received @clientframes' ); done_testing; Net-Async-WebSocket-0.14/t/99pod.t000444001750001750 25514670354144 15367 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok();