WebSocket::Request - WebSocket Request
use WebSocket::Request; my $req = WebSocket::Request->new( host => 'example.com', uri => '/demo' protocol => 'com.example.chat', ) || die( WebSocket::Request->error, "\n" ); # or my $req = WebSocket::Request->new( $headers, $buffer, host => 'example.com', origin => 'https://example.com'); my $req = WebSocket::Request->new( $headers, $buffer, { host => 'example.com', origin => 'https://example.com' }); my $req = WebSocket::Request->new( $headers, host => 'example.com', origin => 'https://example.com'); my $req = WebSocket::Request->new( $headers, { host => 'example.com', origin => 'https://example.com' }); $req->as_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 # Set-WebSocket-Version: 13 # # x##### # Parser my $req = WebSocket::Request->new; $req->parse( <<EOT ); GET /demo HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: example.com Origin: http://example.com Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8 Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0 Set-WebSocket-Version: 13 Tm[K T2u EOT
v0.1.1
Class to build or parse a WebSocket request. It inherits all the methods from WebSocket::Common. For convenience, they are all listed here.
my $req = WebSocket::Request->new( $headers, $buffer, uri => 'wss://example.com/chat', subprotocol => 'com.example.chat' ); my $req = WebSocket::Request->new( $headers, $buffer, { uri => 'wss://example.com/chat', subprotocol => 'com.example.chat' }); my $req = WebSocket::Request->new( $headers, uri => 'wss://example.com/chat', subprotocol => 'com.example.chat' ); my $req = WebSocket::Request->new( $headers, { uri => 'wss://example.com/chat', subprotocol => 'com.example.chat' });
Provided with an optional set of headers, as either an array reference or a HTTP::Headers object, some optional content and an hash or hash reference of parameters, and this instantiates a new WebSocket::Request object. The supported parameters are as follow. Each parameter can be set or changed later using the method with the same name:
buffer
Content buffer
cookies
A Cookie request header string. The string provided must be already properly formatted and encoded and will be added as is. For example:
Cookie
WebSocket::Request->new( cookies => q{lang=en-GB; access_token=eyJwMnMiOiJtaGpZQ3ZqeHZ3TVJrTFY1WGREaHJ3jiwiZXhwIjoxNjMxOTQ5NTc5LCJwMmMiOjUwMDAsImFsZyI6IlBCRVMyLUhTMjy2K0ExMjhLVyIsImVuYyI6IkExMjhlQ00ifQ.E522SASh8v_TIwVLO4RmIS3D76iO0Lqr.29IifZxeNjEoqRjw.x5_em7jOCABhXRJKN8-IFk0YLLXPGZecmWJujQxmTzgaCf9y-6AZhzRWoIfwUkjeZvqfTwvUJwrcHxePznJ7_HYCLUmEjRgHJMQ0c9OBStSJhSSKYtzwR3J3N_PpmcdEtWRWN1SPlnHp9aoLHHgmBSKQpuqNb9Rdkw7-XhAyznx9bMEehZUae1rmBtNRzlGtKyInBUF9iv03zETrCkdfVt2-0IGkkQ.qMayqY2qoKybazs6pntIpw}, host => 'example.com' );
headers
Either an array reference of header-value pairs, or an HTTP::Headers object.
If an array reference is provided, an HTTP::Headers object will be instantiated with it.
host
The Host header value.
Host
max_message_size
Integer. Defaults to 20Kb. This is the maximum payload size.
number1
Value for key1 as used in protocol version 0 to 3 of WebSocket requests.
number2
Value for key2 as used in protocol version 0 to 3 of WebSocket requests.
origin
The Origin header value.
Origin
See rfc6454
protocol
HTTP/1.1. This is the only version supported by rfc6455
secure
Boolean. This is set to true when the connection is using ssl (i.e. wss), false otherwise.
wss
subprotocol
The optional subprotocol which consists of multiple arbitrary identifiers that need to be recognised and supported by the server.
WebSocket::Request->new( subprotocol => 'com.example.chat', ); # or WebSocket::Request->new( subprotocol => [qw( com.example.chat com.example.internal )], );
See rfc6455
uri
The request uri, such as /chat or it could also be a fully qualified uri such as wss://example.com/chat?csrf_token=7a292e44341dc0a052d717980563fa4528dc254bc80f3e735303ed710b764143.1631279571
/chat
wss://example.com/chat?csrf_token=7a292e44341dc0a052d717980563fa4528dc254bc80f3e735303ed710b764143.1631279571
version
The WebSocket protocol version. Defaults to draft-ietf-hybi-17
draft-ietf-hybi-17
Provided with an optional line terminator and this returns a string version of the request, based on all the parameters set in the object.
Set or get the content buffer.
Sets or gets the challenge returned to the client.
Sets or gets the checksum. If a checksum is provided, it returns the current object.
If no checksum is provided, it will compute one based on the value of "number1", "number2" and "challenge". It returns the checksum as a regular string.
Set or get the Connection header value, which should typically be Upgrade.
Connection
Upgrade
Set or get the cookies string to be used in the Cookie request header.
This is a short-cut for "header" in WebSocket::Headers
Set or get the HTTP::Headers object. If none is set, and this method is accessed, a new one will be instantiated.
Calls as_string on HTTP::Headers and returns its value.
as_string
Set or get the Host header value.
Set or get the boolean value. This is set to signal the parsing is complete.
Value of header Sec-WebSocket-Key available in protocol version 4 to 17.
Sec-WebSocket-Key
Value of header Sec-WebSocket-Key1 available in protocol version 0 to 3.
Sec-WebSocket-Key1
Value of header Sec-WebSocket-Key2 available in protocol version 0 to 3.
Sec-WebSocket-Key2
The http method used, such as GET
GET
This is a default method to store a number used for the checksum challenge sent to the client.
Set or get the Origin header value.
See rfc6455 section 1.3for more information and section 4.1, paragraph 8
my $rv = $req->parse( $some_request_data ) || die( $req->error );
Provided with some request content buffer and this will parse it using HTTP::Headers for the headers and the body with this module.
It returns undef and set an error object upon failure, and returns the current object on success.
undef
This method is kind of a misnomer because it actually performs header-parsing post processing mostly. It does some body processing for earlier version of the protocol when the handshake challenge was in the body rather than in the header.
It also tries to find out the protocol version used by the other party.
Returns the current object used.
Set or get the protocol used. Typically HTTP/1.1. This is set upon parsing. You should not have to set this yourself.
HTTP/1.1
Boolean value. True if the connection is using ssl, i.e. wss
Set or get an array object (Module::Generic::Array) of subprotocols.
See rfc6455 for more information
Set or get the Upgrade request header value, which should typically be websocket
websocket
Set or get the request uri. This returns a URI object.
Set the protocol version.
See rfc6455 section 4.1 for more information
Jacques Deguest <jack@deguest.jp>
WebSocket::Response, WebSocket::Headers, WebSocket::Common
Copyright(c) 2021-2023 DEGUEST Pte. Ltd.
You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.
To install WebSocket, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WebSocket
CPAN shell
perl -MCPAN -e shell install WebSocket
For more information on module installation, please visit the detailed CPAN module installation guide.