Code with Finding: |
class HttpReplyMessage {
/**
* MimeHeaderHolder implementation - Begining of reply parsing.
* If we can determine that this reply version number is less then
* 1.0, then we skip the header parsing by returning <strong>true</strong>
* to the MIME parser.
* <p>Otherwise, we parse the status line, and return <strong>false
* </strong> to make the MIME parser continue.
* @return A boolean <strong>true</strong> if the MIME parser should stop
* parsing, <strong>false</strong> otherwise.
* @exception IOException If some IO error occured while reading the
* stream.
* @exception HttpParserException if parsing failed.
*/
public boolean notifyBeginParsing(MimeParser parser)
throws HttpParserException, IOException
{
if ( major <= 0 )
return true;
// Append the whole reply line in some buffer:
HttpBuffer buf = new HttpBuffer();
int ch = parser.read();
int len = 0;
// Skip leading CRLF, a present to Netscape
while((ch == '\r') || (ch =='\n'))
ch = parser.read();
loop:
while (true) {
switch(ch) {
case -1:
throw new HttpParserException("End Of File");
case '\r':
if ((ch = parser.read()) != '\n')
parser.unread(ch);
break loop;
case '\n':
break loop;
default:
buf.append(ch);
len++;
}
// That's a guard against very poor HTTP
if ( len > 16*1024 )
throw new HttpParserException("Invalid HTTP");
ch = parser.read();
}
// Parse the bufer into HTTP version and status code
byte line[] = buf.getByteCopy();
ParseState ps = new ParseState();
ps.ioff = 0;
ps.bufend = line.length;
ps.separator = (byte) ' ';
if ( HttpParser.nextItem(line, ps) < 0 )
throw new RuntimeException("Bad reply: invalid status line ["
+ new String(line, 0, 0, line.length)
+ "]");
// Parse the reply version:
if ((line.length >= 4) && line[4] == (byte) '/' ) {
// A present to broken NCSA servers around
ParseState item = new ParseState();
item.ioff = ps.start+5;
item.bufend = ps.end;
this.major = (short) HttpParser.parseInt(line, item);
item.prepare();
item.ioff++;
this.minor = (short) HttpParser.parseInt(line, item);
} else {
this.major = 1;
this.minor = 0;
}
// Parse the status code:
ps.prepare();
this.status = HttpParser.parseInt(line, ps);
// The rest of the sentence if the reason phrase:
ps.prepare();
HttpParser.skipSpaces(line, ps);
this.reason = new String(line, 0, ps.ioff, line.length-ps.ioff);
return false;
}
}
|