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;
}