请求核心

@constructor
@param {Object} options 请求参数
@param {Requester} requester 请求对象
514982
function Kernel(options, requester) {
    var headers = options.headers;
    // POST请求添加默认的`Content-Type`
    if (options.method === METHOD.POST
        && !util.findHeader(headers, 'Content-Type')
    ) {
        headers['Content-Type'] = 'application/x-www-form-urlencoded';
    }

    var data = options.data;
    if (!util.isString(data) && options.stringify !== false) {
        data = util.stringify(data);
    }

    log(requester, 'info', 'request %s %s %s %s', options.method, options.url, JSON.stringify(headers), data);

    var agents;
    var context = requester.context;
    var agentOptions = context.data.agent;
    if (agentOptions) {
        agents = createAgents(agentOptions);
    }

    var xhr = request(
        {
            url: options.url,
            method: options.method,
            headers: headers,
            body: data,
            timeout: options.timeout
        },
        function (error, res, data) {
            if (!error) {
                // 与client端兼容
                var status = res.status = res.statusCode;
                var level = status >= 400 ? 'error' : 'info';
                log(requester, level, 'response %s %s\n%s', status, JSON.stringify(res.headers), data);
            }
            else {
                log(requester, 'error', error.stack);
                if (error.code) {
                    error = error.code;
                }
            }
            requester.handle(error, res, data);
        },
        agents
    );

    return xhr;
}
1085922
function (args) {
            var req = args.req;
            var handler = args.requestHandler;
            if (handler == null) {
                handler = this.__getRequestHandler();
            }

            req.data = (req.data == null && req.method == "POST")
                    ? handler.prepareRequestBody(req.jsonData, req.requestObject)
                    : req.data;
            req.headers = handler.getRequestHeaders();
            if (req.requestObject.headers) {
                req.headers = ariaUtilsJson.copy(req.headers, false);
                ariaUtilsJson.inject(req.requestObject.headers, req.headers, false);
            }

            // call the server
            var senderObject = {
                classpath : this.$classpath,
                requestObject : req.requestObject,
                requestData : req.jsonData,

                // the following two properties can be used to communicate from the filters to the RequestMgr and bypass
                // the handler:
                responseData : null,
                responseErrorData : null
            };

            var requestObject = {
                sender : senderObject,
                url : req.url,
                async : (req.requestObject.async !== false),
                method : req.method,
                data : req.data,
                headers : req.headers,
                timeout : req.timeout,
                callback : {
                    fn : this._onresponse,
                    onerror : this._onresponse,
                    scope : this,
                    args : {
                        requestObject : req.requestObject,
                        senderObject : senderObject,
                        cb : args.cb,
                        id : args.id,
                        session : args.session,
                        actionQueuing : args.actionQueuing,
                        requestHandler : handler
                    }
                }
            };
            if (handler.expectedResponseType) {
                requestObject.expectedResponseType = handler.expectedResponseType;
            }
            ariaCoreIO.asyncRequest(requestObject);
        }
675117
function XHRProxy(options) { // @arg Object = {} - { verbose }
                             // @options.verbose Boolean = false
//{@dev
    $valid($type(options, "Object|omit"), XHRProxy, "options");
//}@dev

    options = options || {};
    this._verbose = options["verbose"] || false;

//{@dev
    $valid($type(options.verbose, "Boolean|omit"), XHRProxy, "options.verbose");
//}@dev

    this._event = new WMEvent()["register"](ALLOW_XHR_EVENTS);
    this._xhr = null;
    this._lv = FUNCTION_LEVEL_0;

    if (global["XMLHttpRequest"]) { // _runOnBrowser
        this._lv = FUNCTION_LEVEL_1;
        this._xhr = new XMLHttpRequest();

        if ("onload"          in this._xhr &&
            "responseType"    in this._xhr &&
            "withCredentials" in this._xhr) {
            this._lv = FUNCTION_LEVEL_2;
        }
    } else { // _runOnNode -> simulate XMLHttpRequest Object
        this._xhr = {
            "readyState":       READY_STATE_UNSENT,
            // --- request ---
            "method":           "",     // "GET" or "POST"
            "url":              "",
            "async":            true,
            "auth":             "",     // "" or "user:password"
            "requestHeader":    {},     // { header: value, ... }
            // --- response ---
            "response":         null,
            "responseText":     "",
            "responseXML":      null,
            "responseHeaders":  {},     // { header: value, ... }
            "status":           0,
            "statusText":       "",
            "upload":           null,
            "withCredentials":  false
        };
    }

//{@dev
    if (this._verbose) {
        console.log("XMLHttpRequest Level is " + this._lv);
    }
//}@dev

    this._lastRequestURL = "";
    this._lastReadyState = READY_STATE_UNSENT;

    // setup property getter and setter.
    Object.defineProperties(this, {
        "readyState":     { "get": getReadyState                                },
        "response":       { "get": getResponse                                  },
        "responseText":   { "get": getResponseText                              },
        "responseType":   { "get": getResponseType,   "set": setResponseType    },
        "responseXML":    { "get": getResponseXML                               },
        "status":         { "get": getStatus                                    },
        "statusText":     { "get": getStatusText                                },
        "upload":         { "get": getUpload,         "set": setUpload          },
        "withCredentials":{ "get": getWithCredentials,"set": setWithCredentials }
    });
}
1028850
function RequestOptions(opts) {
        if (opts === void 0) { opts = {}; }
        var method = opts.method, headers = opts.headers, body = opts.body, url = opts.url, search = opts.search, params = opts.params, withCredentials = opts.withCredentials, responseType = opts.responseType;
        this.method = method != null ? normalizeMethodName(method) : null;
        this.headers = headers != null ? headers : null;
        this.body = body != null ? body : null;
        this.url = url != null ? url : null;
        this.params = this._mergeSearchParams(params || search);
        this.withCredentials = withCredentials != null ? withCredentials : null;
        this.responseType = responseType != null ? responseType : null;
    }
