Exemple #1
0
 @Test
 public void parseNameValueBlock() throws IOException {
   List<Header> headerBlock =
       headerEntries(
           "cache-control", "no-cache, no-store",
           "set-cookie", "Cookie1\u0000Cookie2",
           ":status", "200 OK",
           ":version", "HTTP/1.1");
   Request request = new Request.Builder().url("http://square.com/").build();
   Response response = Http2xStream.readSpdy3HeadersList(headerBlock).request(request).build();
   Headers headers = response.headers();
   assertEquals(3, headers.size());
   Assert.assertEquals(Protocol.SPDY_3, response.protocol());
   assertEquals(200, response.code());
   assertEquals("OK", response.message());
   assertEquals("no-cache, no-store", headers.get("cache-control"));
   assertEquals("Cookie2", headers.get("set-cookie"));
   assertEquals("cache-control", headers.name(0));
   assertEquals("no-cache, no-store", headers.value(0));
   assertEquals("set-cookie", headers.name(1));
   assertEquals("Cookie1", headers.value(1));
   assertEquals("set-cookie", headers.name(2));
   assertEquals("Cookie2", headers.value(2));
   assertNull(headers.get(":status"));
   assertNull(headers.get(":version"));
 }
  @Override
  public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();

    long t1 = System.nanoTime();
    L.i(
        TAG,
        "Sending request="
            + request.url()
            + "   connection="
            + chain.connection()
            + "    head="
            + request.headers()
            + " request=");

    Response response = chain.proceed(request);
    long t2 = System.nanoTime();
    L.i(
        TAG,
        "Received response="
            + response.request().url()
            + "   connect time="
            + ((t2 - t1) / 1e6d)
            + "    head="
            + response.headers()
            + "    tostring"
            + response.toString());

    return response;
  }
Exemple #3
0
 @Test
 public void readNameValueBlockDropsForbiddenHeadersSpdy3() throws IOException {
   List<Header> headerBlock =
       headerEntries(
           ":status", "200 OK",
           ":version", "HTTP/1.1",
           "connection", "close");
   Request request = new Request.Builder().url("http://square.com/").build();
   Response response = Http2xStream.readSpdy3HeadersList(headerBlock).request(request).build();
   Headers headers = response.headers();
   assertEquals(0, headers.size());
 }
  @Override
  public Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    Log.v(
        "CS_SDK",
        String.format(
            "Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));

    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    Log.v(
        "CS_SDK",
        String.format(
            "Received response for %s in %.1fms%n%s",
            response.request().url(), (t2 - t1) / 1e6d, response.headers()));

    return response;
  }
  @Override
  public T call() throws Exception {
    Request request = null;
    Response response = null;
    Exception exception = null;
    Call call = null;

    try {
      MNSLog.logD("[call] - ");

      if (context.getCancellationHandler().isCancelled()) {
        throw new InterruptedIOException("This task is cancelled!");
      }

      Request.Builder requestBuilder = new Request.Builder();

      // build request url
      String url = message.buildCanonicalURL();
      MNSUtils.signRequest(message);
      requestBuilder = requestBuilder.url(url);

      // set request headers
      for (String key : message.getHeaders().keySet()) {
        requestBuilder = requestBuilder.addHeader(key, message.getHeaders().get(key));
      }

      String contentType = message.getHeaders().get(MNSHeaders.CONTENT_TYPE);
      String content = message.getContent();

      // set request body
      if (message.getContent() != null) {
        MNSUtils.assertTrue(contentType != null, "Content type can't be null when send data!");
        requestBuilder =
            requestBuilder.method(
                message.getMethod().toString(),
                new ProgressTouchableRequestBody(
                    message.getContent().getBytes(), contentType, context.getProgressCallback()));
      } else {
        switch (message.getMethod()) {
          case PUT:
            requestBuilder =
                requestBuilder.method(
                    message.getMethod().toString(), RequestBody.create(null, new byte[0]));
            break;
          case GET:
            requestBuilder = requestBuilder.get();
            break;
          case HEAD:
            requestBuilder = requestBuilder.head();
            break;
          case DELETE:
            requestBuilder = requestBuilder.delete();
            break;
          default:
            break;
        }
      }

      request = requestBuilder.build();

      if (MNSLog.isEnableLog()) {
        MNSLog.logD("request url: " + request.url());
        Map<String, List<String>> headerMap = request.headers().toMultimap();
        for (String key : headerMap.keySet()) {
          MNSLog.logD("requestHeader " + key + ": " + headerMap.get(key).get(0));
        }
      }

      call = client.newCall(request);
      context.getCancellationHandler().setCall(call);

      // send request
      response = call.execute();

      if (MNSLog.isEnableLog()) {
        MNSLog.logD("response code: " + response.code() + " for url: " + request.url());
        Map<String, List<String>> headerMap = response.headers().toMultimap();
        for (String key : headerMap.keySet()) {
          MNSLog.logD("responseHeader " + key + ": " + headerMap.get(key).get(0));
        }
      }
    } catch (Exception e) {
      MNSLog.logE("Encounter local execpiton: " + e.toString());
      if (MNSLog.isEnableLog()) {
        e.printStackTrace();
      }
      exception = new ClientException(e.getMessage(), e);
    }

    if (exception == null && (response.code() == 203 || response.code() >= 300)) {
      try {
        exception = ResponseParsers.parseResponseErrorXML(response);
      } catch (IOException e) {
        exception = new ClientException(e.getMessage(), e);
      }
    } else if (exception == null) {
      try {
        T result = responseParser.parse(response);
        if (context.getCompletedCallback() != null) {
          context.getCompletedCallback().onSuccess(context.getRequest(), result);
        }
        return result;
      } catch (IOException e) {
        exception = new ClientException(e.getMessage(), e);
      }
    }

    // reconstruct exception caused by manually cancelling
    if ((call != null && call.isCanceled()) || context.getCancellationHandler().isCancelled()) {
      exception = new ClientException("Task is cancelled!", exception.getCause(), true);
    }

    if (exception instanceof ClientException) {
      if (context.getCompletedCallback() != null) {
        context
            .getCompletedCallback()
            .onFailure(context.getRequest(), (ClientException) exception, null);
      }
    } else {
      if (context.getCompletedCallback() != null) {
        context
            .getCompletedCallback()
            .onFailure(context.getRequest(), null, (ServiceException) exception);
      }
    }
    throw exception;
  }
 @Override
 public Map<String, List<String>> getResponseHeaders() {
   return response == null ? null : response.headers().toMultimap();
 }