Skip to content

Commit 5edd774

Browse files
mauriciocolliTobiGr
authored andcommitted
Add latest url to the response to make detection of a redirect possible
Will be latest one in this commit because there's need to check the history of redirects as of now.
1 parent 3441946 commit 5edd774

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ public class Response {
1515
private final Map<String, List<String>> responseHeaders;
1616
private final String responseBody;
1717

18-
public Response(int responseCode, String responseMessage, Map<String, List<String>> responseHeaders, @Nullable String responseBody) {
18+
private final String latestUrl;
19+
20+
public Response(int responseCode, String responseMessage, Map<String, List<String>> responseHeaders,
21+
@Nullable String responseBody, @Nullable String latestUrl) {
1922
this.responseCode = responseCode;
2023
this.responseMessage = responseMessage;
2124
this.responseHeaders = responseHeaders != null ? responseHeaders : Collections.<String, List<String>>emptyMap();
2225

2326
this.responseBody = responseBody == null ? "" : responseBody;
27+
this.latestUrl = latestUrl;
2428
}
2529

2630
public int responseCode() {
@@ -40,6 +44,16 @@ public String responseBody() {
4044
return responseBody;
4145
}
4246

47+
/**
48+
* Used for detecting a possible redirection, limited to the latest one.
49+
*
50+
* @return latest url known right before this response object was created
51+
*/
52+
@Nonnull
53+
public String latestUrl() {
54+
return latestUrl;
55+
}
56+
4357
/*//////////////////////////////////////////////////////////////////////////
4458
// Utils
4559
//////////////////////////////////////////////////////////////////////////*/

extractor/src/test/java/org/schabi/newpipe/DownloaderTestImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ public Response execute(@Nonnull Request request) throws IOException, ReCaptchaE
9999
final int responseCode = connection.getResponseCode();
100100
final String responseMessage = connection.getResponseMessage();
101101
final Map<String, List<String>> responseHeaders = connection.getHeaderFields();
102+
final String latestUrl = connection.getURL().toString();
102103

103-
return new Response(responseCode, responseMessage, responseHeaders, response.toString());
104+
return new Response(responseCode, responseMessage, responseHeaders, response.toString(), latestUrl);
104105
} catch (Exception e) {
105106
final int responseCode = connection.getResponseCode();
106107

@@ -112,7 +113,8 @@ public Response execute(@Nonnull Request request) throws IOException, ReCaptchaE
112113
if (responseCode == 429) {
113114
throw new ReCaptchaException("reCaptcha Challenge requested", url);
114115
} else if (responseCode != -1) {
115-
return new Response(responseCode, connection.getResponseMessage(), connection.getHeaderFields(), null);
116+
final String latestUrl = connection.getURL().toString();
117+
return new Response(responseCode, connection.getResponseMessage(), connection.getHeaderFields(), null, latestUrl);
116118
}
117119

118120
throw new IOException("Error occurred while fetching the content", e);

0 commit comments

Comments
 (0)