origin.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 crate::host::Host;
  9. use crate::parser::default_port;
  10. use crate::Url;
  11. use idna::domain_to_unicode;
  12. use std::sync::atomic::{AtomicUsize, Ordering};
  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" | "http" | "https" | "ws" | "wss" => Origin::Tuple(
  24. scheme.to_owned(),
  25. 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", "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, Hash, 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. impl Origin {
  58. /// Creates a new opaque origin that is only equal to itself.
  59. pub fn new_opaque() -> Origin {
  60. static COUNTER: AtomicUsize = AtomicUsize::new(0);
  61. Origin::Opaque(OpaqueOrigin(COUNTER.fetch_add(1, Ordering::SeqCst)))
  62. }
  63. /// Return whether this origin is a (scheme, host, port) tuple
  64. /// (as opposed to an opaque origin).
  65. pub fn is_tuple(&self) -> bool {
  66. matches!(*self, Origin::Tuple(..))
  67. }
  68. /// <https://html.spec.whatwg.org/multipage/#ascii-serialisation-of-an-origin>
  69. pub fn ascii_serialization(&self) -> String {
  70. match *self {
  71. Origin::Opaque(_) => "null".to_owned(),
  72. Origin::Tuple(ref scheme, ref host, port) => {
  73. if default_port(scheme) == Some(port) {
  74. format!("{}://{}", scheme, host)
  75. } else {
  76. format!("{}://{}:{}", scheme, host, port)
  77. }
  78. }
  79. }
  80. }
  81. /// <https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin>
  82. pub fn unicode_serialization(&self) -> String {
  83. match *self {
  84. Origin::Opaque(_) => "null".to_owned(),
  85. Origin::Tuple(ref scheme, ref host, port) => {
  86. let host = match *host {
  87. Host::Domain(ref domain) => {
  88. let (domain, _errors) = domain_to_unicode(domain);
  89. Host::Domain(domain)
  90. }
  91. _ => host.clone(),
  92. };
  93. if default_port(scheme) == Some(port) {
  94. format!("{}://{}", scheme, host)
  95. } else {
  96. format!("{}://{}:{}", scheme, host, port)
  97. }
  98. }
  99. }
  100. }
  101. }
  102. /// Opaque identifier for URLs that have file or other schemes
  103. #[derive(Eq, PartialEq, Hash, Clone, Debug)]
  104. pub struct OpaqueOrigin(usize);