Skip to content

Commit a342d75

Browse files
committed
Merge pull request chris-morgan#62 from Ogeon/master
Implemented request body reading
2 parents 442c30e + c0eff50 commit a342d75

File tree

16 files changed

+84
-74
lines changed

16 files changed

+84
-74
lines changed

src/codegen/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#[feature(macro_rules)];
44

5+
extern crate collections;
6+
57
use std::io::{File, Truncate, Write};
68
use std::os;
79

src/codegen/status.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// No, I don't mind.
88
// That was easy. :-)
99

10+
use collections::hashmap::HashSet;
1011
use std::ascii::StrAsciiExt;
11-
use std::hashmap::HashSet;
1212
use std::vec;
1313
use std::io::IoResult;
1414
use super::get_writer;
@@ -235,14 +235,6 @@ impl Status {
235235
}
236236
}
237237
238-
impl ToStr for Status {
239-
/// Produce the HTTP status message incorporating both code and message,
240-
/// e.g. `ImATeapot.to_str() == \"418 I'm a teapot\"`
241-
fn to_str(&self) -> ~str {
242-
format!(\"{} {}\", self.code().to_str(), self.reason())
243-
}
244-
}
245-
246238
impl fmt::Show for Status {
247239
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248240
write!(f.buf, \"{} {}\", self.code(), self.reason())

src/examples/server/apache_fake/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#[crate_id = "apache_fake"];
66

77
extern crate extra;
8+
extern crate time;
89
extern crate http;
910

1011
use std::io::net::ip::{SocketAddr, Ipv4Addr};
1112
use std::io::Writer;
12-
use extra::time;
1313

1414
use http::server::{Config, Server, Request, ResponseWriter};
1515
use http::headers;

src/examples/server/hello_world/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#[crate_id = "hello_world"];
44

55
extern crate extra;
6+
extern crate time;
67
extern crate http;
78

89
use std::io::net::ip::{SocketAddr, Ipv4Addr};
910
use std::io::Writer;
10-
use extra::time;
1111

1212
use http::server::{Config, Server, Request, ResponseWriter};
1313
use http::headers::content_type::MediaType;

src/examples/server/info/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#[crate_id = "info"];
55

66
extern crate extra;
7+
extern crate time;
78
extern crate http;
89

910
use std::io::net::ip::{SocketAddr, Ipv4Addr};
1011
use std::io::Writer;
11-
use extra::time;
1212

1313
use http::server::{Config, Server, Request, ResponseWriter};
1414
use http::headers::HeaderEnum;

src/examples/server/request_uri/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#[crate_id = "request_uri"];
88

99
extern crate extra;
10+
extern crate time;
1011
extern crate http;
1112

1213
use std::io::net::ip::{SocketAddr, Ipv4Addr};
1314
use std::io::Writer;
14-
use extra::time;
1515

1616
use http::server::{Config, Server, Request, ResponseWriter};
1717
use http::server::request::{Star, AbsoluteUri, AbsolutePath, Authority};

src/http/headers/connection.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// whether they should be interpreted (I recall its being a header name thing for legacy code,
55
// perhaps I should normalise header case or some such thing?)
66

7+
use std::fmt;
78
use std::io::IoResult;
89
use headers::serialization_utils::normalise_header_name;
910

@@ -14,12 +15,13 @@ pub enum Connection {
1415
Token(~str),
1516
Close,
1617
}
17-
impl ToStr for Connection {
18-
fn to_str(&self) -> ~str {
19-
match *self {
20-
Token(ref s) => s.clone(),
21-
Close => ~"close",
22-
}
18+
19+
impl fmt::Show for Connection {
20+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21+
f.buf.write(match *self {
22+
Token(ref s) => s.as_bytes(),
23+
Close => "close".as_bytes(),
24+
})
2325
}
2426
}
2527

src/http/headers/content_type.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The Content-Type entity header, defined in RFC 2616, Section 14.17.
22
use headers::serialization_utils::{push_parameters, WriterUtil};
33
use std::io::IoResult;
4+
use std::fmt;
45

56
#[deriving(Clone, Eq)]
67
pub struct MediaType {
@@ -17,8 +18,8 @@ pub fn MediaType(type_: ~str, subtype: ~str, parameters: ~[(~str, ~str)]) -> Med
1718
}
1819
}
1920

20-
impl ToStr for MediaType {
21-
fn to_str(&self) -> ~str {
21+
impl fmt::Show for MediaType {
22+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2223
// Idea:
2324
//let s = ~"";
2425
//s.push_token(self.type_);
@@ -27,7 +28,7 @@ impl ToStr for MediaType {
2728
//s.push_parameters(self.parameters);
2829
//s
2930
let s = format!("{}/{}", self.type_, self.subtype);
30-
push_parameters(s, self.parameters)
31+
f.buf.write(push_parameters(s, self.parameters).as_bytes())
3132
}
3233
}
3334

src/http/headers/etag.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use headers::serialization_utils::{push_quoted_string, quoted_string, WriterUtil};
22
use std::io::IoResult;
3+
use std::fmt;
34

45
#[deriving(Clone, Eq)]
56
pub struct EntityTag {
@@ -21,12 +22,12 @@ pub fn strong_etag<S: Str>(opaque_tag: S) -> EntityTag {
2122
}
2223
}
2324

24-
impl ToStr for EntityTag {
25-
fn to_str(&self) -> ~str {
25+
impl fmt::Show for EntityTag {
26+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2627
if self.weak {
27-
push_quoted_string(~"W/", self.opaque_tag)
28+
f.buf.write(push_quoted_string(~"W/", self.opaque_tag).as_bytes())
2829
} else {
29-
quoted_string(self.opaque_tag)
30+
f.buf.write(quoted_string(self.opaque_tag).as_bytes())
3031
}
3132
}
3233
}

src/http/headers/host.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The Host request header, defined in RFC 2616, Section 14.23.
22
33
use std::io::Reader;
4+
use std::fmt;
45

56
/// A simple little thing for the host of a request
67
#[deriving(Clone, Eq)]
@@ -14,11 +15,12 @@ pub struct Host {
1415
/// include the scheme.
1516
port: Option<u16>,
1617
}
17-
impl ToStr for Host {
18-
fn to_str(&self) -> ~str {
18+
19+
impl fmt::Show for Host {
20+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1921
match self.port {
20-
Some(port) => format!("{}:{}", self.name, port.to_str()),
21-
None => self.name.clone(),
22+
Some(port) => write!(f.buf, "{}:{}", self.name, port.to_str()),
23+
None => f.buf.write(self.name.as_bytes()),
2224
}
2325
}
2426
}

src/http/headers/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! unknown headers are stored in a map in the traditional way.
66
77
use std::io::IoResult;
8-
use extra::time::{Tm, strptime};
8+
use time::{Tm, strptime};
99
use extra::url::Url;
1010
use rfc2616::{is_token_item, is_separator, CR, LF, SP, HT, COLON};
1111
use method::Method;
@@ -732,7 +732,7 @@ impl HeaderConvertible for Tm {
732732

733733
#[cfg(test)]
734734
mod test {
735-
use extra::time::Tm;
735+
use time::Tm;
736736
use headers::test_utils::{from_stream_with_str, to_stream_into_str};
737737
use super::HeaderConvertible;
738738

@@ -870,8 +870,10 @@ macro_rules! headers_mod {
870870
//$($attrs;)*
871871
$attr;
872872
873+
#[allow(unused_imports)];
873874
use std::io::IoResult;
874875
use extra;
876+
use time;
875877
use collections::treemap::{TreeMap, Entries};
876878
use headers;
877879
use headers::{HeaderEnum, HeaderConvertible, HeaderValueByteIterator};
@@ -1012,7 +1014,7 @@ headers_mod! {
10121014
// RFC 2616, Section 4.5: General Header Fields
10131015
0, "Cache-Control", "Cache-Control", CacheControl, cache_control, ~str;
10141016
1, "Connection", "Connection", Connection, connection, ~[headers::connection::Connection];
1015-
2, "Date", "Date", Date, date, extra::time::Tm;
1017+
2, "Date", "Date", Date, date, time::Tm;
10161018
3, "Pragma", "Pragma", Pragma, pragma, ~str;
10171019
4, "Trailer", "Trailer", Trailer, trailer, ~str;
10181020
5, "Transfer-Encoding", "Transfer-Encoding", TransferEncoding, transfer_encoding, ~[headers::transfer_encoding::TransferCoding];
@@ -1030,10 +1032,10 @@ headers_mod! {
10301032
15, "From", "From", From, from, ~str;
10311033
16, "Host", "Host", Host, host, headers::host::Host;
10321034
17, "If-Match", "If-Match", IfMatch, if_match, ~str;
1033-
18, "If-Modified-Since", "If-Modified-Since", IfModifiedSince, if_modified_since, extra::time::Tm;
1035+
18, "If-Modified-Since", "If-Modified-Since", IfModifiedSince, if_modified_since, time::Tm;
10341036
19, "If-None-Match", "If-None-Match", IfNoneMatch, if_none_match, ~str;
10351037
20, "If-Range", "If-Range", IfRange, if_range, ~str;
1036-
21, "If-Unmodified-Since", "If-Unmodified-Since", IfUnmodifiedSince, if_unmodified_since, extra::time::Tm;
1038+
21, "If-Unmodified-Since", "If-Unmodified-Since", IfUnmodifiedSince, if_unmodified_since, time::Tm;
10371039
22, "Max-Forwards", "Max-Forwards", MaxForwards, max_forwards, uint;
10381040
23, "Proxy-Authorization", "Proxy-Authorization", ProxyAuthorization, proxy_authorization, ~str;
10391041
24, "Range", "Range", Range, range, ~str;
@@ -1050,8 +1052,8 @@ headers_mod! {
10501052
33, "Content-MD5", "Content-Md5", ContentMd5, content_md5, ~str;
10511053
34, "Content-Range", "Content-Range", ContentRange, content_range, ~str;
10521054
35, "Content-Type", "Content-Type", ContentType, content_type, headers::content_type::MediaType;
1053-
36, "Expires", "Expires", Expires, expires, extra::time::Tm;
1054-
37, "Last-Modified", "Last-Modified", LastModified, last_modified, extra::time::Tm;
1055+
36, "Expires", "Expires", Expires, expires, time::Tm;
1056+
37, "Last-Modified", "Last-Modified", LastModified, last_modified, time::Tm;
10551057
}
10561058

10571059
headers_mod! {
@@ -1063,7 +1065,7 @@ headers_mod! {
10631065
// RFC 2616, Section 4.5: General Header Fields
10641066
0, "Cache-Control", "Cache-Control", CacheControl, cache_control, ~str;
10651067
1, "Connection", "Connection", Connection, connection, ~[headers::connection::Connection];
1066-
2, "Date", "Date", Date, date, extra::time::Tm;
1068+
2, "Date", "Date", Date, date, time::Tm;
10671069
3, "Pragma", "Pragma", Pragma, pragma, ~str;
10681070
4, "Trailer", "Trailer", Trailer, trailer, ~str;
10691071
5, "Transfer-Encoding", "Transfer-Encoding", TransferEncoding, transfer_encoding, ~[headers::transfer_encoding::TransferCoding];
@@ -1093,5 +1095,5 @@ headers_mod! {
10931095
25, "Content-Range", "Content-Range", ContentRange, content_range, ~str;
10941096
26, "Content-Type", "Content-Type", ContentType, content_type, headers::content_type::MediaType;
10951097
27, "Expires", "Expires", Expires, expires, ~str; // TODO: Should be Tm
1096-
28, "Last-Modified", "Last-Modified", LastModified, last_modified, extra::time::Tm;
1098+
28, "Last-Modified", "Last-Modified", LastModified, last_modified, time::Tm;
10971099
}

src/http/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#[macro_escape];
1515

1616
extern crate extra;
17+
extern crate time;
1718
extern crate collections;
1819

1920
pub mod buffer;

src/http/method.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,6 @@ pub enum Method {
1717
ExtensionMethod(~str),
1818
}
1919

20-
impl ToStr for Method {
21-
/// Get the proper name of a method, e.g. `Get.to_str() == ~"GET"`
22-
fn to_str(&self) -> ~str {
23-
match *self {
24-
Options => ~"OPTIONS",
25-
Get => ~"GET",
26-
Head => ~"HEAD",
27-
Post => ~"POST",
28-
Put => ~"PUT",
29-
Delete => ~"DELETE",
30-
Trace => ~"TRACE",
31-
Connect => ~"CONNECT",
32-
Patch => ~"PATCH",
33-
ExtensionMethod(ref s) => (*s).clone(),
34-
}
35-
}
36-
}
37-
3820
impl FromStr for Method {
3921
/**
4022
* Get a *known* `Method` from an *ASCII* string, regardless of case.

src/http/rfc2616.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ pub fn is_separator(o: u8) -> bool {
141141
// see https://www.iana.org/assignments/http-parameters/http-parameters.xml
142142

143143
mod content_coding {
144+
use std::fmt;
145+
144146
/// Content-coding value tokens
145147
pub enum ValueToken {
146148
// An encoding format produced by the file compression program "gzip" (GNU zip) as described
@@ -171,14 +173,14 @@ mod content_coding {
171173
// - "exi": W3C Efficient XML Interchange
172174
// - "pack200-gzip" (Network Transfer Format for Java Archives)
173175
}
174-
impl ToStr for ValueToken {
175-
fn to_str(&self) -> ~str {
176-
match *self {
177-
Gzip => ~"gzip",
178-
Compress => ~"compress",
179-
Deflate => ~"deflate",
180-
Identity => ~"identity",
181-
}
176+
impl fmt::Show for ValueToken {
177+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
178+
f.buf.write(match *self {
179+
Gzip => "gzip".as_bytes(),
180+
Compress => "compress".as_bytes(),
181+
Deflate => "deflate".as_bytes(),
182+
Identity => "identity".as_bytes(),
183+
})
182184
}
183185
}
184186
impl FromStr for ValueToken {
@@ -196,6 +198,8 @@ mod content_coding {
196198
}
197199

198200
mod transfer_coding {
201+
use std::fmt;
202+
199203
/// Transfer-coding value tokens
200204
// Identity is in RFC 2616 but is withdrawn in RFC 2616 errata ID 408
201205
// http://www.rfc-editor.org/errata_search.php?rfc=2616&eid=408
@@ -205,14 +209,14 @@ mod transfer_coding {
205209
Compress, // See above
206210
Deflate, // See above
207211
}
208-
impl ToStr for ValueToken {
209-
fn to_str(&self) -> ~str {
210-
match *self {
211-
Chunked => ~"chunked",
212-
Gzip => ~"gzip",
213-
Compress => ~"compress",
214-
Deflate => ~"deflate",
215-
}
212+
impl fmt::Show for ValueToken {
213+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
214+
f.buf.write(match *self {
215+
Chunked => "chunked".as_bytes(),
216+
Gzip => "gzip".as_bytes(),
217+
Compress => "compress".as_bytes(),
218+
Deflate => "deflate".as_bytes(),
219+
})
216220
}
217221
}
218222
}

src/http/server/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate extra;
33
use std::comm::Chan;
44
use std::io::{Listener, Acceptor};
55
use std::io::net::ip::SocketAddr;
6-
use extra::time::precise_time_ns;
6+
use time::precise_time_ns;
77

88
use std::io::net::tcp::TcpListener;
99

0 commit comments

Comments
 (0)