Skip to content

Commit

Permalink
feat:add local protocol check for web (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
binghaiwang authored and PeterRao committed Jan 23, 2018
1 parent 6fc567e commit b7c5daa
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,20 @@ Client.initOptions = function initOptions(options) {
throw new Error('require accessKeyId, accessKeySecret');
}

var isHttpsProtocol = isHttpsWebProtocol();
var opts = {
region: 'oss-cn-hangzhou',
internal: false,
secure: false,
secure: isHttpsProtocol,
bucket: null,
endpoint: null,
cname: false,
};

for (var key in options) {
if (options[key] === undefined) continue;
if (options[key] === undefined) {
continue;
}
opts[key] = options[key];
}
opts.accessKeyId = opts.accessKeyId.trim();
Expand All @@ -79,7 +82,7 @@ Client.initOptions = function initOptions(options) {
}

if (opts.endpoint) {
opts.endpoint = setEndpoint(opts.endpoint);
opts.endpoint = setEndpoint(opts.endpoint, opts.secure);
} else if (opts.region) {
opts.endpoint = setRegion(
opts.region, opts.internal, opts.secure);
Expand Down Expand Up @@ -521,14 +524,15 @@ function getHeader(headers, name) {
return headers[name] || headers[name.toLowerCase()];
}

function setEndpoint(endpoint) {
function setEndpoint(endpoint, secure) {
var url = urlutil.parse(endpoint);

if (!url.protocol) {
url = urlutil.parse('http://' + endpoint);
var protocol = secure ? 'https://' : 'http://';
url = urlutil.parse(protocol + endpoint);
}

if (url.protocol != 'http:' && url.protocol != 'https:') {
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
throw new Error('Endpoint protocol must be http or https.');
}

Expand All @@ -546,3 +550,8 @@ function setRegion(region, internal, secure) {

return urlutil.parse(protocol + region + suffix);
}

//check local web protocol,if https secure default set true , if http secure default set false
function isHttpsWebProtocol() {
return document && document.location && document.location.protocol === 'https:';
}

0 comments on commit b7c5daa

Please sign in to comment.