Skip to content

fix hostname resolving on newConnection #384

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ var onSocketMsg = {
};

var newConnection = function() {
var hostname = urlParts.hostname;

if (urlParts.hostname === '0.0.0.0') {
if (window.location.hostname && !!~window.location.protocol.indexOf('http')) {
Copy link
Member

@SpaceK33z SpaceK33z Aug 24, 2016

Choose a reason for hiding this comment

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

Why the check for http here (not saying it is wrong per se)? If it has a valid reason, could you add a comment above? And also please give an example for when the hostname does not exist in the comments.

We get many PR's around the detection of this url, so comments will help other people here.

Copy link
Contributor Author

@svsool svsool Aug 24, 2016

Choose a reason for hiding this comment

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

for example in electron or any other opened local file in browser, protocol === 'file' and hostname = undefined, hence SockJs url resolves to "http:/sockjs-node" that caused a bug SyntaxError: The URL's scheme must be either 'http:' or 'https:'. 'file:' is not allowed, so we need to be sure that protocol contain 'http' part

Copy link
Member

Choose a reason for hiding this comment

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

Could you add that to the code as a comment though? This is not immediately clear, and since there are no tests yet, it's easy for this to get accidentally removed.

hostname = window.location.hostname;
}
}

sock = new SockJS(url.format({
protocol: urlParts.protocol,
auth: urlParts.auth,
hostname: (urlParts.hostname === '0.0.0.0') ? window.location.hostname : urlParts.hostname,
hostname: hostname,
port: urlParts.port,
pathname: urlParts.path == null || urlParts.path === '/' ? "/sockjs-node" : urlParts.path
}));
Expand Down