NAME

Plack::Handler::H2::Writer - Streaming response writer for HTTP/2

SYNOPSIS

# In your PSGI application using delayed/streaming responses
my $app = sub {
    my $env = shift;
    
    return sub {
        my $responder = shift;
        
        # Send headers first
        my $writer = $responder->([
            200,
            ['Content-Type' => 'text/plain']
        ]);
        
        # Stream data chunks
        $writer->write("First chunk\n");
        $writer->write("Second chunk\n");
        $writer->write("Third chunk\n");
        
        # Close the stream
        $writer->close();
    };
};

DESCRIPTION

Plack::Handler::H2::Writer provides a streaming interface for sending HTTP/2 response bodies in chunks. This is used internally by Plack::Handler::H2 to implement PSGI's streaming response protocol.

When your PSGI application returns a code reference (delayed response), it receives a responder callback. Calling this responder with a status and headers returns a writer object that allows you to send the response body in multiple chunks, which is particularly useful for:

  • Large responses that don't fit in memory

  • Server-sent events

  • Streaming data generation

  • Progressive rendering

METHODS

new

my $writer = Plack::Handler::H2::Writer->new($response);

Constructor. This is called internally by the handler and should not be called directly by application code.

write

$writer->write($chunk);

Writes a chunk of data to the HTTP/2 stream. The chunk should be a string of bytes. This method can be called multiple times to send data incrementally.

Parameters:

  • $chunk - A scalar containing the data to send

Example:

$writer->write("Hello, ");
$writer->write("world!\n");

close

$writer->close();

Closes the HTTP/2 stream, signaling that no more data will be sent. You must call this method when you're done writing data, otherwise the client will wait indefinitely for more data.

After calling close(), you should not call write() again on the same writer object.

Example:

$writer->write("Final data");
$writer->close();

HTTP/2 SPECIFICS

This writer integrates with HTTP/2's streaming model:

  • Each write() call sends data with the HTTP/2 DATA frame

  • The close() method sends an END_STREAM flag

  • Content-Length headers are automatically omitted for streaming responses

  • Backpressure is handled by the HTTP/2 flow control mechanism

SEE ALSO

Plack::Handler::H2, PSGI, Plack

AUTHOR

Rawley Fowler <rawley@molluscsoftware.com>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, or under the BSD 3-Clause License.

See the LICENSE file in the distribution for the full license text.