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 Summary
Constructors Constructor Description RawHeaders()RawHeaders(RawHeaders copyFrom) -
Method Summary
Modifier and Type Method Description voidadd(String fieldName, String value)Add a field with the specified value.voidaddAll(String fieldName, List<String> headerFields)voidaddLine(String line)Add an HTTP header line containing a field name, a literal colon, and a value.voidaddSpdyRequestHeaders(String method, String path, String version, String host, String scheme)static RawHeadersfromBytes(InputStream in)Parses bytes of a response header from an HTTP transport.static RawHeadersfromMultimap(Map<String,List<String>> map, boolean response)Creates a new instance from the given map of fields to values.static RawHeadersfromNameValueBlock(List<String> nameValueBlock)Returns headers for a name value block containing a SPDY response.Stringget(String fieldName)Returns the last value corresponding to the specified field, or null.RawHeadersgetAll(Set<String> fieldNames)StringgetFieldName(int index)Returns the field atpositionor null if that is out of range.intgetHttpMinorVersion()Returns the status line's HTTP minor version.intgetResponseCode()Returns the HTTP status code or -1 if it is unknown.StringgetResponseMessage()Returns the HTTP status message or null if it is unknown.StringgetStatusLine()StringgetValue(int index)Returns the value atindexor null if that is out of range.intlength()Returns the number of field values.Set<String>names()Returns an immutable case-insensitive set of header names.static voidreadHeaders(InputStream in, RawHeaders out)Reads headers or trailers intoout.voidremoveAll(String fieldName)voidset(String fieldName, String value)Set a field with the specified value.voidsetRequestLine(String requestLine)Sets the request line (like "GET / HTTP/1.1").voidsetStatusLine(String statusLine)Sets the response status line (like "HTTP/1.0 200 OK").byte[]toBytes()Returns bytes of a request header for sending on an HTTP transport.Map<String,List<String>>toMultimap(boolean response)Returns an immutable map containing each field to its list of values.List<String>toNameValueBlock()Returns a list of alternating names and values.List<String>values(String name)Returns an immutable list of the header values forname.
-
Constructor Details
-
RawHeaders
public RawHeaders() -
RawHeaders
-
-
Method Details
-
setRequestLine
Sets the request line (like "GET / HTTP/1.1"). -
setStatusLine
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
-
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
Returns the HTTP status message or null if it is unknown. -
addLine
Add an HTTP header line containing a field name, a literal colon, and a value. -
add
Add a field with the specified value. -
removeAll
-
addAll
-
set
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
Returns the field atpositionor null if that is out of range. -
names
Returns an immutable case-insensitive set of header names. -
getValue
Returns the value atindexor null if that is out of range. -
get
Returns the last value corresponding to the specified field, or null. -
values
Returns an immutable list of the header values forname. -
getAll
- Parameters:
fieldNames- a case-insensitive set of HTTP header field names.
-
toBytes
Returns bytes of a request header for sending on an HTTP transport.- Throws:
UnsupportedEncodingException
-
fromBytes
Parses bytes of a response header from an HTTP transport.- Throws:
IOException
-
readHeaders
Reads headers or trailers intoout.- Throws:
IOException
-
toMultimap
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 IOExceptionCreates 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
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
Returns headers for a name value block containing a SPDY response.- Throws:
IOException
-