Interface Request

All Superinterfaces:
org.eclipse.jetty.util.Attributes, org.eclipse.jetty.io.Content.Source
All Known Subinterfaces:
Request.ServeAs
All Known Implementing Classes:
ContextRequest, ErrorHandler.ErrorRequest, GzipRequest, HeaderWrappingRequest, Request.Wrapper, SecureRequestCustomizer.SecureRequest, SecureRequestCustomizer.SecureRequestWithTLSData, StatisticsHandler.MinimumDataRateHandler.MinimumDataRateRequest

public interface Request extends org.eclipse.jetty.util.Attributes, org.eclipse.jetty.io.Content.Source

The representation of an HTTP request, for any protocol version (HTTP/1.1, HTTP/2, HTTP/3).

The typical idiom to read request content is the following:


 public boolean handle(Request request, Response response, Callback callback)
 {
     // Reject requests not appropriate for this handler.
     if (!request.getHttpURI().getPath().startsWith("/yourPath"))
         return false;

     while (true)
     {
         Content.Chunk chunk = request.read();
         if (chunk == null)
         {
             // The chunk is not currently available, demand to be called back.
             request.demand(() -> handle(request, response, callback));
             return true;
         }

         if (Content.Chunk.isError(chunk))
         {
             Throwable failure = error.getCause();

             // Handle errors.
             // If the chunk is not last, then the error can be ignored and reading can be tried again.
             // Otherwise, if the chunk is last, or we do not wish to ignore a non-last error, then
             // mark the handling as complete, either generating a custom
             // response and succeeding the callback, or failing the callback.
             callback.failed(failure);
             return true;
         }

         if (chunk instanceof Trailers trailers)
         {
             HttpFields fields = trailers.getTrailers();

             // Handle trailers.

             // Generate a response.

             // Mark the handling as complete.
             callback.succeeded();

             return true;
         }

         // Normal chunk, process it.
         processChunk(chunk);
         // Release the content after processing.
         chunk.release();

         // Reached end-of-file?
         if (chunk.isLast())
         {
             // Generate a response.

             // Mark the handling as complete.
             callback.succeeded();

             return true;
         }
     }
 }
 
  • Field Details

    • CACHE_ATTRIBUTE

      static final String CACHE_ATTRIBUTE
    • DEFAULT_LOCALES

      static final List<Locale> DEFAULT_LOCALES
  • Method Details

    • getId

      String getId()
      an ID unique within the lifetime scope of the ConnectionMetaData.getId()). This may be a protocol ID (e.g. HTTP/2 stream ID) or it may be unrelated to the protocol.
      See Also:
    • getComponents

      Components getComponents()
      Returns:
      the Components to be used with this Request.
    • getConnectionMetaData

      ConnectionMetaData getConnectionMetaData()
      Returns:
      the ConnectionMetaData associated to this Request
    • getMethod

      String getMethod()
      Returns:
      the HTTP method of this Request
    • getHttpURI

      org.eclipse.jetty.http.HttpURI getHttpURI()
      Returns:
      the HTTP URI of this Request
      See Also:
    • getContext

      Context getContext()
      Get the Context associated with this Request.

      Note that a Request should always have an associated Context since if the Request is not being handled by a ContextHandler then the Context from Server.getContext() will be used.

      Returns:
      the Context associated with this Request. Never null.
      See Also:
    • getContextPath

      static String getContextPath(Request request)
      Get the context path of this Request. This is equivalent to request.getContext().getContextPath().
      Parameters:
      request - The request to get the context path from.
      Returns:
      The encoded context path of the Context or null.
      See Also:
    • getPathInContext

      static String getPathInContext(Request request)

      Returns the canonically encoded path of the URI, scoped to the current context.

      For example, when the request has a Context with contextPath=/ctx and the request's HttpURI canonical path is canonicalPath=/ctx/foo, then pathInContext=/foo.

      Returns:
      The part of the canonically encoded path of the URI after any context path prefix has been removed.
      See Also:
    • getHeaders

      org.eclipse.jetty.http.HttpFields getHeaders()
      Returns:
      the HTTP headers of this Request
    • demand

      void demand(Runnable demandCallback)
      Specified by:
      demand in interface org.eclipse.jetty.io.Content.Source
      Parameters:
      demandCallback - the demand callback to invoke when there is a content chunk available. In addition to the invocation guarantees of Content.Source.demand(Runnable), this implementation serializes the invocation of the Runnable with invocations of any Response.write(boolean, ByteBuffer, Callback) Callback invocations.
      See Also:
      • Content.Source.demand(Runnable)
    • getTrailers

      org.eclipse.jetty.http.HttpFields getTrailers()
      Returns:
      the HTTP trailers of this Request, or null if they are not present
    • getTimeStamp

      static long getTimeStamp(Request request)

      Get the millisecond timestamp at which the request was created, obtained with System.currentTimeMillis(). This method should be used for wall clock time, rather than getHeadersNanoTime(), which is appropriate for measuring latencies.

      Returns:
      The timestamp that the request was received/created in milliseconds
    • getBeginNanoTime

      long getBeginNanoTime()

      Get the nanoTime at which the request arrived to a connector, obtained via System.nanoTime(). This method can be used when measuring latencies.

      Returns:
      The nanoTime at which the request was received/created in nanoseconds
    • getHeadersNanoTime

      long getHeadersNanoTime()

      Get the nanoTime at which the request headers were parsed, obtained via System.nanoTime(). This method can be used when measuring latencies.

      Returns:
      The nanoTime at which the request was ready in nanoseconds
    • isSecure

      boolean isSecure()
    • read

      org.eclipse.jetty.io.Content.Chunk read()

      In addition, the returned Content.Chunk may be a Trailers instance, in case of request content trailers.

      Specified by:
      read in interface org.eclipse.jetty.io.Content.Source
    • consumeAvailable

      boolean consumeAvailable()
      Consume any available content. This bypasses any request wrappers to process the content in read() and reads directly from the HttpStream. This reads until there is no content currently available, or it reaches EOF. The HttpConfiguration.setMaxUnconsumedRequestContentReads(int) configuration can be used to configure how many reads will be attempted by this method.
      Returns:
      true if the content was fully consumed.
    • push

      default void push(org.eclipse.jetty.http.MetaData.Request resource)

      Pushes the given resource to the client.

      Parameters:
      resource - the resource to push
      Throws:
      UnsupportedOperationException - if the push functionality is not supported
      See Also:
    • addIdleTimeoutListener

      void addIdleTimeoutListener(Predicate<TimeoutException> onIdleTimeout)

      Adds a listener for idle timeouts.

      The listener is a predicate function that should return true to indicate that the idle timeout should be handled by the container as a hard failure (see addFailureListener(Consumer)); or false to ignore that specific timeout and for another timeout to occur after another idle period.

      Any pending demand(Runnable) or Response.write(boolean, ByteBuffer, Callback) operations are not affected by this call. Applications need to be mindful of any such pending operations if attempting to make new operations.

      Listeners are processed in sequence, and the first that returns true stops the processing of subsequent listeners, which are therefore not invoked.

      Parameters:
      onIdleTimeout - the predicate function
      See Also:
    • addFailureListener

      void addFailureListener(Consumer<Throwable> onFailure)

      Adds a listener for asynchronous hard errors.

      When a listener is called, the effects of the error will already have taken place:

      Listeners are processed in sequence. When all listeners are invoked then Callback.failed(Throwable) will be called on the callback passed to Request.Handler.handle(Request, Response, Callback).

      Parameters:
      onFailure - the consumer function
      See Also:
    • getTunnelSupport

      TunnelSupport getTunnelSupport()
    • addHttpStreamWrapper

      void addHttpStreamWrapper(Function<HttpStream,HttpStream> wrapper)
    • getSession

      Session getSession(boolean create)

      Get a Session associated with the request. Sessions may not be supported by a given configuration, in which case null will be returned.

      Parameters:
      create - True if the session should be created for the request.
      Returns:
      The session associated with the request or null.
    • asReadOnly

      static Request asReadOnly(Request request)
      Returns a copy of the request that throws UnsupportedOperationException from all mutative methods.
      Returns:
      a copy of the request
    • getLocalAddr

      static String getLocalAddr(Request request)
    • getLocalPort

      static int getLocalPort(Request request)
    • getRemoteAddr

      static String getRemoteAddr(Request request)
    • getRemotePort

      static int getRemotePort(Request request)
    • getServerName

      static String getServerName(Request request)
      Get the logical name the request was sent to, which may be from the authority of the request; the configured server authority; the actual network name of the server;
      Parameters:
      request - The request to get the server name of
      Returns:
      The logical server name or null if it cannot be determined.
    • getServerPort

      static int getServerPort(Request request)
      Get the logical port a request was received on, which may be from the authority of the request; the configured server authority; the default port for the scheme; or the actual network port.
      Parameters:
      request - The request to get the port of
      Returns:
      The port for the request if it can be determined, otherwise -1
    • getLocales

      static List<Locale> getLocales(Request request)
    • asInputStream

      static InputStream asInputStream(Request request)
    • getCharset

      Get a Charset from the request HttpHeader.CONTENT_TYPE, if any.
      Parameters:
      request - The request.
      Returns:
      A Charset or null
      Throws:
      IllegalCharsetNameException - If the charset name is illegal
      UnsupportedCharsetException - If no support for the charset is available
    • extractQueryParameters

      static org.eclipse.jetty.util.Fields extractQueryParameters(Request request)
    • extractQueryParameters

      static org.eclipse.jetty.util.Fields extractQueryParameters(Request request, Charset charset)
    • getParameters

      static org.eclipse.jetty.util.Fields getParameters(Request request) throws Exception
      Throws:
      Exception
    • getParametersAsync

      static CompletableFuture<org.eclipse.jetty.util.Fields> getParametersAsync(Request request)
    • getCookies

      static List<org.eclipse.jetty.http.HttpCookie> getCookies(Request request)
    • toRedirectURI

      static String toRedirectURI(Request request, String location)
      Common point to generate a proper "Location" header for redirects.
      Parameters:
      request - the request the redirect should be based on (needed when relative locations are provided, so that server name, scheme, port can be built out properly)
      location - the location URL to redirect to (can be a relative path)
      Returns:
      the full redirect "Location" URL (including scheme, host, port, path, etc...)
    • serveAs

      static Request serveAs(Request request, org.eclipse.jetty.http.HttpURI uri)
      Return a request with its HttpURI changed to the supplied target. If the passed request or any of the requests that it wraps implements Request.ServeAs then Request.ServeAs.wrap(Request, HttpURI) will be used to do the wrap, otherwise a simple Request.Wrapper may be returned.
      Parameters:
      request - the original request.
      uri - the new URI to target.
      Returns:
      the possibly wrapped request to target the new URI.
    • as

      static <T> T as(Request request, Class<T> type)
    • get

      static <T, R> R get(Request request, Class<T> type, Function<T,R> getter)
    • unWrap

      static Request unWrap(Request request)
    • getContentBytesRead

      static long getContentBytesRead(Request request)
    • newHttpURIFrom

      static org.eclipse.jetty.http.HttpURI newHttpURIFrom(Request request, String newEncodedPathInContext)

      Creates a new HttpURI from the given Request's HttpURI and the given path in context.

      For example, for contextPath=/ctx, request.httpURI=http://host/ctx/path?a=b, and newPathInContext=/newPath, the returned HttpURI is http://host/ctx/newPath?a=b.

      Parameters:
      request - The request to base the new HttpURI on.
      newEncodedPathInContext - The new path in context for the new HttpURI
      Returns:
      A new immutable HttpURI with the path in context replaced, but query string and path parameters retained.
    • getAuthenticationState

      static Request.AuthenticationState getAuthenticationState(Request request)
      Parameters:
      request - The request to enquire.
      Returns:
      the minimal Request.AuthenticationState of the request, or null if no authentication in process.
    • setAuthenticationState

      static void setAuthenticationState(Request request, Request.AuthenticationState state)
      Parameters:
      request - The request to enquire.
      state - the Request.AuthenticationState of the request, or null if no authentication in process.