Class Headers

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

public class Headers extends Object
单个HTTP消息的头字段。值是未解释的字符串; 使用RequestResponse解释头信息 该类维护HTTP消息中的头字段的顺序 这个类从值中删除空白。它从不返回带开头或结尾空白的值
Since:
Java 17+
Author:
Kimi Liu
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
    static class 
    HTTP header: the name is an ASCII string, but the value can be UTF-8.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the number of bytes required to encode these headers using HTTP/1.1.
    static long
     
    static long
     
    boolean
    equals(Object other)
    Returns true if other is a Headers object with the same headers, with the same casing, in the same order.
    get(String name)
    Returns the last value corresponding to the specified field, or null.
    Returns the last value corresponding to the specified field parsed as an HTTP date, or null if either the field is absent or cannot be parsed as a date.
    Returns the last value corresponding to the specified field parsed as an HTTP date, or null if either the field is absent or cannot be parsed as a date.
    static boolean
    hasBody(Response response)
    Returns true if the response must have a (possibly 0-length) body.
    int
     
    static boolean
    hasVaryAll(Headers responseHeaders)
    Returns true if a Vary header contains an asterisk.
    static boolean
    hasVaryAll(Response response)
    Returns true if a Vary header contains an asterisk.
    name(int index)
    Returns the field at position.
    Returns an immutable case-insensitive set of header names.
     
    static Headers
    of(String... namesAndValues)
    Returns headers for the alternating header names and values.
    static Headers
    of(Map<String,String> headers)
    Returns headers for the header names and values in the Map.
    static List<Challenge>
    parseChallenges(Headers responseHeaders, String headerName)
    Parse RFC 7235 challenges.
    static int
    parseSeconds(String value, int defaultValue)
    Returns value as a positive integer, or 0 if it is negative, or defaultValue if it cannot be parsed.
    static void
    receiveHeaders(CookieJar cookieJar, UnoUrl url, Headers headers)
     
    int
    Returns the number of field values.
    static int
    skipUntil(String input, int pos, String characters)
    Returns the next index in input at or after pos that contains a character from characters.
    static int
    skipWhitespace(String input, int pos)
    Returns the next non-whitespace character in input that is white space.
     
     
    value(int index)
    Returns the value at index.
    values(String name)
    Returns an immutable list of the header values for name.
    static Set<String>
    varyFields(Headers responseHeaders)
    Returns the names of the request headers that need to be checked for equality when caching.
    static Headers
    varyHeaders(Headers requestHeaders, Headers responseHeaders)
    Returns the subset of the headers in requestHeaders that impact the content of response's body.
    static Headers
    Returns the subset of the headers in response's request that impact the content of response's body.
    static boolean
    varyMatches(Response cachedResponse, Headers cachedRequest, Request newRequest)
    Returns true if none of the Vary headers have changed between cachedRequest and newRequest.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static Headers of(String... namesAndValues)
      Returns headers for the alternating header names and values. There must be an even number of arguments, and they must alternate between header names and values.
    • of

      public static Headers of(Map<String,String> headers)
      Returns headers for the header names and values in the Map.
    • contentLength

      public static long contentLength(Response response)
    • contentLength

      public static long contentLength(Headers headers)
    • varyMatches

      public static boolean varyMatches(Response cachedResponse, Headers cachedRequest, Request newRequest)
      Returns true if none of the Vary headers have changed between cachedRequest and newRequest.
    • hasVaryAll

      public static boolean hasVaryAll(Response response)
      Returns true if a Vary header contains an asterisk. Such responses cannot be cached.
    • hasVaryAll

      public static boolean hasVaryAll(Headers responseHeaders)
      Returns true if a Vary header contains an asterisk. Such responses cannot be cached.
    • varyFields

      public static Set<String> varyFields(Headers responseHeaders)
      Returns the names of the request headers that need to be checked for equality when caching.
    • varyHeaders

      public static Headers varyHeaders(Response response)
      Returns the subset of the headers in response's request that impact the content of response's body.
    • varyHeaders

      public static Headers varyHeaders(Headers requestHeaders, Headers responseHeaders)
      Returns the subset of the headers in requestHeaders that impact the content of response's body.
    • parseChallenges

      public static List<Challenge> parseChallenges(Headers responseHeaders, String headerName)
      Parse RFC 7235 challenges. This is awkward because we need to look ahead to know how to interpret a token.

      For example, the first line has a parameter name/value pair and the second line has a single token68:

         
      
         WWW-Authenticate: Digest foo=bar
         WWW-Authenticate: Digest foo=
       

      Similarly, the first line has one challenge and the second line has two challenges:

         
      
         WWW-Authenticate: Digest ,foo=bar
         WWW-Authenticate: Digest ,foo
       
    • receiveHeaders

      public static void receiveHeaders(CookieJar cookieJar, UnoUrl url, Headers headers)
    • hasBody

      public static boolean hasBody(Response response)
      Returns true if the response must have a (possibly 0-length) body. See RFC 7231.
    • skipUntil

      public static int skipUntil(String input, int pos, String characters)
      Returns the next index in input at or after pos that contains a character from characters. Returns the input length if none of the requested characters can be found.
    • skipWhitespace

      public static int skipWhitespace(String input, int pos)
      Returns the next non-whitespace character in input that is white space. Result is undefined if input contains newline characters.
    • parseSeconds

      public static int parseSeconds(String value, int defaultValue)
      Returns value as a positive integer, or 0 if it is negative, or defaultValue if it cannot be parsed.
    • newBuilder

      public Headers.Builder newBuilder()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • get

      public String get(String name)
      Returns the last value corresponding to the specified field, or null.
    • getDate

      public Date getDate(String name)
      Returns the last value corresponding to the specified field parsed as an HTTP date, or null if either the field is absent or cannot be parsed as a date.
    • getInstant

      public Instant getInstant(String name)
      Returns the last value corresponding to the specified field parsed as an HTTP date, or null if either the field is absent or cannot be parsed as a date.
    • size

      public int size()
      Returns the number of field values.
    • name

      public String name(int index)
      Returns the field at position.
    • value

      public String value(int index)
      Returns the value at index.
    • names

      public Set<String> names()
      Returns an immutable case-insensitive set of header names.
    • values

      public List<String> values(String name)
      Returns an immutable list of the header values for name.
    • byteCount

      public long byteCount()
      Returns the number of bytes required to encode these headers using HTTP/1.1. This is also the approximate size of HTTP/2 headers before they are compressed with HPACK. This value is intended to be used as a metric: smaller headers are more efficient to encode and transmit.
    • equals

      public boolean equals(Object other)
      Returns true if other is a Headers object with the same headers, with the same casing, in the same order. Note that two headers instances may be semantically equal but not equal according to this method. In particular, none of the following sets of headers are equal according to this method:
         
      
         1. Original
         Content-Type: text/html
         Content-Length: 50
      
         2. Different order
         Content-Length: 50
         Content-Type: text/html
      
         3. Different case
         content-type: text/html
         content-length: 50
      
         4. Different values
         Content-Type: text/html
         Content-Length: 050
       

      Applications that require semantically equal headers should convert them into a canonical form before comparing them for equality.

      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toMultimap

      public Map<String,List<String>> toMultimap()