Interface HttpServerResponse
-
- All Superinterfaces:
StreamBase
,WriteStream<Buffer>
public interface HttpServerResponse extends 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
WriteStream
so it can be used withPipe
to pipe data with flow control.- Author:
- Tim Fox
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description HttpServerResponse
addCookie(Cookie cookie)
Add a cookie.HttpServerResponse
bodyEndHandler(Handler<Void> handler)
Provides a handler that will be called after the last part of the body is written to the wire.long
bytesWritten()
boolean
closed()
HttpServerResponse
closeHandler(Handler<Void> handler)
Set a close handler for the response, this is called when the underlying connection is closed and the response was still using the connection.HttpServerResponse
drainHandler(Handler<Void> handler)
Set a drain handler on the stream.Future<Void>
end()
Ends the response.Future<Void>
end(Buffer chunk)
Same asend()
but writes some data to the response body before ending.Future<Void>
end(String chunk)
Same asend(Buffer)
but writes a String in UTF-8 encoding before ending the response.Future<Void>
end(String chunk, String enc)
Same asend(Buffer)
but writes a String with the specified encoding before ending the response.boolean
ended()
HttpServerResponse
endHandler(Handler<Void> handler)
Set an end handler for the response.HttpServerResponse
exceptionHandler(Handler<Throwable> handler)
Set an exception handler on the write stream.int
getStatusCode()
String
getStatusMessage()
MultiMap
headers()
HttpServerResponse
headersEndHandler(Handler<Void> handler)
Provide a handler that will be called just before the headers are written to the wire.boolean
headWritten()
boolean
isChunked()
Future<HttpServerResponse>
push(HttpMethod method, HostAndPort authority, String path, MultiMap headers)
Push a response to the client.default Future<HttpServerResponse>
push(HttpMethod method, String path)
Likepush(HttpMethod, String, String, MultiMap)
with the host copied from the current request.default Future<HttpServerResponse>
push(HttpMethod method, String path, MultiMap headers)
Likepush(HttpMethod, String, String, MultiMap)
with the host copied from the current request.default Future<HttpServerResponse>
push(HttpMethod method, String host, String path)
Likepush(HttpMethod, String, String, MultiMap)
with no headers.Future<HttpServerResponse>
push(HttpMethod method, String host, String path, MultiMap headers)
Deprecated.instead usepush(HttpMethod, HostAndPort, String, MultiMap)
HttpServerResponse
putHeader(CharSequence name, CharSequence value)
LikeputHeader(String, String)
but using CharSequenceHttpServerResponse
putHeader(CharSequence name, Iterable<CharSequence> values)
LikeputHeader(String, Iterable)
but with CharSequence IterableHttpServerResponse
putHeader(String name, Iterable<String> values)
LikeputHeader(String, String)
but providing multiple values via a String IterableHttpServerResponse
putHeader(String name, String value)
Put an HTTP headerHttpServerResponse
putTrailer(CharSequence name, CharSequence value)
LikeputTrailer(String, String)
but using CharSequenceHttpServerResponse
putTrailer(CharSequence name, Iterable<CharSequence> value)
LikeputTrailer(String, Iterable)
but with CharSequence IterableHttpServerResponse
putTrailer(String name, Iterable<String> values)
LikeputTrailer(String, String)
but providing multiple values via a String IterableHttpServerResponse
putTrailer(String name, String value)
Put an HTTP trailerdefault Cookie
removeCookie(String name)
Expire a cookie, notifying a User Agent to remove it from its cookie jar.Cookie
removeCookie(String name, boolean invalidate)
Remove a cookie from the cookie set.default Cookie
removeCookie(String name, String domain, String path)
Expires a cookie from the cookie set.Cookie
removeCookie(String name, String domain, String path, boolean invalidate)
Remove a cookie from the cookie set.default Set<Cookie>
removeCookies(String name)
Expire all cookies, notifying a User Agent to remove it from its cookie jar.Set<Cookie>
removeCookies(String name, boolean invalidate)
Remove all cookies from the cookie set.default boolean
reset()
Reset this HTTP/2 stream with the error code0
.boolean
reset(long code)
Reset this response:default Future<Void>
send()
Send the request with an empty body.default Future<Void>
send(Buffer body)
Send the request with a bufferbody
.default Future<Void>
send(ReadStream<Buffer> body)
Send the request with a streambody
.default Future<Void>
send(String body)
Send the request with a stringbody
.default Future<Void>
sendFile(String filename)
Send the request with a streambody
.default Future<Void>
sendFile(String filename, long offset)
Same assendFile(String, long, long)
using length @code{Long.MAX_VALUE} which means until the end of the file.Future<Void>
sendFile(String filename, long offset, long length)
Ask the OS to stream a file as specified byfilename
directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system.HttpServerResponse
setChunked(boolean chunked)
Ifchunked
istrue
, 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.HttpServerResponse
setStatusCode(int statusCode)
Set the status code.HttpServerResponse
setStatusMessage(String statusMessage)
Set the status messagedefault HttpServerResponse
setStreamPriority(StreamPriority streamPriority)
Sets the priority of the associated streamHttpServerResponse
setWriteQueueMaxSize(int maxSize)
Set the maximum size of the write queue tomaxSize
.int
streamId()
MultiMap
trailers()
Future<Void>
write(String chunk)
Write aString
to the response body, encoded in UTF-8.Future<Void>
write(String chunk, String enc)
Write aString
to the response body, encoded using the encodingenc
.Future<Void>
writeContinue()
Used to write an interim 100 Continue response to signify that the client should send the rest of the request.Future<Void>
writeCustomFrame(int type, int flags, Buffer payload)
Write an HTTP/2 frame to the response, allowing to extend the HTTP/2 protocol.default Future<Void>
writeCustomFrame(HttpFrame frame)
LikewriteCustomFrame(int, int, Buffer)
but with anHttpFrame
.Future<Void>
writeEarlyHints(MultiMap headers)
Used to write an interim 103 Early Hints response to return some HTTP headers before the final HTTP message.-
Methods inherited from interface io.vertx.core.streams.WriteStream
write, writeQueueFull
-
-
-
-
Method Detail
-
exceptionHandler
HttpServerResponse exceptionHandler(Handler<Throwable> handler)
Description copied from interface:WriteStream
Set an exception handler on the write stream.- Specified by:
exceptionHandler
in interfaceStreamBase
- Specified by:
exceptionHandler
in interfaceWriteStream<Buffer>
- Parameters:
handler
- the exception handler- Returns:
- a reference to this, so the API can be used fluently
-
setWriteQueueMaxSize
HttpServerResponse setWriteQueueMaxSize(int maxSize)
Description copied from interface:WriteStream
Set the maximum size of the write queue tomaxSize
. You will still be able to write to the stream even if there is more thanmaxSize
items in the write queue. This is used as an indicator by classes such asPipe
to provide flow control. The value is defined by the implementation of the stream, e.g in bytes for aNetSocket
, etc...- Specified by:
setWriteQueueMaxSize
in interfaceWriteStream<Buffer>
- Parameters:
maxSize
- the max size of the write stream- Returns:
- a reference to this, so the API can be used fluently
-
drainHandler
HttpServerResponse drainHandler(Handler<Void> handler)
Description copied from interface:WriteStream
Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write queue is ready to accept buffers again. SeePipe
for an example of this being used.The stream implementation defines when the drain handler, for example it could be when the queue size has been reduced to
maxSize / 2
.- Specified by:
drainHandler
in interfaceWriteStream<Buffer>
- Parameters:
handler
- the handler- Returns:
- a reference to this, so the API can be used fluently
-
getStatusCode
int getStatusCode()
- Returns:
- the HTTP status code of the response. The default is
200
representingOK
.
-
setStatusCode
HttpServerResponse setStatusCode(int statusCode)
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.- Returns:
- a reference to this, so the API can be used fluently
-
getStatusMessage
String getStatusMessage()
- Returns:
- the HTTP status message of the response. If this is not specified a default value will be used depending on what
setStatusCode(int)
has been set to.
-
setStatusMessage
HttpServerResponse setStatusMessage(String statusMessage)
Set the status message- Returns:
- a reference to this, so the API can be used fluently
-
setChunked
HttpServerResponse setChunked(boolean chunked)
Ifchunked
istrue
, 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 ofChunked
will be automatically inserted in the response.If
chunked
isfalse
, 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 theContent-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.
- Returns:
- a reference to this, so the API can be used fluently
-
isChunked
boolean isChunked()
- Returns:
- is the response chunked?
-
headers
MultiMap headers()
- Returns:
- The HTTP headers
-
putHeader
HttpServerResponse putHeader(String name, String value)
Put an HTTP header- Parameters:
name
- the header namevalue
- the header value.- Returns:
- a reference to this, so the API can be used fluently
-
putHeader
HttpServerResponse putHeader(CharSequence name, CharSequence value)
LikeputHeader(String, String)
but using CharSequence
-
putHeader
HttpServerResponse putHeader(String name, Iterable<String> values)
LikeputHeader(String, String)
but providing multiple values via a String Iterable
-
putHeader
HttpServerResponse putHeader(CharSequence name, Iterable<CharSequence> values)
LikeputHeader(String, Iterable)
but with CharSequence Iterable
-
trailers
MultiMap trailers()
- Returns:
- The HTTP trailers
-
putTrailer
HttpServerResponse putTrailer(String name, String value)
Put an HTTP trailer- Parameters:
name
- the trailer namevalue
- the trailer value- Returns:
- a reference to this, so the API can be used fluently
-
putTrailer
HttpServerResponse putTrailer(CharSequence name, CharSequence value)
LikeputTrailer(String, String)
but using CharSequence
-
putTrailer
HttpServerResponse putTrailer(String name, Iterable<String> values)
LikeputTrailer(String, String)
but providing multiple values via a String Iterable
-
putTrailer
HttpServerResponse putTrailer(CharSequence name, Iterable<CharSequence> value)
LikeputTrailer(String, Iterable)
but with CharSequence Iterable
-
closeHandler
HttpServerResponse closeHandler(Handler<Void> handler)
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.
- Parameters:
handler
- the handler- Returns:
- a reference to this, so the API can be used fluently
-
endHandler
HttpServerResponse endHandler(Handler<Void> handler)
Set an end handler for the response. This will be called when the response is disposed to allow consistent cleanup of the response.- Parameters:
handler
- the handler- Returns:
- a reference to this, so the API can be used fluently
-
write
Future<Void> write(String chunk, String enc)
Write aString
to the response body, encoded using the encodingenc
.- Parameters:
chunk
- the string to writeenc
- the encoding to use- Returns:
- a future completed with the body result
-
write
Future<Void> write(String chunk)
Write aString
to the response body, encoded in UTF-8.- Parameters:
chunk
- the string to write- Returns:
- a future completed with the body result
-
writeContinue
Future<Void> writeContinue()
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- Returns:
- a reference to this, so the API can be used fluently
-
writeEarlyHints
Future<Void> writeEarlyHints(MultiMap headers)
Used to write an interim 103 Early Hints response to return some HTTP headers before the final HTTP message.- Parameters:
headers
- headers to write- Returns:
- a future
-
end
Future<Void> end(String chunk)
Same asend(Buffer)
but writes a String in UTF-8 encoding before ending the response.- Parameters:
chunk
- the string to write before ending the response- Returns:
- a future completed with the body result
-
end
Future<Void> end(String chunk, String enc)
Same asend(Buffer)
but writes a String with the specified encoding before ending the response.- Parameters:
chunk
- the string to write before ending the responseenc
- the encoding to use- Returns:
- a future completed with the body result
-
end
Future<Void> end(Buffer chunk)
Same asend()
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.- Specified by:
end
in interfaceWriteStream<Buffer>
- Parameters:
chunk
- the buffer to write before ending the response- Returns:
- a future completed with the body result
-
end
Future<Void> end()
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.
- Specified by:
end
in interfaceWriteStream<Buffer>
- Returns:
- a future completed with the body result
-
send
default Future<Void> send()
Send the request with an empty body.- Returns:
- a future notified when the response has been written
-
send
default Future<Void> send(String body)
Send the request with a stringbody
.- Returns:
- a future notified when the response has been written
-
send
default Future<Void> send(Buffer body)
Send the request with a bufferbody
.- Returns:
- a future notified when the response has been written
-
send
default Future<Void> send(ReadStream<Buffer> body)
Send the request with a streambody
.If the
HttpHeaders.CONTENT_LENGTH
is set then the request assumes this is the length of the {stream}, otherwise the request will set a chunkedHttpHeaders.CONTENT_ENCODING
.- Returns:
- a future notified when the last bytes of the response was sent
-
sendFile
default Future<Void> sendFile(String filename)
Send the request with a streambody
.If the
HttpHeaders.CONTENT_LENGTH
is set then the request assumes this is the length of the {stream}, otherwise the request will set a chunkedHttpHeaders.CONTENT_ENCODING
.- Returns:
- a future notified when the response has been written
-
sendFile
default Future<Void> sendFile(String filename, long offset)
Same assendFile(String, long, long)
using length @code{Long.MAX_VALUE} which means until the end of the file.- Parameters:
filename
- path to the file to serveoffset
- offset to start serving from- Returns:
- a future completed with the body result
-
sendFile
Future<Void> sendFile(String filename, long offset, long length)
Ask the OS to stream a file as specified byfilename
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.
- Parameters:
filename
- path to the file to serveoffset
- offset to start serving fromlength
- the number of bytes to send- Returns:
- a future completed with the body result
-
ended
boolean ended()
- Returns:
- has the response already ended?
-
closed
boolean closed()
- Returns:
- has the underlying TCP connection corresponding to the request already been closed?
-
headWritten
boolean headWritten()
- Returns:
- have the headers for the response already been written?
-
headersEndHandler
HttpServerResponse headersEndHandler(Handler<Void> handler)
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.
- Parameters:
handler
- the handler- Returns:
- a reference to this, so the API can be used fluently
-
bodyEndHandler
HttpServerResponse bodyEndHandler(Handler<Void> handler)
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.- Parameters:
handler
- the handler- Returns:
- a reference to this, so the API can be used fluently
-
bytesWritten
long bytesWritten()
- Returns:
- the total number of bytes written for the body of the response.
-
streamId
int streamId()
- Returns:
- the id of the stream of this response, -1 for HTTP/1.x
-
push
default Future<HttpServerResponse> push(HttpMethod method, String host, String path)
Likepush(HttpMethod, String, String, MultiMap)
with no headers.
-
push
default Future<HttpServerResponse> push(HttpMethod method, String path, MultiMap headers)
Likepush(HttpMethod, String, String, MultiMap)
with the host copied from the current request.
-
push
default Future<HttpServerResponse> push(HttpMethod method, String path)
Likepush(HttpMethod, String, String, MultiMap)
with the host copied from the current request.
-
push
Future<HttpServerResponse> push(HttpMethod method, HostAndPort authority, String path, MultiMap headers)
Push a response to the client. Thehandler
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. Thehandler
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.- Parameters:
method
- the method of the promised requestauthority
- the authority of the promised requestpath
- the path of the promised requestheaders
- the headers of the promised request- Returns:
- a future notified when the response can be written
-
push
@Deprecated Future<HttpServerResponse> push(HttpMethod method, String host, String path, MultiMap headers)
Deprecated.instead usepush(HttpMethod, HostAndPort, String, MultiMap)
Push a response to the client. Thehandler
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. Thehandler
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.- Parameters:
method
- the method of the promised requesthost
- the host of the promised requestpath
- the path of the promised requestheaders
- the headers of the promised request- Returns:
- a future notified when the response can be written
-
reset
default boolean reset()
Reset this HTTP/2 stream with the error code0
.
-
reset
boolean reset(long code)
Reset this response:- for HTTP/2, send an HTTP/2 reset frame with the specified error
code
- for HTTP/1.x, close the connection when the current response has not yet been sent
false
is returned as indicator.- Parameters:
code
- the error code- Returns:
true
when reset has been performed
- for HTTP/2, send an HTTP/2 reset frame with the specified error
-
writeCustomFrame
Future<Void> writeCustomFrame(int type, int flags, Buffer payload)
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.
- Parameters:
type
- the 8-bit frame typeflags
- the 8-bit frame flagspayload
- the frame payload- Returns:
- a reference to this, so the API can be used fluently
-
writeCustomFrame
default Future<Void> writeCustomFrame(HttpFrame frame)
LikewriteCustomFrame(int, int, Buffer)
but with anHttpFrame
.- Parameters:
frame
- the frame to write
-
setStreamPriority
default HttpServerResponse setStreamPriority(StreamPriority streamPriority)
Sets the priority of the associated stream This is not implemented for HTTP/1.x.- Parameters:
streamPriority
- the priority for this request's stream
-
addCookie
HttpServerResponse addCookie(Cookie cookie)
Add a cookie. This will be sent back to the client in the response.- Parameters:
cookie
- the cookie- Returns:
- a reference to this, so the API can be used fluently
-
removeCookie
default Cookie removeCookie(String name)
Expire a cookie, notifying a User Agent to remove it from its cookie jar. NOTE: This method will only remove the first occurrence of the given name. Users probably may want to use:removeCookies(String)
- Parameters:
name
- the name of the cookie- Returns:
- the cookie, if it existed, or null
-
removeCookie
Cookie removeCookie(String name, boolean invalidate)
Remove a cookie from the cookie set. If invalidate istrue
then it will expire a cookie, notifying a User Agent to remove it from its cookie jar. NOTE: This method will only expire the first occurrence of the given name. Users probably may want to use:removeCookies(String,boolean)
- Parameters:
name
- the name of the cookie- Returns:
- the cookie, if it existed, or
null
-
removeCookies
default Set<Cookie> removeCookies(String name)
Expire all cookies, notifying a User Agent to remove it from its cookie jar. NOTE: the returnedSet
is read-only. This means any attempt to modify (add or remove to the set), will throwUnsupportedOperationException
.- Parameters:
name
- the name of the cookie- Returns:
- a read only set of affected cookies, if they existed, or an empty set.
-
removeCookies
Set<Cookie> removeCookies(String name, boolean invalidate)
Remove all cookies from the cookie set. If invalidate istrue
then it will expire a cookie, notifying a User Agent to remove it from its cookie jar. NOTE: the returnedSet
is read-only. This means any attempt to modify (add or remove to the set), will throwUnsupportedOperationException
.- Parameters:
name
- the name of the cookieinvalidate
- invalidate from the user agent- Returns:
- a read only set of affected cookies, if they existed, or an empty set.
-
removeCookie
default Cookie removeCookie(String name, String domain, String path)
Expires a cookie from the cookie set. This will notify a User Agent to remove it from its cookie jar.- Parameters:
name
- the name of the cookiedomain
- the domain of the cookiepath
- the path of the cookie- Returns:
- the cookie, if it existed, or
null
-
removeCookie
Cookie removeCookie(String name, String domain, String path, boolean invalidate)
Remove a cookie from the cookie set. If invalidate istrue
then it will expire a cookie, notifying a User Agent to remove it from its cookie jar.- Parameters:
name
- the name of the cookiedomain
- the domain of the cookiepath
- the path of the cookie- Returns:
- the cookie, if it existed, or
null
-
-