Skip to content

Commit

Permalink
Merge pull request #180 from greatest-ape/work-2023-01-29
Browse files Browse the repository at this point in the history
protocol crates: rename some methods, minor improvements
  • Loading branch information
greatest-ape authored Jan 29, 2024
2 parents f30ab82 + 239266d commit 1807c4a
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 82 deletions.
2 changes: 1 addition & 1 deletion crates/http/src/workers/socket/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ where
let mut position = RESPONSE_HEADER.len();

let body_len = response
.write(&mut &mut self.response_buffer[position..])
.write_bytes(&mut &mut self.response_buffer[position..])
.map_err(ConnectionError::ResponseBufferWrite)?;

position += body_len;
Expand Down
2 changes: 1 addition & 1 deletion crates/http/src/workers/socket/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn parse_request(
match http_request.parse(buffer).with_context(|| "httparse")? {
httparse::Status::Complete(_) => {
let path = http_request.path.ok_or(anyhow::anyhow!("no http path"))?;
let request = Request::from_http_get_path(path)?;
let request = Request::parse_http_get_path(path)?;

let opt_peer_ip = if config.network.runs_behind_reverse_proxy {
let header_name = &config.network.reverse_proxy_ip_header_name;
Expand Down
2 changes: 1 addition & 1 deletion crates/http_load_test/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ where
}

if let Some(body_start_index) = opt_body_start_index {
match Response::from_bytes(&interesting_bytes[body_start_index..]) {
match Response::parse_bytes(&interesting_bytes[body_start_index..]) {
Ok(response) => {
match response {
Response::Announce(_) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
});
}
Expand Down
2 changes: 1 addition & 1 deletion crates/http_protocol/benches/bench_request_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
});
}

Expand Down
28 changes: 14 additions & 14 deletions crates/http_protocol/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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=")?;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"?")?;
Expand All @@ -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();
Expand Down Expand Up @@ -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"))
}
Expand All @@ -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, '?');
Expand All @@ -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 {
Expand All @@ -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),
}
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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)],
});
Expand Down Expand Up @@ -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;

Expand Down
22 changes: 11 additions & 11 deletions crates/http_protocol/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down Expand Up @@ -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")?;
Expand Down Expand Up @@ -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();
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion crates/udp/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ mod tests {

let mut buf = Vec::new();

response.write(&mut buf).unwrap();
response.write_bytes(&mut buf).unwrap();

println!("Buffer len: {}", buf.len());

Expand Down
4 changes: 2 additions & 2 deletions crates/udp/src/workers/socket/mio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl SocketWorker {
}

let src = CanonicalSocketAddr::new(src);
let request_parsable = match Request::from_bytes(
let request_parsable = match Request::parse_bytes(
&self.buffer[..bytes_read],
self.config.protocol.max_scrape_torrents,
) {
Expand Down Expand Up @@ -431,7 +431,7 @@ impl SocketWorker {
) {
let mut buffer = Cursor::new(&mut buffer[..]);

if let Err(err) = response.write(&mut buffer) {
if let Err(err) = response.write_bytes(&mut buffer) {
::log::error!("failed writing response to buffer: {:#}", err);

return;
Expand Down
2 changes: 1 addition & 1 deletion crates/udp/src/workers/socket/uring/recv_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl RecvHelper {

let addr = CanonicalSocketAddr::new(addr);

let request = Request::from_bytes(msg.payload_data(), self.max_scrape_torrents)
let request = Request::parse_bytes(msg.payload_data(), self.max_scrape_torrents)
.map_err(|err| Error::RequestParseError(err, addr))?;

Ok((request, addr))
Expand Down
2 changes: 1 addition & 1 deletion crates/udp/src/workers/socket/uring/send_buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl SendBuffer {

let mut cursor = Cursor::new(&mut self.bytes[..]);

match response.write(&mut cursor) {
match response.write_bytes(&mut cursor) {
Ok(()) => {
self.iovec.iov_len = cursor.position() as usize;

Expand Down
4 changes: 2 additions & 2 deletions crates/udp/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn request_and_response(
let mut buffer = Cursor::new(&mut buffer[..]);

request
.write(&mut buffer)
.write_bytes(&mut buffer)
.with_context(|| "write request")?;

let bytes_written = buffer.position() as usize;
Expand All @@ -119,6 +119,6 @@ pub fn request_and_response(
.recv_from(&mut buffer)
.with_context(|| "recv response")?;

Response::from_bytes(&buffer[..bytes_read], true).with_context(|| "parse response")
Response::parse_bytes(&buffer[..bytes_read], true).with_context(|| "parse response")
}
}
2 changes: 1 addition & 1 deletion crates/udp/tests/invalid_connection_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn no_response(
let mut buffer = Cursor::new(&mut buffer[..]);

request
.write(&mut buffer)
.write_bytes(&mut buffer)
.with_context(|| "write request")?;

let bytes_written = buffer.position() as usize;
Expand Down
4 changes: 2 additions & 2 deletions crates/udp_load_test/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Worker {

for _ in events.iter() {
while let Ok(amt) = self.socket.recv(&mut self.buffer) {
match Response::from_bytes(&self.buffer[0..amt], self.addr.is_ipv4()) {
match Response::parse_bytes(&self.buffer[0..amt], self.addr.is_ipv4()) {
Ok(response) => {
if let Some(request) = self.process_response(response) {
self.send_request(request);
Expand Down Expand Up @@ -288,7 +288,7 @@ impl Worker {
fn send_request(&mut self, request: Request) {
let mut cursor = Cursor::new(self.buffer);

match request.write(&mut cursor) {
match request.write_bytes(&mut cursor) {
Ok(()) => {
let position = cursor.position() as usize;
let inner = cursor.get_ref();
Expand Down
5 changes: 3 additions & 2 deletions crates/udp_protocol/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# aquatic_udp_protocol: UDP BitTorrent tracker protocol

[UDP BitTorrent](https://www.bittorrent.org/beps/bep_0015.html) tracker
message parsing and serialization.
UDP BitTorrent tracker message parsing and serialization.

Implements [BEP 015](https://www.bittorrent.org/beps/bep_0015.html) ([more details](https://libtorrent.org/udp_tracker_protocol.html)).
Loading

0 comments on commit 1807c4a

Please sign in to comment.