Skip to content

Commit b512e23

Browse files
committed
Merge pull request #260 from pyfisch/rheaders
refactor(headers): export all headers and utils directly under header
2 parents bd97e65 + 8d0e5bc commit b512e23

37 files changed

+224
-273
lines changed

examples/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::util::copy;
66
use std::io::net::ip::Ipv4Addr;
77

88
use hyper::{Get, Post};
9-
use hyper::header::common::ContentLength;
9+
use hyper::header::ContentLength;
1010
use hyper::server::{Server, Request, Response};
1111
use hyper::uri::RequestUri::AbsolutePath;
1212

src/client/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use url::UrlParser;
2626
use url::ParseError as UrlError;
2727

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

354354
#[cfg(test)]
355355
mod tests {
356-
use header::common::Server;
356+
use header::Server;
357357
use super::{Client, RedirectPolicy};
358358
use url::Url;
359359

src/client/request.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use url::Url;
55

66
use method::{self, Method};
77
use header::Headers;
8-
use header::common::{self, Host};
8+
use header::{self, Host};
99
use net::{NetworkStream, NetworkConnector, HttpConnector, Fresh, Streaming};
1010
use http::{HttpWriter, LINE_ENDING};
1111
use http::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
@@ -95,7 +95,7 @@ impl Request<Fresh> {
9595
let mut chunked = true;
9696
let mut len = 0;
9797

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

106106
// cant do in match above, thanks borrowck
107107
if chunked {
108-
let encodings = match self.headers.get_mut::<common::TransferEncoding>() {
109-
Some(&mut common::TransferEncoding(ref mut encodings)) => {
108+
let encodings = match self.headers.get_mut::<header::TransferEncoding>() {
109+
Some(&mut header::TransferEncoding(ref mut encodings)) => {
110110
//TODO: check if chunked is already in encodings. use HashSet?
111-
encodings.push(common::transfer_encoding::Encoding::Chunked);
111+
encodings.push(header::Encoding::Chunked);
112112
false
113113
},
114114
None => true
115115
};
116116

117117
if encodings {
118-
self.headers.set::<common::TransferEncoding>(
119-
common::TransferEncoding(vec![common::transfer_encoding::Encoding::Chunked]))
118+
self.headers.set::<header::TransferEncoding>(
119+
header::TransferEncoding(vec![header::Encoding::Chunked]))
120120
}
121121
}
122122

src/client/response.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::num::FromPrimitive;
33
use std::io::{BufferedReader, IoResult};
44

55
use header;
6-
use header::common::{ContentLength, TransferEncoding};
7-
use header::common::transfer_encoding::Encoding::Chunked;
6+
use header::{ContentLength, TransferEncoding};
7+
use header::Encoding::Chunked;
88
use net::{NetworkStream, HttpStream};
99
use http::{read_status_line, HttpReader, RawStatus};
1010
use http::HttpReader::{SizedReader, ChunkedReader, EofReader};
@@ -100,8 +100,8 @@ mod tests {
100100
use std::io::BufferedReader;
101101

102102
use header::Headers;
103-
use header::common::TransferEncoding;
104-
use header::common::transfer_encoding::Encoding;
103+
use header::TransferEncoding;
104+
use header::Encoding;
105105
use http::HttpReader::EofReader;
106106
use http::RawStatus;
107107
use mock::MockStream;

src/header/common/accept.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
use header;
4-
use header::shared;
4+
use header::parsing;
55

66
use mime;
77

@@ -15,8 +15,8 @@ use mime;
1515
///
1616
/// ```
1717
/// # use hyper::header::Headers;
18-
/// # use hyper::header::common::Accept;
19-
/// # use hyper::header::shared::qitem;
18+
/// # use hyper::header::Accept;
19+
/// # use hyper::header::qitem;
2020
/// use hyper::mime::Mime;
2121
/// use hyper::mime::TopLevel::Text;
2222
/// use hyper::mime::SubLevel::{Html, Xml};
@@ -26,9 +26,9 @@ use mime;
2626
/// qitem(Mime(Text, Xml, vec![])) ]));
2727
/// ```
2828
#[derive(Clone, PartialEq, Show)]
29-
pub struct Accept(pub Vec<shared::QualityItem<mime::Mime>>);
29+
pub struct Accept(pub Vec<header::QualityItem<mime::Mime>>);
3030

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

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

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

4444
impl header::HeaderFormat for Accept {
4545
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
46-
shared::fmt_comma_delimited(fmt, &self[])
46+
parsing::fmt_comma_delimited(fmt, &self[])
4747
}
4848
}
4949

@@ -53,7 +53,7 @@ bench_header!(bench, Accept, { vec![b"text/plain; q=0.5, text/html".to_vec()] })
5353
fn test_parse_header_no_quality() {
5454
let a: Accept = header::Header::parse_header([b"text/plain; charset=utf-8".to_vec()].as_slice()).unwrap();
5555
let b = Accept(vec![
56-
shared::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 1f32},
56+
header::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 1f32},
5757
]);
5858
assert_eq!(a, b);
5959
}
@@ -62,7 +62,7 @@ fn test_parse_header_no_quality() {
6262
fn test_parse_header_with_quality() {
6363
let a: Accept = header::Header::parse_header([b"text/plain; charset=utf-8; q=0.5".to_vec()].as_slice()).unwrap();
6464
let b = Accept(vec![
65-
shared::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 0.5f32},
65+
header::QualityItem{item: mime::Mime(mime::TopLevel::Text, mime::SubLevel::Plain, vec![(mime::Attr::Charset, mime::Value::Utf8)]), quality: 0.5f32},
6666
]);
6767
assert_eq!(a, b);
6868
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
use header::{self, shared};
1+
use header::{self, Encoding, QualityItem};
22

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

1010
impl_list_header!(AcceptEncoding,
1111
"Accept-Encoding",
12-
Vec<shared::QualityItem<shared::Encoding>>);
12+
Vec<QualityItem<Encoding>>);
1313

1414
#[test]
1515
fn test_parse_header() {
1616
let a: AcceptEncoding = header::Header::parse_header([b"gzip;q=1.0, identity; q=0.5".to_vec()].as_slice()).unwrap();
1717
let b = AcceptEncoding(vec![
18-
shared::QualityItem{item: shared::Gzip, quality: 1f32},
19-
shared::QualityItem{item: shared::Identity, quality: 0.5f32},
18+
QualityItem{item: Encoding::Gzip, quality: 1f32},
19+
QualityItem{item: Encoding::Identity, quality: 0.5f32},
2020
]);
2121
assert_eq!(a, b);
2222
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
use std::fmt::{self};
22

33
use header;
4-
use header::shared;
54

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

916
impl header::Header for AccessControlAllowHeaders {
1017
#[inline]
@@ -13,13 +20,13 @@ impl header::Header for AccessControlAllowHeaders {
1320
}
1421

1522
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowHeaders> {
16-
shared::from_comma_delimited(raw).map(AccessControlAllowHeaders)
23+
header::parsing::from_comma_delimited(raw).map(AccessControlAllowHeaders)
1724
}
1825
}
1926

2027
impl header::HeaderFormat for AccessControlAllowHeaders {
2128
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
2229
let AccessControlAllowHeaders(ref parts) = *self;
23-
shared::fmt_comma_delimited(f, parts.as_slice())
30+
header::parsing::fmt_comma_delimited(f, parts.as_slice())
2431
}
2532
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
use std::fmt::{self};
22

33
use header;
4-
use header::shared;
54
use method;
65

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

1017
impl header::Header for AccessControlAllowMethods {
1118
#[inline]
@@ -14,13 +21,13 @@ impl header::Header for AccessControlAllowMethods {
1421
}
1522

1623
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowMethods> {
17-
shared::from_comma_delimited(raw).map(AccessControlAllowMethods)
24+
header::parsing::from_comma_delimited(raw).map(AccessControlAllowMethods)
1825
}
1926
}
2027

2128
impl header::HeaderFormat for AccessControlAllowMethods {
2229
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
2330
let AccessControlAllowMethods(ref parts) = *self;
24-
shared::fmt_comma_delimited(f, parts.as_slice())
31+
header::parsing::fmt_comma_delimited(f, parts.as_slice())
2532
}
2633
}

src/header/common/access_control/allow_origin.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ use std::str;
55

66
use header;
77

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

src/header/common/access_control/max_age.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
use std::fmt;
22

33
use header;
4-
use header::shared;
54

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

915
impl header::Header for AccessControlMaxAge {
1016
#[inline]
@@ -13,7 +19,7 @@ impl header::Header for AccessControlMaxAge {
1319
}
1420

1521
fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlMaxAge> {
16-
shared::from_one_raw_str(raw).map(AccessControlMaxAge)
22+
header::parsing::from_one_raw_str(raw).map(AccessControlMaxAge)
1723
}
1824
}
1925

0 commit comments

Comments
 (0)