rust-dnsbox/lib/dnsbox-base/src/common_types/uri.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

2019-07-01 15:43:34 +00:00
use crate::errors::*;
2020-03-07 15:57:47 +00:00
use crate::ser::packet::{remaining_bytes, DnsPacketData, DnsPacketWriteContext};
2019-07-01 15:43:34 +00:00
use crate::ser::text::*;
2020-03-07 15:57:47 +00:00
use bytes::{BufMut, Bytes};
2017-12-21 12:32:14 +00:00
use std::fmt;
use std::io::Cursor;
/// A single quoted non-empty URL.
///
/// Actually shouldn't allow escapes (i.e. no backslash in the content);
/// but to make sure we can export and import any data we allow standard
/// escape mechanisms and even accept unquoted input.
///
/// No whitespace allowed, last field.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct UriText(Bytes);
impl DnsPacketData for UriText {
fn deserialize(data: &mut Cursor<Bytes>) -> Result<Self> {
let raw = remaining_bytes(data);
2019-07-01 15:43:34 +00:00
failure::ensure!(!raw.is_empty(), "URI must not be empty");
2017-12-21 12:32:14 +00:00
Ok(UriText(raw))
}
2017-12-27 14:24:11 +00:00
fn serialize(&self, _context: &mut DnsPacketWriteContext, packet: &mut Vec<u8>) -> Result<()> {
2019-07-01 15:43:34 +00:00
failure::ensure!(!self.0.is_empty(), "URI must not be empty");
2017-12-27 14:24:11 +00:00
packet.reserve(self.0.len());
packet.put_slice(&self.0);
Ok(())
}
2017-12-21 12:32:14 +00:00
}
impl DnsTextData for UriText {
2019-07-01 15:43:34 +00:00
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> crate::errors::Result<Self> {
2017-12-21 12:32:14 +00:00
let raw = next_quoted_field(data)?;
2019-07-01 15:43:34 +00:00
failure::ensure!(!raw.is_empty(), "URI must not be empty");
2017-12-21 12:32:14 +00:00
Ok(UriText(raw.into()))
}
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
write!(f, "{}", quote(&self.0))
}
}