@@ -7,12 +7,12 @@ use std::io::net::ip::SocketAddr;
7
7
8
8
use { HttpResult } ;
9
9
use version:: { HttpVersion } ;
10
- use method;
10
+ use method:: Method :: { mod , Get , Head } ;
11
11
use header:: Headers ;
12
- use header:: common:: ContentLength ;
12
+ use header:: common:: { ContentLength , TransferEncoding } ;
13
13
use http:: { read_request_line} ;
14
14
use http:: HttpReader ;
15
- use http:: HttpReader :: { SizedReader , ChunkedReader } ;
15
+ use http:: HttpReader :: { SizedReader , ChunkedReader , EmptyReader } ;
16
16
use uri:: RequestUri ;
17
17
18
18
pub type InternalReader < ' a > = & ' a mut Reader + ' a ;
@@ -22,7 +22,7 @@ pub struct Request<'a> {
22
22
/// The IP address of the remote connection.
23
23
pub remote_addr : SocketAddr ,
24
24
/// The `Method`, such as `Get`, `Post`, etc.
25
- pub method : method :: Method ,
25
+ pub method : Method ,
26
26
/// The headers of the incoming request.
27
27
pub headers : Headers ,
28
28
/// The target request-uri for this request.
@@ -44,14 +44,18 @@ impl<'a> Request<'a> {
44
44
debug ! ( "Headers: [\n {}]" , headers) ;
45
45
46
46
47
- let body = if headers. has :: < ContentLength > ( ) {
47
+ let body = if method == Get || method == Head {
48
+ EmptyReader ( stream)
49
+ } else if headers. has :: < ContentLength > ( ) {
48
50
match headers. get :: < ContentLength > ( ) {
49
51
Some ( & ContentLength ( len) ) => SizedReader ( stream, len) ,
50
52
None => unreachable ! ( )
51
53
}
52
- } else {
54
+ } else if headers . has :: < TransferEncoding > ( ) {
53
55
todo ! ( "check for Transfer-Encoding: chunked" ) ;
54
56
ChunkedReader ( stream, None )
57
+ } else {
58
+ EmptyReader ( stream)
55
59
} ;
56
60
57
61
Ok ( Request {
@@ -71,3 +75,51 @@ impl<'a> Reader for Request<'a> {
71
75
}
72
76
}
73
77
78
+ #[ cfg( test) ]
79
+ mod tests {
80
+ use mock:: MockStream ;
81
+ use super :: Request ;
82
+
83
+ macro_rules! sock(
84
+ ( $s: expr) => ( :: std:: str :: from_str:: <:: std:: io:: net:: ip:: SocketAddr >( $s) . unwrap( ) )
85
+ )
86
+
87
+ #[ test]
88
+ fn test_get_empty_body ( ) {
89
+ let mut stream = MockStream :: with_input ( b"\
90
+ GET / HTTP/1.1\r \n \
91
+ Host: example.domain\r \n \
92
+ \r \n \
93
+ I'm a bad request.\r \n \
94
+ ") ;
95
+
96
+ let mut req = Request :: new ( & mut stream, sock ! ( "127.0.0.1:80" ) ) . unwrap ( ) ;
97
+ assert_eq ! ( req. read_to_string( ) , Ok ( "" . into_string( ) ) ) ;
98
+ }
99
+
100
+ #[ test]
101
+ fn test_head_empty_body ( ) {
102
+ let mut stream = MockStream :: with_input ( b"\
103
+ HEAD / HTTP/1.1\r \n \
104
+ Host: example.domain\r \n \
105
+ \r \n \
106
+ I'm a bad request.\r \n \
107
+ ") ;
108
+
109
+ let mut req = Request :: new ( & mut stream, sock ! ( "127.0.0.1:80" ) ) . unwrap ( ) ;
110
+ assert_eq ! ( req. read_to_string( ) , Ok ( "" . into_string( ) ) ) ;
111
+ }
112
+
113
+ #[ test]
114
+ fn test_post_empty_body ( ) {
115
+ let mut stream = MockStream :: with_input ( b"\
116
+ POST / HTTP/1.1\r \n \
117
+ Host: example.domain\r \n \
118
+ \r \n \
119
+ I'm a bad request.\r \n \
120
+ ") ;
121
+
122
+ let mut req = Request :: new ( & mut stream, sock ! ( "127.0.0.1:80" ) ) . unwrap ( ) ;
123
+ assert_eq ! ( req. read_to_string( ) , Ok ( "" . into_string( ) ) ) ;
124
+ }
125
+ }
0 commit comments