HttpClient: Remove retrying of requests.

This commit is contained in:
Andreas Schildbach 2018-12-14 00:20:34 +01:00
parent 0da46fe14d
commit e1db556f8f

View file

@ -1,5 +1,5 @@
/* /*
* Copyright 2015 the original author or authors. * Copyright the original author or authors.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -168,94 +168,87 @@ public final class HttpClient {
checkNotNull(callback); checkNotNull(callback);
checkNotNull(url); checkNotNull(url);
int tries = 3; final Request.Builder request = new Request.Builder();
request.url(url);
request.headers(Headers.of(headers));
if (postRequest != null)
request.post(RequestBody.create(MediaType.parse(requestContentType), postRequest));
request.header("Accept", SCRAPE_ACCEPT);
if (userAgent != null)
request.header("User-Agent", userAgent);
if (referer != null)
request.header("Referer", referer);
final Cookie sessionCookie = this.sessionCookie;
if (sessionCookie != null && sessionCookie.name().equals(sessionCookieName))
request.header("Cookie", sessionCookie.toString());
while (true) { final OkHttpClient okHttpClient;
final Request.Builder request = new Request.Builder(); if (proxy != null || trustAllCertificates || certificatePinner != null || sslAcceptAllHostnames) {
request.url(url); final OkHttpClient.Builder builder = OKHTTP_CLIENT.newBuilder();
request.headers(Headers.of(headers)); if (proxy != null)
if (postRequest != null) builder.proxy(proxy);
request.post(RequestBody.create(MediaType.parse(requestContentType), postRequest)); if (trustAllCertificates)
request.header("Accept", SCRAPE_ACCEPT); trustAllCertificates(builder);
if (userAgent != null) if (certificatePinner != null)
request.header("User-Agent", userAgent); builder.certificatePinner(certificatePinner);
if (referer != null) if (sslAcceptAllHostnames)
request.header("Referer", referer); builder.hostnameVerifier(SSL_ACCEPT_ALL_HOSTNAMES);
final Cookie sessionCookie = this.sessionCookie; okHttpClient = builder.build();
if (sessionCookie != null && sessionCookie.name().equals(sessionCookieName)) } else {
request.header("Cookie", sessionCookie.toString()); okHttpClient = OKHTTP_CLIENT;
}
final OkHttpClient okHttpClient; final Call call = okHttpClient.newCall(request.build());
if (proxy != null || trustAllCertificates || certificatePinner != null || sslAcceptAllHostnames) { Response response = null;
final OkHttpClient.Builder builder = OKHTTP_CLIENT.newBuilder(); try {
if (proxy != null) response = call.execute();
builder.proxy(proxy); final int responseCode = response.code();
if (trustAllCertificates) final String bodyPeek = response.peekBody(SCRAPE_PEEK_SIZE).string().replaceAll("\\p{C}", "");
trustAllCertificates(builder); if (responseCode == HttpURLConnection.HTTP_OK) {
if (certificatePinner != null)
builder.certificatePinner(certificatePinner);
if (sslAcceptAllHostnames)
builder.hostnameVerifier(SSL_ACCEPT_ALL_HOSTNAMES);
okHttpClient = builder.build();
} else {
okHttpClient = OKHTTP_CLIENT;
}
final Call call = okHttpClient.newCall(request.build()); final HttpUrl redirectUrl = testRedirect(url, bodyPeek);
Response response = null; if (redirectUrl != null)
try { throw new UnexpectedRedirectException(url, redirectUrl);
response = call.execute();
final int responseCode = response.code();
final String bodyPeek = response.peekBody(SCRAPE_PEEK_SIZE).string().replaceAll("\\p{C}", "");
if (responseCode == HttpURLConnection.HTTP_OK) {
final HttpUrl redirectUrl = testRedirect(url, bodyPeek); if (testExpired(bodyPeek))
if (redirectUrl != null) throw new SessionExpiredException();
throw new UnexpectedRedirectException(url, redirectUrl); if (testInternalError(bodyPeek))
throw new InternalErrorException(url, bodyPeek);
if (testExpired(bodyPeek)) // save cookie
throw new SessionExpiredException(); if (sessionCookieName != null) {
if (testInternalError(bodyPeek)) final List<Cookie> cookies = Cookie.parseAll(url, response.headers());
throw new InternalErrorException(url, bodyPeek); for (final Iterator<Cookie> i = cookies.iterator(); i.hasNext();) {
final Cookie cookie = i.next();
// save cookie if (cookie.name().equals(sessionCookieName)) {
if (sessionCookieName != null) { this.sessionCookie = cookie;
final List<Cookie> cookies = Cookie.parseAll(url, response.headers()); break;
for (final Iterator<Cookie> i = cookies.iterator(); i.hasNext();) {
final Cookie cookie = i.next();
if (cookie.name().equals(sessionCookieName)) {
this.sessionCookie = cookie;
break;
}
} }
} }
callback.onSuccessful(bodyPeek, response.body());
return;
} else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST
|| responseCode == HttpURLConnection.HTTP_UNAUTHORIZED
|| responseCode == HttpURLConnection.HTTP_FORBIDDEN
|| responseCode == HttpURLConnection.HTTP_NOT_ACCEPTABLE
|| responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
throw new BlockedException(url, bodyPeek);
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(url, bodyPeek);
} else if (responseCode == HttpURLConnection.HTTP_MOVED_PERM
|| responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
throw new UnexpectedRedirectException(url, HttpUrl.parse(response.header("Location")));
} else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
throw new InternalErrorException(url, bodyPeek);
} else {
final String message = "got response: " + responseCode + " " + response.message();
if (tries-- > 0)
log.info("{}, retrying...", message);
else
throw new IOException(message + ": " + url);
} }
} finally {
if (response != null) callback.onSuccessful(bodyPeek, response.body());
response.close(); return;
} else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST
|| responseCode == HttpURLConnection.HTTP_UNAUTHORIZED
|| responseCode == HttpURLConnection.HTTP_FORBIDDEN
|| responseCode == HttpURLConnection.HTTP_NOT_ACCEPTABLE
|| responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
throw new BlockedException(url, bodyPeek);
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(url, bodyPeek);
} else if (responseCode == HttpURLConnection.HTTP_MOVED_PERM
|| responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
throw new UnexpectedRedirectException(url, HttpUrl.parse(response.header("Location")));
} else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
throw new InternalErrorException(url, bodyPeek);
} else {
final String message = "got response: " + responseCode + " " + response.message();
throw new IOException(message + ": " + url);
} }
} finally {
if (response != null)
response.close();
} }
} }