The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

WebSocket::Message - Common Class for Request and Response

SYNOPSIS

    use WebSocket::Request;
    my $req = WebSocket::Request->new(
        host        => 'example.com',
        uri         => '/demo'
        protocol    => 'com.example.chat',
    ) || die( WebSocket::Request->error, "\n" );

VERSION

    v0.1.0

DESCRIPTION

This is a common class for WebSocket::Request and WebSocket::Response

METHODS

buffer

Sets or gets the buffer. It returns a scalar object

challenge

Sets or gets the challenge returned to the client.

checksum

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.

content

Alias for "buffer"

extensions

Set or get the available extensions. For example permessage-deflate to enable message compression.

You can set this to either a string or a WebSocket::Extension object if you want, for example to set the extension parameters.

See rfc6455 section 9.1 for more information on extension.

headers

If an argument is provided, it takes either an WebSocket::Headers object, or an object that inherits from HTTP::Headers, or an array reference.

It returns a WebSocket::Headers and will instantiate a WebSocket::Headers object if one is not already set.

headers_as_string

Returns the WebSocket::Headers as a string by calling "as_string" in WebSocket::Headers

host

Sets or gets the header value for Host

is_client_error

Returns true if the error code provided is a client error.

is_done

Returns true when the parsing is done.

is_error

Returns true when the provided code is an error

is_redirect

Returns true when the provided code is a redirect, which under the WebSocket is possible during the handshake only.

is_server_error

Returns true when the provided code is a server error.

is_success

Returns true when the provided code is a success

max_message_size

Sets or gets the maximum message size as an integer.

number1

This is a default method to store a number used for the checksum challenge sent to the client.

This method is overriden by WebSocket::Request or WebSocket::Response

number2

This is a default method to store a number used for the checksum challenge sent to the client.

This method is overriden by WebSocket::Request or WebSocket::Response

parse_chunk

Provided with some chunk data, and this will parse it and return a status. See "CONSTANTS" below.

Once parsing is done, you can retrieve the relevant object with "request" which returns a WebSocket::Request object or "response" which returns a WebSocket::Response object.

If an error occurs, this will returns undef and sets an error exception

protocol

Returns the http protocol used, which should always be HTTP/1.1

request

Set or get a WebSocket::Request object. This is set by "parse_chunk"

response

Set or get a WebSocket::Response object. This is set by "parse_chunk"

secure

Boolean value. True when the connection is using ssl, false otherwise.

status_message

Returns the status message provided by the other party.

subprotocol

Set or get an array object of WebSocket protocols.

Returns a Module::Generic::Array object.

See rfc6455 for more information

uri

Set or get the uri of the current request or response. Returns a URI object.

version

Set or get the WebSocket version supported.

PRIVATE METHODS

_append

Provided with some data, or a IO::Socket object, and this will add the data to the current buffer (buffer), or issue a read call on the IO::Socket object and read "max_message_size" bytes of data.

If the resulting buffer exceeds "max_message_size", this will return undef and sets an error object, so you need to check that the size of the current buffer + the data you provide doe snot exceed the value of "max_message_size"

_extract_number

Provided with some value and this will extract any digit, count the number of spaces and return an integer of the number extracted divided by the number of spaces found.

This is called by the methods "number1" and "number2"

CONSTANTS

The following constants are available and can be exported into your name space either individually, or by using the tag :all

PARSE_DONE

Returned by "parse_chunk" when the parsing is done.

PARSE_INCOMPLETE

Returned by "parse_chunk" when the parsing is incomplete.

PARSE_WAITING

Returned by "parse_chunk" when the parsing is ongoing and the double line end of header separator has not yet been reached.

PARSE_MAYBE_MORE

Returned by "parse_chunk" to indicate that maybe more data is expected.

CREDITS

Credits to David Robins for code borrowed from HTTP::Parser for chunk parsing

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

WebSocket::Request, WebSocket::Response, HTTP::Headers, HTTP::Parser

COPYRIGHT & LICENSE

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.