@Override
 public InputStream getMessagePayload() throws IOException {
   if (request.body() == null) {
     return null;
   }
   Buffer buf = new Buffer();
   request.body().writeTo(buf);
   return buf.inputStream();
 }
 @Override
 public String getContentType() {
   if (request.body() != null) {
     return (request.body().contentType() != null)
         ? request.body().contentType().toString()
         : null;
   }
   return null;
 }
  @Override
  public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
      return chain.proceed(originalRequest);
    }

    Request compressedRequest =
        originalRequest
            .newBuilder()
            .header("Content-Encoding", "gzip")
            .method(originalRequest.method(), gzip(originalRequest.body()))
            .build();
    return chain.proceed(compressedRequest);
  }
예제 #4
0
 @Test
 public void put() {
   Request request = fromArgs("-X", "PUT", "http://example.com").createRequest();
   assertEquals("PUT", request.method());
   assertEquals("http://example.com", request.urlString());
   assertNull(request.body());
 }
예제 #5
0
 @Test
 public void userAgent() {
   Request request = fromArgs("-A", "foo", "http://example.com").createRequest();
   assertEquals("GET", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("foo", request.header("User-Agent"));
   assertNull(request.body());
 }
예제 #6
0
 @Test
 public void referer() {
   Request request = fromArgs("-e", "foo", "http://example.com").createRequest();
   assertEquals("GET", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("foo", request.header("Referer"));
   assertNull(request.body());
 }
예제 #7
0
 @Test
 public void dataPut() {
   Request request = fromArgs("-d", "foo", "-X", "PUT", "http://example.com").createRequest();
   Request.Body body = request.body();
   assertEquals("PUT", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
   assertEquals("foo", bodyAsString(body));
 }
  private static String bodyToString(final Request request) {

    try {
      final Request copy = request.newBuilder().build();
      final Buffer buffer = new Buffer();
      copy.body().writeTo(buffer);
      return buffer.readUtf8();
    } catch (final IOException e) {
      return "did not work";
    }
  }
예제 #9
0
 @Test
 public void contentTypeHeader() {
   Request request =
       fromArgs("-d", "foo", "-H", "Content-Type: application/json", "http://example.com")
           .createRequest();
   Request.Body body = request.body();
   assertEquals("POST", request.method());
   assertEquals("http://example.com", request.urlString());
   assertEquals("application/json; charset=utf-8", body.contentType().toString());
   assertEquals("foo", bodyAsString(body));
 }
예제 #10
0
 private byte[] generateBody() throws IOException {
   RequestBody body = mRequest.body();
   if (body != null) {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     BufferedSink sink = Okio.buffer(Okio.sink(out));
     body.writeTo(sink);
     sink.flush();
     return out.toByteArray();
   } else {
     return null;
   }
 }
 private void logRequest(Request request, Response response, String body) throws IOException {
   if (Config.LOGS_ENABLED) {
     String requestString = request.method() + " " + request.urlString();
     if (!request.method().toLowerCase().equals("get")) {
       Buffer buffer = new Buffer();
       request.body().writeTo(buffer);
       requestString += " " + buffer.readUtf8();
     }
     if (response.code() >= 400) {
       LogHelper.d(TAG, "request failed " + requestString);
     } else {
       LogHelper.d(TAG, "request success " + requestString);
     }
     LogHelper.i(TAG, "response = " + response.code() + "  " + body);
   }
 }
예제 #12
0
 @Nullable
 @Override
 public byte[] body() throws IOException {
   RequestBody body = mRequest.body();
   if (body == null) {
     return null;
   }
   OutputStream out = mRequestBodyHelper.createBodySink(firstHeaderValue("Content-Encoding"));
   BufferedSink bufferedSink = Okio.buffer(Okio.sink(out));
   try {
     body.writeTo(bufferedSink);
   } finally {
     bufferedSink.close();
   }
   return mRequestBodyHelper.getDisplayBody();
 }
예제 #13
0
  @Override
  public Response intercept(Chain chain) throws IOException {
    Level level = this.level;

    Request request = chain.request();
    if (level == Level.NONE) {
      return chain.proceed(request);
    }

    boolean logBody = level == Level.BODY;
    boolean logHeaders = logBody || level == Level.HEADERS;

    RequestBody requestBody = request.body();
    boolean hasRequestBody = requestBody != null;

    Connection connection = chain.connection();
    Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1;
    String requestStartMessage =
        "--> " + request.method() + ' ' + requestPath(request.httpUrl()) + ' ' + protocol(protocol);
    if (!logHeaders && hasRequestBody) {
      requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
    }
    logger.log(requestStartMessage);

    if (logHeaders) {
      Headers headers = request.headers();
      for (int i = 0, count = headers.size(); i < count; i++) {
        logger.log(headers.name(i) + ": " + headers.value(i));
      }

      String endMessage = "--> END " + request.method();
      if (logBody && hasRequestBody) {
        Buffer buffer = new Buffer();
        requestBody.writeTo(buffer);

        Charset charset = UTF8;
        MediaType contentType = requestBody.contentType();
        if (contentType != null) {
          contentType.charset(UTF8);
        }

        logger.log("");
        logger.log(buffer.readString(charset));

        endMessage += " (" + requestBody.contentLength() + "-byte body)";
      }
      logger.log(endMessage);
    }

    long startNs = System.nanoTime();
    Response response = chain.proceed(request);
    long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);

    ResponseBody responseBody = response.body();
    logger.log(
        "<-- "
            + protocol(response.protocol())
            + ' '
            + response.code()
            + ' '
            + response.message()
            + " ("
            + tookMs
            + "ms"
            + (!logHeaders ? ", " + responseBody.contentLength() + "-byte body" : "")
            + ')');

    if (logHeaders) {
      Headers headers = response.headers();
      for (int i = 0, count = headers.size(); i < count; i++) {
        logger.log(headers.name(i) + ": " + headers.value(i));
      }

      String endMessage = "<-- END HTTP";
      if (logBody) {
        BufferedSource source = responseBody.source();
        source.request(Long.MAX_VALUE); // Buffer the entire body.
        Buffer buffer = source.buffer();

        Charset charset = UTF8;
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
          charset = contentType.charset(UTF8);
        }

        if (responseBody.contentLength() != 0) {
          logger.log("");
          logger.log(buffer.clone().readString(charset));
        }

        endMessage += " (" + buffer.size() + "-byte body)";
      }
      logger.log(endMessage);
    }

    return response;
  }