1049231
function(data, req, res, servConfig){
        if(!data.cnt) return false;
        // utils.log(utils.chalk.green("----------ajax json:\n", data.type));
        try{
            switch(data.type){
                case 'mjson': //处理mockjson
                    //utils.log(utils.chalk.green(mockjs.mock(JSON.parse(data.cnt))))
                    this.acao && res.setHeader("Access-Control-Allow-Origin","*");
                    data.cnt = JSON.stringify(mockjs.mock(JSON.parse(data.cnt)), null, 4);
                case 'json':
                    this.acao && res.setHeader("Access-Control-Allow-Origin","*");
                    res.writeHead(200, {
                        'Content-Type': utils.MIME['json']
                    });
                    res.write(data.cnt);
                    res.end();
                    break;
                case 'js':
                    //data = new vm.script(data[0]);
                    // 将分解好的url给mock用户*.js 
                    // 只能拦截所起服务下的mock 跨域的不能拦截需用fiddler或charles之类的代理到servermock所起服务下
                    req.parseUrl = req.wwwurl = url.parse(servConfig.protocol + '://' + req.headers.host + req.url);
                    var qstr = req.wwwurl.query ? qs.parse(req.wwwurl.query) : {},
                        query = {};
                    for(var i in qstr){
                        var $ = /(.+)\[(.+)\]/.exec(i);
                        // utils.log(utils.chalk.green("$: ", $));
                        $ ? (query[$[1]] || (query[$[1]] = {})) && (query[$[1]][$[2]] = qstr[i]):
                            query[i] = qstr[i];  
                    }
                    // utils.log(utils.chalk.green("qstr: ", qstr));
                    //extname === 'html' && utils.log(utils.chalk.green(req, '\n\n\n\n', req.wwwurl))
                    req.queryObj = req.wwwurl.queryObj = query;
                    //utils.log(utils.chalk.green('----------mock js func:\n', data.cnt.toString()));
                    vm.runInThisContext('(' + data.cnt + ')')(req, res, require);
                    break;
                default:
                    utils.log(utils.chalk.yellow("mock 暂只支持.mjson .json .js文件"));
                    res.end("mock 暂只支持.mjson .json .js文件");
            }
        } catch(e){
            utils.log(utils.chalk.red("mock error: "), e);
        }
        return true;
    }
