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

Add support for https parameter to force https even on non-443 port #47

Merged
merged 2 commits into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ The parameter can be a string representing the server's hostname, or an object w

Options:
- **hostname**: hostname where Plex Server runs
- **port**: port number Plex Server is listening on (optional, default: 32400)
- **port**: port number Plex Server is listening on (optional, default: `32400`)
- **https**: (optional, default: `false`)
- **username**: plex.tv username (optional / required for PlexHome)
- **password**: plex.tv password (optional / required for PlexHome)
- **token**: plex.tv authentication token (optional)
Expand Down
3 changes: 2 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function PlexAPI(options, deprecatedPort) {

this.hostname = hostname;
this.port = deprecatedPort || opts.port || PLEX_SERVER_PORT;
this.https = opts.https || false;
Copy link
Owner

Choose a reason for hiding this comment

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

this.https = opts.https;

Could that possibly fix the (rare) issue you're describing?

There is a teeny tiny corner case issue which I think is acceptable for the sake of backwards compatibility: if you supply both port: 443 and https: false, it will still use https due to the port number.

Cause then line #197 could be

return this.https === true || (this.port === 443 && this.https === undefined) ? 'https://' : 'http://';

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, that makes more sense! I'll add another commit. I might break out that logic in to a couple lines because it's becoming a little hard to read for a ternary.

Copy link
Owner

Choose a reason for hiding this comment

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

I might break out that logic in to a couple lines ...

👍

this.username = opts.username;
this.password = opts.password;
this.authToken = opts.token;
Expand Down Expand Up @@ -193,7 +194,7 @@ PlexAPI.prototype._generateRelativeUrl = function _generateRelativeUrl(relativeU
};

PlexAPI.prototype._serverScheme = function _serverScheme() {
return this.port === 443 ? 'https://' : 'http://';
return this.port === 443 || this.https ? 'https://' : 'http://';
};

function filterChildrenByCriterias(children, criterias) {
Expand Down
18 changes: 18 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var expect = require('expect.js');

var PlexAPI = require('..');

describe('_serverScheme', function() {
it('should use http by default', function() {
var api = new PlexAPI({hostname: 'localhost'});
expect(api._serverScheme()).to.equal('http://');
});
it('should use https when port 443 is specified', function() {
var api = new PlexAPI({hostname: 'localhost', port: 443});
expect(api._serverScheme()).to.equal('https://');
});
it('should use https when the https parameter is true', function() {
var api = new PlexAPI({hostname: 'localhost', https: true});
expect(api._serverScheme()).to.equal('https://');
});
});