Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add Mojo::WebSocket for low-level protocol functions
- Loading branch information
Showing
5 changed files
with
61 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package Mojo::WebSocket; | ||
use Mojo::Base -strict; | ||
|
||
use Exporter 'import'; | ||
use Mojo::Util qw(b64_encode sha1_bytes); | ||
|
||
our @EXPORT_OK = qw(challenge client_handshake server_handshake); | ||
|
||
# Unique value from RFC 6455 | ||
use constant GUID => '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; | ||
|
||
sub challenge { | ||
my $tx = shift; | ||
|
||
# "permessage-deflate" extension | ||
my $headers = $tx->res->headers; | ||
$tx->compressed(1) | ||
if ($headers->sec_websocket_extensions // '') =~ /permessage-deflate/; | ||
|
||
return _challenge($tx->req->headers->sec_websocket_key) eq | ||
$headers->sec_websocket_accept; | ||
} | ||
|
||
sub client_handshake { | ||
my $tx = shift; | ||
|
||
my $headers = $tx->req->headers; | ||
$headers->upgrade('websocket') unless $headers->upgrade; | ||
$headers->connection('Upgrade') unless $headers->connection; | ||
$headers->sec_websocket_version(13) unless $headers->sec_websocket_version; | ||
|
||
# Generate 16 byte WebSocket challenge | ||
my $challenge = b64_encode sprintf('%16u', int(rand 9 x 16)), ''; | ||
$headers->sec_websocket_key($challenge) unless $headers->sec_websocket_key; | ||
|
||
return $tx; | ||
} | ||
|
||
sub server_handshake { | ||
my $tx = shift; | ||
|
||
my $res_headers = $tx->res->headers; | ||
$res_headers->upgrade('websocket')->connection('Upgrade'); | ||
$res_headers->sec_websocket_accept( | ||
_challenge($tx->req->headers->sec_websocket_key)); | ||
|
||
return $tx; | ||
} | ||
|
||
sub _challenge { b64_encode(sha1_bytes(($_[0] || '') . GUID), '') } | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters