nodejs環境http請求


axios在nodejs環境使用http或者https模塊發送請求。

'use strict';

var utils = require('./../utils');
var settle = require('./../core/settle');
var buildURL = require('./../helpers/buildURL');
var http = require('http');
var https = require('https');
var httpFollow = require('follow-redirects').http;
var httpsFollow = require('follow-redirects').https;
var url = require('url');
var zlib = require('zlib');
var pkg = require('./../../package.json');
var createError = require('../core/createError');
var enhanceError = require('../core/enhanceError');

var isHttps = /https:?/;

/*eslint consistent-return:0*/
module.exports = function httpAdapter(config) {
  return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {//返回一個promise
    var timer;//定時器
    var resolve = function resolve(value) {//resolve前先將定時器清除
      clearTimeout(timer);
      resolvePromise(value);
    };
    var reject = function reject(value) {//reject前先將定時器清除
      clearTimeout(timer);
      rejectPromise(value);
    };
    var data = config.data;//data參數
    var headers = config.headers;//header請求頭對象

    // Set User-Agent (required by some servers)
    // Only set header if it hasn't been set in config
    // See https://github.com/axios/axios/issues/69
    //設置User-Agent請求頭
    if (!headers['User-Agent'] && !headers['user-agent']) {
      headers['User-Agent'] = 'axios/' + pkg.version;
    }

    if (data && !utils.isStream(data)) {
      //如果data參數存在且data不是流
      if (Buffer.isBuffer(data)) {//如果是Buffer,不作操作
        // Nothing to do...
      } else if (utils.isArrayBuffer(data)) {//如果是ArrayBuffer
        data = Buffer.from(new Uint8Array(data));
      } else if (utils.isString(data)) {//如果是字符串
        data = Buffer.from(data, 'utf-8');
      } else {//其他情況,拋出錯誤
        return reject(createError(
          'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
          config
        ));
      }

      // Add Content-Length header if data exists
      //如果data存在,添加Content-Length請求頭,data長度
      headers['Content-Length'] = data.length;
    }

    // HTTP basic authentication
    var auth = undefined;
    if (config.auth) {//如果傳遞了auth配置項,把用戶名和密碼拼起來
      var username = config.auth.username || '';
      var password = config.auth.password || '';
      auth = username + ':' + password;
    }

    // Parse url
    var parsed = url.parse(config.url);//將完整url解析成部分組成的對象
    var protocol = parsed.protocol || 'http:';//協議

    if (!auth && parsed.auth) {//如果url中帶有auth且沒有傳遞配置項auth,就使用url中的auth參數
      var urlAuth = parsed.auth.split(':');
      var urlUsername = urlAuth[0] || '';
      var urlPassword = urlAuth[1] || '';
      auth = urlUsername + ':' + urlPassword;
    }

    if (auth) {//有auth參數,從請求頭中刪除Authorization
      delete headers.Authorization;
    }

    var isHttpsRequest = isHttps.test(protocol);//判斷是否是https協議
    var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
    //根據判斷是否https來獲取到對應的httpAgent

    var options = {
      path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
      method: config.method.toUpperCase(),
      headers: headers,
      agent: agent,
      auth: auth
    };
    //基礎選項,path調用buildURL方法創建一個帶有查詢參數的url

    if (config.socketPath) {//添加socketPath配置到options中
      options.socketPath = config.socketPath;
    } else {//沒有socketPath配置,就添加域名和端口號到options中
      options.hostname = parsed.hostname;
      options.port = parsed.port;
    }

    var proxy = config.proxy;//proxy定義代理服務器的域名和端口號
    if (!proxy && proxy !== false) {//沒有傳遞proxy參數
      var proxyEnv = protocol.slice(0, -1) + '_proxy';//協議名加上_proxy的字符串,代理url的環境變量名字
      var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
      //從環境變量中獲取到代理url
      if (proxyUrl) {//如果存在代理url
        var parsedProxyUrl = url.parse(proxyUrl);//將代理url解析
        var noProxyEnv = process.env.no_proxy || process.env.NO_PROXY;//NO_PROXY環境變量
        var shouldProxy = true;

        if (noProxyEnv) {//如果有NO_PROXY環境變量
          var noProxy = noProxyEnv.split(',').map(function trim(s) {
            return s.trim();
          });//處理noProxyEnv,逗號分開變成一個數組,然后去除每一個元素的空格

          shouldProxy = !noProxy.some(function proxyMatch(proxyElement) {
            //循環noProxy,尋找是否有一個元素和請求url的域名相等
            if (!proxyElement) {//當前proxyElement為空,返回
              return false;
            }
            if (proxyElement === '*') {//如果proxyElement是通配符,返回true
              return true;
            }
            if (proxyElement[0] === '.' &&
                parsed.hostname.substr(parsed.hostname.length - proxyElement.length) === proxyElement &&
                proxyElement.match(/\./g).length === parsed.hostname.match(/\./g).length) {
              return true;
            }

            return parsed.hostname === proxyElement;//判斷proxyElement與請求url的域名是否相等
          });
        }


        if (shouldProxy) {//如果需要代理
          proxy = {
            host: parsedProxyUrl.hostname,//代理域名
            port: parsedProxyUrl.port//代理端口號
          };

          if (parsedProxyUrl.auth) {//如果代理url擁有auth屬性,就給proxy參數加上auth
            var proxyUrlAuth = parsedProxyUrl.auth.split(':');
            proxy.auth = {
              username: proxyUrlAuth[0],
              password: proxyUrlAuth[1]
            };
          }
        }
      }
    }

    if (proxy) {//如果設置了代理參數,根據代理改變options中的配置項
      options.hostname = proxy.host;//主機名
      options.host = proxy.host;//主機
      options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');//host請求頭
      options.port = proxy.port;//端口號
      options.path = protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;//url路徑

      // Basic proxy authorization
      if (proxy.auth) {//如果代理中含有auth參數
        var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');//base64轉碼用戶名和密碼
        options.headers['Proxy-Authorization'] = 'Basic ' + base64;//添加對應請求頭
      }
    }

    var transport;
    var isHttpsProxy = isHttpsRequest && (proxy ? isHttps.test(proxy.protocol) : true);
    //判斷是https協議且含有代理
    if (config.transport) {//如果傳遞了transport,直接存下
      transport = config.transport;
    } else if (config.maxRedirects === 0) {//如果不允許重定向
      transport = isHttpsProxy ? https : http;//判斷使用https模塊還是http模塊
    } else {//如果允許重定向,使用follow-redirects模塊,請求時會自動重定向
      if (config.maxRedirects) {//maxRedirects加入options中
        options.maxRedirects = config.maxRedirects;
      }
      transport = isHttpsProxy ? httpsFollow : httpFollow;
    }

    if (config.maxContentLength && config.maxContentLength > -1) {
      //如果設置了最大正文長度,配置options對應選項
      options.maxBodyLength = config.maxContentLength;
    }

    // Create the request
    //創建請求
    //http.request(option, callback)方法發送http請求,它會返回http.ClientRequest對象
    //通過在http.ClientRequest對象上添加監聽器即可獲得響應對象
    var req = transport.request(options, function handleResponse(res) {
      //傳遞給http.request的callback是response事件的監聽器,只會觸發一次
      //res是http.IncomingMessage對象,可以用來獲取響應狀態碼,響應頭和響應數據
      if (req.aborted) return;//如果請求已經被中止,返回

      // uncompress the response body transparently if required
      var stream = res;
      switch (res.headers['content-encoding']) {
        //判斷content-encoding響應頭,此響應頭指定了用什么編碼來壓縮響應主體
      /*eslint default-case:0*/
      case 'gzip':
      case 'compress':
      case 'deflate':
        // add the unzipper to the body stream processing pipeline
        stream = stream.pipe(zlib.createUnzip());//使用zlib壓縮解壓插件解壓響應主體

        // remove the content-encoding in order to not confuse downstream operations
        //移除content-encoding響應頭為了避免混淆后面的操作
        delete res.headers['content-encoding'];
        break;//跳出
      }

      // return the last request in case of redirects
      var lastRequest = res.req || req;//最后一次請求的請求對象,因為請求有可能發生了重定向

      var response = {//響應數據初始化
        status: res.statusCode,
        statusText: res.statusMessage,
        headers: res.headers,
        config: config,
        request: lastRequest
      };

      if (config.responseType === 'stream') {//如果請求配置中的responseType是stream格式
        response.data = stream;//response.data賦值為stream
        settle(resolve, reject, response);
        //調用settle來根據響應狀態碼來決定是否resolve或者reject當前promise
      } else {//如果配置要求的響應格式不是stream流格式
        var responseBuffer = [];
        stream.on('data', function handleStreamData(chunk) {
          //data事件,接收到數據塊的時候觸發
          responseBuffer.push(chunk);//將數據塊插入responseBuffer數組

          // make sure the content length is not over the maxContentLength if specified
          //如果指定了最大響應正文長度,就判斷是否響應數據長度超過了這個限制
          if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
            //如果響應數據長度超出限制長度,reject當前promise
            reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
              config, null, lastRequest));
          }
        });

        stream.on('error', function handleStreamError(err) {
          //接收流數據塊出錯時觸發error事件
          if (req.aborted) return;//如果請求對象已經中止,返回
          //否則reject當前promise
          reject(enhanceError(err, config, null, lastRequest));
        });

        stream.on('end', function handleStreamEnd() {
          //流數據傳輸完畢時觸發end事件
          var responseData = Buffer.concat(responseBuffer);//將所有數據塊連接成一個整體
          if (config.responseType !== 'arraybuffer') {//如果配置中的響應數據類型不是arrayBuffer
            //就調用toString方法轉換格式,
            responseData = responseData.toString(config.responseEncodingresponseEncoding);
          }

          response.data = responseData;//響應數據賦值
          settle(resolve, reject, response);
          //調用settle來根據響應狀態碼來決定是否resolve或者reject當前promise
        });
      }
    });

    // Handle errors
    req.on('error', function handleRequestError(err) {
      //http.ClientRequest對象綁定error事件,發生錯誤的時候觸發
      if (req.aborted) return;//如果請求對象已經中止,返回
      reject(enhanceError(err, config, null, req));//否則reject當前promise
    });

    // Handle request timeout
    if (config.timeout) {//如果配置項中有超時的配置
      timer = setTimeout(function handleRequestTimeout() {
        //新建一個定時器
        req.abort();//調用abort中止請求
        reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));//reject當前promise
      }, config.timeout);//超時時間過后執行
    }

    if (config.cancelToken) {//如果傳遞了cancelToken配置
      // Handle cancellation
      //設置了cancelToken的請求,只要用戶調用了cancelToken對象的cancel方法,就會執行下面的then回調,中止請求
      config.cancelToken.promise.then(function onCanceled(cancel) {
        //對cancelToken對象上的promise指定resolve后的執行回調
        if (req.aborted) return;//如果請求已經中止,返回

        req.abort();//如果請求沒有中止,就調用abort立即中止
        reject(cancel);//reject當前promise
      });
    }

    // Send the request
    if (utils.isStream(data)) {//如果config.data是流數據,就綁定error事件,讀取數據出錯就reject
      data.on('error', function handleStreamError(err) {
        reject(enhanceError(err, config, null, req));
      }).pipe(req);//將data讀取完畢后用pipe傳遞給http.ClientRequest對象,可讀流傳遞給可寫流,用pipe方法
    } else {
      req.end(data);//否則關閉請求
    }
  });
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM