mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
initial commit
This commit is contained in:
parent
381a398de1
commit
a7083852c3
22 changed files with 446 additions and 15 deletions
10
src/client.rs
Normal file
10
src/client.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
pub mod connection;
|
||||
pub mod error;
|
||||
pub mod response;
|
||||
pub mod socket;
|
||||
|
||||
pub use error::Error;
|
||||
pub use response::Response;
|
||||
pub use socket::Socket;
|
||||
|
||||
// @TODO
|
||||
3
src/client/connection.rs
Normal file
3
src/client/connection.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod input_stream;
|
||||
|
||||
// @TODO
|
||||
4
src/client/connection/input_stream.rs
Normal file
4
src/client/connection/input_stream.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub mod byte_buffer;
|
||||
pub use byte_buffer::ByteBuffer;
|
||||
|
||||
// @TODO
|
||||
86
src/client/connection/input_stream/byte_buffer.rs
Normal file
86
src/client/connection/input_stream/byte_buffer.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
pub mod error;
|
||||
|
||||
pub use error::Error;
|
||||
|
||||
use gio::{prelude::InputStreamExt, Cancellable, InputStream};
|
||||
use glib::{object::IsA, Bytes};
|
||||
|
||||
pub const DEFAULT_CAPACITY: usize = 0x400;
|
||||
pub const DEFAULT_CHUNK_SIZE: usize = 0x100;
|
||||
pub const DEFAULT_MAX_SIZE: usize = 0xfffff;
|
||||
|
||||
pub struct ByteBuffer {
|
||||
bytes: Vec<Bytes>,
|
||||
}
|
||||
|
||||
impl ByteBuffer {
|
||||
/// Create dynamically allocated bytes buffer from `gio::InputStream`
|
||||
///
|
||||
/// Options:
|
||||
/// * `capacity` bytes request to reduce extra memory overwrites (1024 by default)
|
||||
/// * `chunk_size` bytes limit to read per iter (256 by default)
|
||||
/// * `max_size` bytes limit to prevent memory overflow (1M by default)
|
||||
pub fn from_input_stream(
|
||||
input_stream: &InputStream, // @TODO
|
||||
cancellable: Option<&impl IsA<Cancellable>>,
|
||||
capacity: Option<usize>,
|
||||
chunk_size: Option<usize>,
|
||||
max_size: Option<usize>,
|
||||
) -> Result<Self, Error> {
|
||||
// Create buffer with initial capacity
|
||||
let mut buffer: Vec<Bytes> = Vec::with_capacity(match capacity {
|
||||
Some(value) => value,
|
||||
None => DEFAULT_CAPACITY,
|
||||
});
|
||||
|
||||
// Disallow unlimited buffer, use defaults on None
|
||||
let limit = match max_size {
|
||||
Some(value) => value,
|
||||
None => DEFAULT_MAX_SIZE,
|
||||
};
|
||||
|
||||
loop {
|
||||
// Check buffer size to prevent memory overflow
|
||||
if buffer.len() > limit {
|
||||
return Err(Error::Overflow);
|
||||
}
|
||||
|
||||
// Continue bytes reading
|
||||
match input_stream.read_bytes(
|
||||
match chunk_size {
|
||||
Some(value) => value,
|
||||
None => DEFAULT_CHUNK_SIZE,
|
||||
},
|
||||
cancellable,
|
||||
) {
|
||||
Ok(bytes) => {
|
||||
// No bytes were read, end of stream
|
||||
if bytes.len() == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
// Save chunk to buffer
|
||||
buffer.push(bytes);
|
||||
}
|
||||
Err(_) => return Err(Error::Stream),
|
||||
};
|
||||
}
|
||||
|
||||
// Done
|
||||
Ok(Self { bytes: buffer })
|
||||
}
|
||||
|
||||
/// Get link to bytes collected
|
||||
pub fn bytes(&self) -> &Vec<Bytes> {
|
||||
&self.bytes
|
||||
}
|
||||
|
||||
/// Return a copy of the bytes in UTF-8
|
||||
pub fn to_utf8(&self) -> Vec<u8> {
|
||||
self.bytes
|
||||
.iter()
|
||||
.flat_map(|byte| byte.iter())
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
4
src/client/connection/input_stream/byte_buffer/error.rs
Normal file
4
src/client/connection/input_stream/byte_buffer/error.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub enum Error {
|
||||
Overflow,
|
||||
Stream,
|
||||
}
|
||||
8
src/client/error.rs
Normal file
8
src/client/error.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
pub enum Error {
|
||||
Close,
|
||||
Connect,
|
||||
Input,
|
||||
Output,
|
||||
Response,
|
||||
BufferOverflow,
|
||||
}
|
||||
42
src/client/response.rs
Normal file
42
src/client/response.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
pub mod body;
|
||||
pub mod error;
|
||||
pub mod header;
|
||||
|
||||
pub use body::Body;
|
||||
pub use error::Error;
|
||||
pub use header::Header;
|
||||
|
||||
pub struct Response {
|
||||
header: Header,
|
||||
body: Body,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
/// Create new `client::Response`
|
||||
pub fn new(header: Header, body: Body) -> Self {
|
||||
Self { header, body }
|
||||
}
|
||||
|
||||
/// Create new `client::Response` from UTF-8 buffer
|
||||
pub fn from_utf8(buffer: &[u8]) -> Result<Self, Error> {
|
||||
let header = match Header::from_response(buffer) {
|
||||
Ok(result) => result,
|
||||
Err(_) => return Err(Error::Header),
|
||||
};
|
||||
|
||||
let body = match Body::from_response(buffer) {
|
||||
Ok(result) => result,
|
||||
Err(_) => return Err(Error::Body),
|
||||
};
|
||||
|
||||
Ok(Self::new(header, body))
|
||||
}
|
||||
|
||||
pub fn header(&self) -> &Header {
|
||||
&self.header
|
||||
}
|
||||
|
||||
pub fn body(&self) -> &Body {
|
||||
&self.body
|
||||
}
|
||||
}
|
||||
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,
|
||||
}
|
||||
23
src/client/socket.rs
Normal file
23
src/client/socket.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
use gio::{prelude::SocketClientExt, SocketClient, SocketProtocol, TlsCertificateFlags};
|
||||
|
||||
pub struct Socket {
|
||||
gobject: SocketClient,
|
||||
}
|
||||
|
||||
impl Socket {
|
||||
/// Create new `gio::SocketClient` preset for Gemini Protocol
|
||||
pub fn new() -> Self {
|
||||
let gobject = SocketClient::new();
|
||||
|
||||
gobject.set_protocol(SocketProtocol::Tcp);
|
||||
gobject.set_tls_validation_flags(TlsCertificateFlags::INSECURE);
|
||||
gobject.set_tls(true);
|
||||
|
||||
Self { gobject }
|
||||
}
|
||||
|
||||
/// Return ref to `gio::SocketClient` GObject
|
||||
pub fn gobject(&self) -> &SocketClient {
|
||||
self.gobject.as_ref()
|
||||
}
|
||||
}
|
||||
15
src/lib.rs
15
src/lib.rs
|
|
@ -1,14 +1 @@
|
|||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
pub mod client;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue