1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
/** * 监听HTTP请求 * @param {Object} event ipcMain事件对象 * @param {Object} opts 请求配置 * @return {[type]} [description] */ onRequest(event, opts) { logger.debug('onRequest::opts', opts); if (opts['url'].match(CONF.urlblacklist)) { return event .sender .send('request-error-' + opts['hash'], "Blacklist URL"); } let _request = superagent.post(opts['url']); // 设置headers _request.set('User-Agent', USER_AGENT); // 自定义headers for (let _ in opts.headers) { _request.set(_, opts.headers[_]); } // 自定义body const _postData = Object.assign({}, opts.body, opts.data); if (opts['useChunk'] == 1) { logger.debug("request with Chunked"); let _postarr = []; for (var key in _postData) { if (_postData.hasOwnProperty(key)) { let _tmp = encodeURIComponent(_postData[key]).replace(/asunescape((.+?))/g, function ($, $1) { return unescape($1); }); // 后续可能需要二次处理的在这里追加 _postarr.push(`${key}=${_tmp}`); } } let antstream = new AntRead(_postarr.join("&"), { 'step': parseInt(opts['chunkStepMin']), 'stepmax': parseInt(opts['chunkStepMax']) }); xxxxxxx } else { // 通过替换函数方式来实现发包方式切换, 后续可改成别的 const old_send = _request.send; let _postarr = []; if (opts['useMultipart'] == 1) { _request.send = _request.field; for (var key in _postData) { if (_postData.hasOwnProperty(key)) { let _tmp = (_postData[key]).replace(/asunescape((.+?))/g, function ($, $1) { return unescape($1) }); _postarr[key] = _tmp; } } } else { _request.send = old_send; for (var key in _postData) { if (_postData.hasOwnProperty(key)) { let _tmp = encodeURIComponent(_postData[key]).replace(/asunescape((.+?))/g, function ($, $1) { return unescape($1) }); // 后续可能需要二次处理的在这里追加 _postarr.push(`${key}=${_tmp}`); } } _postarr = _postarr.join('&'); }
|
评论