819087
function server(options) {
  status = 'server.started'
  config = _.extend(defaultServerOptions, options)

  open(config.datafile)

  if (config.logToConsole)
    console.log("✔ log to stdout")

  // ws [
  var ServerMessageLog = {}
  var queueMessages = []
  var timer

  function flushMessages(n) {
    queueMessages = _.sortBy(queueMessages, 'time')
    var messages = queueMessages.slice(0, n)
    queueMessages = queueMessages.slice(n)
//    console.log(messages);
    _.each(messages, function (m) {
//      var filename = options.logpath+'/1.log'
//      logString(filename, m.text)
      logMessage(new Date(m.time), m.path, m.data, m.type == 'log' ? '' : m.type)
    })
  }

  console.log("✔ ws server listening on port %d", config.port)

  var wss = new WebSocketServer({port: config.port});

  wss.on('connection', function(ws) {
    ws.on('message', function(m) {
      var message = JSON.parse(m)
      queueMessages.push(message)
      if (queueMessages.length > config.queueMessagesMax)
        flushMessages(config.flushMessagesCount)

      if (timer)
        clearTimeout(timer)
      timer = setTimeout(function () {
        timer = null
        flushMessages(queueMessages.length)
      }, config.serverFlushTimeout)
    })
  })
  // ws ]
}
1307762
function makeRequest(options, callback) {
    request(options, function (error, response, body){
        if (error) {
            callback(error, null);
        }
        else if (response.statusCode == 200) {
            callback( null, JSON.parse(body));
        }
        else {
            callback(new Error('HTTP Response Code: ' + response.statusCode), null);
        }
    });
}
740899
function (options, serverOptions) {

		options = extend(this._defaults, options || {});

		validatorLog.clear();
		validatorLog.neutral('response validation\'s started');

		this._options = options;
		this._serverOptions = serverOptions;
		this._prefFilePath = this._options.restPath + '/preferences.json';
		this._validatorFilePath = this._options.restPath + '/.validation.json';
		this._tempPrefData = '';
		this._setResponseDelay();
		this._validationResponses = this._getValidationResponses(this._options.restPath);
		this._runValidations(this._validationResponses);
		this._restoreResponseDelay();

	}
