From: David ‘Bombe’ Roden Date: Fri, 5 Dec 2025 19:21:32 +0000 (+0100) Subject: ⬆️ Update to latest HttpClient version X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9a082c35daa45a023093a225c1ee9c86f0db8c6f;p=rhynodge.git ⬆️ Update to latest HttpClient version --- diff --git a/build.gradle b/build.gradle index cfbc2ee..aebd2e3 100644 --- a/build.gradle +++ b/build.gradle @@ -46,7 +46,7 @@ repositories { dependencies { implementation group: "org.apache.logging.log4j", name: "log4j-core", version: "2.25.2" - implementation group: "org.apache.httpcomponents", name: "httpclient", version: "4.4" + implementation group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.5.1' implementation group: "org.jsoup", name: "jsoup", version: "1.16.1" implementation group: "javax.mail", name: "mail", version: "1.4.6-rc1" implementation group: "org.apache.commons", name: "commons-lang3", version: "3.1" diff --git a/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java b/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java index d068a1f..e5421a9 100644 --- a/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java +++ b/src/main/java/net/pterodactylus/rhynodge/queries/HttpQuery.java @@ -19,22 +19,22 @@ package net.pterodactylus.rhynodge.queries; import java.io.IOException; import java.util.concurrent.TimeUnit; - import net.pterodactylus.rhynodge.Query; import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.states.FailedState; import net.pterodactylus.rhynodge.states.HttpState; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.protocol.ResponseContentEncoding; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.util.EntityUtils; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.ConnectionConfig; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; +import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy; +import org.apache.hc.client5.http.ssl.HostnameVerificationPolicy; +import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.protocol.ResponseContent; +import org.apache.hc.core5.ssl.SSLContexts; import org.jspecify.annotations.Nullable; /** @@ -68,29 +68,32 @@ public class HttpQuery implements Query { @Override @SuppressWarnings("deprecation") public State state() { - HttpClientBuilder httpClientBuilder = HttpClientBuilder.create() - .setSSLHostnameVerifier((hostname, session) -> true) - .addInterceptorFirst(new ResponseContentEncoding()); + var acceptAllHostnamesStrategy = new DefaultClientTlsStrategy(SSLContexts.createDefault(), HostnameVerificationPolicy.CLIENT, NoopHostnameVerifier.INSTANCE); + var connectionManager = PoolingHttpClientConnectionManagerBuilder.create() + .setTlsSocketStrategy(acceptAllHostnamesStrategy) + .setDefaultConnectionConfig(ConnectionConfig.custom() + .setConnectTimeout(300, TimeUnit.SECONDS) + .setSocketTimeout(300, TimeUnit.SECONDS).build()) + .build(); + var httpClientBuilder = HttpClients.custom() + .setConnectionManager(connectionManager) + .addResponseInterceptorFirst(new ResponseContent()); if ((proxyHost != null) && (proxyPort != -1)) { httpClientBuilder.setProxy(new HttpHost(proxyHost, proxyPort)); } - HttpClient httpClient = httpClientBuilder.build(); - HttpGet get = new HttpGet(uri); + var get = new HttpGet(uri); - try { + try (var httpClient = httpClientBuilder.build()) { /* make request. */ get.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11"); - HttpConnectionParams.setConnectionTimeout(get.getParams(), (int) TimeUnit.SECONDS.toMillis(300)); - HttpConnectionParams.setSoTimeout(get.getParams(), (int) TimeUnit.SECONDS.toMillis(300)); - HttpResponse response = httpClient.execute(get); - if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - return new FailedState(new IllegalStateException(String.format("Invalid HTTP Status: %d", response.getStatusLine().getStatusCode()))); + var response = httpClient.execute(get); + if (response.getCode() != HttpStatus.SC_OK) { + return new FailedState(new IllegalStateException(String.format("Invalid HTTP Status: %d", response.getCode()))); } - HttpEntity entity = response.getEntity(); + var entity = response.getEntity(); /* yay, done! */ - return new HttpState(uri, response.getStatusLine().getStatusCode(), entity.getContentType().getValue(), EntityUtils.toByteArray(entity)); - + return new HttpState(uri, response.getCode(), entity.getContentType(), EntityUtils.toByteArray(entity)); } catch (IOException ioe1) { return new FailedState(ioe1); } diff --git a/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java b/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java index 78c7558..139fa07 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java @@ -24,9 +24,10 @@ import java.io.UnsupportedEncodingException; import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.queries.HttpQuery; -import org.apache.http.HeaderElement; -import org.apache.http.NameValuePair; -import org.apache.http.message.BasicHeaderValueParser; +import org.apache.hc.core5.http.HeaderElement; +import org.apache.hc.core5.http.NameValuePair; +import org.apache.hc.core5.http.message.BasicHeaderValueParser; +import org.apache.hc.core5.http.message.ParserCursor; /** * {@link State} that contains the results of an {@link HttpQuery}. @@ -146,7 +147,7 @@ public class HttpState extends AbstractState { if (contentType == null) { return "ISO-8859-1"; } - HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(contentType, new BasicHeaderValueParser()); + HeaderElement headerElement = BasicHeaderValueParser.INSTANCE.parseHeaderElement(contentType, new ParserCursor(0, contentType.length())); NameValuePair charset = headerElement.getParameterByName("charset"); return (charset != null) ? charset.getValue() : "ISO-8859-1"; } diff --git a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java index b2bc2f0..658986e 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java @@ -17,7 +17,7 @@ package net.pterodactylus.rhynodge.states; -import java.nio.charset.StandardCharsets; +import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -34,10 +34,11 @@ import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.http.NameValuePair; -import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.hc.core5.http.NameValuePair; +import org.apache.hc.core5.net.WWWFormCodec; import static java.lang.String.format; +import static java.nio.charset.StandardCharsets.UTF_8; /** * {@link State} that contains information about an arbitrary number of torrent @@ -391,7 +392,7 @@ public class TorrentState extends AbstractState implements Iterable if ((magnetUri == null) || (magnetUri.length() < 8)) { return Optional.empty(); } - List parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), StandardCharsets.UTF_8); + List parameters = WWWFormCodec.parse(URI.create(magnetUri).getSchemeSpecificPart(), UTF_8); for (NameValuePair parameter : parameters) { if (parameter.getName().equals("xt")) { return Optional.of(parameter.getValue().toLowerCase());