Skip to content

Commit

Permalink
支持签名服务器了,这样子才算比较完整
Browse files Browse the repository at this point in the history
  • Loading branch information
leeight committed May 28, 2015
1 parent 61c6270 commit 1dbe044
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ HttpClient.prototype.sendRequest = function (httpMethod, path, body, headers, pa
headers[H.AUTHORIZATION] = promise;
}
else if (isPromise(promise)) {
return promise.then(function (authorization) {
return promise.then(function (authorization, xbceDate) {
headers[H.AUTHORIZATION] = authorization;
if (xbceDate) {
headers[H.X_BCE_DATE] = xbceDate;
}
return client._doRequest(options, body, outputStream);
});
}
Expand Down
5 changes: 4 additions & 1 deletion test/browser/baidubce-sdk.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5040,8 +5040,11 @@ HttpClient.prototype.sendRequest = function (httpMethod, path, body, headers, pa
headers[H.AUTHORIZATION] = promise;
}
else if (isPromise(promise)) {
return promise.then(function (authorization) {
return promise.then(function (authorization, xbceDate) {
headers[H.AUTHORIZATION] = authorization;
if (xbceDate) {
headers[H.X_BCE_DATE] = xbceDate;
}
return client._doRequest(options, body, outputStream);
});
}
Expand Down
37 changes: 34 additions & 3 deletions test/browser/demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ define(function (require) {
return tasks;
}

function createClient() {
var client = new sdk.BosClient(getBOSConfig());
client.createSignature = function (_, httpMethod, path, params, headers) {
var deferred = sdk.Q.defer();
$.ajax({
// url: 'http://127.0.0.1:1337/ack',
url: $('#g_ss_url').val(),
jsonp: 'callback',
dataType: 'jsonp',
data: {
httpMethod: httpMethod,
path: path,
delay: ~~(Math.random() * 10) + 1,
params: JSON.stringify(params || {}),
headers: JSON.stringify(headers || {})
},
success: function (payload) {
if (payload.statusCode === 200 && payload.signature) {
deferred.resolve(payload.signature, payload.xbceDate);
}
else {
// TODO(leeight) timeout
deferred.reject(new Error('createSignature failed, statusCode = ' + payload.statusCode));
}
}
});
return deferred.promise;
};
return client;
}

function uploadPartFile(state) {
return function (item, callback) {
// item.file
Expand All @@ -71,7 +102,7 @@ define(function (require) {
// item.partNumber
// item.partSize
var blob = item.file.slice(item.start, item.stop + 1);
var client = new sdk.BosClient(getBOSConfig());
var client = createClient();
var key = item.file.name;
var bucket = $('#g_bucket').val();

Expand All @@ -89,7 +120,7 @@ define(function (require) {
function uploadSuperFile(file) {
var startTime = new Date().getTime();

var client = new sdk.BosClient(getBOSConfig());
var client = createClient();
var key = file.name;
var bucket_name = $('#g_bucket').val();

Expand Down Expand Up @@ -153,7 +184,7 @@ define(function (require) {
function uploadSingleFile2(file, opt_startByte, opt_stopByte) {
var startTime = new Date().getTime();

var client = new sdk.BosClient(getBOSConfig());
var client = createClient();
var key = file.name;
var ext = key.split(/\./g).pop();
var bucket = $('#g_bucket').val();
Expand Down
5 changes: 4 additions & 1 deletion test/browser/demo/baidubce-sdk.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5040,8 +5040,11 @@ HttpClient.prototype.sendRequest = function (httpMethod, path, body, headers, pa
headers[H.AUTHORIZATION] = promise;
}
else if (isPromise(promise)) {
return promise.then(function (authorization) {
return promise.then(function (authorization, xbceDate) {
headers[H.AUTHORIZATION] = authorization;
if (xbceDate) {
headers[H.X_BCE_DATE] = xbceDate;
}
return client._doRequest(options, body, outputStream);
});
}
Expand Down
1 change: 1 addition & 0 deletions test/browser/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<tr><th>SK</th><td> <input id="g_sk" value="ec7f4e0174254f6f9020f9680fb1da9f" /></td></tr>
<tr><th>Bucket</th><td> <input id="g_bucket" value="chaichai" /></td></tr>
<tr><th>Host</th><td> <input id="g_host" disabled /> (CORS上线之前暂时不支持修改Host)</td></tr>
<tr><th>签名服务器</th><td> <input id="g_ss_url" value="http://127.0.0.1:1337/ack" /></td></tr>
<tr><th>分片的大小</th><td><input min="5" max="1048576" step="1" id="g_part_size" type="number" value="5" /> (Mb)</td></tr>
<tr><th>分片的数量</th><td><input id="g_chunk_count" type="number" disabled /></td></tr>
<tr><th>文件大小</th><td><input id="g_file_size" type="number" disabled /></td></tr>
Expand Down
85 changes: 85 additions & 0 deletions test/browser/demo/signature_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

var http = require('http');
var url = require('url');
var util = require('util');

var Auth = require('../../../src/auth');

var kCredentials = {
ak: '46bd9968a6194b4bbdf0341f2286ccce',
sk: 'ec7f4e0174254f6f9020f9680fb1da9f'
};

function safeParse(text) {
try {
return JSON.parse(text);
}
catch (ex) {
return null;
}
}

// BosClient.prototype.createSignature = function (credentials, httpMethod, path, params, headers) {
// http://signature_server/ack?httpMethod=$0&path=$1&params=$2&headers=$3
http.createServer(function (req, res) {
console.log(req.url);

// query: { httpMethod: '$0', path: '$1', params: '$2', headers: '$3' },
var query = url.parse(req.url, true).query;

var statusCode = 200;
var signature = null;
if (!query.httpMethod || !query.path || !query.params || !query.headers) {
statusCode = 403;
}
else {
var httpMethod = query.httpMethod;
var path = query.path;
var params = safeParse(query.params) || {};
var headers = safeParse(query.headers) || {};

var auth = new Auth(kCredentials.ak, kCredentials.sk);
signature = auth.generateAuthorization(httpMethod, path, params, headers);
}

// 最多10s的延迟
var delay = Math.min(query.delay || 0, 10);
setTimeout(function () {
var payload = {
statusCode: statusCode,
signature: signature,
xbceDate: new Date().toISOString().replace(/\.\d+Z$/, 'Z')
};

res.writeHead(statusCode, {'Content-Type': 'text/javascript; charset=utf-8'});
if (query.callback) {
res.end(util.format('%s(%s)', query.callback, JSON.stringify(payload)));
}
else {
res.end(JSON.stringify(payload));
}
}, delay * 1000);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');









/* vim: set ts=4 sw=4 sts=4 tw=120: */

0 comments on commit 1dbe044

Please sign in to comment.