@Override
 public Response intercept(final Chain chain) throws IOException {
   long t1 = System.nanoTime();
   final Request request = chain.request();
   Log.v(
       NextClient.TAG,
       String.format(
           "[OkHttp Request] %s %s on %s%n", request.method(), request.url(), chain.connection()));
   if (mHeaders) {
     Log.v(NextClient.TAG, "[Request Headers] " + request.headers());
   }
   final Response response = chain.proceed(request);
   long t2 = System.nanoTime();
   Log.v(
       NextClient.TAG,
       String.format(
           "[OkHttp Response] %s %s (%s:%s) in %.1fms%n ",
           request.method(),
           request.url(),
           response.code(),
           response.message(),
           (t2 - t1) / 1e6d));
   if (mHeaders) {
     Log.v(NextClient.TAG, "[Response Headers] " + response.headers());
   }
   if (mBody) {
     Log.v(NextClient.TAG, "[OkHttp Response Body] " + responseToText(response));
   }
   return response;
 }
 @Override
 public Map<String, String> getAllHeaders() {
   HashMap<String, String> headers = new HashMap<>();
   for (String key : request.headers().names()) {
     headers.put(key, request.header(key));
   }
   return headers;
 }
 /**
  * Prepares the HTTP headers and sends them to the server.
  *
  * <p>For streaming requests with a body, headers must be prepared <strong>before</strong> the
  * output stream has been written to. Otherwise the body would need to be buffered!
  *
  * <p>For non-streaming requests with a body, headers must be prepared <strong>after</strong> the
  * output stream has been written to and closed. This ensures that the {@code Content-Length}
  * header field receives the proper value.
  */
 public void writeRequestHeaders(Request request) throws IOException {
   httpEngine.writingRequestHeaders();
   String requestLine =
       RequestLine.get(
           request,
           httpEngine.getConnection().getRoute().getProxy().type(),
           httpEngine.getConnection().getProtocol());
   httpConnection.writeRequest(request.headers(), requestLine);
 }
  private void recordCookie(Request request, Trace trace) {
    for (String cookie : request.headers("Cookie")) {
      if (cookieSampler.isSampling()) {
        final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        recorder.recordAttribute(AnnotationKey.HTTP_COOKIE, StringUtils.drop(cookie, 1024));
      }

      return;
    }
  }
  @Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

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

    Response response = chain.proceed(request);

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

    return response;
  }
Example #6
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;
  }
Example #7
0
 @Override
 public String headerValue(int index) {
   return mRequest.headers().value(index);
 }
Example #8
0
 @Override
 public String headerName(int index) {
   return mRequest.headers().name(index);
 }
Example #9
0
 @Override
 public int headerCount() {
   return mRequest.headers().size();
 }