Class HttpServer
- All Implemented Interfaces:
Iterable<ComponentType>,Channel,ComponentType,Eligible,Manager
IOSubchannels 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
ConstructorsConstructorDescriptionHttpServer(Channel appChannel, InetSocketAddress serverAddress, Class<? extends Request.In>... fallbacks) Create a new server that creates its ownSocketServerwith 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 TypeMethodDescriptionbooleanReturns if secure (TLS) requests without SNI are allowed.intReturns the size of the application side (receive) buffers.intvoidonAccepted(Accepted event, IOSubchannel netChannel) Creates a new downstream connection asLinkedIOSubchannelof the network connection, aHttpRequestDecoderand aHttpResponseEncoder.voidHandles a close event from downstream by closing the upstream connections.voidonClosed(Closed<?> event, IOSubchannel netChannel) Forwards aClosedevent to the application channel.voidonInput(Input<ByteBuffer> event, IOSubchannel netChannel) Handles data from the client (from upstream).voidonOptions(Request.In.Options event, IOSubchannel appChannel) Provides a fallback handler for an OPTIONS request with asterisk.voidReceives the message body of a response.voidonProtocolSwitchAccepted(ProtocolSwitchAccepted event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) voidonPurge(Purge event, IOSubchannel netChannel) Forwards aPurgeevent to the application channel.voidonRequestCompleted(Request.In.Completed event, IOSubchannel appChannel) Checks whether the request has been handled (value ofRequestevent set totrue) or the status code in the prepared response is no longer “Not Implemented”.voidonResponse(Response event, org.jgrapes.http.HttpServer.WebAppMsgChannel appChannel) Handles a response event from downstream by sending it through anHttpResponseEncoderthat generates the data (encoded information) and sends it upstream withOutputevents.setAcceptNoSni(boolean acceptNoSni) Determines if request from secure (TLS) connections without SNI are accepted.setApplicationBufferSize(int applicationBufferSize) Sets the size of the buffers used forOutputevents 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, setNameMethods 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, unregisterAsGeneratorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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 ownSocketServerwith 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 forOutputevents 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
Hostheader 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
Hostheader 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 asLinkedIOSubchannelof the network connection, aHttpRequestDecoderand 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
HttpRequestDecoderand 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 aClosedevent 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 aPurgeevent 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 anHttpResponseEncoderthat generates the data (encoded information) and sends it upstream withOutputevents.Depending on whether the response has a body, subsequent
Outputevents 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
Responseevent that has a message body can be followed by one or moreOutputevents 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 ofRequestevent 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 asInputandOutputevents on the channel.As a convenience, the channel is associates with the URI that was used to request the protocol switch using
URIas key.- Parameters:
event- the eventappChannel- the channel
-