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,45 +1,62 @@
use header::Header;
use std::fmt::{mod, Show};
use super::util::from_one_raw_str;
use super::{from_comma_delimited, fmt_comma_delimited};
use std::from_str::FromStr;
/// The `Connection` header.
///
/// Describes whether the socket connection should be closed or reused after
/// this request/response is completed.
#[deriving(Clone, PartialEq, Show)]
pub enum Connection {
pub struct Connection(Vec<ConnectionOption>);
/// Values that can be in the `Connection` header.
#[deriving(Clone, PartialEq)]
pub enum ConnectionOption {
/// The `keep-alive` connection value.
KeepAlive,
/// The `close` connection value.
Close
Close,
/// Values in the Connection header that are supposed to be names of other Headers.
///
/// > When a header field aside from Connection is used to supply control
/// > information for or about the current connection, the sender MUST list
/// > the corresponding field-name within the Connection header field.
// TODO: it would be nice if these "Strings" could be stronger types, since
// they are supposed to relate to other Header fields (which we have strong
// types for).
ConnectionHeader(String),
}
impl FromStr for Connection {
fn from_str(s: &str) -> Option<Connection> {
debug!("Connection::from_str =? {}", s);
impl FromStr for ConnectionOption {
fn from_str(s: &str) -> Option<ConnectionOption> {
match s {
"keep-alive" => Some(KeepAlive),
"close" => Some(Close),
_ => None
s => Some(ConnectionHeader(s.to_string()))
}
}
}
impl fmt::Show for ConnectionOption {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
KeepAlive => "keep-alive",
Close => "close",
ConnectionHeader(ref s) => s.as_slice()
}.fmt(fmt)
}
}
impl Header for Connection {
fn header_name(_: Option<Connection>) -> &'static str {
"Connection"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Connection> {
from_one_raw_str(raw)
from_comma_delimited(raw).map(|vec| Connection(vec))
}
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
KeepAlive => "keep-alive",
Close => "close",
}.fmt(fmt)
let Connection(ref parts) = *self;
fmt_comma_delimited(fmt, parts[])
}
}

View File

@@ -6,25 +6,21 @@
//! strongly-typed theme, the [mime](http://seanmonstar.github.io/mime.rs) crate
//! is used, such as `ContentType(pub Mime)`.
pub use self::host::Host;
pub use self::content_length::ContentLength;
pub use self::content_type::ContentType;
pub use self::accept::Accept;
pub use self::connection::Connection;
pub use self::content_length::ContentLength;
pub use self::content_type::ContentType;
pub use self::date::Date;
pub use self::host::Host;
pub use self::location::Location;
pub use self::transfer_encoding::TransferEncoding;
pub use self::upgrade::Upgrade;
pub use self::user_agent::UserAgent;
pub use self::server::Server;
pub use self::date::Date;
pub use self::location::Location;
/// Exposes the Host header.
pub mod host;
/// Exposes the ContentLength header.
pub mod content_length;
/// Exposes the ContentType header.
pub mod content_type;
use std::fmt::{mod, Show};
use std::from_str::FromStr;
use std::str::from_utf8;
/// Exposes the Accept header.
pub mod accept;
@@ -32,17 +28,30 @@ pub mod accept;
/// Exposes the Connection header.
pub mod connection;
/// Exposes the TransferEncoding header.
pub mod transfer_encoding;
/// Exposes the ContentLength header.
pub mod content_length;
/// Exposes the UserAgent header.
pub mod user_agent;
/// Exposes the ContentType header.
pub mod content_type;
/// Exposes the Date header.
pub mod date;
/// Exposes the Host header.
pub mod host;
/// Exposes the Server header.
pub mod server;
/// Exposes the Date header.
pub mod date;
/// Exposes the TransferEncoding header.
pub mod transfer_encoding;
/// Exposes the Upgrade header.
pub mod upgrade;
/// Exposes the UserAgent header.
pub mod user_agent;
/// Exposes the Location header.
pub mod location;
@@ -50,3 +59,29 @@ pub mod location;
pub mod util;
fn from_comma_delimited<T: FromStr>(raw: &[Vec<u8>]) -> Option<Vec<T>> {
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(s.as_slice()
.split([',', ' '].as_slice())
.filter_map(from_str)
.collect())
}
None => None
}
}
fn fmt_comma_delimited<T: Show>(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::Result {
let last = parts.len() - 1;
for (i, part) in parts.iter().enumerate() {
try!(part.fmt(fmt));
if i < last {
try!(", ".fmt(fmt));
}
}
Ok(())
}

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[])
}
}

View File

@@ -0,0 +1,51 @@
use header::Header;
use std::fmt::{mod, Show};
use super::{from_comma_delimited, fmt_comma_delimited};
use std::from_str::FromStr;
/// The `Upgrade` header.
#[deriving(Clone, PartialEq, Show)]
pub struct Upgrade(Vec<Protocol>);
/// Protocol values that can appear in the Upgrade header.
#[deriving(Clone, PartialEq)]
pub enum Protocol {
/// The websocket protocol.
WebSocket,
/// Some other less common protocol.
ProtocolExt(String),
}
impl FromStr for Protocol {
fn from_str(s: &str) -> Option<Protocol> {
match s {
"websocket" => Some(WebSocket),
s => Some(ProtocolExt(s.to_string()))
}
}
}
impl fmt::Show for Protocol {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
WebSocket => "websocket",
ProtocolExt(ref s) => s.as_slice()
}.fmt(fmt)
}
}
impl Header for Upgrade {
fn header_name(_: Option<Upgrade>) -> &'static str {
"Upgrade"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Upgrade> {
from_comma_delimited(raw).map(|vec| Upgrade(vec))
}
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let Upgrade(ref parts) = *self;
fmt_comma_delimited(fmt, parts[])
}
}