HttpServerResponse

Represents a server-side HTTP response.

An instance of this is created and associated to every instance of

see

that.

It allows the developer to control the HTTP response that is sent back to the client for a particular HTTP request.

It contains methods that allow HTTP headers and trailers to be set, and for a body to be written out to the response.

It also allows files to be streamed by the kernel directly from disk to the outgoing HTTP connection, bypassing user space altogether (where supported by the underlying operating system). This is a very efficient way of serving files from the server since buffers do not have to be read one by one from the file and written to the outgoing socket.

It implements @see \io\vertx\jphp\core\streams\WriteStream so it can be used with

see

to pump data with flow control.

package

Default

Methods

__construct

__construct() 

Provides a handler that will be called after the last part of the body is written to the wire.

bodyEndHandler( $arg0) : $this

The handler is called asynchronously of when the response has been received by the client. This provides a hook allowing you to do more operations once the request has been sent over the wire such as resource cleanup.

Arguments

$arg0

callable

Response

$this

a reference to this, so the API can be used fluently

bytesWritten

bytesWritten() : integer

Response

integer

the total number of bytes written for the body of the response.

Close the underlying TCP connection corresponding to the request.

close() : void

closed

closed() : boolean

Response

boolean

has the underlying TCP connection corresponding to the request already been closed?

Set a close handler for the response, this is called when the underlying connection is closed and the response was still using the connection.

closeHandler( $arg0) : $this

For HTTP/1.x it is called when the connection is closed before end() is called, therefore it is not guaranteed to be called.

For HTTP/2 it is called when the related stream is closed, and therefore it will be always be called.

Arguments

$arg0

callable

Response

$this

a reference to this, so the API can be used fluently

drainHandler

drainHandler( $arg0) : $this

Arguments

$arg0

callable

Response

$this

Ends the response. If no data has been written to the response body, the actual response won't get written until this method gets called.

end( $arg0 = null,  $arg1 = null) : void

Once the response has ended, it cannot be used any more. end() Same as @see \io\vertx\jphp\core\http\HttpServerResponse::end but writes a String in UTF-8 encoding before ending the response. param $chunk [string] the string to write before ending the response end($chunk) Same as @see \io\vertx\jphp\core\http\HttpServerResponse::end but writes some data to the response body before ending. If the response is not chunked and no other data has been written then the @code{Content-Length} header will be automatically set. param $chunk [Buffer] the buffer to write before ending the response end($chunk) Same as @see \io\vertx\jphp\core\http\HttpServerResponse::end but writes a String with the specified encoding before ending the response. param $chunk [string] the string to write before ending the response param $enc [string] the encoding to use end($chunk, $enc)

Arguments

$arg0

Buffer | string

$arg1

string

ended

ended() : boolean

Response

boolean

has the response already ended?

Set an end handler for the response. This will be called when the response is disposed to allow consistent cleanup of the response.

endHandler( $arg0) : $this

Arguments

$arg0

callable

Response

$this

a reference to this, so the API can be used fluently

exceptionHandler

exceptionHandler( $arg0) : $this

Arguments

$arg0

callable

Response

$this

getStatusCode

getStatusCode() : integer

Response

integer

the HTTP status code of the response. The default is {@code 200} representing {@code OK}.

getStatusMessage

getStatusMessage() : string

Response

string

