소스 검색

Better URL handling

Rain 1 개월 전
부모
커밋
63cc4111b4
4개의 변경된 파일22개의 추가작업 그리고 6개의 파일을 삭제
  1. 1 0
      client/Cargo.toml
  2. 16 5
      client/src/actions.rs
  3. 3 0
      client/src/main.rs
  4. 2 1
      server/src/main.rs

+ 1 - 0
client/Cargo.toml

@@ -10,6 +10,7 @@ logging = { path = "../logging" }
 
 cursive = "0.21"
 cli-clipboard = "0.4.0"
+url = "2.5.8"
 
 # TODO: Write an HTTP client.
 reqwest = { version = "0.12.15", features = [ "blocking" ] }

+ 16 - 5
client/src/actions.rs

@@ -1,7 +1,10 @@
+use std::{str::FromStr, time::Duration};
+
 use cursive::{views::TextArea, Cursive};
+use url::Url;
 use utils::{binary::checksum, hash::Hashable as _, serialize::Serialize as _};
 
-use crate::{get_appdata, message::Message, ui, Appdata};
+use crate::{API_CONNECTION_TIMEOUT, API_DEFAULT_PORT, Appdata, get_appdata, message::Message, ui};
 
 // TODO (low): Create proper user objects. Related: add a way to verify a user.
 pub fn on_user_click(siv: &mut Cursive, user: &str) {
@@ -29,6 +32,7 @@ pub fn on_message_click(siv: &mut Cursive, message_id: &str) {
     }
 }
 
+// TODO: Sending in progress pop-up for slow connections. Currently just hangs the application.
 pub fn on_input_submit(siv: &mut Cursive, input_field_id: &str) {
     let text = siv
         .call_on_name(input_field_id, |view: &mut TextArea| {
@@ -93,15 +97,20 @@ pub enum NetworkError {
 }
 
 fn fix_url(url: &str) -> Result<String, NetworkError> {
-    if url.is_empty() {
-        return Err(NetworkError::InvalidUrl);
-    }
+    let mut url: String = url.to_string();
 
-    let mut url = url.to_string();
     if !url.starts_with("http://") && !url.starts_with("https://") {
         url = format!("http://{}", url);
     }
 
+    let mut url = Url::from_str(&url).map_err(|_| NetworkError::InvalidUrl)?;
+
+    if url.port().is_none() {
+        url.set_port(Some(API_DEFAULT_PORT)).map_err(|_| NetworkError::InvalidUrl)?;
+    }
+
+    let mut url = url.to_string();
+
     if !url.ends_with('/') {
         url.push('/');
     }
@@ -123,6 +132,7 @@ pub fn send_message(siv: &mut Cursive, message: Message) -> Result<(), NetworkEr
     let resp = reqwest::blocking::Client::new()
         .post(format!("{}", url))
         .body(str)
+        .timeout(Duration::from_secs(API_CONNECTION_TIMEOUT))
         .send()
         .map_err(|e| NetworkError::ReqwestError(e))?;
 
@@ -140,6 +150,7 @@ pub fn load_messages(siv: &mut Cursive) -> Result<(), NetworkError> {
 
     let resp = reqwest::blocking::Client::new()
         .get(url)
+        .timeout(Duration::from_secs(API_CONNECTION_TIMEOUT))
         .send()
         .map_err(|e| NetworkError::ReqwestError(e))?;
 

+ 3 - 0
client/src/main.rs

@@ -25,6 +25,9 @@ const DEFAULT_USERNAME_PREFIX: &str = "Myst";
 const DEFAULT_CHANNEL: &str = "Root";
 const DEFAULT_PASSWORD: &str = "null";
 
+const API_CONNECTION_TIMEOUT: u64 = 2;
+const API_DEFAULT_PORT: u16 = 13337;
+
 const INVALID_MESSAGE_IDENT: &str = "ERR";
 
 const LOGO: &str = include_str!("../assets/logo.txt");

+ 2 - 1
server/src/main.rs

@@ -6,6 +6,7 @@ use utils::{
 
 const MAX_MESSAGE_LENGTH: usize = 2048;
 const MESSAGE_HISTORY_LENGTH: usize = 64;
+const DEFAULT_PORT: u16 = 13337;
 
 #[derive(Debug)]
 struct Appdata {
@@ -90,7 +91,7 @@ fn router(state: &mut Appdata, request: Request) -> Response {
 }
 
 fn main() -> http::Result<()> {
-    let port = args().nth(1).unwrap_or("8080".to_string());
+    let port = args().nth(1).unwrap_or(DEFAULT_PORT.to_string());
 
     println!("Starting on port {}", port);