origin.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2016 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. use host::Host;
  9. use idna::domain_to_unicode;
  10. use parser::default_port;
  11. use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
  12. use Url;
  13. pub fn url_origin(url: &Url) -> Origin {
  14. let scheme = url.scheme();
  15. match scheme {
  16. "blob" => {
  17. let result = Url::parse(url.path());
  18. match result {
  19. Ok(ref url) => url_origin(url),
  20. Err(_) => Origin::new_opaque()
  21. }
  22. },
  23. "ftp" | "gopher" | "http" | "https" | "ws" | "wss" => {
  24. Origin::Tuple(scheme.to_owned(), url.host().unwrap().to_owned(),
  25. url.port_or_known_default().unwrap())
  26. },
  27. // TODO: Figure out what to do if the scheme is a file
  28. "file" => Origin::new_opaque(),
  29. _ => Origin::new_opaque()
  30. }
  31. }
  32. /// The origin of an URL
  33. #[derive(PartialEq, Eq, Clone, Debug)]
  34. #[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
  35. pub enum Origin {
  36. /// A globally unique identifier
  37. Opaque(OpaqueOrigin),
  38. /// Consists of the URL's scheme, host and port
  39. Tuple(String, Host<String>, u16)
  40. }
  41. impl Origin {
  42. /// Creates a new opaque origin that is only equal to itself.
  43. pub fn new_opaque() -> Origin {
  44. static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
  45. Origin::Opaque(OpaqueOrigin(COUNTER.fetch_add(1, Ordering::SeqCst)))
  46. }
  47. /// Return whether this origin is a (scheme, host, port) tuple
  48. /// (as opposed to an opaque origin).
  49. pub fn is_tuple(&self) -> bool {
  50. matches!(*self, Origin::Tuple(..))
  51. }
  52. /// https://html.spec.whatwg.org/multipage/#ascii-serialisation-of-an-origin
  53. pub fn ascii_serialization(&self) -> String {
  54. match *self {
  55. Origin::Opaque(_) => "null".to_owned(),
  56. Origin::Tuple(ref scheme, ref host, port) => {
  57. if default_port(scheme) == Some(port) {
  58. format!("{}://{}", scheme, host)
  59. } else {
  60. format!("{}://{}:{}", scheme, host, port)
  61. }
  62. }
  63. }
  64. }
  65. /// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
  66. pub fn unicode_serialization(&self) -> String {
  67. match *self {
  68. Origin::Opaque(_) => "null".to_owned(),
  69. Origin::Tuple(ref scheme, ref host, port) => {
  70. let host = match *host {
  71. Host::Domain(ref domain) => {
  72. let (domain, _errors) = domain_to_unicode(domain);
  73. Host::Domain(domain)
  74. }
  75. _ => host.clone()
  76. };
  77. if default_port(scheme) == Some(port) {
  78. format!("{}://{}", scheme, host)
  79. } else {
  80. format!("{}://{}:{}", scheme, host, port)
  81. }
  82. }
  83. }
  84. }
  85. }
  86. /// Opaque identifier for URLs that have file or other schemes
  87. #[derive(Eq, PartialEq, Clone, Debug)]
  88. #[cfg_attr(feature="heap_size", derive(HeapSizeOf))]
  89. pub struct OpaqueOrigin(usize);