-- Leo's gemini proxy

-- Connecting to typed-hole.org:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

commit 94df0cfb9269258d36eee5f3a54c1aca3e400a6e

Author: Julien Blanchard <julien@typed-hole.org>

Date: Thu Apr 30 16:26:27 2020 +0200


Use threads in clients, finger accepts two forms of URLs


finger://foo@bar.baz or finger://bar.baz/foo


diff --git a/src/finger/client.rs b/src/finger/client.rs

index 3424dce..ab498b6 100644

--- a/src/finger/client.rs

+++ b/src/finger/client.rs

@@ -1,6 +1,7 @@

use std::io::{Read, Write};

use std::net::TcpStream;

-use std::net::{ToSocketAddrs};

+use std::net::ToSocketAddrs;

+use std::thread;

use std::time::Duration;


use crate::Protocol;

@@ -8,7 +9,6 @@ use crate::Protocol;

pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), String> {

let url = url.get_source_url();

let host = url.host_str().unwrap().to_string();

- let username = url.username();

let urlf = format!("{}:79", host);

let socket = match urlf.to_socket_addrs() {

Ok(mut iter) => iter.next(),

@@ -17,16 +17,24 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin


match socket {

Some(socket) => match TcpStream::connect_timeout(&socket, Duration::new(5, 0)) {

- Ok(mut stream) => {

+ Ok(mut stream) => thread::spawn(move || {

+ let username = if url.username() == "" {

+ url.path().replace("/","")

+ } else {

+ String::from(url.username())

+ };

+

let request = format!("{}\r\n", username);

stream.write_all(request.as_bytes()).unwrap();

let mut res = vec![];

stream.read_to_end(&mut res).unwrap();


Ok((None, res))

- },

+ })

+ .join()

+ .unwrap(),

Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),

},

- None => Err(format!("Could not connect to {}\n", urlf))

+ None => Err(format!("Could not connect to {}\n", urlf)),

}

}

diff --git a/src/gemini/client.rs b/src/gemini/client.rs

index 42501c4..e76b61e 100644

--- a/src/gemini/client.rs

+++ b/src/gemini/client.rs

@@ -1,6 +1,7 @@

-use std::io::{Read, Write};

use native_tls::TlsConnector;

+use std::io::{Read, Write};

use std::net::{TcpStream, ToSocketAddrs};

+use std::thread;

use std::time::Duration;


use crate::protocols::*;

@@ -19,8 +20,8 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin

let der = cert.to_der().unwrap();

let identity = native_tls::Identity::from_pkcs12(&der, "").unwrap();

builder.identity(identity);

- },

- None => ()

+ }

+ None => (),

};


let connector = builder.build().unwrap();

@@ -35,7 +36,7 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin

let mstream = connector.connect(&host, stream);


match mstream {

- Ok(mut stream) => {

+ Ok(mut stream) => thread::spawn(move || {

let url = format!("{}\r\n", url);

stream.write_all(url.as_bytes()).unwrap();

let mut res = vec![];

@@ -45,7 +46,9 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin

let content = res.split_off(clrf_idx.unwrap() + 2);


Ok((Some(res), content))

- }

+ })

+ .join()

+ .unwrap(),

Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),

}

}

diff --git a/src/gopher/client.rs b/src/gopher/client.rs

index 07f4a4c..bc5cf9d 100644

--- a/src/gopher/client.rs

+++ b/src/gopher/client.rs

@@ -1,4 +1,5 @@

use percent_encoding::percent_decode;

+use std::thread;

use std::io::{Read, Write};

use std::net::TcpStream;

use std::net::ToSocketAddrs;

@@ -21,7 +22,7 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin


match socket {

Some(socket) => match TcpStream::connect_timeout(&socket, Duration::new(5, 0)) {

- Ok(mut stream) => {

+ Ok(mut stream) => thread::spawn(move || {

let path = url.path().to_string();


let mut url = match url.query() {

@@ -29,7 +30,8 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin

None => format!("{}\n", path),

};


- let url = if url.starts_with("/0") || url.starts_with("/1") || url.starts_with("/g") {

+ let url = if url.starts_with("/0") || url.starts_with("/1") || url.starts_with("/g")

+ {

url.split_off(2)

} else if url == "/\n" {

String::from("\r\n")

@@ -44,7 +46,9 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin

stream.read_to_end(&mut res).unwrap();


Ok((None, res))

- }

+ })

+ .join()

+ .unwrap(),

Err(e) => Err(format!("Could not connect to {}\n{}", urlf, e)),

},

None => Err(format!("Could not connect to {}\n", urlf)),

diff --git a/src/main.rs b/src/main.rs

index 4876afc..17c4a8b 100644

--- a/src/main.rs

+++ b/src/main.rs

@@ -43,8 +43,8 @@ fn main() {

let content_view = gui.content_view();

let color = gdk::RGBA::from_str(&color).unwrap();

content_view.override_background_color(gtk::StateFlags::NORMAL, Some(&color));

- },

- None => ()

+ }

+ None => (),

}


// Bind back button

@@ -87,7 +87,7 @@ fn main() {

// Visit start URL setting if provided

match settings::start_url() {

Some(url) => route_url(&gui, url),

- None => ()

+ None => (),

}


gui.start();

@@ -343,11 +343,13 @@ fn draw_gemini_content(

let mut end_iter = buffer.get_end_iter();

buffer.insert_markup(

&mut end_iter,

- &format!("<span foreground=\"{}\" font_family=\"{}\">{} {}</span>\n",

- settings::list_color(),

- font_family,

- settings::list_character(),

- item),

+ &format!(

+ "<span foreground=\"{}\" font_family=\"{}\">{} {}</span>\n",

+ settings::list_color(),

+ font_family,

+ settings::list_character(),

+ item

+ ),

);

}

Ok(gemini::parser::TextElement::MonoText(_text)) => {

@@ -359,16 +361,23 @@ fn draw_gemini_content(

true => {

buffer.insert_markup(

&mut end_iter,

- &format!("<span foreground=\"{}\" font_family=\"monospace\">{}</span>\n",

- settings::text_color(),

- text),

+ &format!(

+ "<span foreground=\"{}\" font_family=\"monospace\">{}</span>\n",

+ settings::text_color(),

+ text

+ ),

);

- },

+ }

false => {

- buffer.insert_markup(&mut end_iter, &format!("<span foreground=\"{}\" font_family=\"{}\">{}</span>\n",

- settings::text_color(),

- font_family,

- text));

+ buffer.insert_markup(

+ &mut end_iter,

+ &format!(

+ "<span foreground=\"{}\" font_family=\"{}\">{}</span>\n",

+ settings::text_color(),

+ font_family,

+ text

+ ),

+ );

}

}

}

@@ -400,7 +409,12 @@ fn draw_gopher_content(


buffer.insert_markup(

&mut end_iter,

- &format!("<span foreground=\"{}\" {}>{}</span>\n", settings::text_color(), font_family, text),

+ &format!(

+ "<span foreground=\"{}\" {}>{}</span>\n",

+ settings::text_color(),

+ font_family,

+ text

+ ),

);

}

Ok(gopher::parser::TextElement::LinkItem(link_item)) => {

@@ -437,7 +451,12 @@ fn draw_finger_content(


buffer.insert_markup(

&mut end_iter,

- &format!("<span foreground=\"{}\" {}>{}</span>\n", settings::text_color(), font_family, text),

+ &format!(

+ "<span foreground=\"{}\" {}>{}</span>\n",

+ settings::text_color(),

+ font_family,

+ text

+ ),

);

}

Err(_) => println!("Something failed."),

@@ -573,7 +592,10 @@ fn insert_gopher_file_button(gui: &Arc<Gui>, url: Url, label: String) {

button.set_tooltip_text(Some(&url.to_string()));


button.connect_clicked(move |_| {

- let (_meta, content) = gopher::client::get_data(Gopher {source: url.to_string()}).unwrap();

+ let (_meta, content) = gopher::client::get_data(Gopher {

+ source: url.to_string(),

+ })

+ .unwrap();

client::download(content);

});




---

Served by Pollux Gemini Server.

-- Response ended

-- Page fetched on Mon May 27 20:27:02 2024