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

NAME

WebSocket::Response - WebSocket Response

SYNOPSIS

    use WebSocket::Response;
    my $res = WebSocket::Response->new(
        host        => 'example.com',
        uri         => '/demo',
        origin      => 'http://example.com',
        number1     => 777_007_543,
        number2     => 114_997_259,
        challenge   => "\x47\x30\x22\x2D\x5A\x3F\x47\x58"
    ) || die( WebSocket::Response->error, "\n" );
    $res->as_string;
    # HTTP/1.1 101 WebSocket Protocol Handshake
    # Upgrade: WebSocket
    # Connection: Upgrade
    # Sec-WebSocket-Origin: http://example.com
    # Sec-WebSocket-Location: ws://example.com/demo
    #
    # 0st3Rl&q-2ZU^weu

    # Parser
    $res = WebSocket::Response->new;
    $res->parse( <<EOT );
    HTTP/1.1 101 WebSocket Protocol Handshake
    Upgrade: WebSocket
    Connection: Upgrade
    Sec-WebSocket-Origin: file://
    Sec-WebSocket-Location: ws://example.com/demo
    
    0st3Rl&q-2ZU^weu
    EOT

VERSION

    v0.1.0

DESCRIPTION

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

CONSTRUCTOR

new

    my $req = WebSocket::Response->new( $code, $status, $headers, $buffer,
        host        => 'example.com',
        uri         => 'wss://example.com/chat',
        origin      => 'http://example.com',
        subprotocol => 'com.example.chat'
    );
    my $req = WebSocket::Response->new( $code, $status, $headers, $buffer, {
        host        => 'example.com',
        uri         => 'wss://example.com/chat',
        origin      => 'http://example.com',
        subprotocol => 'com.example.chat'
    });
    my $req = WebSocket::Response->new( $code, $status, $headers,
        host        => 'example.com',
        uri         => 'wss://example.com/chat',
        origin      => 'http://example.com',
        subprotocol => 'com.example.chat'
    );
    my $req = WebSocket::Response->new( $code, $status, $headers, {
        host        => 'example.com',
        uri         => 'wss://example.com/chat',
        origin      => 'http://example.com',
        subprotocol => 'com.example.chat'
    });

Provided with an http code, http status, 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::Response 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

code

An integer representing the status code, such as 101 (switching protocol).

cookies

A Set-Cookie response 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{id=a3fWa; Max-Age=2592000},
        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.

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.

status

The status line, such as Switching Protocol. This is set upon parsing. There should be no need to set this by yourself.

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

version

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

See rfc6455

versions

Same as version, but pass an array of WebSocket::Version objects or version number, or draft identifier such as <draft-ietf-hybi-17>

METHODS

as_string

body_as_string

code

cookies

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

location

number1

number2

parse

    my $rv = $res->parse( $some_response_data ) ||
        die( $res->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

status

Set or get the response status line, such as Switching Protocol

subprotocol

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

See rfc6455 for more information

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

versions

Same as "versions", but pass an array of WebSocket::Version objects or version number, or draft identifier such as <draft-ietf-hybi-17>

Multiple versions are part of the protocol handshake negotiation from protocol version 4 and above.

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

perl

COPYRIGHT & LICENSE

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