com.google.android.exoplayer2.upstream.DataSpec.HttpMethod Java Examples

The following examples show how to use com.google.android.exoplayer2.upstream.DataSpec.HttpMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultHttpDataSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
  byte[] httpBody = dataSpec.httpBody;
  long position = dataSpec.position;
  long length = dataSpec.length;
  boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);

  if (!allowCrossProtocolRedirects) {
    // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
    // 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.
  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);
    } 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)) {
      // POST request follows the redirect and is transformed into a GET request.
      connection.disconnect();
      httpMethod = DataSpec.HTTP_METHOD_GET;
      httpBody = null;
      url = handleRedirect(url, location);
    } else {
      return connection;
    }
  }

  // If we get here we've been redirected more times than are permitted.
  throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}
 
Example #2
Source File: DefaultHttpDataSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/**
 * Configures a connection and opens it.
 *
 * @param url The url to connect to.
 * @param httpMethod The http method.
 * @param httpBody The body data.
 * @param position The byte offset of the requested data.
 * @param length The length of the requested data, or {@link C#LENGTH_UNSET}.
 * @param allowGzip Whether to allow the use of gzip.
 * @param followRedirects Whether to follow redirects.
 * @param requestParameters parameters (HTTP headers) to include in request.
 */
private HttpURLConnection makeConnection(
    URL url,
    @HttpMethod int httpMethod,
    byte[] httpBody,
    long position,
    long length,
    boolean allowGzip,
    boolean followRedirects,
    Map<String, String> requestParameters)
    throws IOException {
  HttpURLConnection connection = openConnection(url);
  connection.setConnectTimeout(connectTimeoutMillis);
  connection.setReadTimeout(readTimeoutMillis);

  Map<String, String> requestHeaders = new HashMap<>();
  if (defaultRequestProperties != null) {
    requestHeaders.putAll(defaultRequestProperties.getSnapshot());
  }
  requestHeaders.putAll(requestProperties.getSnapshot());
  requestHeaders.putAll(requestParameters);

  for (Map.Entry<String, String> property : requestHeaders.entrySet()) {
    connection.setRequestProperty(property.getKey(), property.getValue());
  }

  if (!(position == 0 && length == C.LENGTH_UNSET)) {
    String rangeRequest = "bytes=" + position + "-";
    if (length != C.LENGTH_UNSET) {
      rangeRequest += (position + length - 1);
    }
    connection.setRequestProperty("Range", rangeRequest);
  }
  connection.setRequestProperty("User-Agent", userAgent);
  connection.setRequestProperty("Accept-Encoding", allowGzip ? "gzip" : "identity");
  connection.setInstanceFollowRedirects(followRedirects);
  connection.setDoOutput(httpBody != null);
  connection.setRequestMethod(DataSpec.getStringForHttpMethod(httpMethod));
  
  if (httpBody != null) {
    connection.setFixedLengthStreamingMode(httpBody.length);
    connection.connect();
    OutputStream os = connection.getOutputStream();
    os.write(httpBody);
    os.close();
  } else {
    connection.connect();
  }
  return connection;
}
 
Example #3
Source File: GSYDefaultHttpDataSource.java    From GSYVideoPlayer with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    byte[] httpBody = dataSpec.httpBody;
    long position = dataSpec.position;
    long length = dataSpec.length;
    boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);

    if (!allowCrossProtocolRedirects) {
        // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
        // 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.
    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);
        } 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)) {
            // POST request follows the redirect and is transformed into a GET request.
            connection.disconnect();
            httpMethod = DataSpec.HTTP_METHOD_GET;
            httpBody = null;
            url = handleRedirect(url, location);
        } else {
            return connection;
        }
    }

    // If we get here we've been redirected more times than are permitted.
    throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}
 
Example #4
Source File: DefaultHttpDataSource.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
  byte[] httpBody = dataSpec.httpBody;
  long position = dataSpec.position;
  long length = dataSpec.length;
  boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);
  boolean allowIcyMetadata = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_ICY_METADATA);

  if (!allowCrossProtocolRedirects) {
    // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
    // automatically. This is the behavior we want, so use it.
    return makeConnection(
        url,
        httpMethod,
        httpBody,
        position,
        length,
        allowGzip,
        allowIcyMetadata,
        /* followRedirects= */ true);
  }

  // We need to handle redirects ourselves to allow cross-protocol redirects.
  int redirectCount = 0;
  while (redirectCount++ <= MAX_REDIRECTS) {
    HttpURLConnection connection =
        makeConnection(
            url,
            httpMethod,
            httpBody,
            position,
            length,
            allowGzip,
            allowIcyMetadata,
            /* followRedirects= */ false);
    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);
    } 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)) {
      // POST request follows the redirect and is transformed into a GET request.
      connection.disconnect();
      httpMethod = DataSpec.HTTP_METHOD_GET;
      httpBody = null;
      url = handleRedirect(url, location);
    } else {
      return connection;
    }
  }

  // If we get here we've been redirected more times than are permitted.
  throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}
 
Example #5
Source File: DefaultHttpDataSource.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
  byte[] httpBody = dataSpec.httpBody;
  long position = dataSpec.position;
  long length = dataSpec.length;
  boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);
  boolean allowIcyMetadata = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_ICY_METADATA);

  if (!allowCrossProtocolRedirects) {
    // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
    // automatically. This is the behavior we want, so use it.
    return makeConnection(
        url,
        httpMethod,
        httpBody,
        position,
        length,
        allowGzip,
        allowIcyMetadata,
        /* followRedirects= */ true);
  }

  // We need to handle redirects ourselves to allow cross-protocol redirects.
  int redirectCount = 0;
  while (redirectCount++ <= MAX_REDIRECTS) {
    HttpURLConnection connection =
        makeConnection(
            url,
            httpMethod,
            httpBody,
            position,
            length,
            allowGzip,
            allowIcyMetadata,
            /* followRedirects= */ false);
    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);
    } 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)) {
      // POST request follows the redirect and is transformed into a GET request.
      connection.disconnect();
      httpMethod = DataSpec.HTTP_METHOD_GET;
      httpBody = null;
      url = handleRedirect(url, location);
    } else {
      return connection;
    }
  }

  // If we get here we've been redirected more times than are permitted.
  throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}