the HTTP status message of the response. If this is not specified a default value will be used depending on what {@link #setStatusCode} has been set to.

headers

headers() : \io\vertx\jphp\core\MultiMap

Response

\io\vertx\jphp\core\MultiMap

The HTTP headers

Provide a handler that will be called just before the headers are written to the wire.<p> This provides a hook allowing you to add any more headers or do any more operations before this occurs.

headersEndHandler( $arg0) : $this

Arguments

$arg0

callable

Response

$this

a reference to this, so the API can be used fluently

headWritten

headWritten() : boolean

Response

boolean

have the headers for the response already been written?

isChunked

isChunked() : boolean

Response

boolean

is the response chunked?

Like @see \io\vertx\jphp\core\http\HttpServerResponse::push with the host copied from the current request.

push( $arg0,  $arg1,  $arg2,  $arg3 = null,  $arg4 = null) : $this

param $method [string] param $path [string] param $handler [callable] push($method, $path, $handler)

Like @see \io\vertx\jphp\core\http\HttpServerResponse::push with no headers.

param $method [string] param $host [string] param $path [string] param $handler [callable] push($method, $host, $path, $handler)

Like @see \io\vertx\jphp\core\http\HttpServerResponse::push with the host copied from the current request.

param $method [string] param $path [string] param $headers [MultiMap] param $handler [callable] push($method, $path, $headers, $handler)

Push a response to the client.

The handler will be notified with a success when the push can be sent and with a failure when the client has disabled push or reset the push before it has been sent.

The handler may be queued if the client has reduced the maximum number of streams the server can push concurrently.

Push can be sent only for peer initiated streams and if the response is not ended.

param $method [string] the method of the promised request param $host [string] the host of the promised request param $path [string] the path of the promised request param $headers [MultiMap] the headers of the promised request param $handler [callable] the handler notified when the response can be written push($method, $host, $path, $headers, $handler)

Arguments

$arg0

string

$arg1

string

$arg2

MultiMap | callable | string

$arg3

MultiMap | callable

$arg4

callable

Response

$this

Put an HTTP header

putHeader( $arg0,  $arg1) : $this

Arguments

$arg0

string

$arg1

string

Response

$this

a reference to this, so the API can be used fluently

Put an HTTP trailer

putTrailer( $arg0,  $arg1) : $this

Arguments

$arg0

string

$arg1

string

Response

$this

a reference to this, so the API can be used fluently

Reset this HTTP/2 stream with the error code <code>0</code>.

reset( $arg0 = null) : void

reset()

Reset this HTTP/2 stream with the error code.

param $code [integer] the error code reset($code)

Arguments

$arg0

integer

Same as @see \io\vertx\jphp\core\http\HttpServerResponse::sendFile using offset @code{0} which means starting from the beginning of the file.

sendFile( $arg0,  $arg1 = null,  $arg2 = null,  $arg3 = null) : $this

param $filename [string] path to the file to serve sendFile($filename)

Same as @see \io\vertx\jphp\core\http\HttpServerResponse::sendFile using length @code{Long.MAX_VALUE} which means until the end of the file.

param $filename [string] path to the file to serve param $offset [integer] offset to start serving from sendFile($filename, $offset)

Like @see \io\vertx\jphp\core\http\HttpServerResponse::sendFile but providing a handler which will be notified once the file has been completely written to the wire.

param $filename [string] path to the file to serve param $resultHandler [callable] handler that will be called on completion sendFile($filename, $resultHandler)

Ask the OS to stream a file as specified by filename directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system. This is a very efficient way to serve files.

The actual serve is asynchronous and may not complete until some time after this method has returned.

param $filename [string] path to the file to serve param $offset [integer] offset to start serving from param $length [integer] the number of bytes to send sendFile($filename, $offset, $length)

Like @see \io\vertx\jphp\core\http\HttpServerResponse::sendFile but providing a handler which will be notified once the file has been completely written to the wire.

param $filename [string] path to the file to serve param $offset [integer] the offset to serve from param $resultHandler [callable] handler that will be called on completion sendFile($filename, $offset, $resultHandler)

Like @see \io\vertx\jphp\core\http\HttpServerResponse::sendFile but providing a handler which will be notified once the file has been completely written to the wire.

param $filename [string] path to the file to serve param $offset [integer] the offset to serve from param $length [integer] the length to serve to param $resultHandler [callable] handler that will be called on completion sendFile($filename, $offset, $length, $resultHandler)

Arguments

$arg0

string

$arg1

callable | integer

$arg2

callable | integer

$arg3

callable

Response

$this

a reference to this, so the API can be used fluently

If <code>chunked</code> is <code>true</code>, this response will use HTTP chunked encoding, and each call to write to the body will correspond to a new HTTP chunk sent on the wire.

setChunked( $arg0) : $this

If chunked encoding is used the HTTP header Transfer-Encoding with a value of Chunked will be automatically inserted in the response.

If chunked is false, this response will not use HTTP chunked encoding, and therefore the total size of any data that is written in the respone body must be set in the Content-Length header before any data is written out.

An HTTP chunked response is typically used when you do not know the total size of the request body up front.

Arguments

$arg0

boolean

Response

$this

a reference to this, so the API can be used fluently

Set the status code. If the status message hasn't been explicitly set, a default status message corresponding to the code will be looked-up and used.

setStatusCode( $arg0) : $this

Arguments

$arg0

integer

Response

$this

a reference to this, so the API can be used fluently

Set the status message

setStatusMessage( $arg0) : $this

Arguments

$arg0

string

Response

$this

a reference to this, so the API can be used fluently

Sets the priority of the associated stream <p/> This is not implemented for HTTP/1.x.

setStreamPriority( $arg0) : $this

Arguments

$arg0

StreamPriority | array

Response

$this

setWriteQueueMaxSize

setWriteQueueMaxSize( $arg0) : $this

Arguments

$arg0

integer

Response

$this

streamId

streamId() : integer

Response

integer

the id of the stream of this response, {@literal -1} for HTTP/1.x

trailers

trailers() : \io\vertx\jphp\core\MultiMap

Response

\io\vertx\jphp\core\MultiMap

The HTTP trailers

param $data [Buffer] <b> write($data) </b>

write( $arg0,  $arg1 = null) : $this

Write a \java.lang.String to the response body, encoded in UTF-8.

param $chunk [string] the string to write write($chunk)

Write a \java.lang.String to the response body, encoded using the encoding enc.

param $chunk [string] the string to write param $enc [string] the encoding to use write($chunk, $enc)

Arguments

$arg0

Buffer | string

$arg1

string

Response

$this

Used to write an interim 100 Continue response to signify that the client should send the rest of the request.

writeContinue() : $this

Must only be used if the request contains an "Expect:100-Continue" header

Response

$this

a reference to this, so the API can be used fluently

Like @see \io\vertx\jphp\core\http\HttpServerResponse::writeCustomFrame but with an @see \io\vertx\jphp\core\http\HttpFrame.

writeCustomFrame( $arg0,  $arg1 = null,  $arg2 = null) : $this

param $frame [HttpFrame] the frame to write writeCustomFrame($frame)

Write an HTTP/2 frame to the response, allowing to extend the HTTP/2 protocol.

The frame is sent immediatly and is not subject to flow control.

param $type [integer] the 8-bit frame type param $flags [integer] the 8-bit frame flags param $payload [Buffer] the frame payload writeCustomFrame($type, $flags, $payload)

Arguments

$arg0

integer | HttpFrame

$arg1

integer

$arg2

Buffer

Response

$this

This will return <code>true</code> if there are more bytes in the write queue than the value set using @see \io\vertx\jphp\core\http\HttpServerResponse::setWriteQueueMaxSize

writeQueueFull() : boolean

Response

boolean

true if write queue is full