708955
function(options) {
  if (typeof options !== 'object') {
    options = {};
  }
  this._options = options = _.defaults(options, {
    trustedProxies: ['127.0.0.1'],
    clickjacking: 'deny',
    noMimeSniffing: true,
    log: console.log,
    maxInputSize: 1024 * 1024
  });

  this._app = connect();

  this._app.use(function(req, res, next) {
    // Set body to stream if posting data not yet processed by the bodyparser.
    if (['GET', 'HEAD'].indexOf(req.method) < 0 && typeof req.headers['content-type'] === 'string') {
      req.body = new Readable();
      req.body.wrap(req);
      // Parse the boundary for multipart data.
      var boundary = req.headers['content-type'].match(/^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i);
      req.multipartBoundary = boundary ? boundary[1] || boundary[2] : null;
    }
    next();
  });
  this._app.use(bodyParser.urlencoded({extended: false}));
  this._app.use(bodyParser.json({limit: options.maxInputSize}));
  this._app.use(bodyParser.text({type: 'text/*', limit: this._options.maxInputSize}));
  this._app.use(cookieParser());
  this._app.use(compression());

  this.phases = [
    {name: 'initialize', concurrent: true, error: false, errorStatus: 500},
    {name: 'authentication', concurrent: false, error: false, errorStatus: 401},
    {name: 'authorisation', concurrent: false, error: false, errorStatus: 403},
    {name: 'prevalidation', concurrent: true, error: false, errorStatus: 400},
    {name: 'preprocess', concurrent: false, error: false, errorStatus: 500},
    {name: 'postvalidation', concurrent: true, error: false, errorStatus: 400},
    {name: 'process', concurrent: false, error: false, errorStatus: 500},
    {name: 'postprocess', concurrent: false, error: false, errorStatus: 500},
    {name: '_send', concurrent: false, error: false},
    {name: 'after', concurrent: true, error: false},
    {name: 'error', concurrent: false, error: true, errorStatus: 500},
    {name: '_senderror', concurrent: false, error: true},
    {name: 'aftererror', concurrent: true, error: true}
  ];
  this.addRegisterFunctions(this);

  this._types = {
    'alpha': '[a-z]+',
    'alphanum': '[a-z0-9]+',
    'bool': '(?:1|0|true|false)',
    'float': '\\-?[0-9\\.]+',
    'int': '[1-9][0-9]*',
    'path': '.+?',
    'signed': '\\-?[0-9]+',
    'string': '[^\\/]+',
    'unsigned': '[0-9]+',
    'uuid': '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
  };

  this._send(sendHandler);
  this.error(errorHandler);
  this._senderror(sendHandler);

  var log = this._options.log;
  if (typeof log === 'function') {
    this.after(function() { logHandler.apply(this, [log]); });
    this.aftererror(function() { logHandler.apply(this, [log]); });
  }
}
1232498
async function mitm(options) {
	// 不在参数解构是考虑到时候也许直接把options传给某个库
	const { origin, dest, log = false, host = '0.0.0.0', port = rand(1025, 65535) } = options;
	if (typeof origin !== 'string' || !origin || typeof dest !== 'string' || !dest) {
		throw new TypeError('origin and dest in mitm options must set correctly.');
	}

	if (origin === dest) {
		throw new Error('Same origin and dest may cause request loop.');
	}

	const [originHostname, originPort = 80] = origin.trim().toLowerCase().split(':');
	const [destHostname, destPort = 80] = dest.trim().toLowerCase().split(':');

	const server = new Server();

	// 目前只支持HTTP代理
	server.on('request', (cReq, cRes) => {
		let { hostname, port, pathname } = new URL(cReq.url);
		port = port || 80;

		let opts = {
			protocol: 'http:',
			method: cReq.method,
			path: pathname,
			headers: cReq.headers
		};
		if (hostname === originHostname && port == originPort) {
			opts.hostname = destHostname;
			opts.port = parseInt(destPort, 10);
		} else {
			opts.hostname = hostname;
			opts.port = parseInt(port, 10);
		}
		const sReq = request(opts, sRes => {
			cRes.writeHead(sRes.statusCode, sRes.statusMessage, sRes.headers);
			sRes.pipe(cRes);
		});

		sReq.on('error', function (err) {
			log && logger.error(err.message);
			this.end();
		});

		cReq.pipe(sReq);
	});

	server.on('connect', (req, cSocket, header) => {
		const [hostname, port] = req.url.split(':');
		const sSocket = createConnection(parseInt(port, 10) || 443, hostname, function () {
			cSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
			this.write(header);
			this.pipe(cSocket).pipe(this);
		});

		sSocket.on('error', function (err) {
			log && logger.error(err.message);
			this.end();
		});

		cSocket.on('error', function (err) {
			log && logger.error(err.message);
			this.end();
		});
	});

	server.on('error', err => {
		logger.error(err.message);
	});

	// 什么傻屌操作???
	let resolve = null;
	const delayLog = new Promise(rs => resolve = rs);
	server.listen(parseInt(port, 10), host, err => {
		if (err) {
			throw err;
		}
		resolve();
	});

	await delayLog;
	logger.success(`MITM proxy listening on ${chalk.cyan.underline(`http://${host}:${port}`)} From ${chalk.cyan.underline(origin)} to ${chalk.cyan.underline(dest)}`);


	return server;
}
805059
function (config, options) {
    //获取Mq的配置
    Mq.call(this, config, options);
    this.exchange = options.exchange;
    this.exchangeType = options.type,
        this.consumers = [];
    this.isAlive = false;
    this.options = options.options || {};
}
369004
public <T> HttpClientRequest.Builder<T> options(final URI uri, final HttpClientResponseHandler<T> httpHandler)
    {
        return new HttpClientRequest.Builder<T>(httpClientFactory, HttpClientMethod.OPTIONS, uri, httpHandler);
    }
165561
function init(options){
	options = util._extend({ssl:null, port:12345}, options);
	var serverWorkers = {}; // save main server sockets
	var clientSocket = []; // save local server socket
	var clientOptions = []; // save optioin obj
	var wsServer;
	if(options.ssl)
		wsServer = require('https').createServer(options.ssl);
	else wsServer = require('http').createServer();
	var io = require('socket.io')(wsServer);
	
	if(options.auth)	
	io.set('authorization', function (handshakeData, callback) {
		if(handshakeData.url.match('username='+options.auth.username+'&password='+options.auth.password))
        	callback(null, true); // error first callback style 
        else callback('no auth', false);
    });

	io.on('connection', function(socket){
		// 0. save server sockets & local socket
		socket.on('init', function(data){
			if(data._type == 'localServer'){
				socket._type = 'localServer';
				if(clientSocket.length>0)
					clientSocket.slice(-1)[0].emit('hold');			
				// then handle new one
				clientSocket.push(socket);
				clientOptions.push(data);
				// inform server the new client
				for(i in serverWorkers)
					serverWorkers[i].emit('client', data);
			}else if(data._type == 'server'){
				socket._type = 'server';
				serverWorkers[socket.id] = socket;
			}else{
				// ignore this connection
			}
		})
		// handle req from server, relay data to client
		socket.on('req', function(data){
			if(clientSocket.length > 0){
				data._sid = socket.id;
				clientSocket.slice(-1)[0].emit('req', data)
			}
		})
		// handle res from client, relay the data to the server
		socket.on('res', function(data){
			if(serverWorkers[data._sid])
				serverWorkers[data._sid].emit('res', data);
		});
		socket.on('disconnect', function(reason){
			if(socket._type == 'localServer'){
				var last = false;
				for(i in clientSocket){
					if(socket.id == clientSocket[i].id){
						if(i == clientSocket.length-1)
							last = true;
						clientSocket.splice(i, 1);
						clientOptions.splice(i,1);
						break;
					}
				}
				if(clientSocket.length > 0 && clientOptions.length > 0){
					if(last)
						clientSocket.slice(-1)[0].emit('back');	
					for(i in serverWorkers)
						serverWorkers[i].emit('client', clientOptions.slice(-1)[0]);
				}
				else
					for(i in serverWorkers)
						serverWorkers[i].emit('client', false);
			}
			else if(socket._type == 'server')
				delete serverWorkers[socket.id];
		});
	});
	wsServer.listen(options.port).on('listening', function(){
		options.listening ? options.listening() : noop();
		//console.log('[NLT] Server is ready on port ' + options.port);
	}).on('error', function(e){
		options.error ? options.error(e) : noop();
		//console.log('[NLT] Setup error', e)
	});
}
415051
function handleRequest(url, options, callback) {
  let req = {
    url: url,
    headers: {},
    qs: {},
    json: {},
  }

  req = parseOptions(req, options)
  callback(req)
}
374405
function getAsyncContent(url, options) {
            var requestCfg;
            // Initial options to be merged with the user's options
            options = tiny.extend({
                'method': 'GET',
                'params': '',
                'waiting': '<div class="ch-loading-large"></div>'
            }, defaults, options);

            // Set loading
            setAsyncContent({
                'status': 'waiting',
                'response': options.waiting
            });

            requestCfg = {
                method: options.method,
                success: function(resp) {
                    setAsyncContent({
                        'status': 'done',
                        'response': resp
                    });
                },
                error: function(err) {
                    setAsyncContent({
                        'status': 'error',
                        'response': '<p>Error on ajax call.</p>',
                        'data': err.message || JSON.stringify(err)
                    });
                }
            };

            if (options.cache !== undefined) {
                that._options.cache = options.cache;
            }

            if (options.cache === false && ['GET', 'HEAD'].indexOf(options.method.toUpperCase()) !== -1) {
                requestCfg.cache = false;
            }

            if (options.params) {
                if (['GET', 'HEAD'].indexOf(options.method.toUpperCase()) !== -1) {
                    url += (url.indexOf('?') !== -1 || options.params[0] === '?' ? '' : '?') + options.params;
                } else {
                    requestCfg.data = options.params;
                }
            }

            // Make a request
            tiny.ajax(url, requestCfg);
        }
219944
function buildOptions(request, callback) {

    let options = {};
    let err = null;

     if (request.payload.html !== undefined) {
        options.html = request.payload.html.trim();
    }

    if (request.payload.baseUrl !== undefined) {
        options.baseUrl = request.payload.baseUrl.trim();
    }

    if (request.payload.filters !== undefined) {
        if (request.payload.filters.indexOf(',') > -1) {
            options.filters = trimArray(request.payload.filters.split(','))
        } else {
            options.filters = trimArray(request.payload.filters)
        }
        if (options.filters.length === 0) {
            delete options.filters;
        }
    }

    if (request.payload.dateFormat !== undefined) {
        options.dateFormat = request.payload.dateFormat;
    }

    if (request.payload.textFormat !== undefined) {
        options.textFormat = request.payload.textFormat;
    }

    if (request.payload.overlappingVersions !== undefined) {
        options.overlappingVersions = request.payload.overlappingVersions
    }

    if (request.payload.impliedPropertiesByVersion !== undefined) {
        options.impliedPropertiesByVersion = request.payload.impliedPropertiesByVersion
    }

    if (request.payload.parseLatLonGeo !== undefined) {
        options.parseLatLonGeo = request.payload.parseLatLonGeo
    }


    if (request.payload.url !== undefined) {

        Request(request.payload.url, function (error, response, body) {

            err = error;
            if(!err && response && response.statusCode === 200){
                options.html = body;
                callback(null, options);
            }else{
                callback(err, null);
            }
        });
    }else{
        callback(err, options);
    }

}
558840
function xhr (url, options, callback) {
  if (typeof options === 'function') {
    callback = options
    options = null
  }
  const o = options || {}
  const req = new XMLHttpRequest()
  const method = o.method || 'get'
  const params = o.data || null

  req.queryString = params
  req.open(method, url, true)
  req.setRequestHeader('X-Requested-With', 'XMLHttpRequest')

  if (method.toLowerCase() === 'post') {
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  }

  for (let key in o.headers) {
    if (o.headers.hasOwnProperty(key)) {
      req.setRequestHeader(key, o.headers[key])
    }
  }

  function stateChange () {
    if (req.readyState === 4) {
      if ((/^[20]/).test(req.status)) {
        callback(null, req.responseText)
      } else if ((/^[45]/).test(req.status)) {
        callback(new Error(req.status))
      }
    }
  }

  req.onreadystatechange = stateChange

  try {
    req.send(params)
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('Error: Resolving URL ' + url)
    callback(e)
  }
}