function _submitRequest(params, callback) {
    var self = this;
    var TaskId = params.TaskId;
    if (TaskId && !self._isRunningTask(TaskId)) return;

    var bucket = params.Bucket;
    var region = params.Region;
    var object = params.Key;
    var method = params.method || 'GET';
    var url = params.url;
    var body = params.body;
    var json = params.json;
    var rawBody = params.rawBody;

    // url
    url = url || getUrl({
        ForcePathStyle: self.options.ForcePathStyle,
        protocol: self.options.Protocol,
        domain: self.options.Domain,
        bucket: bucket,
        region: region,
        object: object,
    });
    if (params.action) {
        url = url + '?' + params.action;
    }

    var opt = {
        method: method,
        url: url,
        headers: params.headers,
        qs: params.qs,
        body: body,
        json: json,
    };

    // 获取签名
    opt.headers.Authorization = params.AuthData.Authorization;
    params.AuthData.Token && (opt.headers['token'] = params.AuthData.Token);
    params.AuthData.ClientIP && (opt.headers['clientIP'] = params.AuthData.ClientIP);
    params.AuthData.ClientUA && (opt.headers['clientUA'] = params.AuthData.ClientUA);
    params.AuthData.XCosSecurityToken && (opt.headers['x-cos-security-token'] = params.AuthData.XCosSecurityToken);

    // 清理 undefined 和 null 字段
    opt.headers && (opt.headers = util.clearKey(opt.headers));
    opt = util.clearKey(opt);

    // progress
    if (params.onProgress && typeof params.onProgress === 'function') {
        var contentLength = body && (body.size || body.length) || 0;
        opt.onProgress = function (e) {
            if (TaskId && !self._isRunningTask(TaskId)) return;
            var loaded = e ? e.loaded : 0;
            params.onProgress({loaded: loaded, total: contentLength});
        };
    }
    if (this.options.Timeout) {
        opt.timeout = this.options.Timeout;
    }

    self.emit('before-send', opt);
    var sender = REQUEST(opt, function (err, response, body) {
        if (err === 'abort') return;

        // 返回内容添加 状态码 和 headers
        var hasReturned;
        var cb = function (err, data) {
            TaskId && self.off('inner-kill-task', killTask);
            if (hasReturned) return;
            hasReturned = true;
            var attrs = {};
            response && response.statusCode && (attrs.statusCode = response.statusCode);
            response && response.headers && (attrs.headers = response.headers);

            if (err) {
                err = util.extend(err || {}, attrs);
                callback(err, null);
            } else {
                data = util.extend(data || {}, attrs);
                callback(null, data);
            }
            sender = null;
        };

        // 请求错误，发生网络错误
        if (err) {
            cb({error: err});
            return;
        }

        var jsonRes;
        try {
            jsonRes = body && body.indexOf('<') > -1 && body.indexOf('>') > -1 && util.xml2json(body) || {};
        } catch (e) {
            jsonRes = body || {};
        }

        // 请求返回码不为 200
        var statusCode = response.statusCode;
        var statusSuccess = Math.floor(statusCode / 100) === 2; // 200 202 204 206
        if (!statusSuccess) {
            cb({error: jsonRes.Error || jsonRes});
            return;
        }

        // 不对 body 进行转换，body 直接挂载返回
        if (rawBody) {
            jsonRes = {};
            jsonRes.body = body;
        }

        if (jsonRes.Error) {
            cb({error: jsonRes.Error});
            return;
        }
        cb(null, jsonRes);
    });

    // kill task
    var killTask = function (data) {
        if (data.TaskId === TaskId) {
            sender && sender.abort && sender.abort();
            self.off('inner-kill-task', killTask);
        }
    };
    TaskId && self.on('inner-kill-task', killTask);

}
function _submitRequest(params, callback) {
    var self = this;
    var TaskId = params.TaskId;
    if (TaskId && !self._isRunningTask(TaskId)) return;

    var bucket = params.Bucket;
    var region = params.Region;
    var object = params.Key;
    var method = params.method || 'GET';
    var url = params.url;
    var body = params.body;
    var json = params.json;
    var rawBody = params.rawBody;

    
    url = url || getUrl({
        ForcePathStyle: self.options.ForcePathStyle,
        protocol: self.options.Protocol,
        domain: self.options.Domain,
        bucket: bucket,
        region: region,
        object: object,
    });
    if (params.action) {
        url = url + '?' + params.action;
    }

    var opt = {
        method: method,
        url: url,
        headers: params.headers,
        qs: params.qs,
        body: body,
        json: json,
    };

    
    opt.headers.Authorization = params.AuthData.Authorization;
    params.AuthData.Token && (opt.headers['token'] = params.AuthData.Token);
    params.AuthData.ClientIP && (opt.headers['clientIP'] = params.AuthData.ClientIP);
    params.AuthData.ClientUA && (opt.headers['clientUA'] = params.AuthData.ClientUA);
    params.AuthData.XCosSecurityToken && (opt.headers['x-cos-security-token'] = params.AuthData.XCosSecurityToken);

    
    opt.headers && (opt.headers = util.clearKey(opt.headers));
    opt = util.clearKey(opt);

    
    if (params.onProgress && typeof params.onProgress === 'function') {
        var contentLength = body && (body.size || body.length) || 0;
        opt.onProgress = function (e) {
            if (TaskId && !self._isRunningTask(TaskId)) return;
            var loaded = e ? e.loaded : 0;
            params.onProgress({loaded: loaded, total: contentLength});
        };
    }
    if (this.options.Timeout) {
        opt.timeout = this.options.Timeout;
    }

    self.emit('before-send', opt);
    var sender = REQUEST(opt, function (err, response, body) {
        if (err === 'abort') return;

        
        var hasReturned;
        var cb = function (err, data) {
            TaskId && self.off('inner-kill-task', killTask);
            if (hasReturned) return;
            hasReturned = true;
            var attrs = {};
            response && response.statusCode && (attrs.statusCode = response.statusCode);
            response && response.headers && (attrs.headers = response.headers);

            if (err) {
                err = util.extend(err || {}, attrs);
                callback(err, null);
            } else {
                data = util.extend(data || {}, attrs);
                callback(null, data);
            }
            sender = null;
        };

        
        if (err) {
            cb({error: err});
            return;
        }

        var jsonRes;
        try {
            jsonRes = body && body.indexOf('<') > -1 && body.indexOf('>') > -1 && util.xml2json(body) || {};
        } catch (e) {
            jsonRes = body || {};
        }

        
        var statusCode = response.statusCode;
        var statusSuccess = Math.floor(statusCode / 100) === 2; 
        if (!statusSuccess) {
            cb({error: jsonRes.Error || jsonRes});
            return;
        }

        
        if (rawBody) {
            jsonRes = {};
            jsonRes.body = body;
        }

        if (jsonRes.Error) {
            cb({error: jsonRes.Error});
            return;
        }
        cb(null, jsonRes);
    });

    
    var killTask = function (data) {
        if (data.TaskId === TaskId) {
            sender && sender.abort && sender.abort();
            self.off('inner-kill-task', killTask);
        }
    };
    TaskId && self.on('inner-kill-task', killTask);

}
function _submitRequest ( params , callback ) { var self = this ; var TaskId = params . TaskId ; if ( TaskId && ! self . _isRunningTask ( TaskId ) ) return ; var bucket = params . Bucket ; var region = params . Region ; var object = params . Key ; var method = params . method || 'GET' ; var url = params . url ; var body = params . body ; var json = params . json ; var rawBody = params . rawBody ; // url url = url || getUrl ( { ForcePathStyle : self . options . ForcePathStyle , protocol : self . options . Protocol , domain : self . options . Domain , bucket : bucket , region : region , object : object , } ) ; if ( params . action ) { url = url + '?' + params . action ; } var opt = { method : method , url : url , headers : params . headers , qs : params . qs , body : body , json : json , } ; // 获取签名 opt . headers . Authorization = params . AuthData . Authorization ; params . AuthData . Token && ( opt . headers [ 'token' ] = params . AuthData . Token ) ; params . AuthData . ClientIP && ( opt . headers [ 'clientIP' ] = params . AuthData . ClientIP ) ; params . AuthData . ClientUA && ( opt . headers [ 'clientUA' ] = params . AuthData . ClientUA ) ; params . AuthData . XCosSecurityToken && ( opt . headers [ 'x-cos-security-token' ] = params . AuthData . XCosSecurityToken ) ; // 清理 undefined 和 null 字段 opt . headers && ( opt . headers = util . clearKey ( opt . headers ) ) ; opt = util . clearKey ( opt ) ; // progress if ( params . onProgress && typeof params . onProgress === 'function' ) { var contentLength = body && ( body . size || body . length ) || 0 ; opt . onProgress = function ( e ) { if ( TaskId && ! self . _isRunningTask ( TaskId ) ) return ; var loaded = e ? e . loaded : 0 ; params . onProgress ( { loaded : loaded , total : contentLength } ) ; } ; } if ( this . options . Timeout ) { opt . timeout = this . options . Timeout ; } self . emit ( 'before-send' , opt ) ; var sender = REQUEST ( opt , function ( err , response , body ) { if ( err === 'abort' ) return ; // 返回内容添加 状态码 和 headers var hasReturned ; var cb = function ( err , data ) { TaskId && self . off ( 'inner-kill-task' , killTask ) ; if ( hasReturned ) return ; hasReturned = true ; var attrs = { } ; response && response . statusCode && ( attrs . statusCode = response . statusCode ) ; response && response . headers && ( attrs . headers = response . headers ) ; if ( err ) { err = util . extend ( err || { } , attrs ) ; callback ( err , null ) ; } else { data = util . extend ( data || { } , attrs ) ; callback ( null , data ) ; } sender = null ; } ; // 请求错误，发生网络错误 if ( err ) { cb ( { error : err } ) ; return ; } var jsonRes ; try { jsonRes = body && body . indexOf ( '<' ) > - 1 && body . indexOf ( '>' ) > - 1 && util . xml2json ( body ) || { } ; } catch ( e ) { jsonRes = body || { } ; } // 请求返回码不为 200 var statusCode = response . statusCode ; var statusSuccess = Math . floor ( statusCode / 100 ) === 2 ; // 200 202 204 206 if ( ! statusSuccess ) { cb ( { error : jsonRes . Error || jsonRes } ) ; return ; } // 不对 body 进行转换，body 直接挂载返回 if ( rawBody ) { jsonRes = { } ; jsonRes . body = body ; } if ( jsonRes . Error ) { cb ( { error : jsonRes . Error } ) ; return ; } cb ( null , jsonRes ) ; } ) ; // kill task var killTask = function ( data ) { if ( data . TaskId === TaskId ) { sender && sender . abort && sender . abort ( ) ; self . off ( 'inner-kill-task' , killTask ) ; } } ; TaskId && self . on ( 'inner-kill-task' , killTask ) ; }
发起请求
发起请求