Code coverage report for thinkjs/lib/Lib/Core/Http.js

Statements: 99.4% (166 / 167)      Branches: 100% (93 / 93)      Functions: 96.88% (31 / 32)      Lines: 99.4% (166 / 167)      Ignored: none     

All files » thinkjs/lib/Lib/Core/ » Http.js
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404        2 2 2 2 2 2 2 2 2   2 2   2   70 70   70   70               67 67   67 67 9   58             12 9   3 3             3 3 3         3 1   3 1   3 2     3 1 1   3 3             4 4 4 4 4 4 4   4 8 8   8 4   8 8   8 2 2 2   6   8 2 2 2     4     4             1 1 1 1 1 1 1 1 1 1     1 1           1   1                 9 1     8 8 3 5 1   4               69               7                   8 8 8 1 7 1   6         69     69 69   69   69   69   69   69             69                         54 53                   16 16 1   16 16 13       16 1   16         16 12   16 9   16 16 16             77 5   77 4 4                   4 4 4               6 6                 37 37 3   34 16   34 2   34 34 34 3   31   34               35 35 35 35     69   69                 2   21 21 15 2 13 4   9     21 21 5   21                                             2   7    
/**
 * 对HttpRequest和HttpResponse 2个对象重新包装
 * @type {Object}
 */
var querystring = require('querystring');
var url = require('url');
var cookie = thinkRequire('Cookie');
var EventEmitter = require('events').EventEmitter;
var multiparty = require('multiparty');
var crypto = require('crypto');
var os = require('os');
var path = require('path');
var fs = require('fs');
 
var localIp = '127.0.0.1';
var Http = module.exports = Class(function(){
  'use strict';
  return {
    init: function(req, res){
      this.req = req;
      this.res = res;
      //http对象为EventEmitter的实例
      this.http = new EventEmitter();
      //记录当前请求的开始时间
      this.http.startTime = Date.now();
    },
    /**
     * 执行
     * @param  {Function} callback [description]
     * @return Promise            [description]
     */
    run: function(){
      this._request();
      this._response();
      //数组的indexOf要比字符串的indexOf略快
      var methods = ['POST', 'PUT', 'PATCH'];
      if (methods.indexOf(this.req.method) > -1) {
        return this.getPostData();
      }
      return getPromise(this.http);
    },
    /**
     * 检测是否含有post数据
     * @return {Boolean} [description]
     */
    hasPostData: function(){
      if ('transfer-encoding' in this.req.headers) {
        return true;
      }
      var contentLength = this.req.headers['content-length'] | 0;
      return contentLength > 0;
    },
    /**
     * 含有文件的表单上传
     * @return {[type]} [description]
     */
    _filePost: function(){
      var deferred = getDefer();
      var self = this;
      var form = this.form = new multiparty.Form({
        maxFieldsSize: C('post_max_fields_size'),
        maxFields: C('post_max_fields'),
        maxFilesSize: C('post_max_file_size')
      });
      form.on('file', function(name, value){
        self.http.file[name] = value;
      });
      form.on('field', function(name, value){
        self.http.post[name] = value;
      });
      form.on('close', function(){
        deferred.resolve(self.http);
      });
      //有错误后直接拒绝当前请求
      form.on('error', function(){
        self.res.statusCode = 413;
        self.res.end();
      });
      form.parse(this.req);
      return deferred.promise;
    },
    /**
     * 普通的表单上传
     * @return {[type]} [description]
     */
    _commonPost: function(){
      var buffers = [];
      var length = 0;
      var self = this;
      var deferred = getDefer();
      this.req.on('data', function(chunk){
        buffers.push(chunk);
        length += chunk.length;
      });
      this.req.on('end', function(){
        self.http.payload = Buffer.concat(buffers).toString();
        tag('form_parse', self.http).then(function(){
          //默认使用querystring.parse解析
          if (isEmpty(self.http.post) && self.http.payload) {
            self.http.post = querystring.parse(self.http.payload);
          }
          var post = self.http.post;
          var length = Object.keys(post).length;
          //最大表单数超过限制
          if (length > C('post_max_fields')) {
            self.res.statusCode = 413;
            self.res.end();
            return;
          }
          for(var name in post){
            //单个表单值长度超过限制
            if (post[name].length > C('post_max_fields_size')) {
              self.res.statusCode = 413;
              self.res.end();
              return;
            }
          }
          deferred.resolve(self.http);
        })
      });
      return deferred.promise;
    },
    /**
     * 通过ajax上传文件
     * @return {[type]} [description]
     */
    _ajaxFilePost: function(){
      var self = this;
      var filename = this.req.headers[C('post_ajax_filename_header')];
      var deferred = getDefer();
      var filepath = os.tmpdir() + '/thinkjs_upload/';
      var name = crypto.randomBytes(20).toString('base64').replace(/\+/g, '_').replace(/\//g, '-');
      mkdir(filepath);
      filepath += name + path.extname(filename);
      var stream = fs.createWriteStream(filepath);
      this.req.pipe(stream);
      stream.on('error', function(err){
        deferred.reject(err);
      })
      stream.on('close', function(){
        self.http.file.file = {
          fieldName: 'file',
          originalFilename: filename,
          path: filepath,
          size: fs.statSync(filepath).size
        }
        deferred.resolve(self.http);
      })
      return deferred.promise;
    },
    /**
     * 获取POST过来的数据,包含上传的文件
     * 依赖multiparty库
     * @return {[type]}            [description]
     */
    getPostData: function(){
      //没有post数据,直接返回
      if (!this.hasPostData()) {
        return getPromise(this.http);
      }
      //上传的数据中是否含有文件的检测正则
      var multiReg = /^multipart\/(form-data|related);\s*boundary=(?:"([^"]+)"|([^;]+))$/i;
      if (multiReg.test(this.req.headers['content-type'])) {
        return this._filePost();
      }else if(this.req.headers[C('post_ajax_filename_header')]){ //通过ajax上传文件
        return this._ajaxFilePost();
      }else{
        return this._commonPost();
      }
    },
    /**
     * HttpRequest增强
     * @return {[type]} [description]
     */
    _request: function(){
      var req = {
        //http版本号
        version: this.req.httpVersion,
        //请求方法
        method: this.req.method,
        //请求头
        headers: this.req.headers,
        getHeader: function(name){
          return this.headers[name] || '';
        },
        //请求的Content-Type
        contentType: (this.req.headers['content-type'] || '').split(';')[0].trim(),
        //post信息
        post: {},
        //上传的文件信息
        file: {},
        //请求用户的ip
        ip: function(){
          var connection = this.req.connection;
          var socket = this.req.socket;
          if (connection && connection.remoteAddress !== localIp) {
            return connection.remoteAddress;
          }else if (socket && socket.remoteAddress !== localIp) {
            return socket.remoteAddress;
          }
          return this.headers['x-forwarded-for'] || this.headers['x-real-ip'] || localIp;
        },
        //请求的cookie
        cookie: cookie.parse(this.req.headers.cookie || '')
      };
      extend(this.http, req);
 
      //解析url中的参数
      var urlInfo = url.parse('//' + req.headers.host + this.req.url, true, true);
      this.http.pathname = urlInfo.pathname;
      //query只记录?后面的参数
      this.http.query = urlInfo.query;
      //get包含路由解析追加的参数
      this.http.get = extend({}, urlInfo.query);
      //主机名,带端口
      this.http.host = urlInfo.host;
      //主机名,不带端口
      this.http.hostname = urlInfo.hostname;
      //将原生的request对象放在http上,方便后续在controller等地方使用
      this.http.req = this.req;
    },
    /**
     * HttpResponse增强
     * @return {[type]} [description]
     */
    _response: function(){
      var res = {
        /**
         * 一次请求下,可能会发送多个Cookie,所以这里不能立即发送
         * 需要临时存起来,到输出内容前统一发送
         * @type {Object}
         */
        _cookie: {},
        /**
         * 发送header
         * @param {[type]} name  [description]
         * @param {[type]} value [description]
         */
        setHeader: function(name, value){
          if (!this.res.headersSent) {
            this.res.setHeader(name, value);
          }
        },
        /**
         * 设置cookie
         * @param {[type]} name    [description]
         * @param {[type]} value   [description]
         * @param {[type]} options [description]
         */
        setCookie: function(name, value, options){
          options = options || {};
          if (typeof options === 'number') {
            options = {timeout: options};
          }
          var timeout = options.timeout;
          if (timeout === undefined) {
            timeout = C('cookie_timeout');
          }
          //delete options.timeout;
          //if value is null, remove cookie
          if (value === null) {
            timeout = -1000;
          }
          var defaultOptions = {
            path: C('cookie_path'),
            domain: C('cookie_domain'),
            expires: new Date (Date.now() + timeout * 1000)
          };
          if (timeout === 0) {
            delete defaultOptions.expires;
          }
          for(var key in options){
            defaultOptions[key.toLowerCase()] = options[key];
          }
          defaultOptions.name = name;
          defaultOptions.value = value + '';
          this._cookie[name] = defaultOptions;
        },
        /**
         * 将队列中的cookie发送出去
         * @return {[type]} [description]
         */
        sendCookie: function(){
          var cookies = Object.values(this._cookie).map(function(item){
            return cookie.stringify(item.name, item.value, item);
          });
          if (cookies.length) {
            this.setHeader('Set-Cookie', cookies);
            this._cookie = {};
          }
        },
        /**
         * url跳转
         * @param  {[type]} url  [description]
         * @param  {[type]} code [description]
         * @return {[type]}      [description]
         */
        redirect: function(url, code){
          this.res.statusCode = code || 302;
          this.setHeader('Location', url || '/');
          this.end();
        },
        /**
         * 发送执行时间
         * @param  {[type]} name [description]
         * @return {[type]}      [description]
         */
        sendTime: function(name){
          var time = Date.now() - this.startTime;
          this.setHeader('X-' + (name || 'EXEC-TIME'), time + 'ms');
        },
        /**
         * 输出内容
         * @param  {[type]} obj      [description]
         * @param  {[type]} encoding [description]
         * @return {[type]}          [description]
         */
        echo: function(obj, encoding){
          this.sendCookie();
          if (obj === undefined) {
            return;
          }
          if (isArray(obj) || isObject(obj)) {
            obj = JSON.stringify(obj);
          }
          if (!isString(obj) && !isBuffer(obj)) {
            obj += '';
          }
          var self = this;
          return tag('content_write', this, obj).then(function(obj){
            if (isBuffer(obj)) {
              self.res.write(obj);
            }else{
              self.res.write(obj, encoding || C('encoding'));
            }
            return obj;
          })
        },
        /**
         * 结束URL
         * @return {[type]} [description]
         */
        end: function(){
          this.emit('beforeEnd', this);
          this.sendCookie();
          this.res.end();
          this.emit('afterEnd', this);
        }
      };
      extend(this.http, res);
      //将原生的response对象放在http上,方便后续controller等地方使用
      this.http.res = this.res;
    }
  };
});
/**
 * 获取默认的http信息
 * @param  {[type]} data [description]
 * @return {[type]}      [description]
 */
Http.getDefaultHttp = function(data){
  'use strict';
  data = data || {};
  if (isString(data)) {
    if (data[0] === '{') {
      data = JSON.parse(data);
    }else if (/^[\w]+\=/.test(data)) {
      data = querystring.parse(data);
    }else{
      data = {url: data};
    }
  }
  var url = data.url || '';
  if (url.indexOf('/') !== 0) {
    url = '/' + url;
  }
  return {
    req: {
      httpVersion: '1.1',
      method: (data.method || 'GET').toUpperCase(),
      url: url,
      headers: extend({
        host: data.host || localIp
      }, data.headers),
      connection: {
        remoteAddress: data.ip || localIp
      }
    },
    res: {
      end: data.end || data.close || Http.empty,
      write: data.write || data.send || Http.empty,
      setHeader: Http.empty
    }
  };
};
/**
 * 空函数
 * @return {[type]} [description]
 */
Http.empty = function(data){
  'use strict';
  return data;
}