From 07ef413cff05a8a692288d6226aefc7f5a81d374 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 9 May 2017 00:17:37 +0200 Subject: 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` 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. --- src/main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src') 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> { 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) => { -- cgit v1.2.3