mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 09:45:31 +00:00
http protocol: rename methods for writing to and parsing from bytes
This commit is contained in:
parent
3c906f48ee
commit
38de05a6c4
7 changed files with 30 additions and 30 deletions
|
|
@ -33,7 +33,7 @@ pub fn bench(c: &mut Criterion) {
|
|||
b.iter(|| {
|
||||
buffer.set_position(0);
|
||||
|
||||
Response::write(black_box(&response), black_box(&mut buffer)).unwrap();
|
||||
Response::write_bytes(black_box(&response), black_box(&mut buffer)).unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ static INPUT: &[u8] = b"GET /announce?info_hash=%04%0bkV%3f%5cr%14%a6%b7%98%adC%
|
|||
|
||||
pub fn bench(c: &mut Criterion) {
|
||||
c.bench_function("request-from-bytes", |b| {
|
||||
b.iter(|| Request::from_bytes(black_box(INPUT)))
|
||||
b.iter(|| Request::parse_bytes(black_box(INPUT)))
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ pub struct AnnounceRequest {
|
|||
}
|
||||
|
||||
impl AnnounceRequest {
|
||||
fn write<W: Write>(&self, output: &mut W, url_suffix: &[u8]) -> ::std::io::Result<()> {
|
||||
fn write_bytes<W: Write>(&self, output: &mut W, url_suffix: &[u8]) -> ::std::io::Result<()> {
|
||||
output.write_all(b"GET /announce")?;
|
||||
output.write_all(url_suffix)?;
|
||||
output.write_all(b"?info_hash=")?;
|
||||
|
|
@ -67,7 +67,7 @@ impl AnnounceRequest {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn from_query_string(query_string: &str) -> anyhow::Result<Self> {
|
||||
pub fn parse_query_string(query_string: &str) -> anyhow::Result<Self> {
|
||||
// -- Parse key-value pairs
|
||||
|
||||
let mut opt_info_hash = None;
|
||||
|
|
@ -173,7 +173,7 @@ pub struct ScrapeRequest {
|
|||
}
|
||||
|
||||
impl ScrapeRequest {
|
||||
fn write<W: Write>(&self, output: &mut W, url_suffix: &[u8]) -> ::std::io::Result<()> {
|
||||
fn write_bytes<W: Write>(&self, output: &mut W, url_suffix: &[u8]) -> ::std::io::Result<()> {
|
||||
output.write_all(b"GET /scrape")?;
|
||||
output.write_all(url_suffix)?;
|
||||
output.write_all(b"?")?;
|
||||
|
|
@ -196,7 +196,7 @@ impl ScrapeRequest {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn from_query_string(query_string: &str) -> anyhow::Result<Self> {
|
||||
pub fn parse_query_string(query_string: &str) -> anyhow::Result<Self> {
|
||||
// -- Parse key-value pairs
|
||||
|
||||
let mut info_hashes = Vec::new();
|
||||
|
|
@ -252,14 +252,14 @@ pub enum Request {
|
|||
|
||||
impl Request {
|
||||
/// Parse Request from HTTP request bytes
|
||||
pub fn from_bytes(bytes: &[u8]) -> anyhow::Result<Option<Self>> {
|
||||
pub fn parse_bytes(bytes: &[u8]) -> anyhow::Result<Option<Self>> {
|
||||
let mut headers = [httparse::EMPTY_HEADER; 16];
|
||||
let mut http_request = httparse::Request::new(&mut headers);
|
||||
|
||||
match http_request.parse(bytes) {
|
||||
Ok(httparse::Status::Complete(_)) => {
|
||||
if let Some(path) = http_request.path {
|
||||
Self::from_http_get_path(path).map(Some)
|
||||
Self::parse_http_get_path(path).map(Some)
|
||||
} else {
|
||||
Err(anyhow::anyhow!("no http path"))
|
||||
}
|
||||
|
|
@ -282,7 +282,7 @@ impl Request {
|
|||
/// UTF-8 string, meaning that non-ascii bytes are invalid characters.
|
||||
/// Therefore, these bytes must be converted to their equivalent multi-byte
|
||||
/// UTF-8 encodings.
|
||||
pub fn from_http_get_path(path: &str) -> anyhow::Result<Self> {
|
||||
pub fn parse_http_get_path(path: &str) -> anyhow::Result<Self> {
|
||||
::log::debug!("request GET path: {}", path);
|
||||
|
||||
let mut split_parts = path.splitn(2, '?');
|
||||
|
|
@ -291,11 +291,11 @@ impl Request {
|
|||
let query_string = split_parts.next().with_context(|| "no query string")?;
|
||||
|
||||
if location == "/announce" {
|
||||
Ok(Request::Announce(AnnounceRequest::from_query_string(
|
||||
Ok(Request::Announce(AnnounceRequest::parse_query_string(
|
||||
query_string,
|
||||
)?))
|
||||
} else if location == "/scrape" {
|
||||
Ok(Request::Scrape(ScrapeRequest::from_query_string(
|
||||
Ok(Request::Scrape(ScrapeRequest::parse_query_string(
|
||||
query_string,
|
||||
)?))
|
||||
} else {
|
||||
|
|
@ -305,8 +305,8 @@ impl Request {
|
|||
|
||||
pub fn write<W: Write>(&self, output: &mut W, url_suffix: &[u8]) -> ::std::io::Result<()> {
|
||||
match self {
|
||||
Self::Announce(r) => r.write(output, url_suffix),
|
||||
Self::Scrape(r) => r.write(output, url_suffix),
|
||||
Self::Announce(r) => r.write_bytes(output, url_suffix),
|
||||
Self::Scrape(r) => r.write_bytes(output, url_suffix),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -351,7 +351,7 @@ mod tests {
|
|||
bytes.extend_from_slice(ANNOUNCE_REQUEST_PATH.as_bytes());
|
||||
bytes.extend_from_slice(b" HTTP/1.1\r\n\r\n");
|
||||
|
||||
let parsed_request = Request::from_bytes(&bytes[..]).unwrap().unwrap();
|
||||
let parsed_request = Request::parse_bytes(&bytes[..]).unwrap().unwrap();
|
||||
let reference_request = get_reference_announce_request();
|
||||
|
||||
assert_eq!(parsed_request, reference_request);
|
||||
|
|
@ -365,7 +365,7 @@ mod tests {
|
|||
bytes.extend_from_slice(SCRAPE_REQUEST_PATH.as_bytes());
|
||||
bytes.extend_from_slice(b" HTTP/1.1\r\n\r\n");
|
||||
|
||||
let parsed_request = Request::from_bytes(&bytes[..]).unwrap().unwrap();
|
||||
let parsed_request = Request::parse_bytes(&bytes[..]).unwrap().unwrap();
|
||||
let reference_request = Request::Scrape(ScrapeRequest {
|
||||
info_hashes: vec![InfoHash(REFERENCE_INFO_HASH)],
|
||||
});
|
||||
|
|
@ -432,7 +432,7 @@ mod tests {
|
|||
|
||||
request.write(&mut bytes, &[]).unwrap();
|
||||
|
||||
let parsed_request = Request::from_bytes(&bytes[..]).unwrap().unwrap();
|
||||
let parsed_request = Request::parse_bytes(&bytes[..]).unwrap().unwrap();
|
||||
|
||||
let success = request == parsed_request;
|
||||
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ pub struct AnnounceResponse {
|
|||
}
|
||||
|
||||
impl AnnounceResponse {
|
||||
pub fn write<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
pub fn write_bytes<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
let mut bytes_written = 0usize;
|
||||
|
||||
bytes_written += output.write(b"d8:completei")?;
|
||||
|
|
@ -124,7 +124,7 @@ pub struct ScrapeResponse {
|
|||
}
|
||||
|
||||
impl ScrapeResponse {
|
||||
pub fn write<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
pub fn write_bytes<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
let mut bytes_written = 0usize;
|
||||
|
||||
bytes_written += output.write(b"d5:filesd")?;
|
||||
|
|
@ -160,7 +160,7 @@ impl FailureResponse {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn write<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
pub fn write_bytes<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
let mut bytes_written = 0usize;
|
||||
|
||||
let reason_bytes = self.failure_reason.as_bytes();
|
||||
|
|
@ -184,14 +184,14 @@ pub enum Response {
|
|||
}
|
||||
|
||||
impl Response {
|
||||
pub fn write<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
pub fn write_bytes<W: Write>(&self, output: &mut W) -> ::std::io::Result<usize> {
|
||||
match self {
|
||||
Response::Announce(r) => r.write(output),
|
||||
Response::Failure(r) => r.write(output),
|
||||
Response::Scrape(r) => r.write(output),
|
||||
Response::Announce(r) => r.write_bytes(output),
|
||||
Response::Failure(r) => r.write_bytes(output),
|
||||
Response::Scrape(r) => r.write_bytes(output),
|
||||
}
|
||||
}
|
||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, ::serde_bencode::Error> {
|
||||
pub fn parse_bytes(bytes: &[u8]) -> Result<Self, ::serde_bencode::Error> {
|
||||
::serde_bencode::from_bytes(bytes)
|
||||
}
|
||||
}
|
||||
|
|
@ -285,7 +285,7 @@ mod tests {
|
|||
|
||||
let mut hand_written = Vec::new();
|
||||
|
||||
response.write(&mut hand_written).unwrap();
|
||||
response.write_bytes(&mut hand_written).unwrap();
|
||||
|
||||
let success = hand_written == reference;
|
||||
|
||||
|
|
@ -303,7 +303,7 @@ mod tests {
|
|||
|
||||
let mut hand_written = Vec::new();
|
||||
|
||||
response.write(&mut hand_written).unwrap();
|
||||
response.write_bytes(&mut hand_written).unwrap();
|
||||
|
||||
let success = hand_written == reference;
|
||||
|
||||
|
|
@ -321,7 +321,7 @@ mod tests {
|
|||
|
||||
let mut hand_written = Vec::new();
|
||||
|
||||
response.write(&mut hand_written).unwrap();
|
||||
response.write_bytes(&mut hand_written).unwrap();
|
||||
|
||||
let success = hand_written == reference;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue