Compare commits
No commits in common. "95362fd9e8944788998c2ad139cd2ebaf32f79a9" and "4cf9278cad8d06d3e7250b2c7b03b18941979d0e" have entirely different histories.
95362fd9e8
...
4cf9278cad
7 changed files with 12 additions and 164 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -340,7 +340,6 @@ dependencies = [
|
|||
"mio",
|
||||
"pin-project-lite",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
|
@ -356,17 +355,6 @@ dependencies = [
|
|||
"webpki-roots",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.26.0"
|
||||
|
|
|
@ -10,10 +10,3 @@ tokio = { version = "1.39.2", features = ["io-util", "net"] }
|
|||
tokio-rustls = { version = "0.26.0", default-features = false, features = ["ring"] }
|
||||
url = "2.5.2"
|
||||
webpki-roots = "0.26.3"
|
||||
|
||||
[[example]]
|
||||
name = "main"
|
||||
path = "examples/main.rs"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1.39.2", features = ["macros", "rt-multi-thread", "io-util", "fs"] }
|
||||
|
|
111
examples/main.rs
111
examples/main.rs
|
@ -1,111 +0,0 @@
|
|||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio_rustls::rustls::{
|
||||
client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
|
||||
ClientConfig, SignatureScheme,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), tokio_gemini::LibError> {
|
||||
let mut args = std::env::args();
|
||||
let mut insecure = false;
|
||||
let mut url = "gemini://geminiprotocol.net/".to_owned();
|
||||
if let Some(arg) = args.nth(1) {
|
||||
if arg == "-k" {
|
||||
insecure = true;
|
||||
if let Some(arg) = args.nth(2) {
|
||||
url = arg;
|
||||
}
|
||||
} else {
|
||||
url = arg;
|
||||
}
|
||||
}
|
||||
|
||||
let client = if insecure {
|
||||
tokio_gemini::Client::from(get_insecure_config())
|
||||
} else {
|
||||
tokio_gemini::Client::default()
|
||||
};
|
||||
let mut resp = client.request(&url).await?;
|
||||
{
|
||||
let status_code = resp.status().status_code();
|
||||
let status_num: u8 = status_code.into();
|
||||
eprintln!("{} {:?}", status_num, status_code);
|
||||
}
|
||||
if resp.status().reply_type() == tokio_gemini::ReplyType::Success {
|
||||
let mime = resp.mime()?;
|
||||
eprintln!("Mime: {}", mime);
|
||||
let mut buf = [0u8, 128];
|
||||
let body = resp.body();
|
||||
if mime.type_() == mime::TEXT {
|
||||
loop {
|
||||
let n = body.read(&mut buf).await?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
print!("{}", std::str::from_utf8(&buf[..n])?);
|
||||
}
|
||||
} else {
|
||||
eprintln!("Downloading into content.bin");
|
||||
let mut f = tokio::fs::File::create("content.bin").await?;
|
||||
loop {
|
||||
let n = body.read(&mut buf).await?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
f.write_all(&buf[..n]).await?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
eprintln!("Message: {}", resp.message());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_insecure_config() -> ClientConfig {
|
||||
ClientConfig::builder()
|
||||
.dangerous()
|
||||
.with_custom_certificate_verifier(std::sync::Arc::new(NoCertVerification {}))
|
||||
.with_no_client_auth()
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct NoCertVerification;
|
||||
|
||||
impl ServerCertVerifier for NoCertVerification {
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
end_entity: &tokio_rustls::rustls::pki_types::CertificateDer<'_>,
|
||||
intermediates: &[tokio_rustls::rustls::pki_types::CertificateDer<'_>],
|
||||
server_name: &tokio_rustls::rustls::pki_types::ServerName<'_>,
|
||||
ocsp_response: &[u8],
|
||||
now: tokio_rustls::rustls::pki_types::UnixTime,
|
||||
) -> Result<ServerCertVerified, tokio_rustls::rustls::Error> {
|
||||
Ok(ServerCertVerified::assertion())
|
||||
}
|
||||
|
||||
fn verify_tls12_signature(
|
||||
&self,
|
||||
message: &[u8],
|
||||
cert: &tokio_rustls::rustls::pki_types::CertificateDer<'_>,
|
||||
dss: &tokio_rustls::rustls::DigitallySignedStruct,
|
||||
) -> Result<HandshakeSignatureValid, tokio_rustls::rustls::Error> {
|
||||
Ok(HandshakeSignatureValid::assertion())
|
||||
}
|
||||
|
||||
fn verify_tls13_signature(
|
||||
&self,
|
||||
message: &[u8],
|
||||
cert: &tokio_rustls::rustls::pki_types::CertificateDer<'_>,
|
||||
dss: &tokio_rustls::rustls::DigitallySignedStruct,
|
||||
) -> Result<HandshakeSignatureValid, tokio_rustls::rustls::Error> {
|
||||
Ok(HandshakeSignatureValid::assertion())
|
||||
}
|
||||
|
||||
fn supported_verify_schemes(&self) -> Vec<tokio_rustls::rustls::SignatureScheme> {
|
||||
vec![
|
||||
SignatureScheme::ECDSA_NISTP256_SHA256,
|
||||
SignatureScheme::ECDSA_NISTP384_SHA384,
|
||||
SignatureScheme::ECDSA_NISTP521_SHA512,
|
||||
]
|
||||
}
|
||||
}
|
18
src/error.rs
18
src/error.rs
|
@ -1,11 +1,8 @@
|
|||
#[derive(Debug)]
|
||||
pub enum LibError {
|
||||
IoError(std::io::Error),
|
||||
InvalidUrlError(InvalidUrl),
|
||||
StatusOutOfRange(u8),
|
||||
MessageNotUtf8(std::string::FromUtf8Error),
|
||||
BodyNotUtf8(std::str::Utf8Error),
|
||||
InvalidMime(mime::FromStrError),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for LibError {
|
||||
|
@ -43,21 +40,6 @@ impl From<std::string::FromUtf8Error> for LibError {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<std::str::Utf8Error> for LibError {
|
||||
#[inline]
|
||||
fn from(err: std::str::Utf8Error) -> Self {
|
||||
Self::BodyNotUtf8(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<mime::FromStrError> for LibError {
|
||||
#[inline]
|
||||
fn from(err: mime::FromStrError) -> Self {
|
||||
Self::InvalidMime(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InvalidUrl {
|
||||
ParseError(url::ParseError),
|
||||
SchemeNotGemini,
|
||||
|
|
15
src/lib.rs
15
src/lib.rs
|
@ -1,10 +1,10 @@
|
|||
pub mod error;
|
||||
pub mod response;
|
||||
pub mod status;
|
||||
mod error;
|
||||
mod response;
|
||||
mod status;
|
||||
|
||||
pub use error::*;
|
||||
pub use response::Response;
|
||||
pub use status::*;
|
||||
use error::*;
|
||||
use response::Response;
|
||||
use status::Status;
|
||||
|
||||
use std::net::ToSocketAddrs;
|
||||
use std::sync::Arc;
|
||||
|
@ -79,9 +79,6 @@ impl Client {
|
|||
message.push(buf[0].into());
|
||||
}
|
||||
}
|
||||
if message.last().is_some_and(|c| c == &b'\r') {
|
||||
message.pop();
|
||||
}
|
||||
let message = String::from_utf8(message)?;
|
||||
Ok(Response::new(status, message, stream))
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use crate::{status::Status, LibError};
|
||||
use crate::status::Status;
|
||||
|
||||
type BodyStream = tokio_rustls::client::TlsStream<tokio::net::TcpStream>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Response {
|
||||
status: Status,
|
||||
message: String,
|
||||
|
@ -26,11 +25,11 @@ impl Response {
|
|||
&self.message
|
||||
}
|
||||
|
||||
pub fn mime(self: &Self) -> Result<mime::Mime, LibError> {
|
||||
self.message.parse().map_err(|e| LibError::InvalidMime(e))
|
||||
pub fn mime(self: &Self) -> Result<mime::Mime, mime::FromStrError> {
|
||||
self.message.parse()
|
||||
}
|
||||
|
||||
pub fn body(self: &mut Self) -> &mut BodyStream {
|
||||
&mut self.body
|
||||
pub fn body(self: &Self) -> &BodyStream {
|
||||
&self.body
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::error::LibError;
|
|||
|
||||
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Status {
|
||||
status_code: StatusCode,
|
||||
reply_type: ReplyType,
|
||||
|
|
Loading…
Add table
Reference in a new issue