Skip to content

Commit

Permalink
fix undefined mime type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Nov 2, 2024
1 parent 7215258 commit c1c0666
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/client/response/meta/mime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! MIME type parser for different data types:
//!
//! * UTF-8 buffer with entire response or just with meta slice (that include **header**)
//! * UTF-8 buffer with entire response or just with meta slice (that include entire **header**)
//! * String (that include **header**)
//! * [Uri](https://docs.gtk.org/glib/struct.Uri.html) (that include **extension**)
//! * `std::Path` (that include **extension**)
Expand Down Expand Up @@ -30,10 +30,12 @@ pub enum Mime {
} // @TODO

impl Mime {
/// Create new `Self` from UTF-8 buffer
/// Create new `Self` from UTF-8 buffer (that includes **header**)
///
/// * result could be `None` for some [status codes](https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes) that does not expect MIME type in header
/// * includes `Self::from_string` parser, it means that given buffer should contain some **header** (not filepath or any other type of strings)
/// * result could be `None` for some [status codes](https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes)
/// that does not expect MIME type in header
/// * includes `Self::from_string` parser,
/// it means that given buffer should contain some **header** (not filepath or any other type of strings)
pub fn from_utf8(buffer: &[u8]) -> Result<Option<Self>, Error> {
// Define max buffer length for this method
const MAX_LEN: usize = 0x400; // 1024
Expand Down Expand Up @@ -75,8 +77,10 @@ impl Mime {

/// Create new `Self` from string that includes **header**
///
/// * result could be `None` for some [status codes](https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes)
/// that does not expect MIME type
/// **Return**
///
/// * `None` if MIME type not found
/// * `Error::Undefined` if status code 2* and type not found in `Mime` enum
pub fn from_string(value: &str) -> Result<Option<Self>, Error> {
// Text
if value.contains("text/gemini") {
Expand Down Expand Up @@ -121,14 +125,13 @@ impl Mime {
return Ok(Some(Self::AudioOgg));
}

// Some type exist, but not defined yet
/* @TODO unstable
if value.contains("/") {
// Some type exist, but not defined yet (on status code is 2*)
if value.starts_with("2") && value.contains("/") {
return Err(Error::Undefined);
} */
}

// Done
Ok(None) // may be empty (for some status codes)
Ok(None) // may be empty (status code ^2*)
}

/// Create new `Self` from [Uri](https://docs.gtk.org/glib/struct.Uri.html)
Expand Down

0 comments on commit c1c0666

Please sign in to comment.