origin.rs 3.9 KB

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