Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an anchor tag for parsing URLs instead of a regex #1046

Merged
merged 3 commits into from Dec 11, 2014
Merged

Use an anchor tag for parsing URLs instead of a regex #1046

merged 3 commits into from Dec 11, 2014

Conversation

btoews
Copy link

@btoews btoews commented Dec 10, 2014

There are a couple of problems with the regex being used for parsing the Ajax URL and determining if it is same origin:

  • Doesn't allow . in scheme.
  • Doesn't handle leading whitespace.
  • Doesn't handle malformed URLs (https:///google.com).
  • Doesn't enforce that the protocols match.
  • Doesn't terminate the host section on ? or #.

The regex could be updated to better match the URL parsing spec, but it is much simpler to let the browser do the heavy lifting for us.

This PR creates an anchor tag and sets it's href to the Ajax URL. It then compares the protocol and host of this anchor tag with those of window.location.

/cc @mislav

if (!settings.crossDomain) {
urlAnchor = document.createElement('a')
urlAnchor.href = settings.url
settings.crossDomain = window.location.protocol + '//' + window.location.host !== urlAnchor.protocol + '//' + urlAnchor.host
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think location could be used directly instead of window.location, but window.location is used in other parts of this file so I went with that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add parentheses here so that the comparison is more readable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added parens in b91c8aa.

@mislav
Copy link
Collaborator

mislav commented Dec 11, 2014

I see what you did there, sir! 🙈 🙊

mislav added a commit that referenced this pull request Dec 11, 2014
Use an anchor tag for parsing URLs instead of a regex
@mislav mislav merged commit 2c587b0 into madrobby:master Dec 11, 2014
@mislav
Copy link
Collaborator

mislav commented Dec 11, 2014

Thanks!

@btoews
Copy link
Author

btoews commented Dec 11, 2014

Turns out this probably broke IE support. They always add the port to a.host, but not to location.host. If you want to revert the merge I'll fix it tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants