示例#1
0
  private static Request transformRequest(HttpRequest request, Object tag) {
    Request.Builder builder = new Request.Builder();

    RequestLine requestLine = request.getRequestLine();
    String method = requestLine.getMethod();
    builder.url(requestLine.getUri());

    String contentType = null;
    for (Header header : request.getAllHeaders()) {
      String name = header.getName();
      if ("Content-Type".equals(name)) {
        contentType = header.getValue();
      } else {
        builder.header(name, header.getValue());
      }
    }

    RequestBody body = null;
    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
      if (entity != null) {
        // Wrap the entity in a custom Body which takes care of the content, length, and type.
        body = new HttpEntityBody(entity, contentType);

        Header encoding = entity.getContentEncoding();
        if (encoding != null) {
          builder.header(encoding.getName(), encoding.getValue());
        }
      }
    }
    builder.method(method, body);
    builder.tag(tag);

    return builder.build();
  }
示例#2
0
 private Request networkRequest(Request request)
 {
     com.squareup.okhttp.Request.Builder builder = request.newBuilder();
     if (request.header("Host") == null)
     {
         builder.header("Host", Util.hostHeader(request.httpUrl()));
     }
     if ((connection == null || connection.getProtocol() != Protocol.HTTP_1_0) && request.header("Connection") == null)
     {
         builder.header("Connection", "Keep-Alive");
     }
     if (request.header("Accept-Encoding") == null)
     {
         transparentGzip = true;
         builder.header("Accept-Encoding", "gzip");
     }
     CookieHandler cookiehandler = client.getCookieHandler();
     if (cookiehandler != null)
     {
         java.util.Map map = OkHeaders.toMultimap(builder.build().headers(), null);
         OkHeaders.addCookies(builder, cookiehandler.get(request.uri(), map));
     }
     if (request.header("User-Agent") == null)
     {
         builder.header("User-Agent", Version.userAgent());
     }
     return builder.build();
 }
  private HttpEngine newHttpEngine(
      String method, Connection connection, RetryableOutputStream requestBody) throws IOException {
    Request.Builder builder =
        new Request.Builder()
            .url(getURL())
            .method(method, null /* No body; that's passed separately. */);
    Headers headers = requestHeaders.build();
    for (int i = 0; i < headers.size(); i++) {
      builder.addHeader(headers.name(i), headers.value(i));
    }

    boolean bufferRequestBody;
    if (fixedContentLength != -1) {
      bufferRequestBody = false;
      builder.header("Content-Length", Long.toString(fixedContentLength));
    } else if (chunkLength > 0) {
      bufferRequestBody = false;
      builder.header("Transfer-Encoding", "chunked");
    } else {
      bufferRequestBody = true;
    }

    Request request = builder.build();

    // If we're currently not using caches, make sure the engine's client doesn't have one.
    OkHttpClient engineClient = client;
    if (engineClient.getOkResponseCache() != null && !getUseCaches()) {
      engineClient = client.clone().setOkResponseCache(null);
    }

    return new HttpEngine(engineClient, request, bufferRequestBody, connection, requestBody);
  }
 /**
  * Set header parameters to the request builder, including default headers.
  *
  * @param headerParams Header parameters in the ofrm of Map
  * @param reqBuilder Reqeust.Builder
  */
 public void processHeaderParams(Map<String, String> headerParams, Request.Builder reqBuilder) {
   for (Entry<String, String> param : headerParams.entrySet()) {
     reqBuilder.header(param.getKey(), parameterToString(param.getValue()));
   }
   for (Entry<String, String> header : defaultHeaderMap.entrySet()) {
     if (!headerParams.containsKey(header.getKey())) {
       reqBuilder.header(header.getKey(), parameterToString(header.getValue()));
     }
   }
 }
示例#5
0
  public Response send(final Request.Builder requestBuilder, StringMap headers)
      throws QiniuException {
    if (headers != null) {
      headers.forEach(
          new StringMap.Consumer() {
            @Override
            public void accept(String key, Object value) {
              requestBuilder.header(key, value.toString());
            }
          });
    }

    requestBuilder.header("User-Agent", userAgent());
    long start = System.currentTimeMillis();
    com.squareup.okhttp.Response res = null;
    Response r;
    double duration = (System.currentTimeMillis() - start) / 1000.0;
    IpTag tag = new IpTag();
    try {
      res = httpClient.newCall(requestBuilder.tag(tag).build()).execute();
    } catch (IOException e) {
      e.printStackTrace();
      throw new QiniuException(e);
    }
    r = Response.create(res, tag.ip, duration);
    if (r.statusCode >= 300) {
      throw new QiniuException(r);
    }

    return r;
  }
示例#6
0
 private void addCookieHeader(Request.Builder requestBuilder) {
   SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
   String cookie = preferences.getString(PrefConstants.PREF_COOKIE, null);
   if (cookie != null) {
     requestBuilder.header("Cookie", cookie);
   }
 }
示例#7
0
  private APIResponse get_single(final String urlString, int expectedReturnCode) {
    if (!NetworkUtils.isOnline(context)) {
      return new APIResponse(context);
    }

    if (AppConstants.VERBOSE_LOG) {
      Log.d(this.getClass().getName(), "API GET " + urlString);
    }

    Request.Builder requestBuilder = new Request.Builder().url(urlString);
    addCookieHeader(requestBuilder);
    requestBuilder.header("User-Agent", this.customUserAgent);

    return new APIResponse(context, httpClient, requestBuilder.build(), expectedReturnCode);
  }
示例#8
0
    /** Returns a strategy to use assuming the request can use the network. */
    private CacheStrategy getCandidate() {
      // No cached response.
      if (cacheResponse == null) {
        return new CacheStrategy(request, null);
      }

      // Drop the cached response if it's missing a required handshake.
      if (request.isHttps() && cacheResponse.handshake() == null) {
        return new CacheStrategy(request, null);
      }

      // If this response shouldn't have been stored, it should never be used
      // as a response source. This check should be redundant as long as the
      // persistence store is well-behaved and the rules are constant.
      if (!isCacheable(cacheResponse, request)) {
        return new CacheStrategy(request, null);
      }

      CacheControl requestCaching = request.cacheControl();
      if (requestCaching.noCache() || hasConditions(request)) {
        return new CacheStrategy(request, null);
      }

      long ageMillis = cacheResponseAge();
      long freshMillis = computeFreshnessLifetime();

      if (requestCaching.maxAgeSeconds() != -1) {
        freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds()));
      }

      long minFreshMillis = 0;
      if (requestCaching.minFreshSeconds() != -1) {
        minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds());
      }

      long maxStaleMillis = 0;
      CacheControl responseCaching = cacheResponse.cacheControl();
      if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) {
        maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds());
      }

      if (!responseCaching.noCache() && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) {
        Response.Builder builder = cacheResponse.newBuilder();
        if (ageMillis + minFreshMillis >= freshMillis) {
          builder.addHeader("Warning", "110 HttpURLConnection \"Response is stale\"");
        }
        long oneDayMillis = 24 * 60 * 60 * 1000L;
        if (ageMillis > oneDayMillis && isFreshnessLifetimeHeuristic()) {
          builder.addHeader("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
        }
        return new CacheStrategy(null, builder.build());
      }

      Request.Builder conditionalRequestBuilder = request.newBuilder();

      if (etag != null) {
        conditionalRequestBuilder.header("If-None-Match", etag);
      } else if (lastModified != null) {
        conditionalRequestBuilder.header("If-Modified-Since", lastModifiedString);
      } else if (servedDate != null) {
        conditionalRequestBuilder.header("If-Modified-Since", servedDateString);
      }

      Request conditionalRequest = conditionalRequestBuilder.build();
      return hasConditions(conditionalRequest)
          ? new CacheStrategy(conditionalRequest, cacheResponse)
          : new CacheStrategy(conditionalRequest, null);
    }
示例#9
0
  @Override
  protected void download() {
    File destination = new File(request.getDestination());
    final boolean fileExists = destination.exists();

    if (request.isDeleteOnFailure() && fileExists) {
      Log.w(TAG, "File already exists");
      if (request.getFeedfileType() != FeedImage.FEEDFILETYPE_FEEDIMAGE) {
        onFail(DownloadError.ERROR_FILE_EXISTS, null);
        return;
      } else {
        onSuccess();
        return;
      }
    }

    OkHttpClient httpClient = AntennapodHttpClient.getHttpClient();
    RandomAccessFile out = null;
    InputStream connection;
    ResponseBody responseBody = null;

    try {
      final URI uri = URIUtil.getURIFromRequestUrl(request.getSource());
      Request.Builder httpReq =
          new Request.Builder().url(uri.toURL()).header("User-Agent", ClientConfig.USER_AGENT);
      if (request.getIfModifiedSince() > 0) {
        long threeDaysAgo = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 3;
        if (request.getIfModifiedSince() > threeDaysAgo) {
          Date date = new Date(request.getIfModifiedSince());
          String httpDate = HttpDate.format(date);
          Log.d(TAG, "addHeader(\"If-Modified-Since\", \"" + httpDate + "\")");
          httpReq.addHeader("If-Modified-Since", httpDate);
        }
      }

      // add authentication information
      String userInfo = uri.getUserInfo();
      if (userInfo != null) {
        String[] parts = userInfo.split(":");
        if (parts.length == 2) {
          String credentials = encodeCredentials(parts[0], parts[1], "ISO-8859-1");
          httpReq.header("Authorization", credentials);
        }
      } else if (!StringUtils.isEmpty(request.getUsername()) && request.getPassword() != null) {
        String credentials =
            encodeCredentials(request.getUsername(), request.getPassword(), "ISO-8859-1");
        httpReq.header("Authorization", credentials);
      }

      // add range header if necessary
      if (fileExists) {
        request.setSoFar(destination.length());
        httpReq.addHeader("Range", "bytes=" + request.getSoFar() + "-");
        Log.d(TAG, "Adding range header: " + request.getSoFar());
      }

      Response response = null;
      try {
        response = httpClient.newCall(httpReq.build()).execute();
      } catch (IOException e) {
        Log.e(TAG, e.toString());
        if (e.getMessage().contains("PROTOCOL_ERROR")) {
          httpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
          response = httpClient.newCall(httpReq.build()).execute();
        } else {
          throw e;
        }
      }
      responseBody = response.body();
      String contentEncodingHeader = response.header("Content-Encoding");
      boolean isGzip = StringUtils.equalsIgnoreCase(contentEncodingHeader, "gzip");

      Log.d(TAG, "Response code is " + response.code());

      if (!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
        Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoding");
        if (userInfo != null) {
          String[] parts = userInfo.split(":");
          if (parts.length == 2) {
            String credentials = encodeCredentials(parts[0], parts[1], "UTF-8");
            httpReq.header("Authorization", credentials);
          }
        } else if (!StringUtils.isEmpty(request.getUsername()) && request.getPassword() != null) {
          String credentials =
              encodeCredentials(request.getUsername(), request.getPassword(), "UTF-8");
          httpReq.header("Authorization", credentials);
        }
        response = httpClient.newCall(httpReq.build()).execute();
        responseBody = response.body();
        contentEncodingHeader = response.header("Content-Encoding");
        isGzip = StringUtils.equalsIgnoreCase(contentEncodingHeader, "gzip");
      }

      if (!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_NOT_MODIFIED) {
        Log.d(
            TAG,
            "Feed '" + request.getSource() + "' not modified since last update, Download canceled");
        onCancelled();
        return;
      }

      if (!response.isSuccessful() || response.body() == null) {
        final DownloadError error;
        final String details;
        if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
          error = DownloadError.ERROR_UNAUTHORIZED;
          details = String.valueOf(response.code());
        } else {
          error = DownloadError.ERROR_HTTP_DATA_ERROR;
          details = String.valueOf(response.code());
        }
        onFail(error, details);
        return;
      }

      if (!StorageUtils.storageAvailable(
          ClientConfig.applicationCallbacks.getApplicationInstance())) {
        onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null);
        return;
      }

      connection = new BufferedInputStream(responseBody.byteStream());

      String contentRangeHeader = (fileExists) ? response.header("Content-Range") : null;

      if (fileExists
          && response.code() == HttpStatus.SC_PARTIAL_CONTENT
          && !StringUtils.isEmpty(contentRangeHeader)) {
        String start =
            contentRangeHeader.substring("bytes ".length(), contentRangeHeader.indexOf("-"));
        request.setSoFar(Long.valueOf(start));
        Log.d(TAG, "Starting download at position " + request.getSoFar());

        out = new RandomAccessFile(destination, "rw");
        out.seek(request.getSoFar());
      } else {
        destination.delete();
        destination.createNewFile();
        out = new RandomAccessFile(destination, "rw");
      }

      byte[] buffer = new byte[BUFFER_SIZE];
      int count = 0;
      request.setStatusMsg(R.string.download_running);
      Log.d(TAG, "Getting size of download");
      request.setSize(responseBody.contentLength() + request.getSoFar());
      Log.d(TAG, "Size is " + request.getSize());
      if (request.getSize() < 0) {
        request.setSize(DownloadStatus.SIZE_UNKNOWN);
      }

      long freeSpace = StorageUtils.getFreeSpaceAvailable();
      Log.d(TAG, "Free space is " + freeSpace);

      if (request.getSize() != DownloadStatus.SIZE_UNKNOWN && request.getSize() > freeSpace) {
        onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE, null);
        return;
      }

      Log.d(TAG, "Starting download");
      try {
        while (!cancelled && (count = connection.read(buffer)) != -1) {
          out.write(buffer, 0, count);
          request.setSoFar(request.getSoFar() + count);
          request.setProgressPercent(
              (int) (((double) request.getSoFar() / (double) request.getSize()) * 100));
        }
      } catch (IOException e) {
        Log.e(TAG, Log.getStackTraceString(e));
      }
      if (cancelled) {
        onCancelled();
      } else {
        // check if size specified in the response header is the same as the size of the
        // written file. This check cannot be made if compression was used
        if (!isGzip
            && request.getSize() != DownloadStatus.SIZE_UNKNOWN
            && request.getSoFar() != request.getSize()) {
          onFail(
              DownloadError.ERROR_IO_ERROR,
              "Download completed but size: "
                  + request.getSoFar()
                  + " does not equal expected size "
                  + request.getSize());
          return;
        } else if (request.getSize() > 0 && request.getSoFar() == 0) {
          onFail(DownloadError.ERROR_IO_ERROR, "Download completed, but nothing was read");
          return;
        }
        onSuccess();
      }

    } catch (IllegalArgumentException e) {
      e.printStackTrace();
      onFail(DownloadError.ERROR_MALFORMED_URL, e.getMessage());
    } catch (SocketTimeoutException e) {
      e.printStackTrace();
      onFail(DownloadError.ERROR_CONNECTION_ERROR, e.getMessage());
    } catch (UnknownHostException e) {
      e.printStackTrace();
      onFail(DownloadError.ERROR_UNKNOWN_HOST, e.getMessage());
    } catch (IOException e) {
      e.printStackTrace();
      onFail(DownloadError.ERROR_IO_ERROR, e.getMessage());
    } catch (NullPointerException e) {
      // might be thrown by connection.getInputStream()
      e.printStackTrace();
      onFail(DownloadError.ERROR_CONNECTION_ERROR, request.getSource());
    } finally {
      IOUtils.closeQuietly(out);
      AntennapodHttpClient.cleanup();
      IOUtils.closeQuietly(responseBody);
    }
  }