Class CacheControlHeaderParser

java.lang.Object
org.apache.hc.client5.http.impl.cache.CacheControlHeaderParser

@Internal @Contract(threading=IMMUTABLE) class CacheControlHeaderParser extends Object
A parser for the HTTP Cache-Control header that can be used to extract information about caching directives.

This class is thread-safe and has a singleton instance (INSTANCE).

The parseResponse(Iterator) method takes an HTTP header and returns a ResponseCacheControl object containing the relevant caching directives. The header can be either a FormattedHeader object, which contains a pre-parsed CharArrayBuffer, or a plain Header object, in which case the value will be parsed and stored in a new CharArrayBuffer.

This parser only supports two directives: "max-age" and "s-maxage". If either of these directives are present in the header, their values will be parsed and stored in the ResponseCacheControl object. If both directives are present, the value of "s-maxage" takes precedence.

  • Field Details

    • INSTANCE

      public static final CacheControlHeaderParser INSTANCE
      The singleton instance of this parser.
    • LOG

      private static final org.slf4j.Logger LOG
      The logger for this class.
    • EQUAL_CHAR

      private static final char EQUAL_CHAR
      See Also:
    • TOKEN_DELIMS

      private static final org.apache.hc.core5.util.Tokenizer.Delimiter TOKEN_DELIMS
      The set of characters that can delimit a token in the header.
    • VALUE_DELIMS

      private static final org.apache.hc.core5.util.Tokenizer.Delimiter VALUE_DELIMS
      The set of characters that can delimit a value in the header.
    • tokenParser

      private final org.apache.hc.core5.util.Tokenizer tokenParser
      The token parser used to extract values from the header.
  • Constructor Details

    • CacheControlHeaderParser

      protected CacheControlHeaderParser()
      Constructs a new instance of this parser.
  • Method Details

    • parse

      public void parse(Iterator<org.apache.hc.core5.http.Header> headerIterator, BiConsumer<String,String> consumer)
    • parseResponse

      public final ResponseCacheControl parseResponse(Iterator<org.apache.hc.core5.http.Header> headerIterator)
      Parses the specified response header and returns a new ResponseCacheControl instance containing the relevant caching directives.

      The returned ResponseCacheControl instance will contain the values for "max-age" and "s-maxage" caching directives parsed from the input header. If the input header does not contain any caching directives or if the directives are malformed, the returned ResponseCacheControl instance will have default values for "max-age" and "s-maxage" (-1).

      Parameters:
      headerIterator - the header to parse, cannot be null
      Returns:
      a new ResponseCacheControl instance containing the relevant caching directives parsed from the response header
      Throws:
      IllegalArgumentException - if the input header is null
    • parse

      public final ResponseCacheControl parse(org.apache.hc.core5.http.HttpResponse response)
    • parse

      public final ResponseCacheControl parse(HttpCacheEntry cacheEntry)
    • parseRequest

      public final RequestCacheControl parseRequest(Iterator<org.apache.hc.core5.http.Header> headerIterator)
      Parses the specified request header and returns a new RequestCacheControl instance containing the relevant caching directives.
      Parameters:
      headerIterator - the header to parse, cannot be null
      Returns:
      a new RequestCacheControl instance containing the relevant caching directives parsed from the request header
      Throws:
      IllegalArgumentException - if the input header is null
    • parse

      public final RequestCacheControl parse(org.apache.hc.core5.http.HttpRequest request)
    • parseSeconds

      private static long parseSeconds(String name, String value)