| 1 | package Net::WebSocket; | ||
| 2 | |||
| 3 | our $VERSION = '0.05-TRIAL3'; | ||
| 4 | |||
| 5 | =encoding utf-8 | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::WebSocket - WebSocket in Perl | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Net::WebSocket::Handshake::Client (); | ||
| 14 | use Net::WebSocket::HTTP_R (); | ||
| 15 | |||
| 16 | my $handshake = Net::WebSocket::Handshake::Client->new( | ||
| 17 | uri => $uri, | ||
| 18 | ); | ||
| 19 | |||
| 20 | syswrite $inet, $handshake->to_string() or die $!; | ||
| 21 | |||
| 22 | #You can parse HTTP headers however you want; | ||
| 23 | #Net::WebSocket makes no assumptions about this. | ||
| 24 | my $resp = HTTP::Response->parse($hdrs_txt); | ||
| 25 | |||
| 26 | #If you use an interface that’s compatible with HTTP::Response, | ||
| 27 | #then you can take advantage of this convenience function; | ||
| 28 | #otherwise you’ll need to do a bit more work. | ||
| 29 | Net::WebSocket::HTTP_R::handshake_parse_response( $handshake, $resp ); | ||
| 30 | |||
| 31 | #See below about IO::Framed | ||
| 32 | my $parser = Net::WebSocket::Parser->new( | ||
| 33 | IO::Framed::Read->new($inet), | ||
| 34 | $leftover_from_header_read, #can be nonempty on the client | ||
| 35 | ); | ||
| 36 | |||
| 37 | my $iof_w = IO::Framed::Write->new($inet); | ||
| 38 | |||
| 39 | my $ept = Net::WebSocket::Endpoint::Client->new( | ||
| 40 | parser => $parser, | ||
| 41 | out => $iof_w, | ||
| 42 | ); | ||
| 43 | |||
| 44 | $iof_w->write( | ||
| 45 | Net::WebSocket::Frame::text->new( payload_sr => \'Hello, world' ) | ||
| 46 | ); | ||
| 47 | |||
| 48 | #Determine that $inet can be read from … | ||
| 49 | |||
| 50 | my $msg = $ept->get_next_message(); | ||
| 51 | |||
| 52 | #… or, if we timeout while waiting for $inet to be ready for reading: | ||
| 53 | |||
| 54 | $ept->check_heartbeat(); | ||
| 55 | exit if $ept->is_closed(); | ||
| 56 | |||
| 57 | =head1 BETA QUALITY | ||
| 58 | |||
| 59 | This is a beta release. It should be safe for production, but there could | ||
| 60 | still be small changes to the API. Please check the changelog before | ||
| 61 | upgrading. | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | This distribution provides a set of fundamental tools for communicating via | ||
| 66 | L<WebSocket|https://tools.ietf.org/html/rfc6455>. | ||
| 67 | It is only concerned with the protocol itself; | ||
| 68 | the underlying transport mechanism is up to you: it could be a file, | ||
| 69 | a UNIX socket, ordinary TCP/IP, some funky C<tie()>d object, or whatever. | ||
| 70 | |||
| 71 | Net::WebSocket also “has no opinions” about how you should do I/O or HTTP | ||
| 72 | headers. There are too many different ways to accomplish HTTP header | ||
| 73 | management in particular for it to be sensible for a WebSocket library to | ||
| 74 | impose any one approach. As a result of this, Net::WebSocket can probably | ||
| 75 | fit your project with minimal overhead. There are some examples | ||
| 76 | of how you might write complete applications (client or server) | ||
| 77 | in the distribution’s F<demo/> directory. | ||
| 78 | |||
| 79 | Net::WebSocket is not a “quick” WebSocket solution; L<Mojolicious> and | ||
| 80 | the like fill that role amply. | ||
| 81 | Net::WebSocket’s purpose is to support anything | ||
| 82 | that the WebSocket protocol itself can do, as lightly as possible and without | ||
| 83 | prejudice as to how you want to do it: extensions, blocking/non-blocking I/O, | ||
| 84 | arbitrary HTTP headers, etc. Net::WebSocket will likely require more of an | ||
| 85 | investment up-front, but its flexibility should allow it to do anything that | ||
| 86 | can be done with WebSocket, and much more cleanly than a more “monolithic” | ||
| 87 | solution would likely allow. | ||
| 88 | |||
| 89 | =head1 OVERVIEW | ||
| 90 | |||
| 91 | Here are the main modules: | ||
| 92 | |||
| 93 | =over | ||
| 94 | |||
| 95 | =item L<Net::WebSocket::Handshake::Server> | ||
| 96 | |||
| 97 | =item L<Net::WebSocket::Handshake::Client> | ||
| 98 | |||
| 99 | Logic for handshakes. These are probably most useful in tandem with | ||
| 100 | modules like L<HTTP::Request> and L<HTTP::Response>. | ||
| 101 | |||
| 102 | |||
| 103 | =item L<Net::WebSocket::Endpoint::Server> | ||
| 104 | |||
| 105 | =item L<Net::WebSocket::Endpoint::Client> | ||
| 106 | |||
| 107 | The highest-level abstraction that this distribution provides. It parses input | ||
| 108 | and responds to control frames and timeouts. You can use this to receive | ||
| 109 | streamed (i.e., fragmented) transmissions as well. | ||
| 110 | |||
| 111 | =item L<Net::WebSocket::Streamer::Server> | ||
| 112 | |||
| 113 | =item L<Net::WebSocket::Streamer::Client> | ||
| 114 | |||
| 115 | Useful for sending streamed (fragmented) data rather than | ||
| 116 | a full message in a single frame. | ||
| 117 | |||
| 118 | =item L<Net::WebSocket::Parser> | ||
| 119 | |||
| 120 | Translate WebSocket frames out of a filehandle into useful data for | ||
| 121 | your application. | ||
| 122 | |||
| 123 | =item Net::WebSocket::Frame::* | ||
| 124 | |||
| 125 | Useful for creating raw frames. For data frames (besides continuation), | ||
| 126 | these will be your bread-and-butter. See L<Net::WebSocket::Frame::text> | ||
| 127 | for sample usage. | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =head1 IMPLEMENTATION NOTES | ||
| 132 | |||
| 133 | =head2 Handshakes | ||
| 134 | |||
| 135 | WebSocket uses regular HTTP headers for its handshakes. Because there are | ||
| 136 | many different solutions around for parsing HTTP headers, Net::WebSocket | ||
| 137 | tries to be “agnostic” about how that’s done. The liability of this is | ||
| 138 | that you, the library user, will need to implement some of the handshake | ||
| 139 | logic yourself. If you’re building from the ground up that’s not a lot of | ||
| 140 | fun, but if you’ve already got a solution in place for parsing headers then | ||
| 141 | Net::WebSocket can fit into that quite easily. | ||
| 142 | |||
| 143 | =head2 Masking | ||
| 144 | |||
| 145 | As per L<the specification|https://tools.ietf.org/html/rfc6455#section-5.1>, | ||
| 146 | client serializers “MUST” mask the data randomly, whereas server serializers | ||
| 147 | “MUST NOT” do this. Net::WebSocket does this for you automatically, | ||
| 148 | but you need to distinguish | ||
| 149 | between client serializers—which mask their payloads—and server serializers, | ||
| 150 | which don’t mask. | ||
| 151 | |||
| 152 | This module used to do this with L<Bytes::Random::Secure::Tiny>, but | ||
| 153 | that seems like overkill given that the masking is only there to accommodate | ||
| 154 | peculiarities of certain proxies. We now just use Perl’s C<rand()> | ||
| 155 | built-in. | ||
| 156 | |||
| 157 | (You should probably use TLS if cryptographically secure masking is something | ||
| 158 | you actually care about?) | ||
| 159 | |||
| 160 | =head2 Text vs. Binary | ||
| 161 | |||
| 162 | Recall that in some languages—like JavaScript!—the difference between | ||
| 163 | “text” and “binary” is much more significant than for us in Perl. | ||
| 164 | |||
| 165 | =head2 Use of L<IO::Framed> | ||
| 166 | |||
| 167 | CPAN’s L<IO::Framed> provides a straightforward interface for chunking up | ||
| 168 | data from byte streams into frames. It also provides a write buffer for | ||
| 169 | non-blocking writes, and it retries on EINTR. You don’t have to use it | ||
| 170 | (which is why it’s not listed as a requirement), but you’ll need to provide | ||
| 171 | a compatible interface if you don’t. | ||
| 172 | |||
| 173 | See the demo scripts that use L<IO::Framed> for an example of when you may | ||
| 174 | need a different solution here. | ||
| 175 | |||
| 176 | =head1 EXTENSION SUPPORT | ||
| 177 | |||
| 178 | The WebSocket specification describes several methods of extending the | ||
| 179 | protocol, all of which Net::WebSocket supports: | ||
| 180 | |||
| 181 | =over | ||
| 182 | |||
| 183 | =item * The three reserved bits in each frame’s header. | ||
| 184 | (See L<Net::WebSocket::Frame>.) This is used, e.g., in the | ||
| 185 | L<permessage-deflate extension|https://tools.ietf.org/html/rfc7692>. | ||
| 186 | |||
| 187 | =item * Additional opcodes: 3-7 and 11-15. You’ll need to subclass | ||
| 188 | L<Net::WebSocket::Frame> for this, and you will likely want to subclass | ||
| 189 | L<Net::WebSocket::Parser>. | ||
| 190 | If you’re using the custom classes for streaming, then you can | ||
| 191 | also subclass L<Net::WebSocket::Streamer>. See each of those modules for | ||
| 192 | more information on doing this. | ||
| 193 | |||
| 194 | B<THIS IS NOT WELL TESTED.> Proceed with caution, and please file bug | ||
| 195 | reports as needed. (I personally don’t know of any applications that | ||
| 196 | actually use this.) | ||
| 197 | |||
| 198 | =item * Apportion part of the payload data for the extension. This you | ||
| 199 | can do in your application. | ||
| 200 | |||
| 201 | =back | ||
| 202 | |||
| 203 | =head1 TODO | ||
| 204 | |||
| 205 | =over | ||
| 206 | |||
| 207 | =item * Support the L<permessage-deflate|https://tools.ietf.org/html/rfc7692> | ||
| 208 | extension natively. | ||
| 209 | |||
| 210 | =item * Add tests, especially for extension support. | ||
| 211 | |||
| 212 | =back | ||
| 213 | |||
| 214 | =head1 SEE ALSO | ||
| 215 | |||
| 216 | L<Mojolicious> is probably CPAN’s easiest WebSocket implementation to get | ||
| 217 | a server up and running. If you’re building a project from scratch, you | ||
| 218 | may find this to be a better fit for you than Net::WebSocket. | ||
| 219 | |||
| 220 | L<Protocol::WebSocket> is an older module that supports | ||
| 221 | pre-standard versions of the WebSocket protocol. It’s similar to this one | ||
| 222 | in that it gives you just the protocol itself, but it doesn’t give you | ||
| 223 | things like automatic ping/pong/close, classes for each message type, etc. | ||
| 224 | |||
| 225 | L<Net::WebSocket::Server> implements only server behaviors and | ||
| 226 | gives you more automation than P::WS. | ||
| 227 | |||
| 228 | L<Net::WebSocket::EV> uses XS to call a C library. | ||
| 229 | |||
| 230 | =head1 REPOSITORY | ||
| 231 | |||
| 232 | L<https://github.com/FGasper/p5-Net-WebSocket> | ||
| 233 | |||
| 234 | =head1 AUTHOR | ||
| 235 | |||
| 236 | Felipe Gasper (FELIPE) | ||
| 237 | |||
| 238 | =head1 COPYRIGHT | ||
| 239 | |||
| 240 | Copyright 2017 by L<Gasper Software Consulting, LLC|http://gaspersoftware.com> | ||
| 241 | |||
| 242 | =head1 LICENSE | ||
| 243 | |||
| 244 | This distribution is released under the same license as Perl. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | 1; |