Run rustfmt
This commit is contained in:
@ -8,8 +8,6 @@ pub fn parse_request(stream: &mut http::Stream) -> Result<http::Request, String>
|
||||
let mut parser = Parser::new_request_parser(&buf[..size]);
|
||||
let header_size = parser.parse().unwrap();
|
||||
|
||||
|
||||
|
||||
let mut header_fields = Vec::new();
|
||||
for (name, value) in parser.headers {
|
||||
header_fields.push(http::HeaderField {
|
||||
@ -126,8 +124,13 @@ impl Parser<'_> {
|
||||
self.next(*char);
|
||||
match self.state {
|
||||
State::Finish => return Ok(self.header_size),
|
||||
State::Error => return Err(format!("invalid character at position {}", self.header_size - 1)),
|
||||
_ => {},
|
||||
State::Error => {
|
||||
return Err(format!(
|
||||
"invalid character at position {}",
|
||||
self.header_size - 1
|
||||
))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
return Err(String::from("input too short"));
|
||||
@ -135,134 +138,120 @@ impl Parser<'_> {
|
||||
|
||||
fn next(&mut self, char: u8) {
|
||||
self.header_size += 1;
|
||||
let get_str = || {
|
||||
std::str::from_utf8(&self.buf[self.str_start..self.header_size - 1]).unwrap()
|
||||
};
|
||||
let get_str =
|
||||
|| std::str::from_utf8(&self.buf[self.str_start..self.header_size - 1]).unwrap();
|
||||
self.state = match &self.state {
|
||||
State::Error => State::Error,
|
||||
State::Finish => State::Error,
|
||||
State::Method => {
|
||||
match char {
|
||||
0x41..=0x5A => State::Method,
|
||||
0x20 => {
|
||||
self.method = Some(get_str());
|
||||
self.str_start = self.header_size;
|
||||
State::Uri
|
||||
},
|
||||
_ => State::Error,
|
||||
State::Method => match char {
|
||||
0x41..=0x5A => State::Method,
|
||||
0x20 => {
|
||||
self.method = Some(get_str());
|
||||
self.str_start = self.header_size;
|
||||
State::Uri
|
||||
}
|
||||
_ => State::Error,
|
||||
},
|
||||
State::Uri => {
|
||||
match char {
|
||||
0x21..=0x7E => State::Uri,
|
||||
0x20 => {
|
||||
self.uri = Some(get_str());
|
||||
self.str_start = self.header_size;
|
||||
State::Http(&State::HeaderName)
|
||||
},
|
||||
_ => State::Error,
|
||||
State::Uri => match char {
|
||||
0x21..=0x7E => State::Uri,
|
||||
0x20 => {
|
||||
self.uri = Some(get_str());
|
||||
self.str_start = self.header_size;
|
||||
State::Http(&State::HeaderName)
|
||||
}
|
||||
_ => State::Error,
|
||||
},
|
||||
State::Http(next) => {
|
||||
match char {
|
||||
0x48 | 0x54 | 0x50 => State::Http(next),
|
||||
0x2F => {
|
||||
let http = get_str();
|
||||
self.str_start = self.header_size;
|
||||
if http != "HTTP" {
|
||||
State::Error
|
||||
} else {
|
||||
State::HttpVersion(next)
|
||||
}
|
||||
},
|
||||
_ => State::Error,
|
||||
State::Http(next) => match char {
|
||||
0x48 | 0x54 | 0x50 => State::Http(next),
|
||||
0x2F => {
|
||||
let http = get_str();
|
||||
self.str_start = self.header_size;
|
||||
if http != "HTTP" {
|
||||
State::Error
|
||||
} else {
|
||||
State::HttpVersion(next)
|
||||
}
|
||||
}
|
||||
_ => State::Error,
|
||||
},
|
||||
State::HttpVersion(next) => {
|
||||
match char {
|
||||
0x30..=0x39 | 0x2E => State::HttpVersion(next),
|
||||
0x0D => {
|
||||
match next {
|
||||
State::HeaderName => {
|
||||
self.http_version = Some(get_str());
|
||||
State::CRLF(next)
|
||||
},
|
||||
_ => State::Error,
|
||||
}
|
||||
},
|
||||
0x20 => {
|
||||
match next {
|
||||
State::StatusCode => {
|
||||
self.http_version = Some(get_str());
|
||||
self.str_start = self.header_size;
|
||||
State::StatusCode
|
||||
},
|
||||
_ => State::Error,
|
||||
}
|
||||
State::HttpVersion(next) => match char {
|
||||
0x30..=0x39 | 0x2E => State::HttpVersion(next),
|
||||
0x0D => match next {
|
||||
State::HeaderName => {
|
||||
self.http_version = Some(get_str());
|
||||
State::CRLF(next)
|
||||
}
|
||||
_ => State::Error,
|
||||
}
|
||||
},
|
||||
State::StatusCode => {
|
||||
match char {
|
||||
0x30..=0x39 => State::StatusCode,
|
||||
0x20 => {
|
||||
self.status_code = Some(get_str());
|
||||
},
|
||||
0x20 => match next {
|
||||
State::StatusCode => {
|
||||
self.http_version = Some(get_str());
|
||||
self.str_start = self.header_size;
|
||||
State::StatusMessage
|
||||
},
|
||||
State::StatusCode
|
||||
}
|
||||
_ => State::Error,
|
||||
}
|
||||
},
|
||||
_ => State::Error,
|
||||
},
|
||||
State::StatusMessage => {
|
||||
match char {
|
||||
0x20..=0x7E => State::StatusMessage,
|
||||
0x0D => {
|
||||
self.status_message = Some(get_str());
|
||||
State::CRLF(&State::HeaderName)
|
||||
},
|
||||
_ => State::Error,
|
||||
State::StatusCode => match char {
|
||||
0x30..=0x39 => State::StatusCode,
|
||||
0x20 => {
|
||||
self.status_code = Some(get_str());
|
||||
self.str_start = self.header_size;
|
||||
State::StatusMessage
|
||||
}
|
||||
_ => State::Error,
|
||||
},
|
||||
State::HeaderName => {
|
||||
match char {
|
||||
0x0D => {
|
||||
if self.header_size == self.str_start + 1 {
|
||||
State::CRLF(&State::Finish)
|
||||
} else {
|
||||
State::Error
|
||||
}
|
||||
},
|
||||
0x3A => {
|
||||
let header_name = get_str();
|
||||
self.headers.push((header_name, ""));
|
||||
self.str_start = self.header_size;
|
||||
State::HeaderValue
|
||||
},
|
||||
0x00..=0x1F | 0x7F | 0x80..=0xFF |
|
||||
0x20 | 0x28 | 0x29 | 0x2C | 0x2F |
|
||||
0x3A..=0x40 | 0x5B..=0x5D | 0x7B | 0x7D => State::Error,
|
||||
_ => State::HeaderName,
|
||||
State::StatusMessage => match char {
|
||||
0x20..=0x7E => State::StatusMessage,
|
||||
0x0D => {
|
||||
self.status_message = Some(get_str());
|
||||
State::CRLF(&State::HeaderName)
|
||||
}
|
||||
_ => State::Error,
|
||||
},
|
||||
State::HeaderValue => {
|
||||
match char {
|
||||
0x20..=0x7E | 0x09 => State::HeaderValue,
|
||||
0x0D => {
|
||||
self.headers.last_mut().unwrap().1 = get_str().trim();
|
||||
State::CRLF(&State::HeaderName)
|
||||
},
|
||||
_ => State::Error,
|
||||
State::HeaderName => match char {
|
||||
0x0D => {
|
||||
if self.header_size == self.str_start + 1 {
|
||||
State::CRLF(&State::Finish)
|
||||
} else {
|
||||
State::Error
|
||||
}
|
||||
}
|
||||
}
|
||||
State::CRLF(next) => {
|
||||
match char {
|
||||
0x0A => {
|
||||
self.str_start = self.header_size;
|
||||
*next.clone()
|
||||
},
|
||||
_ => State::Error,
|
||||
0x3A => {
|
||||
let header_name = get_str();
|
||||
self.headers.push((header_name, ""));
|
||||
self.str_start = self.header_size;
|
||||
State::HeaderValue
|
||||
}
|
||||
0x00..=0x1F
|
||||
| 0x7F
|
||||
| 0x80..=0xFF
|
||||
| 0x20
|
||||
| 0x28
|
||||
| 0x29
|
||||
| 0x2C
|
||||
| 0x2F
|
||||
| 0x3A..=0x40
|
||||
| 0x5B..=0x5D
|
||||
| 0x7B
|
||||
| 0x7D => State::Error,
|
||||
_ => State::HeaderName,
|
||||
},
|
||||
State::HeaderValue => match char {
|
||||
0x20..=0x7E | 0x09 => State::HeaderValue,
|
||||
0x0D => {
|
||||
self.headers.last_mut().unwrap().1 = get_str().trim();
|
||||
State::CRLF(&State::HeaderName)
|
||||
}
|
||||
_ => State::Error,
|
||||
},
|
||||
State::CRLF(next) => match char {
|
||||
0x0A => {
|
||||
self.str_start = self.header_size;
|
||||
*next.clone()
|
||||
}
|
||||
_ => State::Error,
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -312,7 +301,10 @@ mod tests {
|
||||
assert_eq!(3, parser.headers.len());
|
||||
assert_eq!(("Host", "www.example.com"), parser.headers[0]);
|
||||
assert_eq!(("Content-Length", "13"), parser.headers[1]);
|
||||
assert_eq!(("User-Agent", "Mozilla/5.0 (X11; Linux x86_64)"), parser.headers[2]);
|
||||
assert_eq!(
|
||||
("User-Agent", "Mozilla/5.0 (X11; Linux x86_64)"),
|
||||
parser.headers[2]
|
||||
);
|
||||
|
||||
assert_eq!("username=test", &request[size..]);
|
||||
}
|
||||
@ -357,7 +349,10 @@ mod tests {
|
||||
|
||||
assert_eq!(2, parser.headers.len());
|
||||
assert_eq!(("Content-Length", "12"), parser.headers[0]);
|
||||
assert_eq!(("Content-Type", "text/plain; charset=us-ascii"), parser.headers[1]);
|
||||
assert_eq!(
|
||||
("Content-Type", "text/plain; charset=us-ascii"),
|
||||
parser.headers[1]
|
||||
);
|
||||
|
||||
assert_eq!("Hello world!", &response[size..]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user