From 1dbe044eb92f390bb144d8f0764c39d9122758fe Mon Sep 17 00:00:00 2001 From: leeight Date: Thu, 28 May 2015 22:19:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=AD=BE=E5=90=8D=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E4=BA=86=EF=BC=8C=E8=BF=99=E6=A0=B7=E5=AD=90?= =?UTF-8?q?=E6=89=8D=E7=AE=97=E6=AF=94=E8=BE=83=E5=AE=8C=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/http_client.js | 5 +- test/browser/baidubce-sdk.bundle.js | 5 +- test/browser/demo/app.js | 37 ++++++++++- test/browser/demo/baidubce-sdk.bundle.js | 5 +- test/browser/demo/index.html | 1 + test/browser/demo/signature_server.js | 85 ++++++++++++++++++++++++ 6 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 test/browser/demo/signature_server.js diff --git a/src/http_client.js b/src/http_client.js index 6c162b8..3dadf4d 100644 --- a/src/http_client.js +++ b/src/http_client.js @@ -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); }); } diff --git a/test/browser/baidubce-sdk.bundle.js b/test/browser/baidubce-sdk.bundle.js index b6ffd22..5ca860a 100644 --- a/test/browser/baidubce-sdk.bundle.js +++ b/test/browser/baidubce-sdk.bundle.js @@ -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); }); } diff --git a/test/browser/demo/app.js b/test/browser/demo/app.js index 8817949..f213be4 100644 --- a/test/browser/demo/app.js +++ b/test/browser/demo/app.js @@ -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 @@ -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(); @@ -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(); @@ -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(); diff --git a/test/browser/demo/baidubce-sdk.bundle.js b/test/browser/demo/baidubce-sdk.bundle.js index b6ffd22..5ca860a 100644 --- a/test/browser/demo/baidubce-sdk.bundle.js +++ b/test/browser/demo/baidubce-sdk.bundle.js @@ -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); }); } diff --git a/test/browser/demo/index.html b/test/browser/demo/index.html index 4255c1c..0d9e8ee 100644 --- a/test/browser/demo/index.html +++ b/test/browser/demo/index.html @@ -37,6 +37,7 @@ SK Bucket Host (CORS上线之前暂时不支持修改Host) + 签名服务器 分片的大小 (Mb) 分片的数量 文件大小 diff --git a/test/browser/demo/signature_server.js b/test/browser/demo/signature_server.js new file mode 100644 index 0000000..334b19c --- /dev/null +++ b/test/browser/demo/signature_server.js @@ -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¶ms=$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: */