I wrote a flawed implementation of detecting if two urls are on the same domain. I realize this isn’t rocket science, but it seems like there would be a standard lib that has this built-in. My google-fu has failed me. Are there any libraries out there I can require or attribute the cargo-culted code to?
SOP says scheme, port are identical
AND
hosts match, and subdomain(s) is/are a subset of the origin
so
google.com matches google.com
a.google.com matches google.com
Turns out
a.b.google.com matches [b.google.com, google.com] but not [c.b.google.com, a.google.com]
Is only applicable when you can manipulate document.domain which is not the case for me. Direct host matches are required.
From this Wikipedia article it looks like the scheme, host and port have to be the same to satisfy the same origin policy.
http://en.wikipedia.org/wiki/Same_origin_policy
If you use the Domainatrix library I found you could change the code to something like this for your test, but it runs a little slow for me. Another option would be to use this RegEx to find the domain of a url. The RegEx is faster but may not work in all cases. I found the RegEx here, btw.
Remove subdomain from string in ruby