}
}
private String readHttpResponseContent(InetSocketAddress socketAddress, String resource,
LoggerFactory loggerFactory) throws IOException {
ConnectionLogger 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);
}
}