Basic Cookie and Set-Cookie (only parsing) headers implementation.
This commit is contained in:
		
				
					committed by
					
						 Sean McArthur
						Sean McArthur
					
				
			
			
				
	
			
			
			
						parent
						
							b7957ed36c
						
					
				
				
					commit
					a85cc476e1
				
			
							
								
								
									
										46
									
								
								src/header/common/cookie.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/header/common/cookie.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| use header::Header; | ||||
| use std::fmt::{mod, Show}; | ||||
| use std::str::from_utf8; | ||||
|  | ||||
| /// The `Cookie` header | ||||
| /// | ||||
| /// If the user agent does attach a Cookie header field to an HTTP | ||||
| /// request, the user agent must send the cookie-string | ||||
| /// as the value of the header field. | ||||
| /// | ||||
| /// When the user agent generates an HTTP request, the user agent MUST NOT  | ||||
| /// attach more than one Cookie header field. | ||||
| #[deriving(Clone, PartialEq, Show)] | ||||
| pub struct Cookie(pub Vec<String>); | ||||
|  | ||||
| impl Header for Cookie { | ||||
|     fn header_name(_: Option<Cookie>) -> &'static str { | ||||
|         "Cookie" | ||||
|     } | ||||
|  | ||||
|     fn parse_header(raw: &[Vec<u8>]) -> Option<Cookie> { | ||||
|         let mut cookies: Vec<String> = vec![]; | ||||
|         for cookies_raw in raw.iter() { | ||||
|             match from_utf8(cookies_raw.as_slice()) { | ||||
|                 Some(cookies_str) => { | ||||
|                     for cookie in cookies_str.split(';') { | ||||
|                         cookies.push(cookie.to_string()) | ||||
|                     } | ||||
|                 }, | ||||
|                 None => return None | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|         if !cookies.is_empty() { | ||||
|             Some(Cookie(cookies)) | ||||
|         } else { | ||||
|             None | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||||
|         let Cookie(ref value) = *self; | ||||
|         value.connect("; ").fmt(fmt) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -25,6 +25,11 @@ use std::str::from_utf8; | ||||
|  | ||||
| /// Exposes the Accept header. | ||||
| pub mod accept; | ||||
| /// Exposes the Authorization header. | ||||
| pub mod authorization; | ||||
|  | ||||
| /// Exposes the Cookie header. | ||||
| pub mod cookie; | ||||
|  | ||||
| /// Exposes the Connection header. | ||||
| pub mod connection; | ||||
| @@ -41,9 +46,15 @@ pub mod date; | ||||
| /// Exposes the Host header. | ||||
| pub mod host; | ||||
|  | ||||
| /// Exposes the Location header. | ||||
| pub mod location; | ||||
|  | ||||
| /// Exposes the Server header. | ||||
| pub mod server; | ||||
|  | ||||
| /// Exposes the Set-Cookie header. | ||||
| pub mod set_cookie; | ||||
|  | ||||
| /// Exposes the TransferEncoding header. | ||||
| pub mod transfer_encoding; | ||||
|  | ||||
| @@ -54,11 +65,8 @@ pub mod upgrade; | ||||
| pub mod user_agent; | ||||
|  | ||||
|  | ||||
| /// Exposes the Location header. | ||||
| pub mod location; | ||||
| pub mod util; | ||||
|  | ||||
| /// Exposes the Authorization header. | ||||
| pub mod authorization; | ||||
|  | ||||
| fn from_comma_delimited<T: FromStr>(raw: &[Vec<u8>]) -> Option<Vec<T>> { | ||||
|     if raw.len() != 1 { | ||||
| @@ -86,4 +94,3 @@ fn fmt_comma_delimited<T: Show>(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::R | ||||
|     } | ||||
|     Ok(()) | ||||
| } | ||||
| pub mod util; | ||||
|   | ||||
							
								
								
									
										42
									
								
								src/header/common/set_cookie.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/header/common/set_cookie.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| use header::Header; | ||||
| use std::fmt; | ||||
| use std::str::from_utf8; | ||||
|  | ||||
| /// The `Set-Cookie` header | ||||
| /// | ||||
| /// Informally, the Set-Cookie response header contains the header name | ||||
| /// "Set-Cookie" followed by a ":" and a cookie.  Each cookie begins with | ||||
| /// a name-value-pair, followed by zero or more attribute-value pairs. | ||||
| #[deriving(Clone, PartialEq, Show)] | ||||
| pub struct SetCookie(pub Vec<String>); | ||||
|  | ||||
| impl Header for SetCookie { | ||||
|     fn header_name(_: Option<SetCookie>) -> &'static str { | ||||
|         "Set-Cookie" | ||||
|     } | ||||
|  | ||||
|     fn parse_header(raw: &[Vec<u8>]) -> Option<SetCookie> { | ||||
|         let mut set_cookies: Vec<String> = vec![]; | ||||
|         for set_cookies_raw in raw.iter() { | ||||
|             match from_utf8(set_cookies_raw.as_slice()) { | ||||
|                 Some(set_cookies_str) => { | ||||
|                     if !set_cookies_str.is_empty() { | ||||
|                         set_cookies.push(set_cookies_str.to_string()); | ||||
|                     } | ||||
|                 }, | ||||
|                 None => () | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|         if !set_cookies.is_empty() { | ||||
|             Some(SetCookie(set_cookies)) | ||||
|         } else { | ||||
|             None | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fn fmt_header(&self, _: &mut fmt::Formatter) -> fmt::Result { | ||||
|         unimplemented!() | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user