use crate::errors::{TransportError, TransportErrorCode};
use crate::media_types::{TextSource, base_media_type, derives_text_of, text_source_of};
use crate::types::{Representation, ResourceDescriptor, ResourceDescriptorRepresentations};
pub fn primary_representation(resource: &ResourceDescriptor) -> Option<&Representation> {
match &resource.representations {
ResourceDescriptorRepresentations::Representation(only) => Some(only),
ResourceDescriptorRepresentations::List(several) => several.first(),
}
}
pub fn primary_media_type(resource: &ResourceDescriptor) -> Option<&str> {
primary_representation(resource).map(|first| first.media_type.as_str())
}
pub fn resource_entity_types(resource: &ResourceDescriptor) -> &[String] {
resource.entity_types.as_deref().unwrap_or_default()
}
const HTTP_WHITE_SPACE: [char; 4] = ['\n', '\r', '\t', ' '];
fn is_http_token(text: &str) -> bool {
!text.is_empty()
&& text
.chars()
.all(|c| c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c))
}
fn is_of_a_parameter_value(c: char) -> bool {
c == '\t' || (' '..='~').contains(&c) || ('\u{80}'..='\u{ff}').contains(&c)
}
fn quoted_string(input: &[char], start: usize) -> (String, usize) {
let mut position = start + 1;
let mut value = String::new();
loop {
while position < input.len() && input[position] != '"' && input[position] != '\\' {
value.push(input[position]);
position += 1;
}
if position >= input.len() {
break;
}
let mark = input[position];
position += 1;
if mark == '"' {
break;
}
if position >= input.len() {
value.push('\\');
break;
}
value.push(input[position]);
position += 1;
}
(value, position)
}
fn charset(content_type: &str) -> Option<String> {
let input: Vec<char> = content_type
.trim_matches(HTTP_WHITE_SPACE)
.chars()
.collect();
let slash = input.iter().position(|&c| c == '/')?;
if !is_http_token(&input[..slash].iter().collect::<String>()) {
return None;
}
let mut position = input[slash..]
.iter()
.position(|&c| c == ';')
.map_or(input.len(), |at| slash + at);
let subtype: String = input[slash + 1..position].iter().collect();
if !is_http_token(subtype.trim_end_matches(HTTP_WHITE_SPACE)) {
return None;
}
while position < input.len() {
position += 1; while position < input.len() && HTTP_WHITE_SPACE.contains(&input[position]) {
position += 1;
}
let name_start = position;
while position < input.len() && input[position] != ';' && input[position] != '=' {
position += 1;
}
let name = input[name_start..position]
.iter()
.collect::<String>()
.to_ascii_lowercase();
if position < input.len() {
if input[position] == ';' {
continue;
}
position += 1; }
if position >= input.len() {
break;
}
let value = if input[position] == '"' {
let (value, end) = quoted_string(&input, position);
position = end;
while position < input.len() && input[position] != ';' {
position += 1;
}
value
} else {
let value_start = position;
while position < input.len() && input[position] != ';' {
position += 1;
}
let value: String = input[value_start..position].iter().collect();
let value = value.trim_end_matches(HTTP_WHITE_SPACE);
if value.is_empty() {
continue;
}
value.to_owned()
};
if name == "charset" && value.chars().all(is_of_a_parameter_value) {
return Some(value);
}
}
None
}
fn refusal(message: String) -> TransportError {
TransportError::without_response(message, TransportErrorCode::Error)
}
fn bytes_are_text(media_type: &str) -> Result<(), String> {
let source = text_source_of(media_type);
let named = base_media_type(media_type);
let word = source.as_str();
if derives_text_of(media_type) {
Err(format!(
"The bytes of media type `{named}` are not its text, which is derived by reading them (text source `{word}`): read the resource's anchored text instead"
))
} else if source == TextSource::None {
Err(format!(
"The bytes of media type `{named}` are not text, and the type has none (text source `{word}`): read its bytes instead"
))
} else {
Ok(())
}
}
pub fn decode_representation(bytes: &[u8], media_type: &str) -> Result<String, TransportError> {
bytes_are_text(media_type).map_err(refusal)?;
let stated = charset(media_type).filter(|label| !label.is_empty());
#[cfg(feature = "charsets")]
let read = every_charset(stated.as_deref(), bytes);
#[cfg(not(feature = "charsets"))]
let read = utf8_alone(stated.as_deref(), bytes);
read.map_err(|not_decoded| {
refusal(format!(
"The resource's text is {not_decoded}: read its bytes instead"
))
})
}
#[cfg(feature = "charsets")]
include!(concat!(env!("OUT_DIR"), "/refused_encodings.rs"));
#[cfg(feature = "charsets")]
fn every_charset(label: Option<&str>, bytes: &[u8]) -> Result<String, String> {
let encoding = match label {
None => encoding_rs::UTF_8,
Some(label) => encoding_rs::Encoding::for_label(label.as_bytes())
.filter(|named| {
!REFUSED_ENCODINGS.contains(&named.name().to_ascii_lowercase().as_str())
})
.ok_or_else(|| format!("{label}, which is no charset this reader decodes"))?,
};
Ok(encoding.decode(bytes).0.into_owned())
}
#[cfg(any(test, not(feature = "charsets")))]
include!(concat!(env!("OUT_DIR"), "/utf8_labels.rs"));
#[cfg(any(test, not(feature = "charsets")))]
fn utf8_alone(label: Option<&str>, bytes: &[u8]) -> Result<String, String> {
const ALONE: &str = "and only UTF-8 is decoded without the `charsets` feature";
const ASCII_WHITE_SPACE: [char; 5] = ['\t', '\n', '\u{c}', '\r', ' '];
if let Some(label) = label {
let name = label.trim_matches(ASCII_WHITE_SPACE).to_ascii_lowercase();
if !UTF_8_LABELS.contains(&name.as_str()) {
return Err(format!("{label}, {ALONE}"));
}
}
if bytes.starts_with(&[0xfe, 0xff]) || bytes.starts_with(&[0xff, 0xfe]) {
return Err(format!("UTF-16 by its byte-order mark, {ALONE}"));
}
let bytes = bytes.strip_prefix(&[0xef, 0xbb, 0xbf]).unwrap_or(bytes);
Ok(String::from_utf8_lossy(bytes).into_owned())
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::Value;
fn table(text: &str) -> Value {
serde_json::from_str(text).expect("the table is JSON")
}
fn stated<'a>(case: &'a Value, key: &str) -> &'a str {
case[key]
.as_str()
.unwrap_or_else(|| panic!("a case states no {key}: {case}"))
}
fn bytes_of(hexadecimal: &str) -> Vec<u8> {
hexadecimal
.split_whitespace()
.map(|byte| u8::from_str_radix(byte, 16).expect("a byte"))
.collect()
}
fn read(content_type: &str, bytes: &[u8]) -> Result<String, String> {
bytes_are_text(content_type)?;
let label = charset(content_type).filter(|label| !label.is_empty());
utf8_alone(label.as_deref(), bytes)
}
#[test]
fn every_resource_the_table_refuses_has_the_fault_it_states_and_no_other_has_one() {
use crate::required::{Fault, fault_of, schema};
use crate::transport::FrameRefusalReason;
let table = table(include_str!("../specs/resources/reader-cases.json"));
let (mut lacking, mut malformed) = (0, 0);
for case in table["cases"].as_array().expect("cases") {
let why = stated(case, "why");
let fault = fault_of(schema("ResourceDescriptor"), &case["resource"]);
if case.get("refused") != Some(&Value::Bool(true)) {
assert_eq!(fault, None, "{why}");
continue;
}
let reason = match stated(case, "reason") {
"lacks" => {
lacking += 1;
FrameRefusalReason::Lacks
}
"malformed" => {
malformed += 1;
FrameRefusalReason::Malformed
}
other => panic!("{why}: {other} is no reason"),
};
let member = stated(case, "member").to_owned();
assert_eq!(fault, Some(Fault { member, reason }), "{why}");
}
assert!(
lacking > 0 && malformed > 0,
"the table refuses no resource that lacks a member, or none that is malformed"
);
}
#[cfg(feature = "charsets")]
#[test]
fn the_reader_of_utf8_alone_reads_a_case_as_the_table_has_it_or_refuses_it() {
let table = table(include_str!("../specs/text/decode-cases.json"));
let (mut read_as_stated, mut refused) = (0, 0);
for case in table["cases"].as_array().expect("cases") {
let why = stated(case, "why");
let content_type = stated(case, "contentType");
let bytes = bytes_of(stated(case, "bytes"));
let label = charset(content_type).filter(|label| !label.is_empty());
let utf8_is_stated = label.as_deref().is_none_or(|label| {
encoding_rs::Encoding::for_label_no_replacement(label.as_bytes())
== Some(encoding_rs::UTF_8)
});
let utf16_is_marked = encoding_rs::Encoding::for_bom(&bytes)
.is_some_and(|(encoding, _)| encoding != encoding_rs::UTF_8);
let answer = read(content_type, &bytes);
match case.get("text").and_then(Value::as_str) {
Some(text) if utf8_is_stated && !utf16_is_marked => {
read_as_stated += 1;
assert_eq!(answer.as_deref(), Ok(text), "{why}");
}
_ => {
refused += 1;
let refusal = answer.expect_err(why);
if let Some(label) = case.get("refused").and_then(Value::as_str) {
assert!(refusal.contains(label), "{why}: {refusal}");
}
}
}
}
assert!(
read_as_stated >= 25 && refused >= 40,
"{read_as_stated} read and {refused} refused"
);
}
#[test]
fn the_reader_of_utf8_alone_takes_every_label_of_utf8_and_refuses_every_other() {
let table = table(include_str!("../specs/text/encoding-cases.json"));
let (mut taken, mut refused) = (0, 0);
let listed = table["cases"]
.as_array()
.expect("cases")
.iter()
.map(|case| (stated(case, "encoding"), &case["labels"]));
for (encoding, labels) in listed {
for label in labels.as_array().expect("labels") {
let label = label.as_str().expect("a label is text");
let answer = read(&format!("text/plain; charset={label}"), b"\xc3\xa9");
if encoding == "utf-8" {
taken += 1;
assert_eq!(answer.as_deref(), Ok("é"), "{label}");
} else {
refused += 1;
let refusal = answer.expect_err(label);
assert!(refusal.contains(label), "{label}: {refusal}");
}
}
}
assert!(
taken >= 6 && refused >= 200,
"{taken} taken and {refused} refused"
);
}
#[cfg(feature = "charsets")]
#[test]
fn the_reader_of_utf8_alone_reads_malformed_utf8_as_the_standards_decoder_does() {
const TURNS: [u8; 31] = [
0x00, 0x41, 0x7f, 0x80, 0x8f, 0x90, 0x9f, 0xa0, 0xbf, 0xc0, 0xc1, 0xc2, 0xdf, 0xe0,
0xe1, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf3, 0xf4, 0xf5, 0xf7, 0xf8, 0xfb, 0xfc,
0xfd, 0xfe, 0xff,
];
let mut sequences: Vec<Vec<u8>> = vec![vec![]];
for _ in 0..3 {
let longer: Vec<Vec<u8>> = sequences
.iter()
.flat_map(|sequence| {
TURNS.iter().map(move |&byte| {
let mut longer = sequence.clone();
longer.push(byte);
longer
})
})
.collect();
sequences.extend(longer);
sequences.sort();
sequences.dedup();
}
assert!(sequences.len() > 30_000);
for sequence in sequences {
let answer = utf8_alone(None, &sequence);
if sequence.starts_with(&[0xfe, 0xff]) || sequence.starts_with(&[0xff, 0xfe]) {
assert!(answer.is_err(), "{sequence:02x?}");
continue;
}
let standard = encoding_rs::UTF_8.decode_without_bom_handling(&sequence).0;
assert_eq!(answer.as_deref(), Ok(&*standard), "{sequence:02x?}");
}
}
#[test]
fn the_reader_of_utf8_alone_refuses_utf16_by_its_mark_and_says_so() {
for mark in [&b"\xff\xfeh\x00"[..], &b"\xfe\xff\x00h"[..]] {
let refusal = read("text/plain", mark).expect_err("UTF-16 is not decoded");
assert!(refusal.contains("UTF-16"), "{refusal}");
}
}
}