/** * Returns an input stream from the server in the case of error such as the requested file (txt, * htm, html) is not found on the remote server. */ @Override public InputStream getErrorStream() { try { Response response = getResponse(true); if (HttpHeaders.hasBody(response) && response.code() >= HTTP_BAD_REQUEST) { return response.body().byteStream(); } return null; } catch (IOException e) { return null; } }
/** * Returns the subset of the headers in {@code response}'s request that impact the content of * response's body. */ public static Headers varyHeaders(Response response) { // Use the request headers sent over the network, since that's what the // response varies on. Otherwise OkHttp-supplied headers like // "Accept-Encoding: gzip" may be lost. Headers requestHeaders = response.networkResponse().request().headers(); Headers responseHeaders = response.headers(); return varyHeaders(requestHeaders, responseHeaders); }
@Override public ResponseBody openResponseBody(Response response) throws IOException { streamAllocation.eventListener.responseBodyStart(streamAllocation.call); String contentType = response.header("Content-Type"); if (!HttpHeaders.hasBody(response)) { Source source = newFixedLengthSource(0); return new RealResponseBody(contentType, 0, Okio.buffer(source)); } if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) { Source source = newChunkedSource(response.request().url()); return new RealResponseBody(contentType, -1L, Okio.buffer(source)); } long contentLength = HttpHeaders.contentLength(response); if (contentLength != -1) { Source source = newFixedLengthSource(contentLength); return new RealResponseBody(contentType, contentLength, Okio.buffer(source)); } return new RealResponseBody(contentType, -1L, Okio.buffer(newUnknownLengthSource())); }
private static Headers varyHeaders(URLConnection urlConnection, Headers responseHeaders) { if (HttpHeaders.hasVaryAll(responseHeaders)) { Set<String> varyFields = HttpHeaders.varyFields(responseHeaders); if (varyFields.isEmpty()) { return new Headers.Builder().build();
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers()); && HttpHeaders.hasBody(networkResponse)) { GzipSource responseBody = new GzipSource(networkResponse.body().source()); Headers strippedHeaders = networkResponse.headers().newBuilder()
@Override public long contentLength() { return HttpHeaders.contentLength(okHeaders); }
/** * Returns true if a Vary header contains an asterisk. Such responses cannot be cached. */ public static boolean hasVaryAll(Headers responseHeaders) { return varyFields(responseHeaders).contains("*"); }
if (HttpHeaders.hasVaryAll(responseHeaders)) { varyHeaders = HttpHeaders.varyHeaders(request.headers(), responseHeaders);
/** * Returns true if a Vary header contains an asterisk. Such responses cannot be cached. */ public static boolean hasVaryAll(Response response) { return hasVaryAll(response.headers()); }
/** * Returns the RFC 7235 authorization challenges appropriate for this response's code. If the * response code is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the * response code is 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. * Otherwise this returns an empty list of challenges. * * <p>If a challenge uses the {@code token68} variant instead of auth params, there is exactly one * auth param in the challenge at key {@code null}. Invalid headers and challenges are ignored. * No semantic validation is done, for example that {@code Basic} auth must have a {@code realm} * auth param, this is up to the caller that interprets these challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers()); && HttpHeaders.hasBody(networkResponse)) { GzipSource responseBody = new GzipSource(networkResponse.body().source()); Headers strippedHeaders = networkResponse.headers().newBuilder()
public static long contentLength(Response response) { return contentLength(response.headers()); }
private static Set<String> varyFields(Response response) { return varyFields(response.headers()); }
if (HttpHeaders.hasVaryAll(responseHeaders)) { varyHeaders = HttpHeaders.varyHeaders(request.headers(), responseHeaders);
/** * Returns true if a Vary header contains an asterisk. Such responses cannot be cached. */ public static boolean hasVaryAll(Response response) { return hasVaryAll(response.headers()); }
private static Headers varyHeaders(URLConnection urlConnection, Headers responseHeaders) { if (HttpHeaders.hasVaryAll(responseHeaders)) { Set<String> varyFields = HttpHeaders.varyFields(responseHeaders); if (varyFields.isEmpty()) { return Util.EMPTY_HEADERS;
/** * Returns the RFC 7235 authorization challenges appropriate for this response's code. If the * response code is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the * response code is 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. * Otherwise this returns an empty list of challenges. * * <p>If a challenge uses the {@code token68} variant instead of auth params, there is exactly one * auth param in the challenge at key {@code null}. Invalid headers and challenges are ignored. * No semantic validation is done, for example that {@code Basic} auth must have a {@code realm} * auth param, this is up to the caller that interprets these challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
/** like {@link #get(Request)} except doesn't throw unsupported on not found */ Response call(Request request) throws IOException { try (Response response = client.newCall(request).execute()) { if (!HttpHeaders.hasBody(response)) return response; // buffer response so tests can read it. Otherwise the finally block will drop it ResponseBody toReturn; try (ResponseBody body = response.body()) { Buffer buffer = new Buffer(); body.source().readAll(buffer); toReturn = ResponseBody.create(body.contentType(), body.contentLength(), buffer); } return response.newBuilder().body(toReturn).build(); } }
@Override public ResponseBody openResponseBody(Response response) throws IOException { streamAllocation.eventListener.responseBodyStart(streamAllocation.call); String contentType = response.header("Content-Type"); if (!HttpHeaders.hasBody(response)) { Source source = newFixedLengthSource(0); return new RealResponseBody(contentType, 0, Okio.buffer(source)); } if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) { Source source = newChunkedSource(response.request().url()); return new RealResponseBody(contentType, -1L, Okio.buffer(source)); } long contentLength = HttpHeaders.contentLength(response); if (contentLength != -1) { Source source = newFixedLengthSource(contentLength); return new RealResponseBody(contentType, contentLength, Okio.buffer(source)); } return new RealResponseBody(contentType, -1L, Okio.buffer(newUnknownLengthSource())); }
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers()); && HttpHeaders.hasBody(networkResponse)) { GzipSource responseBody = new GzipSource(networkResponse.body().source()); Headers strippedHeaders = networkResponse.headers().newBuilder()