fix(headers): Allow IPv6 Addresses in Host header
This commit is contained in:
		
				
					committed by
					
						 Sean McArthur
						Sean McArthur
					
				
			
			
				
	
			
			
			
						parent
						
							c3c53dda32
						
					
				
				
					commit
					8541ac72d7
				
			| @@ -71,18 +71,28 @@ impl FromStr for Host { | |||||||
|     type Err = ::Error; |     type Err = ::Error; | ||||||
|  |  | ||||||
|     fn from_str(s: &str) -> ::Result<Host> { |     fn from_str(s: &str) -> ::Result<Host> { | ||||||
|         let (host_port, res) = domain_to_unicode(s); |         let idx = s.rfind(':'); | ||||||
|         if res.is_err() { |  | ||||||
|             return Err(::Error::Header) |  | ||||||
|         } |  | ||||||
|         let idx = host_port.rfind(':'); |  | ||||||
|         let port = idx.and_then( |         let port = idx.and_then( | ||||||
|             |idx| s[idx + 1..].parse().ok() |             |idx| s[idx + 1..].parse().ok() | ||||||
|         ); |         ); | ||||||
|         let hostname = match idx { |         let hostname_encoded = match port { | ||||||
|             None => host_port, |             None => s, | ||||||
|             Some(idx) => host_port[..idx].to_owned() |             Some(_) => &s[..idx.unwrap()] | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|  |         let hostname = if hostname_encoded.starts_with("[") { | ||||||
|  |             if !hostname_encoded.ends_with("]") { | ||||||
|  |                 return Err(::Error::Header) | ||||||
|  |             } | ||||||
|  |             hostname_encoded.to_owned() | ||||||
|  |         } else { | ||||||
|  |             let (hostname, res) = domain_to_unicode(hostname_encoded); | ||||||
|  |             if res.is_err() { | ||||||
|  |                 return Err(::Error::Header) | ||||||
|  |             } | ||||||
|  |             hostname | ||||||
|  |         }; | ||||||
|  |  | ||||||
|         Ok(Host { |         Ok(Host { | ||||||
|             hostname: hostname, |             hostname: hostname, | ||||||
|             port: port |             port: port | ||||||
| @@ -110,6 +120,24 @@ mod tests { | |||||||
|             hostname: "foo.com".to_owned(), |             hostname: "foo.com".to_owned(), | ||||||
|             port: Some(8080) |             port: Some(8080) | ||||||
|         })); |         })); | ||||||
|  |  | ||||||
|  |         let host = Header::parse_header([b"foo.com".to_vec()].as_ref()); | ||||||
|  |         assert_eq!(host.ok(), Some(Host { | ||||||
|  |             hostname: "foo.com".to_owned(), | ||||||
|  |             port: None | ||||||
|  |         })); | ||||||
|  |  | ||||||
|  |         let host = Header::parse_header([b"[::1]:8080".to_vec()].as_ref()); | ||||||
|  |         assert_eq!(host.ok(), Some(Host { | ||||||
|  |             hostname: "[::1]".to_owned(), | ||||||
|  |             port: Some(8080) | ||||||
|  |         })); | ||||||
|  |  | ||||||
|  |         let host = Header::parse_header([b"[::1]".to_vec()].as_ref()); | ||||||
|  |         assert_eq!(host.ok(), Some(Host { | ||||||
|  |             hostname: "[::1]".to_owned(), | ||||||
|  |             port: None | ||||||
|  |         })); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user