Class HttpServer

All Implemented Interfaces:
Iterable<ComponentType>, Channel, ComponentType, Eligible, Manager

public class HttpServer extends Component
A converter component that receives and sends byte buffers on a network channel and web application layer messages on 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.

  • 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 channel
      networkChannel - the channel for network level I/O
      fallbacks - 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 own SocketServer with the given address and uses it for network level I/O.
      Parameters:
      appChannel - this component’s channel
      serverAddress - the address to listen on
      fallbacks - fall backs
  • Method Details

    • matchLevels

      public int matchLevels()
      Returns:
      the matchLevels
    • setMatchLevels

      public HttpServer setMatchLevels(int matchLevels)
      Sets the number of elements from the request path used in the match value of the generated events (see Event.defaultCriterion()), defaults to 1.
      Parameters:
      matchLevels - the matchLevels to set
      Returns:
      the http server for easy chaining
    • setApplicationBufferSize

      public HttpServer setApplicationBufferSize(int applicationBufferSize)
      Sets the size of the buffers used for Output 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

      public int applicationBufferSize()
      Returns the size of the application side (receive) buffers.
      Returns:
      the value or -1 if not set
    • setAcceptNoSni

      public HttpServer setAcceptNoSni(boolean acceptNoSni)
      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 to false, 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

      public boolean 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 as LinkedIOSubchannel of the network connection, a HttpRequestDecoder and a HttpResponseEncoder.
      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 occurs
      InterruptedException
    • onClosed

      @Handler(channels=org.jgrapes.http.HttpServer.NetworkChannel.class) public void onClosed(Closed<?> event, IOSubchannel netChannel)
      Forwards a Closed event to the application channel.
      Parameters:
      event - the event
      netChannel - the net channel
    • onPurge

      @Handler(channels=org.jgrapes.http.HttpServer.NetworkChannel.class) public void onPurge(Purge event, IOSubchannel netChannel)
      Forwards a Purge event to the application channel.
      Parameters:
      event - the event
      netChannel - 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 an HttpResponseEncoder that generates the data (encoded information) and sends it upstream with Output 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 more Output 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

      Checks whether the request has been handled (value of Request event set to true) 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 event
      appChannel - 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 event
      appChannel - 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 as Input and Output 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 event
      appChannel - the channel