diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java index ac792dc756..a2ba0ece98 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java @@ -2,9 +2,10 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; /** * A Data class used to hold the results from requests made by the Downloader implementation. @@ -12,19 +13,24 @@ public class Response { private final int responseCode; private final String responseMessage; - private final Map> responseHeaders; + private final SortedMap> responseHeaders; private final String responseBody; - private final String latestUrl; public Response(final int responseCode, final String responseMessage, - @Nullable final Map> responseHeaders, + @Nonnull final Map> responseHeaders, @Nullable final String responseBody, @Nullable final String latestUrl) { this.responseCode = responseCode; this.responseMessage = responseMessage; - this.responseHeaders = responseHeaders == null ? Collections.emptyMap() : responseHeaders; + + if (responseHeaders instanceof SortedMap) { + this.responseHeaders = (SortedMap>) responseHeaders; + } else { + this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + this.responseHeaders.putAll(responseHeaders); + } this.responseBody = responseBody == null ? "" : responseBody; this.latestUrl = latestUrl; @@ -70,14 +76,9 @@ public String latestUrl() { * @return the first value assigned to this header */ @Nullable - public String getHeader(final String name) { - for (final Map.Entry> headerEntry : responseHeaders.entrySet()) { - final String key = headerEntry.getKey(); - if (key != null && key.equalsIgnoreCase(name) && !headerEntry.getValue().isEmpty()) { - return headerEntry.getValue().get(0); - } - } - - return null; + public String getHeader(@Nonnull final String name) { + // Header lookup is case-insensitive + final var values = responseHeaders.getOrDefault(name, List.of()); + return values.isEmpty() ? null : values.get(0); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSuggestionExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSuggestionExtractor.java index 5304ed713f..946ad24d67 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSuggestionExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSuggestionExtractor.java @@ -67,7 +67,7 @@ public List suggestionList(final String query) throws IOException, Extra final String contentTypeHeader = response.getHeader("Content-Type"); if (isNullOrEmpty(contentTypeHeader) || !contentTypeHeader.contains("application/json")) { throw new ExtractionException("Invalid response type (got \"" + contentTypeHeader - + "\", excepted a JSON response) (response code " + + "\", expected a JSON response) (response code " + response.responseCode() + ")"); }