origin.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #[cfg(feature = "heapsize")] use heapsize::HeapSizeOf;
  9. use host::Host;
  10. use idna::domain_to_unicode;
  11. use parser::default_port;
  12. use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
  13. use Url;
  14. pub fn url_origin(url: &Url) -> Origin {
  15. let scheme = url.scheme();
  16. match scheme {
  17. "blob" => {
  18. let result = Url::parse(url.path());
  19. match result {
  20. Ok(ref url) => url_origin(url),
  21. Err(_) => Origin::new_opaque()
  22. }
  23. },
  24. "ftp" | "gopher" | "http" | "https" | "ws" | "wss" => {
  25. Origin::Tuple(scheme.to_owned(), url.host().unwrap().to_owned(),
  26. url.port_or_known_default().unwrap())
  27. },
  28. // TODO: Figure out what to do if the scheme is a file
  29. "file" => Origin::new_opaque(),
  30. _ => Origin::new_opaque()
  31. }
  32. }
  33. /// The origin of an URL
  34. ///
  35. /// Two URLs with the same origin are considered
  36. /// to originate from the same entity and can therefore trust
  37. /// each other.
  38. ///
  39. /// The origin is determined based on the scheme as follows:
  40. ///
  41. /// - If the scheme is "blob" the origin is the origin of the
  42. /// URL contained in the path component. If parsing fails,
  43. /// it is an opaque origin.
  44. /// - If the scheme is "ftp", "gopher", "http", "https", "ws", or "wss",
  45. /// then the origin is a tuple of the scheme, host, and port.
  46. /// - If the scheme is anything else, the origin is opaque, meaning
  47. /// the URL does not have the same origin as any other URL.
  48. ///
  49. /// For more information see https://url.spec.whatwg.org/#origin
  50. #[derive(PartialEq, Eq, Clone, Debug)]
  51. pub enum Origin {
  52. /// A globally unique identifier
  53. Opaque(OpaqueOrigin),
  54. /// Consists of the URL's scheme, host and port
  55. Tuple(String, Host<String>, u16)
  56. }
  57. #[cfg(feature = "heapsize")]
  58. impl HeapSizeOf for Origin {
  59. fn heap_size_of_children(&self) -> usize {
  60. match *self {
  61. Origin::Tuple(ref scheme, ref host, _) => {
  62. scheme.heap_size_of_children() +
  63. host.heap_size_of_children()
  64. },
  65. _ => 0,
  66. }
  67. }
  68. }
  69. impl Origin {
  70. /// Creates a new opaque origin that is only equal to itself.
  71. pub fn new_opaque() -> Origin {
  72. static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
  73. Origin::Opaque(OpaqueOrigin(COUNTER.fetch_add(1, Ordering::SeqCst)))
  74. }
  75. /// Return whether this origin is a (scheme, host, port) tuple
  76. /// (as opposed to an opaque origin).
  77. pub fn is_tuple(&self) -> bool {
  78. matches!(*self, Origin::Tuple(..))
  79. }
  80. /// https://html.spec.whatwg.org/multipage/#ascii-serialisation-of-an-origin
  81. pub fn ascii_serialization(&self) -> String {
  82. match *self {
  83. Origin::Opaque(_) => "null".to_owned(),
  84. Origin::Tuple(ref scheme, ref host, port) => {
  85. if default_port(scheme) == Some(port) {
  86. format!("{}://{}", scheme, host)
  87. } else {
  88. format!("{}://{}:{}", scheme, host, port)
  89. }
  90. }
  91. }
  92. }
  93. /// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
  94. pub fn unicode_serialization(&self) -> String {
  95. match *self {
  96. Origin::Opaque(_) => "null".to_owned(),
  97. Origin::Tuple(ref scheme, ref host, port) => {
  98. let host = match *host {
  99. Host::Domain(ref domain) => {
  100. let (domain, _errors) = domain_to_unicode(domain);
  101. Host::Domain(domain)
  102. }
  103. _ => host.clone()
  104. };
  105. if default_port(scheme) == Some(port) {
  106. format!("{}://{}", scheme, host)
  107. } else {
  108. format!("{}://{}:{}", scheme, host, port)
  109. }
  110. }
  111. }
  112. }
  113. }
  114. /// Opaque identifier for URLs that have file or other schemes
  115. #[derive(Eq, PartialEq, Clone, Debug)]
  116. pub struct OpaqueOrigin(usize);
  117. #[cfg(feature = "heapsize")]
  118. known_heap_size!(0, OpaqueOrigin);