Beispiel #1
0
    @Override
    public WipBrowserTab attach(TabDebugEventListener listener) throws IOException {
      LoggerFactory connectionLoggerFactory = browserImpl.getConnectionLoggerFactory();
      ConnectionLogger connectionLogger;
      if (connectionLoggerFactory == null) {
        connectionLogger = null;
      } else {
        connectionLogger = connectionLoggerFactory.newTabConnectionLogger();
      }
      String webSocketDebuggerUrl = description.webSocketDebuggerUrl();

      if (webSocketDebuggerUrl == null) {
        throw new IOException("Tab is already attached");
      }

      URI uri = URI.create(webSocketDebuggerUrl);
      WsConnection socket;
      if (USE_OLD_WEBSOCKET) {
        socket =
            Hybi00WsConnection.connect(
                browserImpl.getSocketAddress(),
                DEFAULT_CONNECTION_TIMEOUT_MS,
                uri.getPath(),
                "empty origin",
                connectionLogger);
      } else {
        socket =
            Hybi17WsConnection.connect(
                browserImpl.getSocketAddress(),
                DEFAULT_CONNECTION_TIMEOUT_MS,
                uri.getPath(),
                Hybi17WsConnection.MaskStrategy.TRANSPARENT_MASK,
                connectionLogger);
      }

      return new WipTabImpl(socket, browserImpl, listener, description.url());
    }
Beispiel #2
0
  private String readHttpResponseContent(
      InetSocketAddress socketAddress, String resource, LoggerFactory loggerFactory)
      throws IOException {
    ConnectionLogger browserConnectionLogger;
    if (loggerFactory == null) {
      browserConnectionLogger = null;
    } else {
      browserConnectionLogger = loggerFactory.newBrowserConnectionLogger();
    }
    final SocketWrapper socketWrapper =
        new SocketWrapper(
            socketAddress,
            DEFAULT_CONNECTION_TIMEOUT_MS,
            browserConnectionLogger,
            HandshakeUtil.ASCII_CHARSET);
    try {
      if (browserConnectionLogger != null) {
        browserConnectionLogger.start();
        browserConnectionLogger.setConnectionCloser(
            new ConnectionLogger.ConnectionCloser() {
              @Override
              public void closeConnection() {
                socketWrapper
                    .getShutdownRelay()
                    .sendSignal(null, new Exception("UI close request"));
              }
            });
      }
      LoggableOutputStream output = socketWrapper.getLoggableOutput();
      writeHttpLine(output, "GET " + resource + " HTTP/1.1");
      writeHttpLine(output, "User-Agent: ChromeDevTools for Java SDK");
      writeHttpLine(output, "Host: " + socketAddress.getHostName() + ":" + socketAddress.getPort());
      writeHttpLine(output, "");
      output.getOutputStream().flush();

      LoggableInputStream input = socketWrapper.getLoggableInput();

      HandshakeUtil.HttpResponse httpResponse =
          HandshakeUtil.readHttpResponse(HandshakeUtil.createLineReader(input.getInputStream()));

      if (httpResponse.getCode() != 200) {
        throw new IOException(
            "Unrecognized respose: "
                + httpResponse.getCode()
                + " "
                + httpResponse.getReasonPhrase());
      }

      String lengthStr = httpResponse.getFields().get("content-length");
      if (lengthStr == null) {
        throw new IOException("Unrecognizable respose: no content-length");
      }
      int length;
      try {
        length = Integer.parseInt(lengthStr.trim());
      } catch (NumberFormatException e) {
        throw new IOException("Unrecognizable respose: incorrect content-length");
      }
      byte[] responseBytes = new byte[length];
      {
        int readSoFar = 0;
        while (readSoFar < length) {
          int res = input.getInputStream().read(responseBytes, readSoFar, length - readSoFar);
          if (res == -1) {
            throw new IOException("Unexpected EOS");
          }
          readSoFar += res;
        }
      }
      return new String(responseBytes, HandshakeUtil.UTF_8_CHARSET);
    } finally {
      if (browserConnectionLogger != null) {
        browserConnectionLogger.handleEos();
      }
      socketWrapper.getShutdownRelay().sendSignal(null, null);
    }
  }