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.0
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:
Content buffer
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' );
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.
The Host header value.
Host
Integer. Defaults to 20Kb. This is the maximum payload size.
The Origin header value.
Origin
See rfc6454
HTTP/1.1. This is the only version supported by rfc6455
Boolean. This is set to true when the connection is using ssl (i.e. wss), false otherwise.
wss
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
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
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.
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.
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.
The http method used, such as GET
GET
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>
perl
Copyright(c) 2021 DEGUEST Pte. Ltd. DEGUEST Pte. Ltd.
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.