Class SafeHttpFetcher

java.lang.Object
io.meeds.commons.http.SafeHttpFetcher
All Implemented Interfaces:
Closeable, AutoCloseable

public class SafeHttpFetcher extends Object implements Closeable
Reads a URL the platform was given over HTTP, within the bounds of a SafeFetchPolicy: the one place to fetch a user-, sender- or agent-controlled URL from the server.

Addresses are the SafeAddressGuard's: it is the resolver of the connection manager (GuardedDnsResolver), so the address of every connection is judged when the connection is opened — there is one lookup, and it is the checked one; the guard also checks each URL's shape before it is requested, and redirects are followed here, one hop at a time, each target checked again. Bounds: a connect timeout, a read timeout, a deadline over the whole read — redirects included — enforced by cancelling the request, a body limit counted on the decoded bytes (a compressed body cannot inflate past it), a redirect count, and the declared content type checked before the body is read. A SafeFetchRequest narrows these bounds, never widens them. Nothing of the platform's goes out: no cookie store, no credentials, no proxy, no retry. The headers sent are the HTTP client's own — Host, Connection, the policy's User-Agent and the Accept-Encoding of the compressions it decodes — and the ones the request names: Accept, If-None-Match, If-Modified-Since. Nothing of the URL goes into a log: it may embed a secret.

Thread-safe; one instance per consumer, closed when the consumer goes. A closed fetcher refuses every read with an IllegalStateException.

  • Constructor Details

    • SafeHttpFetcher

      public SafeHttpFetcher(SafeFetchPolicy policy)
      The fetcher of a policy.
      Parameters:
      policy - what may be read and how far the fetcher goes
  • Method Details

    • getPolicy

      public SafeFetchPolicy getPolicy()
      Returns:
      the policy this fetcher reads under
    • getGuard

      public SafeAddressGuard getGuard()
      Returns:
      the guard, whose URL rules a caller may apply before a read
    • fetch

      public SafeFetchResponse fetch(URI uri) throws SafeFetchException
      Reads a URL with a plain GET under the policy.
      Parameters:
      uri - the URL
      Returns:
      what the server answered
      Throws:
      SafeFetchException - with the reason nothing usable was read
      IllegalStateException - when the fetcher is closed
    • fetch

      public SafeFetchResponse fetch(SafeFetchRequest request) throws SafeFetchException
      Reads a URL, following redirects within the policy's count, each target checked by the guard before it is requested and judged again by the resolver when its connection opens.
      Parameters:
      request - the read
      Returns:
      what the server answered
      Throws:
      SafeFetchException - with the reason nothing usable was read
      IllegalStateException - when the fetcher is closed
    • close

      public void close()
      Stops the deadline thread and closes the connections; a read started afterwards is refused.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable