NAME

Linux::Event::HTTP::Response - HTTP response message

SYNOPSIS

my $response = Linux::Event::HTTP::Response->new(
    status => 200,
    headers => [
        [ 'Content-Type', 'text/plain' ],
    ],
    body => "hello\n",
);

Server callbacks receive the same Response class:

sub on_request ($conn, $req, $res) {
    $res->status(200);
    $res->header('Content-Type', 'text/plain');
    $res->body("hello\n");
}

DESCRIPTION

Linux::Event::HTTP::Response represents one HTTP response message. It is not a socket, transaction, connection, or writable transport handle. The same message class is used for locally constructed outgoing responses and parsed incoming client responses.

Its public message API conforms directly to the Uniform::HTTP 0.02 message contract by behavior without inheriting from a Uniform class. Duplicate fields, field order, original field-name spelling, buffered-body state, completeness, and mutability are reported explicitly while transport and Transaction state remain outside the message.

A Response owns status, reason, version, headers, complete scalar-body data, and message completion state. It does not retain its peer Request or the Connection that happens to carry it. Exchange lifecycle, output progress, cancellation, Upgrade, and incremental body production belong to Linux::Event::HTTP::Transaction and the protocol Connection.

Selecting a complete scalar body on a locally constructed Response makes the message body complete immediately. That does not mean the message has been written to a transport. Incremental body production is selected through the owning Transaction; the Response records only that its body is incomplete until the producer announces its final bytes.

A received client Response normally exposes body bytes incrementally through the Client callback path. If the caller explicitly requests bounded whole-body buffering, body returns that completed scalar after the message boundary is reached. Received response metadata remains committed and read-only either way.

METHODS

new

Constructs a mutable response message. status defaults to 200 and version defaults to 1.1. headers is an optional array reference of [name, value] pairs. body is an optional complete scalar byte body.

status

Gets or sets an HTTP response status from 100 through 599 before message commit.

reason

Gets or sets the optional HTTP/1 reason phrase before message commit. Passing undef clears it; no standard reason phrase is synthesized.

version

Gets or sets the HTTP version before message commit. Passing undef clears the represented version; an HTTP executor will reject an unset version when needed.

Gets the first matching field value. The setter form replaces all fields of the same ASCII case-insensitive name with one field at the position of the first occurrence, or appends it when absent.

add_header

Adds another header field while preserving existing same-name fields.

remove_header

Removes all fields with the supplied ASCII case-insensitive name.

header_values

Returns an array reference containing all matching values in message order. An absent field returns an empty array reference. Values are never implicitly comma-joined.

header_count, header_name, header_value

Provide exact indexed access to fields in message order while preserving the original field names. A non-negative index beyond the end returns undef; negative and non-integer indexes are programmer errors.

headers_are_lossless

Returns true because duplicate occurrences, inter-field order, and original field-name spelling are retained.

content_length

Returns the declared Content-Length as an integer, or undef when absent.

body

Gets or sets the complete scalar byte body. Setting it is available only while a locally constructed Response is mutable and declares that its message body is complete. Incremental output is selected through the owning Transaction rather than through the Response message. Passing undef is an error; an explicit empty body is ''.

For a received client Response, the getter returns the complete body only when the client was explicitly asked to buffer it within a bounded limit. Otherwise received body bytes remain incremental and body returns undef.

has_buffered_body

Returns true only when a complete scalar body buffer is locally available, including an explicit empty buffer.

is_complete

Returns whether the complete HTTP message body is known or its final boundary has been reached. This is deliberately independent of whether an outgoing message has started or finished writing to a transport.

is_mutable

Returns true until the Response metadata is committed to protocol execution and false afterward. Mutators throw once the Response is committed.

SEE ALSO

Uniform::HTTP, Linux::Event::HTTP::Request, Linux::Event::HTTP::Transaction.