Skip to content

Commit b07aa30

Browse files
Isira-SeneviratneTobiGr
authored andcommitted
Use TreeMap in Response
1 parent d57a206 commit b07aa30

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

  • extractor/src/main/java/org/schabi/newpipe/extractor/downloader

extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Collections;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.TreeMap;
89

910
/**
1011
* A Data class used to hold the results from requests made by the Downloader implementation.
@@ -14,17 +15,18 @@ public class Response {
1415
private final String responseMessage;
1516
private final Map<String, List<String>> responseHeaders;
1617
private final String responseBody;
17-
1818
private final String latestUrl;
1919

2020
public Response(final int responseCode,
2121
final String responseMessage,
22-
@Nullable final Map<String, List<String>> responseHeaders,
22+
@Nonnull final Map<String, List<String>> responseHeaders,
2323
@Nullable final String responseBody,
2424
@Nullable final String latestUrl) {
2525
this.responseCode = responseCode;
2626
this.responseMessage = responseMessage;
27-
this.responseHeaders = responseHeaders == null ? Collections.emptyMap() : responseHeaders;
27+
28+
this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
29+
this.responseHeaders.putAll(responseHeaders);
2830

2931
this.responseBody = responseBody == null ? "" : responseBody;
3032
this.latestUrl = latestUrl;
@@ -71,13 +73,8 @@ public String latestUrl() {
7173
*/
7274
@Nullable
7375
public String getHeader(final String name) {
74-
for (final Map.Entry<String, List<String>> headerEntry : responseHeaders.entrySet()) {
75-
final String key = headerEntry.getKey();
76-
if (key != null && key.equalsIgnoreCase(name) && !headerEntry.getValue().isEmpty()) {
77-
return headerEntry.getValue().get(0);
78-
}
79-
}
80-
81-
return null;
76+
// Header lookup is case-insensitive
77+
final var values = responseHeaders.getOrDefault(name, Collections.emptyList());
78+
return values.isEmpty() ? null : values.get(0);
8279
}
8380
}

0 commit comments

Comments
 (0)