add bits to deal with Upgrade requests

This commit is contained in:
Sean McArthur
2014-09-24 17:31:56 -07:00
parent 68e2278339
commit 0ab52c9009
9 changed files with 314 additions and 60 deletions

View File

@@ -1,7 +1,7 @@
use header::Header;
use std::fmt::{mod, Show};
use std::fmt;
use std::from_str::FromStr;
use std::str::from_utf8;
use super::{from_comma_delimited, fmt_comma_delimited};
/// The `Transfer-Encoding` header.
///
@@ -28,7 +28,7 @@ pub struct TransferEncoding(pub Vec<Encoding>);
/// # use hyper::header::Headers;
/// # let mut headers = Headers::new();
/// headers.set(TransferEncoding(vec![Gzip, Chunked]));
#[deriving(Clone, PartialEq, Show)]
#[deriving(Clone, PartialEq)]
pub enum Encoding {
/// The `chunked` encoding.
Chunked,
@@ -43,6 +43,18 @@ pub enum Encoding {
EncodingExt(String)
}
impl fmt::Show for Encoding {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Chunked => "chunked",
Gzip => "gzip",
Deflate => "deflate",
Compress => "compress",
EncodingExt(ref s) => s.as_slice()
}.fmt(fmt)
}
}
impl FromStr for Encoding {
fn from_str(s: &str) -> Option<Encoding> {
match s {
@@ -61,31 +73,12 @@ impl Header for TransferEncoding {
}
fn parse_header(raw: &[Vec<u8>]) -> Option<TransferEncoding> {
if raw.len() != 1 {
return None;
}
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
match from_utf8(unsafe { raw.as_slice().unsafe_get(0).as_slice() }) {
Some(s) => {
Some(TransferEncoding(s.as_slice()
.split([',', ' '].as_slice())
.filter_map(from_str)
.collect()))
}
None => None
}
from_comma_delimited(raw).map(|vec| TransferEncoding(vec))
}
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let TransferEncoding(ref parts) = *self;
let last = parts.len() - 1;
for (i, part) in parts.iter().enumerate() {
try!(part.fmt(fmt));
if i < last {
try!(", ".fmt(fmt));
}
}
Ok(())
fmt_comma_delimited(fmt, parts[])
}
}