Skip to content

Commit 7ee2a0d

Browse files
committed
Fixed http codec bug, server gives client IP in stream.
1 parent aacd6ab commit 7ee2a0d

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

examples/async-server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ fn main() {
2424
let f = server.incoming()
2525
// we don't wanna save the stream if it drops
2626
.map_err(|InvalidConnection { error, .. }| error)
27-
.for_each(|upgrade| {
27+
.for_each(|(upgrade, addr)| {
28+
println!("Got a connection from: {}", addr);
2829
// check if it has the protocol we want
2930
if !upgrade.protocols().iter().any(|s| s == "rust-websocket") {
3031
// reject it if it doesn't
@@ -41,7 +42,7 @@ fn main() {
4142
// simple echo server impl
4243
.and_then(|s| {
4344
let (sink, stream) = s.split();
44-
stream.filter_map(|m| {
45+
stream.take_while(|m| Ok(!m.is_close())).filter_map(|m| {
4546
println!("Message from Client: {:?}", m);
4647
match m {
4748
OwnedMessage::Ping(p) => Some(OwnedMessage::Pong(p)),

src/client/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl<'u> ClientBuilder<'u> {
631631
message
632632
.ok_or(WebSocketError::ProtocolError(
633633
"Connection closed before handshake could complete."))
634-
.and_then(|message| builder.validate(&message).map(|_| (message, stream)))
634+
.and_then(|message| builder.validate(&message).map(|()| (message, stream)))
635635
})
636636

637637
// output the final client and metadata

src/codec/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Decoder for HttpClientCodec {
5252
// TODO: this is ineffecient, but hyper does not give us a better way to parse
5353
match split_off_http(src) {
5454
Some(buf) => {
55-
let mut reader = BufReader::new(&*src as &[u8]);
55+
let mut reader = BufReader::with_capacity(&*buf as &[u8], buf.len());
5656
let res = match parse_response(&mut reader) {
5757
Err(hyper::Error::Io(ref e)) if e.kind() == io::ErrorKind::UnexpectedEof => {
5858
return Ok(None)

src/server/async.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::io;
22
use std::net::ToSocketAddrs;
3+
use std::net::SocketAddr;
34
use server::{WsServer, NoTlsAcceptor};
45
use tokio_core::net::{TcpListener, TcpStream};
56
use futures::{Stream, Future};
@@ -15,7 +16,8 @@ use tokio_tls::{TlsAcceptorExt, TlsStream};
1516

1617
pub type Server<S> = WsServer<S, TcpListener>;
1718

18-
pub type Incoming<S> = Box<Stream<Item = Upgrade<S>, Error = InvalidConnection<S, BytesMut>>>;
19+
pub type Incoming<S> = Box<Stream<Item = (Upgrade<S>, SocketAddr),
20+
Error = InvalidConnection<S, BytesMut>>>;
1921

2022
pub enum AcceptError<E> {
2123
Io(io::Error),
@@ -43,7 +45,7 @@ impl WsServer<NoTlsAcceptor, TcpListener> {
4345
error: e.into(),
4446
}
4547
})
46-
.and_then(|(stream, _)| {
48+
.and_then(|(stream, a)| {
4749
stream.into_ws()
4850
.map_err(|(stream, req, buf, err)| {
4951
InvalidConnection {
@@ -53,6 +55,7 @@ impl WsServer<NoTlsAcceptor, TcpListener> {
5355
error: err,
5456
}
5557
})
58+
.map(move |u| (u, a))
5659
});
5760
Box::new(future)
5861
}
@@ -85,7 +88,7 @@ impl WsServer<TlsAcceptor, TcpListener> {
8588
error: e.into(),
8689
}
8790
})
88-
.and_then(move |(stream, _)| {
91+
.and_then(move |(stream, a)| {
8992
acceptor.accept_async(stream)
9093
.map_err(|e| {
9194
InvalidConnection {
@@ -96,8 +99,9 @@ impl WsServer<TlsAcceptor, TcpListener> {
9699
error: io::Error::new(io::ErrorKind::Other, e).into(),
97100
}
98101
})
102+
.map(move |s| (s, a))
99103
})
100-
.and_then(|stream| {
104+
.and_then(|(stream, a)| {
101105
stream.into_ws()
102106
.map_err(|(stream, req, buf, err)| {
103107
InvalidConnection {
@@ -107,6 +111,7 @@ impl WsServer<TlsAcceptor, TcpListener> {
107111
error: err,
108112
}
109113
})
114+
.map(move |u| (u, a))
110115
});
111116
Box::new(future)
112117
}

0 commit comments

Comments
 (0)