refactor(headers): Use header!() for CORS headers.
This is the last bunch of headers that should use the new macro. Moved them out of their own folder so that the macro works. Changed them, so that they are more in line with the other headers. BREAKING CHANGE: `AccessControlAllowHeaders` and `AccessControlRequestHeaders` values are case insensitive now. `AccessControlAllowOrigin` variants are now `Any` and `Value` to match the other headers.
This commit is contained in:
		
							
								
								
									
										55
									
								
								src/header/common/access_control_allow_origin.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								src/header/common/access_control_allow_origin.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| use std::fmt::{self}; | ||||
| use std::str; | ||||
|  | ||||
| use url::Url; | ||||
| use header; | ||||
|  | ||||
| /// 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, Debug)] | ||||
| pub enum AccessControlAllowOrigin { | ||||
|     /// Allow all origins | ||||
|     Any, | ||||
|     /// Allow one particular origin | ||||
|     Value(Url), | ||||
| } | ||||
|  | ||||
| impl header::Header for AccessControlAllowOrigin { | ||||
|     fn header_name() -> &'static str { | ||||
|         "Access-Control-Allow-Origin" | ||||
|     } | ||||
|  | ||||
|     fn parse_header(raw: &[Vec<u8>]) -> Option<AccessControlAllowOrigin> { | ||||
|         if raw.len() == 1 { | ||||
|             match str::from_utf8(unsafe { &raw.get_unchecked(0)[..] }) { | ||||
|                 Ok(s) => { | ||||
|                     if s == "*" { | ||||
|                         Some(AccessControlAllowOrigin::Any) | ||||
|                     } else { | ||||
|                         Url::parse(s).ok().map( | ||||
|                             |url| AccessControlAllowOrigin::Value(url)) | ||||
|                     } | ||||
|                 }, | ||||
|                 _ => return None, | ||||
|             } | ||||
|         } else { | ||||
|             return None; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl header::HeaderFormat for AccessControlAllowOrigin { | ||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||
|         match *self { | ||||
|             AccessControlAllowOrigin::Any => write!(f, "*"), | ||||
|             AccessControlAllowOrigin::Value(ref url) => | ||||
|                 write!(f, "{}", url) | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user