NAME

Net::Blossom::Server::Response - Framework-neutral Blossom server response

SYNOPSIS

use Net::Blossom::Server::Response;

my $response = Net::Blossom::Server::Response->json(
    { ok => 1 },
    status => 201,
);

my $status = $response->status;

DESCRIPTION

Net::Blossom::Server::Response represents a server response before any PSGI, PAGI, or other gateway adapter turns it into gateway-native output.

Responses may carry scalar bodies, array reference bodies, or stream objects. Gateway adapters are responsible for mapping those body forms to their native output protocol.

CONSTRUCTORS

new

my $response = Net::Blossom::Server::Response->new(%args);

Required status must be an HTTP status code from 100 through 599.

Optional headers defaults to an empty hash reference. Header names are case-insensitive and duplicate case-insensitive names croak. Header values must be defined scalars and must not contain CR or LF.

Optional body defaults to the empty string. It may be a scalar byte string, an array reference of scalar byte strings, or a stream object that provides read or getline.

json

my $response = Net::Blossom::Server::Response->json($data, %opts);

Encodes $data as canonical UTF-8 JSON. status defaults to 200. The response includes Content-Type: application/json and Content-Length unless those headers are supplied in headers.

text

my $response = Net::Blossom::Server::Response->text($body, %opts);

Builds a text response. status defaults to 200. The response includes Content-Type: text/plain; charset=utf-8 and Content-Length unless those headers are supplied in headers.

empty

my $response = Net::Blossom::Server::Response->empty($status, %opts);

Builds an empty response. status defaults to 204. The response includes Content-Length: 0 unless supplied in headers.

redirect

my $response = Net::Blossom::Server::Response->redirect($location, %opts);

Builds a redirect response. status defaults to 307. $location is required. The response includes Location and Content-Length: 0.

error

my $response = Net::Blossom::Server::Response->error($status, $reason, %opts);

Builds an empty error response. $reason, when defined and non-empty, is sent as X-Reason and therefore must not contain CR or LF.

ACCESSORS

status

Returns the HTTP status code.

body

Returns the scalar, array reference, or stream body.

METHODS

headers

my $headers = $response->headers;

Returns a copy hash reference of response headers.

my $value = $response->header($name);

Returns a header value using case-insensitive lookup. Returns undef when the header is absent.

header_pairs

my $pairs = $response->header_pairs;

Returns an array reference suitable for PSGI-style [name => value, ...] headers.

body_chunks

my $chunks = $response->body_chunks;

Returns scalar and array reference bodies as an array reference of chunks. Stream bodies croak because they must be consumed by the gateway adapter.

INTERNAL METHODS

BUILDARGS

Normalizes constructor arguments for Class::Tiny.

BUILD

Validates the constructed object for Class::Tiny.