@Override public MediaType contentType() { String contentTypeHeader = okHeaders.get("Content-Type"); return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader); }
/** Returns the last value corresponding to the specified field, or null. */ public @Nullable String get(String name) { return get(namesAndValues, name); }
public @Nullable String header(String name) { return headers.get(name); }
private static boolean bodyHasUnknownEncoding(Headers headers) { String contentEncoding = headers.get("Content-Encoding"); return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity") && !contentEncoding.equalsIgnoreCase("gzip"); } }
public @Nullable String header(String name, @Nullable String defaultValue) { String result = headers.get(name); return result != null ? result : defaultValue; }
public static Part create(@Nullable Headers headers, RequestBody body) { if (body == null) { throw new NullPointerException("body == null"); } if (headers != null && headers.get("Content-Type") != null) { throw new IllegalArgumentException("Unexpected header: Content-Type"); } if (headers != null && headers.get("Content-Length") != null) { throw new IllegalArgumentException("Unexpected header: Content-Length"); } return new Part(headers, body); }
/** * 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. */ public @Nullable Date getDate(String name) { String value = get(name); return value != null ? HttpDate.parse(value) : null; }
@Override public long getIfModifiedSince() { return stringToLong(request.headers().get("If-Modified-Since")); }
public static long contentLength(Headers headers) { return stringToLong(headers.get("Content-Length")); }
private boolean bodyEncoded(Headers headers) { String contentEncoding = headers.get("Content-Encoding"); return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity"); } }
/** Returns the last value corresponding to the specified field, or null. */ public @Nullable String get(String name) { return get(namesAndValues, name); }
public @Nullable String header(String name, @Nullable String defaultValue) { String result = headers.get(name); return result != null ? result : defaultValue; }
@Override public String getHeaderField(String fieldName) { return fieldName == null ? StatusLine.get(response).toString() : response.headers().get(fieldName); }
public static Part create(@Nullable Headers headers, RequestBody body) { if (body == null) { throw new NullPointerException("body == null"); } if (headers != null && headers.get("Content-Type") != null) { throw new IllegalArgumentException("Unexpected header: Content-Type"); } if (headers != null && headers.get("Content-Length") != null) { throw new IllegalArgumentException("Unexpected header: Content-Length"); } return new Part(headers, body); }
/** * 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. */ public @Nullable Date getDate(String name) { String value = get(name); return value != null ? HttpDate.parse(value) : null; }
/** * Returns the value of the field corresponding to the {@code fieldName}, or null if there is no * such field. If the field has multiple values, the last value is returned. */ @Override public String getHeaderField(String fieldName) { try { return fieldName == null ? StatusLine.get(getResponse(true)).toString() : getHeaders().get(fieldName); } catch (IOException e) { return null; } }
public Response response(DiskLruCache.Snapshot snapshot) { String contentType = responseHeaders.get("Content-Type"); String contentLength = responseHeaders.get("Content-Length"); Request cacheRequest = new Request.Builder() .url(url) .method(requestMethod, null) .headers(varyHeaders) .build(); return new Response.Builder() .request(cacheRequest) .protocol(protocol) .code(code) .message(message) .headers(responseHeaders) .body(new CacheResponseBody(snapshot, contentType, contentLength)) .handshake(handshake) .sentRequestAtMillis(sentRequestMillis) .receivedResponseAtMillis(receivedResponseMillis) .build(); } }
String contentLengthString = headers.get("content-length"); long byteCount = contentLengthString != null ? Long.parseLong(contentLengthString)
/** Combines cached headers with a network headers as defined by RFC 7234, 4.3.4. */ private static Headers combine(Headers cachedHeaders, Headers networkHeaders) { Headers.Builder result = new Headers.Builder(); for (int i = 0, size = cachedHeaders.size(); i < size; i++) { String fieldName = cachedHeaders.name(i); String value = cachedHeaders.value(i); if ("Warning".equalsIgnoreCase(fieldName) && value.startsWith("1")) { continue; // Drop 100-level freshness warnings. } if (isContentSpecificHeader(fieldName) || !isEndToEnd(fieldName) || networkHeaders.get(fieldName) == null) { Internal.instance.addLenient(result, fieldName, value); } } for (int i = 0, size = networkHeaders.size(); i < size; i++) { String fieldName = networkHeaders.name(i); if (!isContentSpecificHeader(fieldName) && isEndToEnd(fieldName)) { Internal.instance.addLenient(result, fieldName, networkHeaders.value(i)); } } return result.build(); }
private void writeHttpResponse(Socket socket, BufferedSink sink, MockResponse response) throws IOException { sleepIfDelayed(response.getHeadersDelay(TimeUnit.MILLISECONDS)); sink.writeUtf8(response.getStatus()); sink.writeUtf8("\r\n"); writeHeaders(sink, response.getHeaders()); Buffer body = response.getBody(); if (body == null) return; sleepIfDelayed(response.getBodyDelay(TimeUnit.MILLISECONDS)); throttledTransfer(response, socket, body, sink, body.size(), false); if ("chunked".equalsIgnoreCase(response.getHeaders().get("Transfer-Encoding"))) { writeHeaders(sink, response.getTrailers()); } }