Skip to content

Commit

Permalink
Add HttpDataSource.getResponseCode to provide the status code associa…
Browse files Browse the repository at this point in the history
…ted with the most recent HTTP response.

PiperOrigin-RevId: 266218104
  • Loading branch information
ojw28 committed Aug 30, 2019
1 parent 2451211 commit f5c1e8b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
postroll ad ([#6314](https://github.com/google/ExoPlayer/issues/6314)).
* Fix audio selection issue where languages are compared by bit rate
([#6335](https://github.com/google/ExoPlayer/issues/6335)).
* Add `HttpDataSource.getResponseCode` to provide the status code associated
with the most recent HTTP response.

### 2.10.4 ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ public void clearAllRequestProperties() {
requestProperties.clear();
}

@Override
public int getResponseCode() {
return responseInfo == null || responseInfo.getHttpStatusCode() <= 0
? -1
: responseInfo.getHttpStatusCode();
}

@Override
public Map<String, List<String>> getResponseHeaders() {
return responseInfo == null ? Collections.emptyMap() : responseInfo.getAllHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public Uri getUri() {
return response == null ? null : Uri.parse(response.request().url().toString());
}

@Override
public int getResponseCode() {
return response == null ? -1 : response.code();
}

@Override
public Map<String, List<String>> getResponseHeaders() {
return response == null ? Collections.emptyMap() : response.headers().toMultimap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
@Nullable private HttpURLConnection connection;
@Nullable private InputStream inputStream;
private boolean opened;
private int responseCode;

private long bytesToSkip;
private long bytesToRead;
Expand Down Expand Up @@ -234,6 +235,11 @@ public Uri getUri() {
return connection == null ? null : Uri.parse(connection.getURL().toString());
}

@Override
public int getResponseCode() {
return connection == null || responseCode <= 0 ? -1 : responseCode;
}

@Override
public Map<String, List<String>> getResponseHeaders() {
return connection == null ? Collections.emptyMap() : connection.getHeaderFields();
Expand Down Expand Up @@ -270,7 +276,6 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
dataSpec, HttpDataSourceException.TYPE_OPEN);
}

int responseCode;
String responseMessage;
try {
responseCode = connection.getResponseCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ public InvalidResponseCodeException(
*/
void clearAllRequestProperties();

/**
* When the source is open, returns the HTTP response status code associated with the last {@link
* #open} call. Otherwise, returns a negative value.
*/
int getResponseCode();

@Override
Map<String, List<String>> getResponseHeaders();
}

0 comments on commit f5c1e8b

Please sign in to comment.