@Override
  public synchronized void onResponse(Response response) throws IOException {
    Buffer buffer = new Buffer();
    Response.Body body = response.body();
    body.source().readAll(buffer);

    responses.add(new RecordedResponse(response.request(), response, buffer.readUtf8(), null));
    notifyAll();
  }
  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";
    }
  }
 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);
   }
 }
예제 #4
0
  private void readControlFrame() throws IOException {
    Buffer buffer = null;
    if (frameBytesRead < frameLength) {
      buffer = new Buffer();

      if (isClient) {
        source.readFully(buffer, frameLength);
      } else {
        while (frameBytesRead < frameLength) {
          int toRead = (int) Math.min(frameLength - frameBytesRead, maskBuffer.length);
          int read = source.read(maskBuffer, 0, toRead);
          if (read == -1) throw new EOFException();
          toggleMask(maskBuffer, read, maskKey, frameBytesRead);
          buffer.write(maskBuffer, 0, read);
          frameBytesRead += read;
        }
      }
    }

    switch (opcode) {
      case OPCODE_CONTROL_PING:
        frameCallback.onPing(buffer);
        break;
      case OPCODE_CONTROL_PONG:
        frameCallback.onPong(buffer);
        break;
      case OPCODE_CONTROL_CLOSE:
        int code = 1000;
        String reason = "";
        if (buffer != null) {
          long bufferSize = buffer.size();
          if (bufferSize == 1) {
            throw new ProtocolException("Malformed close payload length of 1.");
          } else if (bufferSize != 0) {
            code = buffer.readShort();
            validateCloseCode(code, false);

            reason = buffer.readUtf8();
          }
        }
        frameCallback.onClose(code, reason);
        closed = true;
        break;
      default:
        throw new ProtocolException("Unknown control opcode: " + toHexString(opcode));
    }
  }