Module bus.http

Class Headers

java.lang.Object
org.miaixz.bus.http.Headers

public class Headers extends Object
Header fields of an HTTP message.

Maintains the order of header fields. Values are stored as uninterpreted strings, with leading and trailing whitespace removed. Instances are immutable. It is recommended to interpret header fields through Request and Response.

Since:
Java 17+
Author:
Kimi Liu
  • Method Details

    • of

      public static Headers of(String... namesAndValues)
      Creates a Headers instance from an array of names and values.

      Requires an even number of arguments, alternating between names and values.

      Parameters:
      namesAndValues - The array of names and values.
      Returns:
      A Headers instance.
      Throws:
      NullPointerException - if namesAndValues is null.
      IllegalArgumentException - if the number of arguments is odd or contains nulls.
    • of

      public static Headers of(Map<String,String> headers)
      Creates a Headers instance from a map.
      Parameters:
      headers - The map of header names and values.
      Returns:
      A Headers instance.
      Throws:
      NullPointerException - if headers is null.
      IllegalArgumentException - if a name or value is null.
    • contentLength

      public static long contentLength(Response response)
      Gets the Content-Length of a response.
      Parameters:
      response - The response.
      Returns:
      The Content-Length value, or -1 if invalid.
    • contentLength

      public static long contentLength(Headers headers)
      Gets the Content-Length from headers.
      Parameters:
      headers - The headers.
      Returns:
      The Content-Length value, or -1 if invalid.
    • varyMatches

      public static boolean varyMatches(Response cachedResponse, Headers cachedRequest, Request newRequest)
      Checks if the Vary header matches.
      Parameters:
      cachedResponse - The cached response.
      cachedRequest - The cached request.
      newRequest - The new request.
      Returns:
      true if the Vary header matches.
    • hasVaryAll

      public static boolean hasVaryAll(Response response)
      Checks for the presence of a Vary: * header.
      Parameters:
      response - The response.
      Returns:
      true if Vary: * is present.
    • hasVaryAll

      public static boolean hasVaryAll(Headers responseHeaders)
      Checks for the presence of a Vary: * header.
      Parameters:
      responseHeaders - The response headers.
      Returns:
      true if Vary: * is present.
    • varyFields

      public static Set<String> varyFields(Headers responseHeaders)
      Gets the set of Vary fields.
      Parameters:
      responseHeaders - The response headers.
      Returns:
      A set of Vary fields.
    • varyHeaders

      public static Headers varyHeaders(Response response)
      Gets the request headers that affect the response body.
      Parameters:
      response - The response.
      Returns:
      The headers that affect the response body.
    • varyHeaders

      public static Headers varyHeaders(Headers requestHeaders, Headers responseHeaders)
      Gets the request headers that affect the response body.
      Parameters:
      requestHeaders - The request headers.
      responseHeaders - The response headers.
      Returns:
      The headers that affect the response body.
    • parseChallenges

      public static List<Challenge> parseChallenges(Headers responseHeaders, String headerName)
      Parses RFC 7235 challenges.
      Parameters:
      responseHeaders - The response headers.
      headerName - The header name.
      Returns:
      A list of authentication challenges.
    • receiveHeaders

      public static void receiveHeaders(CookieJar cookieJar, UnoUrl url, Headers headers)
      Handles received Cookie headers.
      Parameters:
      cookieJar - The cookie manager.
      url - The URL.
      headers - The headers.
    • hasBody

      public static boolean hasBody(Response response)
      Checks if the response includes a body.
      Parameters:
      response - The response.
      Returns:
      true if the response includes a body.
    • skipUntil

      public static int skipUntil(String input, int pos, String characters)
      Skips until a character from the given set is found.
      Parameters:
      input - The input string.
      pos - The starting position.
      characters - The set of target characters.
      Returns:
      The position of the target character.
    • skipWhitespace

      public static int skipWhitespace(String input, int pos)
      Skips whitespace characters.
      Parameters:
      input - The input string.
      pos - The starting position.
      Returns:
      The position of the first non-whitespace character.
    • parseSeconds

      public static int parseSeconds(String value, int defaultValue)
      Parses seconds from a string.
      Parameters:
      value - The string value.
      defaultValue - The default value.
      Returns:
      The number of seconds.
    • newBuilder

      public Headers.Builder newBuilder()
      Creates a new Builder instance.
      Returns:
      A Builder instance.
    • hashCode

      public int hashCode()
      Calculates the hash code.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code value.
    • get

      public String get(String name)
      Gets the last value for the given header name.
      Parameters:
      name - The header name.
      Returns:
      The header value, or null if it does not exist.
    • getDate

      public Date getDate(String name)
      Gets the date value for the given header name.
      Parameters:
      name - The header name.
      Returns:
      The date value, or null if invalid.
    • getInstant

      public Instant getInstant(String name)
      Gets the Instant value for the given header name.
      Parameters:
      name - The header name.
      Returns:
      The Instant value, or null if invalid.
    • size

      public int size()
      Gets the number of headers.
      Returns:
      The number of headers.
    • name

      public String name(int index)
      Gets the header name at the specified index.
      Parameters:
      index - The index.
      Returns:
      The header name.
    • value

      public String value(int index)
      Gets the header value at the specified index.
      Parameters:
      index - The index.
      Returns:
      The header value.
    • names

      public Set<String> names()
      Gets the set of header names.
      Returns:
      An unmodifiable set of header names.
    • values

      public List<String> values(String name)
      Gets the list of values for the given header name.
      Parameters:
      name - The header name.
      Returns:
      An unmodifiable list of header values.
    • byteCount

      public long byteCount()
      Gets the encoded byte count of the headers.
      Returns:
      The encoded byte count.
    • equals

      public boolean equals(Object other)
      Compares two Headers objects for equality.
      Overrides:
      equals in class Object
      Parameters:
      other - The other object.
      Returns:
      true if the headers are exactly equal.
    • toString

      public String toString()
      Returns the string representation of the headers.
      Overrides:
      toString in class Object
      Returns:
      The header string.
    • toMultimap

      public Map<String,List<String>> toMultimap()
      Converts to a multi-valued map.
      Returns:
      A map of header names to lists of values.