Skip to content

Commit

Permalink
Merge pull request #260 from pyfisch/rheaders
Browse files Browse the repository at this point in the history
refactor(headers): export all headers and utils directly under header
  • Loading branch information
reem committed Jan 21, 2015
2 parents bd97e65 + 8d0e5bc commit b512e23
Show file tree
Hide file tree
Showing 37 changed files with 224 additions and 273 deletions.
2 changes: 1 addition & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::util::copy;
use std::io::net::ip::Ipv4Addr;

use hyper::{Get, Post};
use hyper::header::common::ContentLength;
use hyper::header::ContentLength;
use hyper::server::{Server, Request, Response};
use hyper::uri::RequestUri::AbsolutePath;

Expand Down
4 changes: 2 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use url::UrlParser;
use url::ParseError as UrlError;

use header::{Headers, Header, HeaderFormat};
use header::common::{ContentLength, Location};
use header::{ContentLength, Location};
use method::Method;
use net::{NetworkConnector, HttpConnector, ContextVerifier};
use status::StatusClass::Redirection;
Expand Down Expand Up @@ -353,7 +353,7 @@ fn get_host_and_port(url: &Url) -> HttpResult<(String, Port)> {

#[cfg(test)]
mod tests {
use header::common::Server;
use header::Server;
use super::{Client, RedirectPolicy};
use url::Url;

Expand Down
14 changes: 7 additions & 7 deletions src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use url::Url;

use method::{self, Method};
use header::Headers;
use header::common::{self, Host};
use header::{self, Host};
use net::{NetworkStream, NetworkConnector, HttpConnector, Fresh, Streaming};
use http::{HttpWriter, LINE_ENDING};
use http::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Request<Fresh> {
let mut chunked = true;
let mut len = 0;

match self.headers.get::<common::ContentLength>() {
match self.headers.get::<header::ContentLength>() {
Some(cl) => {
chunked = false;
len = **cl;
Expand All @@ -105,18 +105,18 @@ impl Request<Fresh> {

// cant do in match above, thanks borrowck
if chunked {
let encodings = match self.headers.get_mut::<common::TransferEncoding>() {
Some(&mut common::TransferEncoding(ref mut encodings)) => {
let encodings = match self.headers.get_mut::<header::TransferEncoding>() {
Some(&mut header::TransferEncoding(ref mut encodings)) => {
//TODO: check if chunked is already in encodings. use HashSet?
encodings.push(common::transfer_encoding::Encoding::Chunked);
encodings.push(header::Encoding::Chunked);
false
},
None => true
};

if encodings {
self.headers.set::<common::TransferEncoding>(
common::TransferEncoding(vec![common::transfer_encoding::Encoding::Chunked]))
self.headers.set::<header::TransferEncoding>(
header::TransferEncoding(vec![header::Encoding::Chunked]))
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/client/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::num::FromPrimitive;
use std::io::{BufferedReader, IoResult};

use header;
use header::common::{ContentLength, TransferEncoding};
use header::common::transfer_encoding::Encoding::Chunked;
use header::{ContentLength, TransferEncoding};
use header::Encoding::Chunked;
use net::{NetworkStream, HttpStream};
use http::{read_status_line, HttpReader, RawStatus};
use http::HttpReader::{SizedReader, ChunkedReader, EofReader};
Expand Down Expand Up @@ -100,8 +100,8 @@ mod tests {
use std::io::BufferedReader;

use header::Headers;
use header::common::TransferEncoding;
use header::common::transfer_encoding::Encoding;
use header::TransferEncoding;
use header::Encoding;
use http::HttpReader::EofReader;
use http::RawStatus;
use mock::MockStream;
Expand Down
18 changes: 9 additions & 9 deletions src/header/common/accept.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use header;
use header::shared;
use header::parsing;

use mime;

Expand All @@ -15,8 +15,8 @@ use mime;
///
/// ```
/// # use hyper::header::Headers;
/// # use hyper::header::common::Accept;
/// # use hyper::header::shared::qitem;
/// # use hyper::header::Accept;
/// # use hyper::header::qitem;
/// use hyper::mime::Mime;
/// use hyper::mime::TopLevel::Text;
/// use hyper::mime::SubLevel::{Html, Xml};
Expand All @@ -26,9 +26,9 @@ use mime;
/// qitem(Mime(Text, Xml, vec![])) ]));
/// ```
#[derive(Clone, PartialEq, Show)]
pub struct Accept(pub Vec<shared::QualityItem<mime::Mime>>);
pub struct Accept(pub Vec<header::QualityItem<mime::Mime>>);

deref!(Accept => Vec<shared::QualityItem<mime::Mime>>);
deref!(Accept => Vec<header::QualityItem<mime::Mime>>);

impl header::Header for Accept {
fn header_name(_: Option<Accept>) -> &'static str {
Expand All @@ -37,13 +37,13 @@ impl header::Header for Accept {

fn parse_header(raw: &[Vec<u8>]) -> Option<Accept> {
// TODO: Return */* if no value is given.
shared::from_comma_delimited(raw).map(Accept)
parsing::from_comma_delimited(raw).map(Accept)
}
}

impl header::HeaderFormat for Accept {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
shared::fmt_comma_delimited(fmt, &self[])
parsing::fmt_comma_delimited(fmt, &self[])
}
}

Expand All @@ -53,7 +53,7 @@ bench_header!(bench, Accept, { vec![b"text/plain; q=0.5, text/html".to_vec()] })
fn test_parse_header_no_quality() {
let a: Accept = header::Header::parse_header([b"text/plain; charset=utf-8".to_vec()].as_slice()).unwrap();
let b = Accept(vec![
shared::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 1f32},
header::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 1f32},
]);
assert_eq!(a, b);
}
Expand All @@ -62,7 +62,7 @@ fn test_parse_header_no_quality() {
fn test_parse_header_with_quality() {
let a: Accept = header::Header::parse_header([b"text/plain; charset=utf-8; q=0.5".to_vec()].as_slice()).unwrap();
let b = Accept(vec![
shared::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 0.5f32},
header::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 0.5f32},
]);
assert_eq!(a, b);
}
10 changes: 5 additions & 5 deletions src/header/common/accept_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use header::{self, shared};
use header::{self, Encoding, QualityItem};

/// The `Accept-Encoding` header
///
/// The `Accept-Encoding` header can be used by clients to indicate what
/// response encodings they accept.
#[derive(Clone, PartialEq, Show)]
pub struct AcceptEncoding(pub Vec<shared::QualityItem<shared::Encoding>>);
pub struct AcceptEncoding(pub Vec<QualityItem<Encoding>>);

impl_list_header!(AcceptEncoding,
"Accept-Encoding",
Vec<shared::QualityItem<shared::Encoding>>);
Vec<QualityItem<Encoding>>);

#[test]
fn test_parse_header() {
let a: AcceptEncoding = header::Header::parse_header([b"gzip;q=1.0, identity; q=0.5".to_vec()].as_slice()).unwrap();
let b = AcceptEncoding(vec![
shared::QualityItem{item: shared::Gzip, quality: 1f32},
shared::QualityItem{item: shared::Identity, quality: 0.5f32},
QualityItem{item: Encoding::Gzip, quality: 1f32},
QualityItem{item: Encoding::Identity, quality: 0.5f32},
]);
assert_eq!(a, b);
}
17 changes: 12 additions & 5 deletions src/header/common/access_control/allow_headers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use std::fmt::{self};

use header;
use header::shared;

#[derive(Clone)]
struct AccessControlAllowHeaders(pub Vec<String>);
/// The `Access-Control-Allow-Headers` response header,
/// part of [CORS](http://www.w3.org/TR/cors/).
///
/// > The `Access-Control-Allow-Headers` header indicates, as part of the
/// > response to a preflight request, which header field names can be used
/// > during the actual request.
///
/// Spec: www.w3.org/TR/cors/#access-control-allow-headers-response-header
#[derive(Clone, PartialEq, Show)]
pub struct AccessControlAllowHeaders(pub Vec<String>);

impl header::Header for AccessControlAllowHeaders {
#[inline]
Expand All @@ -13,13 +20,13 @@ impl header::Header for AccessControlAllowHeaders {
}

fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowHeaders> {
shared::from_comma_delimited(raw).map(AccessControlAllowHeaders)
header::parsing::from_comma_delimited(raw).map(AccessControlAllowHeaders)
}
}

impl header::HeaderFormat for AccessControlAllowHeaders {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
let AccessControlAllowHeaders(ref parts) = *self;
shared::fmt_comma_delimited(f, parts.as_slice())
header::parsing::fmt_comma_delimited(f, parts.as_slice())
}
}
17 changes: 12 additions & 5 deletions src/header/common/access_control/allow_methods.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::fmt::{self};

use header;
use header::shared;
use method;

#[derive(Clone)]
struct AccessControlAllowMethods(pub Vec<method::Method>);
/// The `Access-Control-Allow-Methods` response header,
/// part of [CORS](http://www.w3.org/TR/cors/).
///
/// > The `Access-Control-Allow-Methods` header indicates, as part of the
/// > response to a preflight request, which methods can be used during the
/// > actual request.
///
/// Spec: www.w3.org/TR/cors/#access-control-allow-methods-response-header
#[derive(Clone, PartialEq, Show)]
pub struct AccessControlAllowMethods(pub Vec<method::Method>);

impl header::Header for AccessControlAllowMethods {
#[inline]
Expand All @@ -14,13 +21,13 @@ impl header::Header for AccessControlAllowMethods {
}

fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowMethods> {
shared::from_comma_delimited(raw).map(AccessControlAllowMethods)
header::parsing::from_comma_delimited(raw).map(AccessControlAllowMethods)
}
}

impl header::HeaderFormat for AccessControlAllowMethods {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
let AccessControlAllowMethods(ref parts) = *self;
shared::fmt_comma_delimited(f, parts.as_slice())
header::parsing::fmt_comma_delimited(f, parts.as_slice())
}
}
14 changes: 12 additions & 2 deletions src/header/common/access_control/allow_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ use std::str;

use header;

#[derive(Clone)]
enum AccessControlAllowOrigin {
/// The `Access-Control-Allow-Origin` response header,
/// part of [CORS](http://www.w3.org/TR/cors/).
///
/// > The `Access-Control-Allow-Origin` header indicates whether a resource
/// > can be shared based by returning the value of the Origin request header,
/// > "*", or "null" in the response.
///
/// Spec: www.w3.org/TR/cors/#access-control-allow-origin-response-header
#[derive(Clone, PartialEq, Show)]
pub enum AccessControlAllowOrigin {
/// Allow all origins
AllowStar,
/// Allow one particular origin
AllowOrigin(url::Url),
}

Expand Down
14 changes: 10 additions & 4 deletions src/header/common/access_control/max_age.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::fmt;

use header;
use header::shared;

#[derive(Clone)]
struct AccessControlMaxAge(pub u32);
/// The `Access-Control-Max-Age` response header,
/// part of [CORS](http://www.w3.org/TR/cors/).
///
/// > The `Access-Control-Max-Age` header indicates how long the results of a
/// > preflight request can be cached in a preflight result cache.
///
/// Spec: www.w3.org/TR/cors/#access-control-max-age-response-header
#[derive(Clone, Copy, PartialEq, Show)]
pub struct AccessControlMaxAge(pub u32);

impl header::Header for AccessControlMaxAge {
#[inline]
Expand All @@ -13,7 +19,7 @@ impl header::Header for AccessControlMaxAge {
}

fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlMaxAge> {
shared::from_one_raw_str(raw).map(AccessControlMaxAge)
header::parsing::from_one_raw_str(raw).map(AccessControlMaxAge)
}
}

Expand Down
30 changes: 13 additions & 17 deletions src/header/common/access_control/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/// Exposes the AccessControlAllowHeaders header
pub mod allow_headers;

/// Exposes the AccessControlAllowMethods header
pub mod allow_methods;

/// Exposes the AccessControlAllowOrigin header
pub mod allow_origin;

/// Exposes the AccessControlMaxAge header
pub mod max_age;

/// Exposes the AccessControlRequestHeaders header
pub mod request_headers;

/// Exposes the AccessControlRequestMethod header
pub mod request_method;
pub use self::allow_headers::AccessControlAllowHeaders;
pub use self::allow_methods::AccessControlAllowMethods;
pub use self::allow_origin::AccessControlAllowOrigin;
pub use self::max_age::AccessControlMaxAge;
pub use self::request_headers::AccessControlRequestHeaders;
pub use self::request_method::AccessControlRequestMethod;

mod allow_headers;
mod allow_methods;
mod allow_origin;
mod max_age;
mod request_headers;
mod request_method;
16 changes: 11 additions & 5 deletions src/header/common/access_control/request_headers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::fmt::{self};

use header;
use header::shared;

#[derive(Clone)]
struct AccessControlRequestHeaders(pub Vec<String>);
/// The `Access-Control-Request-Headers` request header,
/// part of [CORS](http://www.w3.org/TR/cors/).
///
/// > The `Access-Control-Request-Headers` header indicates which headers will
/// > be used in the actual request as part of the preflight request.
///
/// Spec: www.w3.org/TR/cors/#access-control-request-headers-request-header
#[derive(Clone, PartialEq, Show)]
pub struct AccessControlRequestHeaders(pub Vec<String>);

impl header::Header for AccessControlRequestHeaders {
#[inline]
Expand All @@ -13,13 +19,13 @@ impl header::Header for AccessControlRequestHeaders {
}

fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlRequestHeaders> {
shared::from_comma_delimited(raw).map(AccessControlRequestHeaders)
header::parsing::from_comma_delimited(raw).map(AccessControlRequestHeaders)
}
}

impl header::HeaderFormat for AccessControlRequestHeaders {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
let AccessControlRequestHeaders(ref parts) = *self;
shared::fmt_comma_delimited(f, parts.as_slice())
header::parsing::fmt_comma_delimited(f, parts.as_slice())
}
}
Loading

0 comments on commit b512e23

Please sign in to comment.