Class RawHeaders

java.lang.Object
com.squareup.okhttp.internal.http.RawHeaders

public final class RawHeaders
extends Object
The HTTP status and unparsed header fields of a single HTTP message. Values are represented as uninterpreted strings; use RequestHeaders and ResponseHeaders for interpreted headers. This class maintains the order of the header fields within the HTTP message.

This class tracks fields line-by-line. A field with multiple comma- separated values on the same line will be treated as a field with a single value by this class. It is the caller's responsibility to detect and split on commas if their field permits multiple values. This simplifies use of single-valued fields whose values routinely contain commas, such as cookies or dates.

This class trims whitespace from values. It never returns values with leading or trailing whitespace.

  • Constructor Details

    • RawHeaders

      public RawHeaders()
    • RawHeaders

      public RawHeaders​(RawHeaders copyFrom)
  • Method Details

    • setRequestLine

      public void setRequestLine​(String requestLine)
      Sets the request line (like "GET / HTTP/1.1").
    • setStatusLine

      public void setStatusLine​(String statusLine) throws IOException
      Sets the response status line (like "HTTP/1.0 200 OK").
      Throws:
      IOException
    • addSpdyRequestHeaders

      public void addSpdyRequestHeaders​(String method, String path, String version, String host, String scheme)
      Parameters:
      method - like "GET", "POST", "HEAD", etc.
      path - like "/foo/bar.html"
      version - like "HTTP/1.1"
      host - like "www.android.com:1234"
      scheme - like "https"
    • getStatusLine

      public String getStatusLine()
    • getHttpMinorVersion

      public int getHttpMinorVersion()
      Returns the status line's HTTP minor version. This returns 0 for HTTP/1.0 and 1 for HTTP/1.1. This returns 1 if the HTTP version is unknown.
    • getResponseCode

      public int getResponseCode()
      Returns the HTTP status code or -1 if it is unknown.
    • getResponseMessage

      public String getResponseMessage()
      Returns the HTTP status message or null if it is unknown.
    • addLine

      public void addLine​(String line)
      Add an HTTP header line containing a field name, a literal colon, and a value.
    • add

      public void add​(String fieldName, String value)
      Add a field with the specified value.
    • removeAll

      public void removeAll​(String fieldName)
    • addAll

      public void addAll​(String fieldName, List<String> headerFields)
    • set

      public void set​(String fieldName, String value)
      Set a field with the specified value. If the field is not found, it is added. If the field is found, the existing values are replaced.
    • length

      public int length()
      Returns the number of field values.
    • getFieldName

      public String getFieldName​(int index)
      Returns the field at position or null if that is out of range.
    • names

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

      public String getValue​(int index)
      Returns the value at index or null if that is out of range.
    • get

      public String get​(String fieldName)
      Returns the last value corresponding to the specified field, or null.
    • values

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

      public RawHeaders getAll​(Set<String> fieldNames)
      Parameters:
      fieldNames - a case-insensitive set of HTTP header field names.
    • toBytes

      public byte[] toBytes() throws UnsupportedEncodingException
      Returns bytes of a request header for sending on an HTTP transport.
      Throws:
      UnsupportedEncodingException
    • fromBytes

      public static RawHeaders fromBytes​(InputStream in) throws IOException
      Parses bytes of a response header from an HTTP transport.
      Throws:
      IOException
    • readHeaders

      public static void readHeaders​(InputStream in, RawHeaders out) throws IOException
      Reads headers or trailers into out.
      Throws:
      IOException
    • toMultimap

      public Map<String,​List<String>> toMultimap​(boolean response)
      Returns an immutable map containing each field to its list of values. The status line is mapped to null.
    • fromMultimap

      public static RawHeaders fromMultimap​(Map<String,​List<String>> map, boolean response) throws IOException
      Creates a new instance from the given map of fields to values. If present, the null field's last element will be used to set the status line.
      Throws:
      IOException
    • toNameValueBlock

      public List<String> toNameValueBlock()
      Returns a list of alternating names and values. Names are all lower case. No names are repeated. If any name has multiple values, they are concatenated using "\0" as a delimiter.
    • fromNameValueBlock

      public static RawHeaders fromNameValueBlock​(List<String> nameValueBlock) throws IOException
      Returns headers for a name value block containing a SPDY response.
      Throws:
      IOException