The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WebSocket::Request - WebSocket Request

SYNOPSIS

    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

VERSION

    v0.1.0

DESCRIPTION

Class to build or parse a WebSocket request. It inherits all the methods from WebSocket::Common. For convenience, they are all listed here.

CONSTRUCTOR

new

    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:

    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.

max_message_size

Integer. Defaults to 20Kb. This is the maximum payload size.

number1
number2
origin

The Origin header value.

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.

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

version

The WebSocket protocol version. Defaults to draft-ietf-hybi-17

See rfc6455

METHODS

as_string

Provided with an optional line terminator and this returns a string version of the request, based on all the parameters set in the object.

buffer

Set or get the content buffer.

challenge

checksum

connection

Set or get the Connection header value, which should typically be Upgrade.

cookies

Set or get the cookies string to be used in the Cookie request header.

headers

Set or get the HTTP::Headers object. If none is set, and this method is accessed, a new one will be instantiated.

headers_as_string

Calls as_string on HTTP::Headers and returns its value.

host

Set or get the Host header value.

is_done

Set or get the boolean value. This is set to signal the parsing is complete.

key

key1

key2

method

The http method used, such as GET

number1

number2

origin

Set or get the Origin header value.

See rfc6455 section 1.3for more information and section 4.1, paragraph 8

parse

    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.

parse_body

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.

protocol

Set or get the protocol used. Typically HTTP/1.1. This is set upon parsing. You should not have to set this yourself.

secure

Boolean value. True if the connection is using ssl, i.e. wss

subprotocol

Set or get an array object (Module::Generic::Array) of subprotocols.

See rfc6455 for more information

upgrade

Set or get the Upgrade request header value, which should typically be websocket

uri

Set or get the request uri. This returns a URI object.

version

Set the protocol version.

See rfc6455 section 4.1 for more information

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

perl

COPYRIGHT & LICENSE

Copyright(c) 2021 DEGUEST Pte. Ltd. DEGUEST Pte. Ltd.