Browse Source

[gopher] add support for binary downloads

tags/0.8.6
Julien Blanchard 1 year ago
parent
commit
0578df055d
6 changed files with 491 additions and 545 deletions
  1. 436
    543
      Cargo.lock
  2. 1
    1
      Cargo.toml
  3. 12
    0
      src/draw.rs
  4. 1
    1
      src/gopher/client.rs
  5. 38
    0
      src/gopher/link.rs
  6. 3
    0
      src/gopher/parser.rs

+ 436
- 543
Cargo.lock
File diff suppressed because it is too large
View File


+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
1 1
 [package]
2 2
 name = "castor"
3
-version = "0.8.5"
3
+version = "0.8.6"
4 4
 authors = ["Julien Blanchard <julien@typed-hole.org>"]
5 5
 edition = "2018"
6 6
 

+ 12
- 0
src/draw.rs View File

@@ -180,6 +180,9 @@ pub fn gopher_content(
180 180
             Ok(crate::gopher::parser::TextElement::Image(link_item)) => {
181 181
                 gopher_link(&gui, link_item);
182 182
             }
183
+            Ok(crate::gopher::parser::TextElement::Binary(link_item)) => {
184
+                gopher_link(&gui, link_item);
185
+            }
183 186
             Err(_) => println!("Something failed."),
184 187
         }
185 188
     }
@@ -291,6 +294,15 @@ pub fn gopher_link(gui: &Arc<Gui>, link_item: String) {
291 294
             let image_label = format!("{} [Image]", button_label);
292 295
             insert_gopher_file_button(&gui, url, image_label);
293 296
         }
297
+        Ok(GopherLink::File(url, label)) => {
298
+            let button_label = if label.is_empty() {
299
+                url.clone().to_string()
300
+            } else {
301
+                label
302
+            };
303
+            let file_label = format!("{} [File]", button_label);
304
+            insert_gopher_file_button(&gui, url, file_label);
305
+        }
294 306
         Ok(GopherLink::Gemini(url, label)) => {
295 307
             insert_button(&gui, url, label);
296 308
         }

+ 1
- 1
src/gopher/client.rs View File

@@ -30,7 +30,7 @@ pub fn get_data<T: Protocol>(url: T) -> Result<(Option<Vec<u8>>, Vec<u8>), Strin
30 30
                     None => format!("{}\r\n", path),
31 31
                 };
32 32
 
33
-                let url = if url.starts_with("/0") || url.starts_with("/1") || url.starts_with("/g")
33
+                let url = if url.starts_with("/0") || url.starts_with("/1") || url.starts_with("/g")  || url.starts_with("/I")  || url.starts_with("/9")
34 34
                 {
35 35
                     url.split_off(2)
36 36
                 } else if url == "/\n" {

+ 38
- 0
src/gopher/link.rs View File

@@ -3,6 +3,7 @@ use url::Url;
3 3
 
4 4
 #[derive(Debug)]
5 5
 pub enum Link {
6
+    File(Url, String),
6 7
     Gemini(Url, String),
7 8
     Gopher(Url, String),
8 9
     Http(Url, String),
@@ -99,6 +100,43 @@ impl FromStr for Link {
99 100
             } else {
100 101
                 Err(ParseError)
101 102
             }
103
+        } else if line.starts_with('9') {
104
+            let label = els.next().expect("no label");
105
+            let path = els.next();
106
+            let host = els.next();
107
+            let port = els.next();
108
+
109
+            if let Some(host) = host {
110
+                if let Some(p) = path {
111
+                    let mut text = String::from(label);
112
+                    let selector = text.remove(0);
113
+
114
+                    let path = if p.starts_with('/') {
115
+                        p.to_string()
116
+                    } else {
117
+                        format!("/{}", p)
118
+                    };
119
+
120
+                    if let Some(port) = port {
121
+                        match Url::parse(&format!(
122
+                            "gopher://{}:{}/{}{}",
123
+                            host, port, selector, path
124
+                        )) {
125
+                            Ok(url) => Ok(Link::File(url, text)),
126
+                            Err(e) => {
127
+                                println!("ERR {:?}", e);
128
+                                Err(ParseError)
129
+                            }
130
+                        }
131
+                    } else {
132
+                        Err(ParseError)
133
+                    }
134
+                } else {
135
+                    Err(ParseError)
136
+                }
137
+            } else {
138
+                Err(ParseError)
139
+            }
102 140
         } else if line.starts_with('[') {
103 141
             let mut url = String::from(line);
104 142
             let url = url.split_off(4);

+ 3
- 0
src/gopher/parser.rs View File

@@ -3,6 +3,7 @@ use std::str::FromStr;
3 3
 
4 4
 #[derive(Debug)]
5 5
 pub enum TextElement {
6
+    Binary(String),
6 7
     ExternalLinkItem(String),
7 8
     LinkItem(String),
8 9
     Image(String),
@@ -32,6 +33,8 @@ impl FromStr for TextElement {
32 33
                 Ok(TextElement::ExternalLinkItem(colors::cleanup(line)))
33 34
             } else if line.starts_with('I') || line.starts_with('g') {
34 35
                 Ok(TextElement::Image(colors::cleanup(line)))
36
+            } else if line.starts_with('9') {
37
+                Ok(TextElement::Binary(String::from(line)))
35 38
             } else if line.contains("://") {
36 39
                 if line.contains("gopher://") {
37 40
                     Ok(TextElement::LinkItem(String::from(line)))

Loading…
Cancel
Save