move rendering of headers to the Headers object
This commit is contained in:
@@ -32,8 +32,8 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut res = req
|
let mut res = req
|
||||||
.start().ok().expect("Error writing Headers.")
|
.start().unwrap()
|
||||||
.send().ok().expect("Error sending Request.");
|
.send().unwrap();
|
||||||
|
|
||||||
println!("Response: {}", res.status);
|
println!("Response: {}", res.status);
|
||||||
println!("{}", res.headers);
|
println!("{}", res.headers);
|
||||||
|
|||||||
@@ -112,7 +112,6 @@ impl Request<Fresh> {
|
|||||||
try_io!(write!(self.body, "{} {} {}", self.method, uri, self.version))
|
try_io!(write!(self.body, "{} {} {}", self.method, uri, self.version))
|
||||||
try_io!(self.body.write(LINE_ENDING));
|
try_io!(self.body.write(LINE_ENDING));
|
||||||
|
|
||||||
debug!("{}", self.headers);
|
|
||||||
|
|
||||||
let mut chunked = true;
|
let mut chunked = true;
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
@@ -142,10 +141,8 @@ impl Request<Fresh> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (name, header) in self.headers.iter() {
|
debug!("headers [\n{}]", self.headers);
|
||||||
try_io!(write!(self.body, "{}: {}", name, header));
|
try_io!(write!(self.body, "{}", self.headers));
|
||||||
try_io!(self.body.write(LINE_ENDING));
|
|
||||||
}
|
|
||||||
|
|
||||||
try_io!(self.body.write(LINE_ENDING));
|
try_io!(self.body.write(LINE_ENDING));
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ use std::sync::RWLock;
|
|||||||
use uany::{UncheckedAnyDowncast, UncheckedAnyMutDowncast};
|
use uany::{UncheckedAnyDowncast, UncheckedAnyMutDowncast};
|
||||||
use typeable::Typeable;
|
use typeable::Typeable;
|
||||||
|
|
||||||
use http::read_header;
|
use http::{read_header, LineEnding};
|
||||||
use {HttpResult};
|
use {HttpResult};
|
||||||
|
|
||||||
/// Common Headers
|
/// Common Headers
|
||||||
@@ -244,11 +244,10 @@ impl Headers {
|
|||||||
|
|
||||||
impl fmt::Show for Headers {
|
impl fmt::Show for Headers {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
try!("Headers {\n".fmt(fmt));
|
|
||||||
for (k, v) in self.iter() {
|
for (k, v) in self.iter() {
|
||||||
try!(write!(fmt, "\t{}: {}\n", k, v));
|
try!(write!(fmt, "{}: {}{}", k, v, LineEnding));
|
||||||
}
|
}
|
||||||
"}".fmt(fmt)
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,7 +348,7 @@ mod tests {
|
|||||||
use mime::{Mime, Text, Plain};
|
use mime::{Mime, Text, Plain};
|
||||||
use super::CaseInsensitive;
|
use super::CaseInsensitive;
|
||||||
use super::{Headers, Header};
|
use super::{Headers, Header};
|
||||||
use super::common::{ContentLength, ContentType, Accept};
|
use super::common::{ContentLength, ContentType, Accept, Host};
|
||||||
|
|
||||||
fn mem(s: &str) -> MemReader {
|
fn mem(s: &str) -> MemReader {
|
||||||
MemReader::new(s.as_bytes().to_vec())
|
MemReader::new(s.as_bytes().to_vec())
|
||||||
@@ -443,5 +442,20 @@ mod tests {
|
|||||||
*headers.get_mut::<ContentLength>().unwrap() = ContentLength(20);
|
*headers.get_mut::<ContentLength>().unwrap() = ContentLength(20);
|
||||||
assert_eq!(*headers.get::<ContentLength>().unwrap(), ContentLength(20));
|
assert_eq!(*headers.get::<ContentLength>().unwrap(), ContentLength(20));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_headers_show() {
|
||||||
|
let mut headers = Headers::new();
|
||||||
|
headers.set(ContentLength(15));
|
||||||
|
headers.set(Host("foo.bar".into_string()));
|
||||||
|
|
||||||
|
let s = headers.to_string();
|
||||||
|
// hashmap's iterators have arbitrary order, so we must sort first
|
||||||
|
let mut pieces = s[].split_str("\r\n").collect::<Vec<&str>>();
|
||||||
|
pieces.sort();
|
||||||
|
let s = pieces.into_iter().rev().collect::<Vec<&str>>().connect("\r\n");
|
||||||
|
assert_eq!(s[], "Host: foo.bar\r\nContent-Length: 15\r\n");
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
src/http.rs
18
src/http.rs
@@ -1,5 +1,6 @@
|
|||||||
//! Pieces pertaining to the HTTP message protocol.
|
//! Pieces pertaining to the HTTP message protocol.
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
use std::fmt;
|
||||||
use std::io::{mod, Reader, IoResult};
|
use std::io::{mod, Reader, IoResult};
|
||||||
use std::u16;
|
use std::u16;
|
||||||
|
|
||||||
@@ -211,6 +212,23 @@ pub const LF: u8 = b'\n';
|
|||||||
pub const STAR: u8 = b'*';
|
pub const STAR: u8 = b'*';
|
||||||
pub const LINE_ENDING: &'static [u8] = &[CR, LF];
|
pub const LINE_ENDING: &'static [u8] = &[CR, LF];
|
||||||
|
|
||||||
|
/// A `Show`able struct to easily write line endings to a formatter.
|
||||||
|
pub struct LineEnding;
|
||||||
|
|
||||||
|
impl fmt::Show for LineEnding {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.write(LINE_ENDING)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Slice<u8> for LineEnding {
|
||||||
|
fn as_slice(&self) -> &[u8] {
|
||||||
|
LINE_ENDING
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Determines if byte is a token char.
|
/// Determines if byte is a token char.
|
||||||
///
|
///
|
||||||
/// > ```notrust
|
/// > ```notrust
|
||||||
|
|||||||
@@ -97,11 +97,9 @@ impl Response<Fresh> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (name, header) in self.headers.iter() {
|
|
||||||
debug!("header {}: {}", name, header);
|
debug!("headers [\n{}]", self.headers);
|
||||||
try!(write!(self.body, "{}: {}", name, header));
|
try!(write!(self.body, "{}", self.headers));
|
||||||
try!(self.body.write(LINE_ENDING));
|
|
||||||
}
|
|
||||||
|
|
||||||
try!(self.body.write(LINE_ENDING));
|
try!(self.body.write(LINE_ENDING));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user