aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2017-05-09 00:17:37 +0200
committerTeddy Wing2017-05-09 00:26:21 +0200
commit07ef413cff05a8a692288d6226aefc7f5a81d374 (patch)
tree327c99f8f06a9d708791d97621716d3021ce59ca /src
parent3408f710221daece5d170dbd6a0dbfde64af8c39 (diff)
downloadHearURL-07ef413cff05a8a692288d6226aefc7f5a81d374.tar.bz2
open_steam(): Parse URL input
Parse the URL coming in from the TCP stream to check that it's a valid URL. This allows us to fail early and not try to open strings that are not URLs. Leverage the "url" crate to do the parsing. Return a `Box<Error>` from `open_stream()` to enable us to return either an `io` or a `Url` error in our `Result`. Remove the `trim_right()` call on `url` since it's now a `Url` type instead of a `String`, and as a result of parsing to a `Url`, it no longer has the problematic trailing newline.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs
index 60780fc..7956797 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,11 @@
extern crate getopts;
+extern crate url;
use getopts::Options;
+use url::Url;
use std::env;
+use std::error::Error;
use std::io::{self, Write};
use std::io::prelude::*;
use std::net::TcpListener;
@@ -10,7 +13,7 @@ use std::process::Command;
const DEFAULT_PORT: u16 = 37705;
-fn open_stream(browser: String, port: u16) -> io::Result<()> {
+fn open_stream(browser: String, port: u16) -> Result<(), Box<Error>> {
let listener = TcpListener::bind(
format!("127.0.0.1:{}", port)
)?;
@@ -21,13 +24,12 @@ fn open_stream(browser: String, port: u16) -> io::Result<()> {
let mut url = String::new();
stream.read_to_string(&mut url)?;
+ let url = Url::parse(url.as_str())?;
+
Command::new("open")
.arg("-a")
.arg(&browser)
-
- // Trim the trailing newline, otherwise this doesn't
- // work
- .arg(&url.trim_right())
+ .arg(&url.as_str())
.spawn()?;
}
Err(e) => {