public static Header[] parseAll(final HttpDataReceiver datareceiver)
throws HttpException, IOException {
ArrayList headerLines = new ArrayList();
CharArrayBuffer current = null;
CharArrayBuffer previous = null;
for (;;) {
if (current == null) {
current = new CharArrayBuffer(64);
} else {
current.clear();
}
int l = datareceiver.readLine(current);
if (l == -1 || current.length() < 1) {
break;
}
// Parse the header name and value
// Check for folded headers first
// Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
// discussion on folded headers
if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
// we have continuation folded header
// so append value
int i = 0;
while (i < current.length()) {
char ch = current.charAt(i);
if (ch != ' ' && ch != '\t') {
break;
}
i++;
}
previous.append(' ');
previous.append(current, i, current.length() - i);
} else {
headerLines.add(current);
previous = current;
current = null;
}
}
Header[] headers = new Header[headerLines.size()];
for (int i = 0; i < headerLines.size(); i++) {
CharArrayBuffer buffer = (CharArrayBuffer) headerLines.get(i);
int colon = buffer.indexOf(':');
if (colon == -1) {
throw new ProtocolException("Invalid header: " + buffer.toString());
}
String s = buffer.substringTrimmed(0, colon);
if (s.equals("")) {
throw new ProtocolException("Invalid header: " + buffer.toString());
}
headers[i] = new BufferedHeader(s, buffer, colon + 1);
}
return headers;
}