Class HttpServer
- All Implemented Interfaces:
Iterable<ComponentType>
,Channel
,ComponentType
,Eligible
,Manager
IOSubchannel
s of its channel.Each IOSubchannel
represents a connection established by
the browser. The HttpServer
fires Request
events
(and Input
events, if there is associated data) on the
subchannels. Web application components (short “weblets”) handle
these events and use
IOSubchannel.respond(org.jgrapes.core.Event)
to send Response
events and, if applicable, Output
events with data belonging to the response.
Events must be fired by weblets while handling the Request
or Input
events only (to be precise: while handling events
processed by the associated EventProcessor
) to ensure
that responses and their associated data do not interleave.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.jgrapes.core.Channel
Channel.Default
-
Field Summary
-
Constructor Summary
ConstructorDescriptionHttpServer
(Channel appChannel, InetSocketAddress serverAddress, Class<? extends Request.In>... fallbacks) Create a new server that creates its ownSocketServer
with the given address and uses it for network level I/O.HttpServer
(Channel appChannel, Channel networkChannel, Class<? extends Request.In>... fallbacks) Create a new server that uses the networkChannel for network level I/O. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns if secure (TLS) requests without SNI are allowed.int
Returns the size of the application side (receive) buffers.int
void
onAccepted
(Accepted event, IOSubchannel netChannel) Creates a new downstream connection asLinkedIOSubchannel
of the network connection, aHttpRequestDecoder
and aHttpResponseEncoder
.void
Handles a close event from downstream by closing the upstream connections.void
onClosed
(Closed<?> event, IOSubchannel netChannel) Forwards aClosed
event to the application channel.void
onInput
(Input<ByteBuffer> event, IOSubchannel netChannel) Handles data from the client (from upstream).void
onOptions
(Request.In.Options event, IOSubchannel appChannel) Provides a fallback handler for an OPTIONS request with asterisk.void
Receives the message body of a response.void
onProtocolSwitchAccepted
(ProtocolSwitchAccepted event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) void
onPurge
(Purge event, IOSubchannel netChannel) Forwards aPurge
event to the application channel.void
onRequestCompleted
(Request.In.Completed event, IOSubchannel appChannel) Checks whether the request has been handled (value ofRequest
event set totrue
) or the status code in the prepared response is no longer “Not Implemented”.void
onResponse
(Response event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) Handles a response event from downstream by sending it through anHttpResponseEncoder
that generates the data (encoded information) and sends it upstream withOutput
events.setAcceptNoSni
(boolean acceptNoSni) Determines if request from secure (TLS) connections without SNI are accepted.setApplicationBufferSize
(int applicationBufferSize) Sets the size of the buffers used forOutput
events on the application channel.setMatchLevels
(int matchLevels) Sets the number of elements from the request path used in the match value of the generated events (seeEvent.defaultCriterion()
), defaults to 1.Methods inherited from class org.jgrapes.core.Component
channel, component, defaultCriterion, isEligibleFor, setName
Methods inherited from class org.jgrapes.core.internal.ComponentVertex
activeEventPipeline, addHandler, attach, channelReplacements, children, componentPath, componentVertex, detach, fire, initComponentsHandlers, iterator, name, newEventPipeline, newEventPipeline, parent, registerAsGenerator, root, toString, unregisterAsGenerator
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
HttpServer
@SafeVarargs public HttpServer(Channel appChannel, Channel networkChannel, Class<? extends Request.In>... fallbacks) Create a new server that uses the networkChannel for network level I/O.As a convenience the server can provide fall back handlers for the specified types of requests. The fall back handler simply returns 404 ( "Not found").
- Parameters:
appChannel
- this component’s channelnetworkChannel
- the channel for network level I/Ofallbacks
- the requests for which a fall back handler is provided
-
HttpServer
@SafeVarargs public HttpServer(Channel appChannel, InetSocketAddress serverAddress, Class<? extends Request.In>... fallbacks) Create a new server that creates its ownSocketServer
with the given address and uses it for network level I/O.- Parameters:
appChannel
- this component’s channelserverAddress
- the address to listen onfallbacks
- fall backs
-
-
Method Details
-
matchLevels
- Returns:
- the matchLevels
-
setMatchLevels
Sets the number of elements from the request path used in the match value of the generated events (seeEvent.defaultCriterion()
), defaults to 1.- Parameters:
matchLevels
- the matchLevels to set- Returns:
- the http server for easy chaining
-
setApplicationBufferSize
Sets the size of the buffers used forOutput
events on the application channel.Defaults to the upstream buffer size minus 512 (estimate for added protocol overhead).
- Parameters:
applicationBufferSize
- the size to set- Returns:
- the http server for easy chaining
-
applicationBufferSize
Returns the size of the application side (receive) buffers.- Returns:
- the value or -1 if not set
-
setAcceptNoSni
Determines if request from secure (TLS) connections without SNI are accepted.Secure (TLS) requests usually transfer the name of the server that they want to connect to during handshake. The HTTP server checks that the
Host
header field of decoded requests matches the name used to establish the connection. If, however, the connection is made using the IP-address, the client does not have a host name. If such connections are to be accepted, this flag, which defaults tofalse
, must be set.Note that in request accepted without SNI, the
Host
header field will be modified to contain the IP-address of the indicated host to prevent accidental matching with virtual host names.- Parameters:
acceptNoSni
- the value to set- Returns:
- the http server for easy chaining
-
acceptNoSni
Returns if secure (TLS) requests without SNI are allowed.- Returns:
- the result
-
onAccepted
@Handler(channels=org.jgrapes.http.HttpServer.NetworkChannel.class) public void onAccepted(Accepted event, IOSubchannel netChannel) Creates a new downstream connection asLinkedIOSubchannel
of the network connection, aHttpRequestDecoder
and aHttpResponseEncoder
.- Parameters:
event
- the accepted event
-
onInput
@Handler(channels=org.jgrapes.http.HttpServer.NetworkChannel.class) public void onInput(Input<ByteBuffer> event, IOSubchannel netChannel) throws ProtocolException, InterruptedException Handles data from the client (from upstream).The data is send through the
HttpRequestDecoder
and events are sent downstream according to the decoding results.- Parameters:
event
- the event- Throws:
ProtocolException
- if a protocol exception occursInterruptedException
-
onClosed
@Handler(channels=org.jgrapes.http.HttpServer.NetworkChannel.class) public void onClosed(Closed<?> event, IOSubchannel netChannel) Forwards aClosed
event to the application channel.- Parameters:
event
- the eventnetChannel
- the net channel
-
onPurge
@Handler(channels=org.jgrapes.http.HttpServer.NetworkChannel.class) public void onPurge(Purge event, IOSubchannel netChannel) Forwards aPurge
event to the application channel.- Parameters:
event
- the eventnetChannel
- the net channel
-
onResponse
@Handler public void onResponse(Response event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) throws InterruptedException Handles a response event from downstream by sending it through anHttpResponseEncoder
that generates the data (encoded information) and sends it upstream withOutput
events.Depending on whether the response has a body, subsequent
Output
events can follow.- Parameters:
event
- the response event- Throws:
InterruptedException
- if the execution was interrupted
-
onOutput
@Handler public void onOutput(Output<?> event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) throws InterruptedException Receives the message body of a response.A
Response
event that has a message body can be followed by one or moreOutput
events from downstream that contain the data. An Output event with the end of record flag set signals the end of the message body.- Parameters:
event
- the event with the data- Throws:
InterruptedException
- if the execution was interrupted
-
onClose
@Handler public void onClose(Close event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) throws InterruptedException Handles a close event from downstream by closing the upstream connections.- Parameters:
event
- the close event- Throws:
InterruptedException
- if the execution was interrupted
-
onRequestCompleted
@Handler public void onRequestCompleted(Request.In.Completed event, IOSubchannel appChannel) throws InterruptedException Checks whether the request has been handled (value ofRequest
event set totrue
) or the status code in the prepared response is no longer “Not Implemented”.If not, but a fall back has been set, send a “Not Found” response. If this isn’t the case either, send the default response (“Not implemented”) to the client.
- Parameters:
event
- the request completed eventappChannel
- the application channel- Throws:
InterruptedException
- if the execution was interrupted
-
onOptions
@Handler(priority=-2147483648) public void onOptions(Request.In.Options event, IOSubchannel appChannel) Provides a fallback handler for an OPTIONS request with asterisk.Simply responds with “OK”.
- Parameters:
event
- the eventappChannel
- the application channel
-
onProtocolSwitchAccepted
@Handler public void onProtocolSwitchAccepted(ProtocolSwitchAccepted event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) Send the response indicating that the protocol switch was accepted and causes subsequent data to be handled asInput
andOutput
events on the channel.As a convenience, the channel is associates with the URI that was used to request the protocol switch using
URI
as key.- Parameters:
event
- the eventappChannel
- the channel
-