Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Mime to MediaType #295

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt::{self, Debug};
use std::pin::Pin;
use std::task::{Context, Poll};

use crate::{mime, Mime};
use crate::{media_type, MediaType};
use crate::{Status, StatusCode};

pin_project_lite::pin_project! {
Expand Down Expand Up @@ -46,15 +46,15 @@ pin_project_lite::pin_project! {
///
/// # Content Encoding
///
/// By default `Body` will come with a fallback Mime type that is used by `Request` and
/// `Response` if no other type has been set, and no other Mime type can be inferred.
/// By default `Body` will come with a fallback media type that is used by `Request` and
/// `Response` if no other type has been set, and no other media type can be inferred.
///
/// It's _strongly_ recommended to always set a mime type on both the `Request` and `Response`,
/// It's _strongly_ recommended to always set a media type on both the `Request` and `Response`,
/// and not rely on the fallback mechanisms. However, they're still there if you need them.
pub struct Body {
#[pin]
reader: Box<dyn AsyncBufRead + Unpin + Send + Sync + 'static>,
mime: Mime,
media_type: MediaType,
length: Option<usize>,
bytes_read: usize
}
Expand All @@ -63,8 +63,8 @@ pin_project_lite::pin_project! {
impl Body {
/// Create a new empty `Body`.
///
/// The body will have a length of `0`, and the Mime type set to `application/octet-stream` if
/// no other mime type has been set or can be sniffed.
/// The body will have a length of `0`, and the media type set to `application/octet-stream` if
/// no other media type has been set or can be sniffed.
///
/// # Examples
///
Expand All @@ -77,15 +77,15 @@ impl Body {
pub fn empty() -> Self {
Self {
reader: Box::new(io::empty()),
mime: mime::BYTE_STREAM,
media_type: media_type::BYTE_STREAM,
length: Some(0),
bytes_read: 0,
}
}

/// Create a `Body` from a reader with an optional length.
///
/// The Mime type is set to `application/octet-stream` if no other mime type has been set or can
/// The media type is set to `application/octet-stream` if no other media type has been set or can
/// be sniffed. If a `Body` has no length, HTTP implementations will often switch over to
/// framed messages such as [Chunked
/// Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding).
Expand All @@ -108,7 +108,7 @@ impl Body {
) -> Self {
Self {
reader: Box::new(reader),
mime: mime::BYTE_STREAM,
media_type: media_type::BYTE_STREAM,
length: len,
bytes_read: 0,
}
Expand All @@ -133,7 +133,7 @@ impl Body {

/// Create a `Body` from a Vec of bytes.
///
/// The Mime type is set to `application/octet-stream` if no other mime type has been set or can
/// The media type is set to `application/octet-stream` if no other media type has been set or can
/// be sniffed. If a `Body` has no length, HTTP implementations will often switch over to
/// framed messages such as [Chunked
/// Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding).
Expand All @@ -151,7 +151,7 @@ impl Body {
/// ```
pub fn from_bytes(bytes: Vec<u8>) -> Self {
Self {
mime: mime::BYTE_STREAM,
media_type: media_type::BYTE_STREAM,
length: Some(bytes.len()),
reader: Box::new(io::Cursor::new(bytes)),
bytes_read: 0,
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Body {

/// Create a `Body` from a String
///
/// The Mime type is set to `text/plain` if no other mime type has been set or can
/// The media type is set to `text/plain` if no other media type has been set or can
/// be sniffed. If a `Body` has no length, HTTP implementations will often switch over to
/// framed messages such as [Chunked
/// Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding).
Expand All @@ -201,7 +201,7 @@ impl Body {
/// ```
pub fn from_string(s: String) -> Self {
Self {
mime: mime::PLAIN,
media_type: media_type::PLAIN,
length: Some(s.len()),
reader: Box::new(io::Cursor::new(s.into_bytes())),
bytes_read: 0,
Expand Down Expand Up @@ -232,7 +232,7 @@ impl Body {

/// Creates a `Body` from a type, serializing it as JSON.
///
/// # Mime
/// # MediaType
///
/// The encoding is set to `application/json`.
///
Expand All @@ -249,7 +249,7 @@ impl Body {
let body = Self {
length: Some(bytes.len()),
reader: Box::new(io::Cursor::new(bytes)),
mime: mime::JSON,
media_type: media_type::JSON,
bytes_read: 0,
};
Ok(body)
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Body {

/// Creates a `Body` from a type, serializing it using form encoding.
///
/// # Mime
/// # MediaType
///
/// The encoding is set to `application/x-www-form-urlencoded`.
///
Expand Down Expand Up @@ -314,7 +314,7 @@ impl Body {
let body = Self {
length: Some(bytes.len()),
reader: Box::new(io::Cursor::new(bytes)),
mime: mime::FORM,
media_type: media_type::FORM,
bytes_read: 0,
};
Ok(body)
Expand Down Expand Up @@ -351,7 +351,7 @@ impl Body {

/// Create a `Body` from a file.
///
/// The Mime type set to `application/octet-stream` if no other mime type has
/// The media type set to `application/octet-stream` if no other media type has
/// been set or can be sniffed.
///
/// # Examples
Expand All @@ -375,13 +375,13 @@ impl Body {

// Look at magic bytes first, look at extension second, fall back to
// octet stream.
let mime = peek_mime(&mut file)
let media_type = peek_media_type(&mut file)
.await?
.or_else(|| guess_ext(path))
.unwrap_or(mime::BYTE_STREAM);
.unwrap_or(media_type::BYTE_STREAM);

Ok(Self {
mime,
media_type,
length: Some(len as usize),
reader: Box::new(io::BufReader::new(file)),
bytes_read: 0,
Expand Down Expand Up @@ -410,14 +410,14 @@ impl Body {
self.length.map(|length| length == 0)
}

/// Returns the mime type of this Body.
pub fn mime(&self) -> &Mime {
&self.mime
/// Returns the media type of this Body.
pub fn media_type(&self) -> &MediaType {
&self.media_type
}

/// Sets the mime type of this Body.
pub fn set_mime(&mut self, mime: impl Into<Mime>) {
self.mime = mime.into();
/// Sets the media type of this Body.
pub fn set_media_type(&mut self, media_type: impl Into<MediaType>) {
self.media_type = media_type.into();
}
}

Expand Down Expand Up @@ -494,26 +494,26 @@ impl AsyncBufRead for Body {
}
}

/// Look at first few bytes of a file to determine the mime type.
/// Look at first few bytes of a file to determine the media type.
/// This is used for various binary formats such as images and videos.
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
async fn peek_mime(file: &mut async_std::fs::File) -> io::Result<Option<Mime>> {
async fn peek_media_type(file: &mut async_std::fs::File) -> io::Result<Option<MediaType>> {
// We need to read the first 300 bytes to correctly infer formats such as tar.
let mut buf = [0_u8; 300];
file.read(&mut buf).await?;
let mime = Mime::sniff(&buf).ok();
let media_type = MediaType::sniff(&buf).ok();

// Reset the file cursor back to the start.
file.seek(io::SeekFrom::Start(0)).await?;
Ok(mime)
Ok(media_type)
}

/// Look at the extension of a file to determine the mime type.
/// Look at the extension of a file to determine the media type.
/// This is useful for plain-text formats such as HTML and CSS.
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
fn guess_ext(path: &std::path::Path) -> Option<Mime> {
fn guess_ext(path: &std::path::Path) -> Option<MediaType> {
let ext = path.extension().map(|p| p.to_str()).flatten();
ext.and_then(Mime::from_extension)
ext.and_then(MediaType::from_extension)
}

#[cfg(test)]
Expand Down
14 changes: 7 additions & 7 deletions src/headers/header_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use crate::headers::HeaderValues;
use crate::Error;
use crate::{Cookie, Mime};
use crate::{Cookie, MediaType};

/// A header value.
#[derive(Clone, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -46,10 +46,10 @@ impl HeaderValue {
}
}

impl From<Mime> for HeaderValue {
fn from(mime: Mime) -> Self {
impl From<MediaType> for HeaderValue {
fn from(media_type: MediaType) -> Self {
HeaderValue {
inner: format!("{}", mime),
inner: format!("{}", media_type),
}
}
}
Expand All @@ -62,10 +62,10 @@ impl From<Cookie<'_>> for HeaderValue {
}
}

impl From<&Mime> for HeaderValue {
fn from(mime: &Mime) -> Self {
impl From<&MediaType> for HeaderValue {
fn from(media_type: &MediaType) -> Self {
HeaderValue {
inner: format!("{}", mime),
inner: format!("{}", media_type),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
//! # The Body Type
//!
//! In HTTP, [`Body`](struct.Body.html) types are optional. The content of a `Body` is a stream of
//! bytes with a specific encoding; this encoding is its [`Mime` type](struct.Mime.html). The `Mime` can
//! bytes with a specific encoding; this encoding is its [`MediaType` type](struct.MediaType.html). The `MediaType` can
//! be set using the [`set_content_type`](struct.Request.html#method.set_content_type) method, and
//! there are many different possible `Mime` types.
//! there are many different possible `MediaType` types.
//!
//! `http-types`' `Body` struct can take anything that implements
//! [`AsyncBufRead`](https://docs.rs/futures/0.3.1/futures/io/trait.AsyncBufRead.html) and stream
Expand Down Expand Up @@ -122,7 +122,7 @@ pub mod cache;
pub mod conditional;
pub mod content;
pub mod headers;
pub mod mime;
pub mod media_type;
pub mod other;
pub mod proxies;
pub mod server;
Expand Down Expand Up @@ -155,7 +155,7 @@ pub use version::Version;
pub use trailers::Trailers;

#[doc(inline)]
pub use mime::Mime;
pub use media_type::MediaType;

#[doc(inline)]
pub use headers::Headers;
Expand Down
64 changes: 64 additions & 0 deletions src/media_type/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::ParamKind;
use crate::MediaType;

macro_rules! utf8_media_type_const {
($name:ident, $desc:expr, $base:expr, $sub:expr) => {
media_type_const!(
with_params,
$name,
$desc,
$base,
$sub,
Some(ParamKind::Utf8),
";charset=utf-8"
);
};
}
macro_rules! media_type_const {
($name:ident, $desc:expr, $base:expr, $sub:expr) => {
media_type_const!(with_params, $name, $desc, $base, $sub, None, "");
};

(with_params, $name:ident, $desc:expr, $base:expr, $sub:expr, $params:expr, $doccomment:expr) => {
media_type_const!(doc_expanded, $name, $desc, $base, $sub, $params,
concat!(
"Content-Type for ",
$desc,
".\n\n# media type\n\n```text\n",
$base, "/", $sub, $doccomment, "\n```")
);
};

(doc_expanded, $name:ident, $desc:expr, $base:expr, $sub:expr, $params:expr, $doccomment:expr) => {
#[doc = $doccomment]
pub const $name: MediaType = MediaType {
essence: String::new(),
basetype: String::new(),
subtype: String::new(),
params: $params,
static_essence: Some(concat!($base, "/", $sub)),
static_basetype: Some($base),
static_subtype: Some($sub),
};
};
}

utf8_media_type_const!(JAVASCRIPT, "JavaScript", "application", "javascript");
utf8_media_type_const!(CSS, "CSS", "text", "css");
utf8_media_type_const!(HTML, "HTML", "text", "html");
utf8_media_type_const!(PLAIN, "Plain text", "text", "plain");
utf8_media_type_const!(XML, "XML", "application", "xml");
media_type_const!(ANY, "matching anything", "*", "*");
media_type_const!(JSON, "JSON", "application", "json");
media_type_const!(SVG, "SVG", "image", "svg+xml");
media_type_const!(PNG, "PNG images", "image", "png");
media_type_const!(JPEG, "JPEG images", "image", "jpeg");
media_type_const!(SSE, "Server Sent Events", "text", "event-stream");
media_type_const!(BYTE_STREAM, "byte streams", "application", "octet-stream");
media_type_const!(FORM, "forms", "application", "x-www-form-urlencoded");
media_type_const!(MULTIPART_FORM, "multipart forms", "multipart", "form-data");
media_type_const!(WASM, "webassembly", "application", "wasm");
// There are multiple `.ico` media types known, but `image/x-icon`
// is what most browser use. See:
// https://en.wikipedia.org/wiki/ICO_%28file_format%29#MIME_type
media_type_const!(ICO, "ICO icons", "image", "x-icon");
Loading