Ticket #21906: 21906.patch

File 21906.patch, 3.4 KB (added by taylor.smock, 4 years ago)

Only add User-Agent to the request headers once, preferring the user set User-Agent string, if any. Also implement Destroyable in the Plugin main class.

  • plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java

    diff --git a/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java b/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java
    index b322196fe..7e62eccac 100644
    a b import org.openstreetmap.josm.tools.Utils;  
    3636public final class Http2Client extends org.openstreetmap.josm.tools.HttpClient {
    3737
    3838    private static final Map<Duration, HttpClient> clientForConnectTimeout = new ConcurrentHashMap<>();
     39    private static final String USER_AGENT_STRING = "User-Agent";
    3940    private HttpRequest request;
    4041    private HttpResponse<InputStream> response;
    4142
    public final class Http2Client extends org.openstreetmap.josm.tools.HttpClient {  
    5657
    5758    HttpRequest createRequest() throws IOException {
    5859        HttpRequest.Builder requestBuilder;
     60        Map<String, String> headers = getHeaders();
    5961        try {
    6062            requestBuilder = HttpRequest.newBuilder()
    6163                      .uri(getURL().toURI())
    6264                      .method(getRequestMethod(), hasRequestBody()
    6365                              ? BodyPublishers.ofByteArray(getRequestBody())
    6466                              : BodyPublishers.noBody())
    65                       .header("User-Agent", Version.getInstance().getFullAgentString());
     67                      .header(USER_AGENT_STRING, headers.getOrDefault(USER_AGENT_STRING, Version.getInstance().getFullAgentString()));
    6668        } catch (URISyntaxException e) {
    6769            throw new IOException(e);
    6870        }
    public final class Http2Client extends org.openstreetmap.josm.tools.HttpClient {  
    7779        if (!isUseCache()) {
    7880            requestBuilder.header("Cache-Control", "no-cache");
    7981        }
    80         for (Map.Entry<String, String> header : getHeaders().entrySet()) {
    81             if (header.getValue() != null) {
     82        for (Map.Entry<String, String> header : headers.entrySet()) {
     83            if (header.getValue() != null && !USER_AGENT_STRING.equals(header.getKey())) {
    8284                try {
    8385                    requestBuilder.header(header.getKey(), header.getValue());
    8486                } catch (IllegalArgumentException e) {
  • plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Plugin.java

    diff --git a/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Plugin.java b/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Plugin.java
    index dd2fc9061..1d8d4ca99 100644
    a b package org.openstreetmap.josm.plugins.http2;  
    33
    44import org.openstreetmap.josm.plugins.Plugin;
    55import org.openstreetmap.josm.plugins.PluginInformation;
     6import org.openstreetmap.josm.tools.Destroyable;
     7import org.openstreetmap.josm.tools.Http1Client;
    68import org.openstreetmap.josm.tools.HttpClient;
    79
    810/**
    911 * Provides HTTP/2 support.
    1012 */
    11 public class Http2Plugin extends Plugin {
     13public class Http2Plugin extends Plugin implements Destroyable {
    1214
    1315    /**
    1416     * Constructs a new {@code Http2Plugin}.
    public class Http2Plugin extends Plugin {  
    1820        super(info);
    1921        HttpClient.setFactory(Http2Client::new);
    2022    }
     23
     24    @Override
     25    public void destroy() {
     26        HttpClient.setFactory(Http1Client::new);
     27    }
    2128}