# Manually parsing HTTP response header

Due to some constraints I had to manually parse an HTTP response coming out of a stream.

I could not use buffered reader or scanner because the perform buffering and end up buffering bytes beyond the response header itself.

I'll be hitting my AWS S3 and only expect 200 or 206 status response.

This seems to work, but I'd appreciate some review.

final InputStream reader = socket.getInputStream();
stringBuilder.setLength(0);
while (true) {

//fail
return false;
}

}

//check status
final String status = stringBuilder.toString();
if (!(status.contains("200") || status.contains("206"))) {
//fail
return false;
}

stringBuilder.setLength(0);
while (true) {

return false;
}

final int currentCount = stringBuilder.length();
if (currentCount > 4 &&
stringBuilder.charAt(currentCount - 1) == '\n' &&
stringBuilder.charAt(currentCount - 2) == '\r' &&
stringBuilder.charAt(currentCount - 3) == '\n' &&
stringBuilder.charAt(currentCount - 4) == '\r') {
break; //response consumed
}
}

final String completeResponse = stringBuilder.toString();


This is a bit surprising:

stringBuilder.setLength(0);


Seeing the posted code starting with this is fishy, as it suggests that there is more code before it, and that the method is not decomposed enough, and should be multiple methods instead.

This is a bit surprising:

    final char read = (char) reader.read();


The read() method returns -1 in case of EOF. I suggest to change this check to read < 0 or read == EOF to avoid making reviewers ponder.

        MiscUtils.closeAndIgnore(socket, reader);


A common name for this kind of thing is closeQuietly, usually in a utility class called IOUtils (in Apache commons, for example). I suggest to follow that good example. MiscUtils is a bad idea.

    if (read == '\n')


It's recommended to use braces always, even with single-statement expressions like this.

if (!(status.contains("200") || status.contains("206"))) {


I'd go for the more strict and compact regular expression check:

if (!status.matches("HTTP/.* 20[06] .*")) {


It would be good to split the posted code to two methods:

• A method that consumes the status line (the first line)
• A method that consumes the remaining lines