Fix handling long Reason Phrase

rfc2616 does not specify the maximum length of *Reason Phrase* so it's
a good practice to handle even unreasonably long input.

16 char. buffer is not enough to correctly handle even the common `304
Moved Permanently`. Increase buffer size to more realistic 32. Also, up
to 128 more characters will be read and ignored, providing even greater
versatility without increasing memory usage.

Issue #163
This commit is contained in:
Dawid Ciężarkiewicz
2014-11-30 18:26:25 -08:00
parent db72cb5187
commit f3a5c0124a

View File

@@ -609,11 +609,11 @@ pub fn read_status<R: Reader>(stream: &mut R) -> HttpResult<RawStatus> {
_ => return Err(HttpStatusError) _ => return Err(HttpStatusError)
} }
let mut buf = [b' ', ..16]; let mut buf = [b' ', ..32];
{ {
let mut bufwrt = BufWriter::new(&mut buf); let mut bufwrt = BufWriter::new(&mut buf);
loop { 'read: loop {
match try!(stream.read_byte()) { match try!(stream.read_byte()) {
CR => match try!(stream.read_byte()) { CR => match try!(stream.read_byte()) {
LF => break, LF => break,
@@ -622,8 +622,16 @@ pub fn read_status<R: Reader>(stream: &mut R) -> HttpResult<RawStatus> {
b => match bufwrt.write_u8(b) { b => match bufwrt.write_u8(b) {
Ok(_) => (), Ok(_) => (),
Err(_) => { Err(_) => {
// what sort of reason phrase is this long? for _ in range(0u, 128) {
return Err(HttpStatusError); match try!(stream.read_byte()) {
CR => match try!(stream.read_byte()) {
LF => break 'read,
_ => return Err(HttpStatusError)
},
_ => { /* ignore */ }
}
}
return Err(HttpStatusError)
} }
} }
} }