-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use socket2::{Domain, Socket, Type}; | ||
use std::net::SocketAddr; | ||
use ureq::Connector; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct BindConnector { | ||
bind_addr: SocketAddr, | ||
} | ||
|
||
impl BindConnector { | ||
pub fn new_bind(bind_addr: SocketAddr) -> Self { | ||
Self { bind_addr } | ||
} | ||
} | ||
|
||
impl Connector for BindConnector { | ||
fn connect(&self, addr: &std::net::SocketAddr) -> std::io::Result<std::net::TcpStream> { | ||
let socket = Socket::new(Domain::for_address(addr.to_owned()), Type::STREAM, None)?; | ||
socket.bind(&self.bind_addr.into())?; | ||
socket.connect(&addr.to_owned().into())?; | ||
Ok(socket.into()) | ||
} | ||
|
||
fn connect_timeout( | ||
&self, | ||
addr: &std::net::SocketAddr, | ||
timeout: std::time::Duration, | ||
) -> std::io::Result<std::net::TcpStream> { | ||
let socket = Socket::new(Domain::for_address(addr.to_owned()), Type::STREAM, None)?; | ||
socket.bind(&self.bind_addr.into())?; | ||
socket.connect_timeout(&addr.to_owned().into(), timeout)?; | ||
Ok(socket.into()) | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let agent = ureq::builder() | ||
.connector(BindConnector::new_bind("127.0.0.1:54321".parse().unwrap())) | ||
.build(); | ||
|
||
let result = agent.get("http://127.0.0.1:8080/").call(); | ||
|
||
match result { | ||
Err(err) => { | ||
println!("{:?}", err); | ||
std::process::exit(1); | ||
} | ||
Ok(response) => { | ||
assert_eq!(response.status(), 200); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::fmt; | ||
use std::io::Result as IoResult; | ||
use std::net::{SocketAddr, TcpStream}; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
/// A custom Connector to override the default TcpStream connector. | ||
pub trait Connector: Send + Sync { | ||
fn connect(&self, addr: &SocketAddr) -> IoResult<TcpStream> { | ||
TcpStream::connect(addr) | ||
} | ||
|
||
fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> IoResult<TcpStream> { | ||
TcpStream::connect_timeout(addr, timeout) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct StdTcpConnector; | ||
|
||
impl Connector for StdTcpConnector {} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct ArcConnector(Arc<dyn Connector>); | ||
|
||
impl<R> From<R> for ArcConnector | ||
where | ||
R: Connector + 'static, | ||
{ | ||
fn from(r: R) -> Self { | ||
Self(Arc::new(r)) | ||
} | ||
} | ||
|
||
impl fmt::Debug for ArcConnector { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "ArcConnector(...)") | ||
} | ||
} | ||
|
||
impl std::ops::Deref for ArcConnector { | ||
type Target = dyn Connector; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.0.as_ref() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters