vertx / io.vertx.core.http / HttpServerResponse

HttpServerResponse

interface HttpServerResponse : WriteStream<Buffer>

Represents a server-side HTTP response.

An instance of this is created and associated to every instance of HttpServerRequest 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 io.vertx.core.streams.WriteStream so it can be used with io.vertx.core.streams.Pump to pump data with flow control.

Author
Tim Fox

Functions

bodyEndHandler

abstract fun bodyEndHandler(handler: Handler<Void>): HttpServerResponse

Provides a handler that will be called after the last part of the body is written to the wire. 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.

bytesWritten

abstract fun bytesWritten(): Long

close

abstract fun close(): Unit

Close the underlying TCP connection corresponding to the request.

closeHandler

abstract fun closeHandler(handler: Handler<Void>): HttpServerResponse

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

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.

closed

abstract fun closed(): Boolean

drainHandler

abstract fun drainHandler(handler: Handler<Void>): HttpServerResponse

end

abstract fun end(chunk: String): Unit

Same as #end(Buffer) but writes a String in UTF-8 encoding before ending the response.

abstract fun end(chunk: String, enc: String): Unit

Same as #end(Buffer) but writes a String with the specified encoding before ending the response.

abstract fun end(chunk: Buffer): Unit

Same as #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.

abstract fun end(): Unit

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.

Once the response has ended, it cannot be used any more.

endHandler

abstract fun endHandler(handler: Handler<Void>): HttpServerResponse

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

ended

abstract fun ended(): Boolean

exceptionHandler

abstract fun exceptionHandler(handler: Handler<Throwable>): HttpServerResponse

getStatusCode

abstract fun getStatusCode(): Int

getStatusMessage

abstract fun getStatusMessage(): String

headWritten

abstract fun headWritten(): Boolean

headers

abstract fun headers(): MultiMap

headersEndHandler

abstract fun headersEndHandler(handler: Handler<Void>): HttpServerResponse

Provide a handler that will be called just before the headers are written to the wire.

This provides a hook allowing you to add any more headers or do any more operations before this occurs.

isChunked

abstract fun isChunked(): Boolean

push

abstract fun push(method: HttpMethod, host: String, path: String, handler: Handler<AsyncResult<HttpServerResponse>>): HttpServerResponse

Like #push(HttpMethod, String, String, MultiMap, Handler) with no headers.

abstract fun push(method: HttpMethod, path: String, headers: MultiMap, handler: Handler<AsyncResult<HttpServerResponse>>): HttpServerResponse
abstract fun push(method: HttpMethod, path: String, handler: Handler<AsyncResult<HttpServerResponse>>): HttpServerResponse

Like #push(HttpMethod, String, String, MultiMap, Handler) with the host copied from the current request.

abstract fun push(method: HttpMethod, host: String, path: String, headers: MultiMap, handler: Handler<AsyncResult<HttpServerResponse>>): HttpServerResponse

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.

putHeader

abstract fun putHeader(name: String, value: String): HttpServerResponse

Put an HTTP header

abstract fun putHeader(name: CharSequence, value: CharSequence): HttpServerResponse

Like #putHeader(String, String) but using CharSequence

abstract fun putHeader(name: String, values: MutableIterable<String>): HttpServerResponse

Like #putHeader(String, String) but providing multiple values via a String Iterable

abstract fun putHeader(name: CharSequence, values: MutableIterable<CharSequence>): HttpServerResponse

Like #putHeader(String, Iterable) but with CharSequence Iterable

putTrailer

abstract fun putTrailer(name: String, value: String): HttpServerResponse

Put an HTTP trailer

abstract fun putTrailer(name: CharSequence, value: CharSequence): HttpServerResponse

Like #putTrailer(String, String) but using CharSequence

abstract fun putTrailer(name: String, values: MutableIterable<String>): HttpServerResponse

Like #putTrailer(String, String) but providing multiple values via a String Iterable

abstract fun putTrailer(name: CharSequence, value: MutableIterable<CharSequence>): HttpServerResponse

Like #putTrailer(String, Iterable) but with CharSequence Iterable

reset

open fun reset(): Unit

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

abstract fun reset(code: Long): Unit

Reset this HTTP/2 stream with the error code.

sendFile

open fun sendFile(filename: String): HttpServerResponse

Same as #sendFile(String, long) using offset @code{0} which means starting from the beginning of the file.

open fun sendFile(filename: String, offset: Long): HttpServerResponse

Same as #sendFile(String, long, long) using length @code{Long.MAX_VALUE} which means until the end of the file.

abstract fun sendFile(filename: String, offset: Long, length: Long): HttpServerResponse

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.

open fun sendFile(filename: String, resultHandler: Handler<AsyncResult<Void>>): HttpServerResponse

Like #sendFile(String) but providing a handler which will be notified once the file has been completely written to the wire.

open fun sendFile(filename: String, offset: Long, resultHandler: Handler<AsyncResult<Void>>): HttpServerResponse

Like #sendFile(String, long) but providing a handler which will be notified once the file has been completely written to the wire.

abstract fun sendFile(filename: String, offset: Long, length: Long, resultHandler: Handler<AsyncResult<Void>>): HttpServerResponse

Like #sendFile(String, long, long) but providing a handler which will be notified once the file has been completely written to the wire.

setChunked

abstract fun setChunked(chunked: Boolean): HttpServerResponse

If chunked is true, 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.

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.

setStatusCode

abstract fun setStatusCode(statusCode: Int): HttpServerResponse

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.

setStatusMessage

abstract fun setStatusMessage(statusMessage: String): HttpServerResponse

Set the status message

setWriteQueueMaxSize

abstract fun setWriteQueueMaxSize(maxSize: Int): HttpServerResponse

streamId

abstract fun streamId(): Int

trailers

abstract fun trailers(): MultiMap

write

abstract fun write(data: Buffer): HttpServerResponseabstract fun write(chunk: String, enc: String): HttpServerResponse

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

abstract fun write(chunk: String): HttpServerResponse

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

writeContinue

abstract fun writeContinue(): HttpServerResponse

Used to write an interim 100 Continue response to signify that the client should send the rest of the request. Must only be used if the request contains an "Expect:100-Continue" header

writeCustomFrame

abstract fun writeCustomFrame(type: Int, flags: Int, payload: Buffer): HttpServerResponse

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.

open fun writeCustomFrame(frame: HttpFrame): HttpServerResponse

Like #writeCustomFrame(int, int, Buffer) but with an HttpFrame.