1
1
//! HTTP Server
2
- use std:: io:: net:: tcp:: { TcpListener , TcpAcceptor } ;
3
2
use std:: io:: { Acceptor , Listener , IoResult , EndOfFile , IncomingConnections } ;
4
3
use std:: io:: net:: ip:: { IpAddr , Port , SocketAddr } ;
5
4
6
5
pub use self :: request:: Request ;
7
6
pub use self :: response:: Response ;
8
7
8
+ use net:: { NetworkListener , NetworkAcceptor , NetworkStream } ;
9
+ use net:: { HttpListener , HttpAcceptor } ;
10
+
9
11
pub mod request;
10
12
pub mod response;
11
13
12
14
/// A server can listen on a TCP socket.
13
15
///
14
16
/// Once listening, it will create a `Request`/`Response` pair for each
15
17
/// incoming connection, and hand them to the provided handler.
16
- pub struct Server {
18
+ pub struct Server < L = HttpListener > {
17
19
ip : IpAddr ,
18
20
port : Port
19
21
}
20
22
23
+ impl Server < HttpListener > {
24
+ /// Creates a new server that will handle `HttpStream`s.
25
+ pub fn http ( ip : IpAddr , port : Port ) -> Server {
26
+ Server {
27
+ ip : ip,
28
+ port : port
29
+ }
30
+ }
31
+ }
21
32
22
- impl Server {
33
+ impl < L : NetworkListener < S , A > , S : NetworkStream , A : NetworkAcceptor < S > > Server < L > {
23
34
24
- /// Creates a server to be used for `http` conenctions .
25
- pub fn http ( ip : IpAddr , port : Port ) -> Server {
35
+ /// Creates a server that can listen for and handle `NetworkStreams` .
36
+ pub fn new ( ip : IpAddr , port : Port ) -> Server < L > {
26
37
Server {
27
38
ip : ip,
28
39
port : port
29
40
}
30
41
}
31
42
43
+
32
44
/// Binds to a socket, and starts handling connections.
33
- pub fn listen < H : Handler + ' static > ( self , handler : H ) -> IoResult < Listening > {
34
- let mut listener = try!( TcpListener :: bind ( self . ip . to_string ( ) . as_slice ( ) , self . port ) ) ;
45
+ pub fn listen < H : Handler < A , S > + ' static > ( self , handler : H ) -> IoResult < Listening < A > > {
46
+ let mut listener: L = try!( NetworkListener :: bind ( self . ip . to_string ( ) . as_slice ( ) , self . port ) ) ;
35
47
let socket = try!( listener. socket_name ( ) ) ;
36
48
let acceptor = try!( listener. listen ( ) ) ;
37
- let worker = acceptor. clone ( ) ;
49
+ let mut worker = acceptor. clone ( ) ;
38
50
39
51
spawn ( proc ( ) {
40
- let mut acceptor = worker;
41
- handler. handle ( Incoming { from : acceptor. incoming ( ) } ) ;
52
+ handler. handle ( Incoming { from : worker. incoming ( ) } ) ;
42
53
} ) ;
43
54
44
55
Ok ( Listening {
@@ -51,12 +62,12 @@ impl Server {
51
62
52
63
/// An iterator over incoming connections, represented as pairs of
53
64
/// hyper Requests and Responses.
54
- pub struct Incoming < ' a > {
55
- from : IncomingConnections < ' a , TcpAcceptor >
65
+ pub struct Incoming < ' a , A : ' a = HttpAcceptor > {
66
+ from : IncomingConnections < ' a , A >
56
67
}
57
68
58
- impl < ' a > Iterator < ( Request , Response ) > for Incoming < ' a > {
59
- fn next ( & mut self ) -> Option < ( Request , Response ) > {
69
+ impl < ' a , A : NetworkAcceptor < S > , S : NetworkStream > Iterator < ( Request < S > , Response < S > ) > for Incoming < ' a , A > {
70
+ fn next ( & mut self ) -> Option < ( Request < S > , Response < S > ) > {
60
71
for conn in self . from {
61
72
match conn {
62
73
Ok ( stream) => {
@@ -85,30 +96,30 @@ impl<'a> Iterator<(Request, Response)> for Incoming<'a> {
85
96
}
86
97
87
98
/// A listening server, which can later be closed.
88
- pub struct Listening {
89
- acceptor : TcpAcceptor ,
99
+ pub struct Listening < A = HttpAcceptor > {
100
+ acceptor : A ,
90
101
/// The socket address that the server is bound to.
91
102
pub socket_addr : SocketAddr ,
92
103
}
93
104
94
- impl Listening {
105
+ impl < A : NetworkAcceptor < S > , S : NetworkStream > Listening < A > {
95
106
/// Stop the server from listening to it's socket address.
96
107
pub fn close ( mut self ) -> IoResult < ( ) > {
97
108
debug ! ( "closing server" ) ;
98
- self . acceptor . close_accept ( )
109
+ self . acceptor . close ( )
99
110
}
100
111
}
101
112
102
113
/// A handler that can handle incoming requests for a server.
103
- pub trait Handler : Send {
114
+ pub trait Handler < A : NetworkAcceptor < S > , S : NetworkStream > : Send {
104
115
/// Receives a `Request`/`Response` pair, and should perform some action on them.
105
116
///
106
117
/// This could reading from the request, and writing to the response.
107
- fn handle ( self , Incoming ) ;
118
+ fn handle ( self , Incoming < A > ) ;
108
119
}
109
120
110
- impl Handler for fn ( Incoming ) {
111
- fn handle ( self , incoming : Incoming ) {
121
+ impl < A : NetworkAcceptor < S > , S : NetworkStream > Handler < A , S > for fn ( Incoming < A > ) {
122
+ fn handle ( self , incoming : Incoming < A > ) {
112
123
( self ) ( incoming)
113
124
}
114
125
}
0 commit comments