A utility class for parsing http header values according to
RFC-2616 Section 4 and 19.3.
| Method from org.apache.commons.httpclient.HttpParser Detail: |
public static Header[] parseHeaders(InputStream is) throws IOException, HttpException {
LOG.trace("enter HeaderParser.parseHeaders(InputStream, String)");
return parseHeaders(is, "US-ASCII");
} Deprecated! use - #parseHeaders(InputStream, String)
Parses headers from the given stream. Headers with the same name are not
combined. |
public static Header[] parseHeaders(InputStream is,
String charset) throws IOException, HttpException {
LOG.trace("enter HeaderParser.parseHeaders(InputStream, String)");
ArrayList headers = new ArrayList();
String name = null;
StringBuffer value = null;
for (; ;) {
String line = HttpParser.readLine(is, charset);
if ((line == null) || (line.trim().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 ((line.charAt(0) == ' ") || (line.charAt(0) == '\t")) {
// we have continuation folded header
// so append value
if (value != null) {
value.append(' ");
value.append(line.trim());
}
} else {
// make sure we save the previous name,value pair if present
if (name != null) {
headers.add(new Header(name, value.toString()));
}
// Otherwise we should have normal HTTP header line
// Parse the header name and value
int colon = line.indexOf(":");
if (colon < 0) {
throw new ProtocolException("Unable to parse header: " + line);
}
name = line.substring(0, colon).trim();
value = new StringBuffer(line.substring(colon + 1).trim());
}
}
// make sure we save the last name,value pair if present
if (name != null) {
headers.add(new Header(name, value.toString()));
}
return (Header[]) headers.toArray(new Header[headers.size()]);
}
Parses headers from the given stream. Headers with the same name are not
combined. |
public static String readLine(InputStream inputStream) throws IOException {
LOG.trace("enter HttpParser.readLine(InputStream)");
return readLine(inputStream, "US-ASCII");
} Deprecated! use - #readLine(InputStream, String)
Read up to "\n" from an (unchunked) input stream.
If the stream ends before the line terminator is found,
the last part of the string will still be returned.
If no input data available, null is returned |
public static String readLine(InputStream inputStream,
String charset) throws IOException {
LOG.trace("enter HttpParser.readLine(InputStream, String)");
byte[] rawdata = readRawLine(inputStream);
if (rawdata == null) {
return null;
}
// strip CR and LF from the end
int len = rawdata.length;
int offset = 0;
if (len > 0) {
if (rawdata[len - 1] == '\n") {
offset++;
if (len > 1) {
if (rawdata[len - 2] == '\r") {
offset++;
}
}
}
}
final String result =
EncodingUtil.getString(rawdata, 0, len - offset, charset);
if (Wire.HEADER_WIRE.enabled()) {
String logoutput = result;
if (offset == 2)
logoutput = result + "\r\n";
else if (offset == 1)
logoutput = result + "\n";
Wire.HEADER_WIRE.input(logoutput);
}
return result;
}
Read up to "\n" from an (unchunked) input stream.
If the stream ends before the line terminator is found,
the last part of the string will still be returned.
If no input data available, null is returned. |
public static byte[] readRawLine(InputStream inputStream) throws IOException {
LOG.trace("enter HttpParser.readRawLine()");
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int ch;
while ((ch = inputStream.read()) >= 0) {
buf.write(ch);
if (ch == '\n") { // be tolerant (RFC-2616 Section 19.3)
break;
}
}
if (buf.size() == 0) {
return null;
}
return buf.toByteArray();
}
Return byte array from an (unchunked) input stream.
Stop reading when "\n" terminator encountered
If the stream ends before the line terminator is found,
the last part of the string will still be returned.
If no input data available, null is returned. |