origin.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #[derive(PartialEq, Eq, Clone, Debug)]
  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. #[cfg(feature = "heapsize")]
  42. impl HeapSizeOf for Origin {
  43. fn heap_size_of_children(&self) -> usize {
  44. match *self {
  45. Origin::Tuple(ref scheme, ref host, _) => {
  46. scheme.heap_size_of_children() +
  47. host.heap_size_of_children()
  48. },
  49. _ => 0,
  50. }
  51. }
  52. }
  53. impl Origin {
  54. /// Creates a new opaque origin that is only equal to itself.
  55. pub fn new_opaque() -> Origin {
  56. static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
  57. Origin::Opaque(OpaqueOrigin(COUNTER.fetch_add(1, Ordering::SeqCst)))
  58. }
  59. /// Return whether this origin is a (scheme, host, port) tuple
  60. /// (as opposed to an opaque origin).
  61. pub fn is_tuple(&self) -> bool {
  62. matches!(*self, Origin::Tuple(..))
  63. }
  64. /// https://html.spec.whatwg.org/multipage/#ascii-serialisation-of-an-origin
  65. pub fn ascii_serialization(&self) -> String {
  66. match *self {
  67. Origin::Opaque(_) => "null".to_owned(),
  68. Origin::Tuple(ref scheme, ref host, port) => {
  69. if default_port(scheme) == Some(port) {
  70. format!("{}://{}", scheme, host)
  71. } else {
  72. format!("{}://{}:{}", scheme, host, port)
  73. }
  74. }
  75. }
  76. }
  77. /// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
  78. pub fn unicode_serialization(&self) -> String {
  79. match *self {
  80. Origin::Opaque(_) => "null".to_owned(),
  81. Origin::Tuple(ref scheme, ref host, port) => {
  82. let host = match *host {
  83. Host::Domain(ref domain) => {
  84. let (domain, _errors) = domain_to_unicode(domain);
  85. Host::Domain(domain)
  86. }
  87. _ => host.clone()
  88. };
  89. if default_port(scheme) == Some(port) {
  90. format!("{}://{}", scheme, host)
  91. } else {
  92. format!("{}://{}:{}", scheme, host, port)
  93. }
  94. }
  95. }
  96. }
  97. }
  98. /// Opaque identifier for URLs that have file or other schemes
  99. #[derive(Eq, PartialEq, Clone, Debug)]
  100. pub struct OpaqueOrigin(usize);
  101. #[cfg(feature = "heapsize")]
  102. known_heap_size!(0, OpaqueOrigin);