Search in sources :

Example 1 with HttpMethod

use of com.google.android.exoplayer2.upstream.DataSpec.HttpMethod in project ExoPlayer by google.

the class DefaultHttpDataSource method makeConnection.

/**
 * Establishes a connection, following redirects to do so where permitted.
 */
private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
    URL url = new URL(dataSpec.uri.toString());
    @HttpMethod int httpMethod = dataSpec.httpMethod;
    @Nullable byte[] httpBody = dataSpec.httpBody;
    long position = dataSpec.position;
    long length = dataSpec.length;
    boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);
    if (!allowCrossProtocolRedirects && !keepPostFor302Redirects) {
        // automatically. This is the behavior we want, so use it.
        return makeConnection(url, httpMethod, httpBody, position, length, allowGzip, /* followRedirects= */
        true, dataSpec.httpRequestHeaders);
    }
    // We need to handle redirects ourselves to allow cross-protocol redirects or to keep the POST
    // request method for 302.
    int redirectCount = 0;
    while (redirectCount++ <= MAX_REDIRECTS) {
        HttpURLConnection connection = makeConnection(url, httpMethod, httpBody, position, length, allowGzip, /* followRedirects= */
        false, dataSpec.httpRequestHeaders);
        int responseCode = connection.getResponseCode();
        String location = connection.getHeaderField("Location");
        if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD) && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT || responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) {
            connection.disconnect();
            url = handleRedirect(url, location, dataSpec);
        } else if (httpMethod == DataSpec.HTTP_METHOD_POST && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER)) {
            connection.disconnect();
            boolean shouldKeepPost = keepPostFor302Redirects && responseCode == HttpURLConnection.HTTP_MOVED_TEMP;
            if (!shouldKeepPost) {
                // POST request follows the redirect and is transformed into a GET request.
                httpMethod = DataSpec.HTTP_METHOD_GET;
                httpBody = null;
            }
            url = handleRedirect(url, location, dataSpec);
        } else {
            return connection;
        }
    }
    // If we get here we've been redirected more times than are permitted.
    throw new HttpDataSourceException(new NoRouteToHostException("Too many redirects: " + redirectCount), dataSpec, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, HttpDataSourceException.TYPE_OPEN);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) NoRouteToHostException(java.net.NoRouteToHostException) URL(java.net.URL) HttpMethod(com.google.android.exoplayer2.upstream.DataSpec.HttpMethod) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)1 HttpMethod (com.google.android.exoplayer2.upstream.DataSpec.HttpMethod)1 HttpURLConnection (java.net.HttpURLConnection)1 NoRouteToHostException (java.net.NoRouteToHostException)1 URL (java.net.URL)1