Skip to content

Commit

Permalink
sketching
Browse files Browse the repository at this point in the history
  • Loading branch information
softprops committed Jan 2, 2016
0 parents commit ee0afaa
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
target
Cargo.lock
9 changes: 9 additions & 0 deletions Cargo.toml
@@ -0,0 +1,9 @@
[package]
name = "hyperlocal"
version = "0.1.0"
authors = ["softprops <d.tangren@gmail.com>"]

[dependencies]
hyper = "0.7"
unix_socket = "0.5"
url = "0.5"
103 changes: 103 additions & 0 deletions src/lib.rs
@@ -0,0 +1,103 @@
extern crate hyper;
extern crate unix_socket;
extern crate url;

use hyper::client::IntoUrl;
use hyper::net::{NetworkConnector};
use std::io::{self, Read, Write};
use std::net::SocketAddr;
use std::time::Duration;
use unix_socket::UnixStream;
use url::{parse_path, Host, Url, SchemeData, RelativeSchemeData};
use url::ParseError as UrlError;

pub struct SocketConnector;

pub struct SocketStream(pub UnixStream);

impl NetworkConnector for SocketConnector {
type Stream = SocketStream;

fn connect(&self, host: &str, _: u16, scheme: &str) -> hyper::Result<SocketStream> {
Ok(try!(match scheme {
"unix" => {
Ok(SocketStream(try!(UnixStream::connect(host))))
},
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput,
"Invalid scheme for Http"))
}
}))
}
}

impl hyper::net::NetworkStream for SocketStream {
#[inline]
fn peer_addr(&mut self) -> io::Result<SocketAddr> {
// self.0.peer_addr()
Err(io::Error::new(io::ErrorKind::InvalidInput, "unix domain sockets do not apply here"))
}

#[inline]
fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.0.set_read_timeout(dur)
}

#[inline]
fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.0.set_write_timeout(dur)
}
}


impl Read for SocketStream {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.read(buf)
}
}

impl Write for SocketStream {
#[inline]
fn write(&mut self, msg: &[u8]) -> std::io::Result<usize> {
self.0.write(msg)
}

#[inline]
fn flush(&mut self) -> std::io::Result<()> {
self.0.flush()
}
}

pub struct DomainUrl<'a> {
socket: &'a str,
path: &'a str
}

impl<'a> DomainUrl<'a> {
pub fn new(socket: &'a str, path: &'a str) -> DomainUrl<'a> {
DomainUrl {
socket: socket, path: path
}
}
}

impl<'a> IntoUrl for DomainUrl<'a> {
fn into_url(self) -> Result<Url, UrlError> {
let (path, query, fragment) = try!(parse_path(self.path));
Ok(Url {
scheme: "unix".to_owned(),
scheme_data: SchemeData::Relative(
RelativeSchemeData {
username: "".to_owned(),
password: None,
host: Host::Domain(self.socket.to_owned()),
port: Some(0),
default_port: None,
path: path
}),
query: query,
fragment: fragment
})
}
}
10 changes: 10 additions & 0 deletions src/main.rs
@@ -0,0 +1,10 @@
extern crate hyper;
extern crate hyperlocal;

use hyperlocal::{DomainUrl, SocketConnector};

fn main() {
let client = hyper::Client::with_connector(hyperlocal::SocketConnector);
let mut res = client.get(DomainUrl::new("/var/run/docker.sock", "info")).send().unwrap();
std::io::copy(&mut res, &mut std::io::stdout()).unwrap();
}

0 comments on commit ee0afaa

Please sign in to comment.