mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-04-01 09:35:33 +00:00
initial commit
This commit is contained in:
parent
381a398de1
commit
a7083852c3
22 changed files with 446 additions and 15 deletions
46
src/client/response/body.rs
Normal file
46
src/client/response/body.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
pub mod error;
|
||||
pub use error::Error;
|
||||
|
||||
use glib::GString;
|
||||
|
||||
pub struct Body {
|
||||
buffer: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Body {
|
||||
/// Construct from response buffer
|
||||
pub fn from_response(response: &[u8] /* @TODO */) -> Result<Self, Error> {
|
||||
let start = Self::start(response)?;
|
||||
|
||||
let buffer = match response.get(start..) {
|
||||
Some(result) => result,
|
||||
None => return Err(Error::Buffer),
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
buffer: Vec::from(buffer),
|
||||
})
|
||||
}
|
||||
|
||||
// Getters
|
||||
pub fn buffer(&self) -> &Vec<u8> {
|
||||
&self.buffer
|
||||
}
|
||||
|
||||
pub fn to_gstring(&self) -> Result<GString, Error> {
|
||||
match GString::from_utf8(self.buffer.to_vec()) {
|
||||
Ok(result) => Ok(result),
|
||||
Err(_) => Err(Error::Decode),
|
||||
}
|
||||
}
|
||||
|
||||
// Tools
|
||||
fn start(buffer: &[u8]) -> Result<usize, Error> {
|
||||
for (offset, &byte) in buffer.iter().enumerate() {
|
||||
if byte == b'\n' {
|
||||
return Ok(offset + 1);
|
||||
}
|
||||
}
|
||||
Err(Error::Format)
|
||||
}
|
||||
}
|
||||
6
src/client/response/body/error.rs
Normal file
6
src/client/response/body/error.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
pub enum Error {
|
||||
Buffer,
|
||||
Decode,
|
||||
Format,
|
||||
Status,
|
||||
}
|
||||
4
src/client/response/error.rs
Normal file
4
src/client/response/error.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub enum Error {
|
||||
Header,
|
||||
Body,
|
||||
}
|
||||
70
src/client/response/header.rs
Normal file
70
src/client/response/header.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
pub mod error;
|
||||
pub mod meta;
|
||||
pub mod mime;
|
||||
pub mod status;
|
||||
|
||||
pub use error::Error;
|
||||
pub use meta::Meta;
|
||||
pub use mime::Mime;
|
||||
pub use status::Status;
|
||||
|
||||
pub struct Header {
|
||||
status: Status,
|
||||
meta: Option<Meta>,
|
||||
mime: Option<Mime>,
|
||||
// @TODO
|
||||
// charset: Option<Charset>,
|
||||
// language: Option<Language>,
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// Construct from response buffer
|
||||
/// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters
|
||||
pub fn from_response(response: &[u8] /* @TODO */) -> Result<Self, Error> {
|
||||
let end = Self::end(response)?;
|
||||
|
||||
let buffer = match response.get(..end) {
|
||||
Some(result) => result,
|
||||
None => return Err(Error::Buffer),
|
||||
};
|
||||
|
||||
let meta = match Meta::from_header(buffer) {
|
||||
Ok(result) => Some(result),
|
||||
Err(_) => None,
|
||||
};
|
||||
|
||||
let mime = mime::from_header(buffer); // optional
|
||||
// let charset = charset::from_header(buffer); @TODO
|
||||
// let language = language::from_header(buffer); @TODO
|
||||
|
||||
let status = match status::from_header(buffer) {
|
||||
Ok(result) => result,
|
||||
Err(_) => return Err(Error::Status),
|
||||
};
|
||||
|
||||
Ok(Self { status, meta, mime })
|
||||
}
|
||||
|
||||
// Getters
|
||||
pub fn status(&self) -> &Status {
|
||||
&self.status
|
||||
}
|
||||
|
||||
pub fn mime(&self) -> &Option<Mime> {
|
||||
&self.mime
|
||||
}
|
||||
|
||||
pub fn meta(&self) -> &Option<Meta> {
|
||||
&self.meta
|
||||
}
|
||||
|
||||
// Tools
|
||||
fn end(buffer: &[u8]) -> Result<usize, Error> {
|
||||
for (offset, &byte) in buffer.iter().enumerate() {
|
||||
if byte == b'\r' {
|
||||
return Ok(offset);
|
||||
}
|
||||
}
|
||||
Err(Error::Format)
|
||||
}
|
||||
}
|
||||
1
src/client/response/header/charset.rs
Normal file
1
src/client/response/header/charset.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
// @TODO
|
||||
5
src/client/response/header/error.rs
Normal file
5
src/client/response/header/error.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
pub enum Error {
|
||||
Buffer,
|
||||
Format,
|
||||
Status,
|
||||
}
|
||||
1
src/client/response/header/language.rs
Normal file
1
src/client/response/header/language.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
// @TODO
|
||||
26
src/client/response/header/meta.rs
Normal file
26
src/client/response/header/meta.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
pub mod error;
|
||||
pub use error::Error;
|
||||
|
||||
use glib::GString;
|
||||
|
||||
pub struct Meta {
|
||||
buffer: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Meta {
|
||||
pub fn from_header(buffer: &[u8] /* @TODO */) -> Result<Self, Error> {
|
||||
let buffer = match buffer.get(2..) {
|
||||
Some(bytes) => bytes.to_vec(),
|
||||
None => return Err(Error::Undefined),
|
||||
};
|
||||
|
||||
Ok(Self { buffer })
|
||||
}
|
||||
|
||||
pub fn to_gstring(&self) -> Result<GString, Error> {
|
||||
match GString::from_utf8(self.buffer.clone()) {
|
||||
Ok(result) => Ok(result),
|
||||
Err(_) => Err(Error::Undefined),
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/client/response/header/meta/error.rs
Normal file
4
src/client/response/header/meta/error.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub enum Error {
|
||||
Decode,
|
||||
Undefined,
|
||||
}
|
||||
62
src/client/response/header/mime.rs
Normal file
62
src/client/response/header/mime.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
use glib::{GString, Uri};
|
||||
use std::path::Path;
|
||||
|
||||
pub enum Mime {
|
||||
TextGemini,
|
||||
TextPlain,
|
||||
ImagePng,
|
||||
ImageGif,
|
||||
ImageJpeg,
|
||||
ImageWebp,
|
||||
} // @TODO
|
||||
|
||||
pub fn from_header(buffer: &[u8] /* @TODO */) -> Option<Mime> {
|
||||
from_string(&match GString::from_utf8(buffer.to_vec()) {
|
||||
Ok(result) => result,
|
||||
Err(_) => return None, // @TODO error handler?
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_path(path: &Path) -> Option<Mime> {
|
||||
match path.extension().and_then(|extension| extension.to_str()) {
|
||||
Some("gmi") | Some("gemini") => Some(Mime::TextGemini),
|
||||
Some("txt") => Some(Mime::TextPlain),
|
||||
Some("png") => Some(Mime::ImagePng),
|
||||
Some("gif") => Some(Mime::ImageGif),
|
||||
Some("jpeg") | Some("jpg") => Some(Mime::ImageJpeg),
|
||||
Some("webp") => Some(Mime::ImageWebp),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_string(value: &str) -> Option<Mime> {
|
||||
if value.contains("text/gemini") {
|
||||
return Some(Mime::TextGemini);
|
||||
}
|
||||
|
||||
if value.contains("text/plain") {
|
||||
return Some(Mime::TextPlain);
|
||||
}
|
||||
|
||||
if value.contains("image/gif") {
|
||||
return Some(Mime::ImageGif);
|
||||
}
|
||||
|
||||
if value.contains("image/jpeg") {
|
||||
return Some(Mime::ImageJpeg);
|
||||
}
|
||||
|
||||
if value.contains("image/webp") {
|
||||
return Some(Mime::ImageWebp);
|
||||
}
|
||||
|
||||
if value.contains("image/png") {
|
||||
return Some(Mime::ImagePng);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn from_uri(uri: &Uri) -> Option<Mime> {
|
||||
from_path(Path::new(&uri.to_string()))
|
||||
}
|
||||
31
src/client/response/header/status.rs
Normal file
31
src/client/response/header/status.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
pub mod error;
|
||||
pub use error::Error;
|
||||
|
||||
use glib::GString;
|
||||
|
||||
/// https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes
|
||||
pub enum Status {
|
||||
Input,
|
||||
SensitiveInput,
|
||||
Success,
|
||||
Redirect,
|
||||
} // @TODO
|
||||
|
||||
pub fn from_header(buffer: &[u8] /* @TODO */) -> Result<Status, Error> {
|
||||
match buffer.get(0..2) {
|
||||
Some(bytes) => match GString::from_utf8(bytes.to_vec()) {
|
||||
Ok(string) => from_string(string.as_str()),
|
||||
Err(_) => Err(Error::Decode),
|
||||
},
|
||||
None => Err(Error::Undefined),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_string(code: &str) -> Result<Status, Error> {
|
||||
match code {
|
||||
"10" => Ok(Status::Input),
|
||||
"11" => Ok(Status::SensitiveInput),
|
||||
"20" => Ok(Status::Success),
|
||||
_ => Err(Error::Undefined),
|
||||
}
|
||||
}
|
||||
4
src/client/response/header/status/error.rs
Normal file
4
src/client/response/header/status/error.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub enum Error {
|
||||
Undefined,
|
||||
Decode,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue