aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/framework-src/modules/faststatic.js
blob: 5cca676b496ef74357b543272b4610be1e3368fa (plain) (blame)
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
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileOverview serving static files, including js and css, and cacheing
 *               and minifying.
 *
 * Terminology Note:
 *    "path" is confusing because paths can be part of URLs and part
 *     of filesystem paths, and static files have both types of paths
 *     associated with them.  Therefore, in this module:
 *
 *      LOCALDIR or LOCALFILE refers to directories or files on the filesystem.
 *
 *      HREF is used to describe things that go in a URL.
 */

import("fileutils.{readFile,readFileBytes}");
import("yuicompressor");
import("stringutils");
import("varz");
import("ejs.EJS");

jimport("java.lang.System.out.println");

//----------------------------------------------------------------
// Content Type Guessing
//----------------------------------------------------------------

var _contentTypes = {
  'gif': 'image/gif',
  'png': 'image/png',
  'jpg': 'image/jpeg',
  'jpeg': 'image/jpeg',
  'css': 'text/css',
  'js': 'application/x-javascript',
  'txt': 'text/plain',
  'html': 'text/html; charset=utf-8',
  'ico': 'image/x-icon',
  'swf': 'application/x-shockwave-flash',
  'zip': 'application/zip',
  'xml': 'application/xml'
};

var _gzipableTypes = {
  'text/css': true,
  'application/x-javascript': true,
  'text/html; charset=utf-8': true
};

function _guessContentType(path) {
  var ext = path.split('.').pop().toLowerCase();
  return _contentTypes[ext] || 'text/plain';
}

//----------------------------------------------------------------

function _getCache(name) {
  var m = 'faststatic';
  if (!appjet.cache[m]) {
    appjet.cache[m] = {};
  }
  var c = appjet.cache[m];
  if (!c[name]) {
    c[name] = {};
  }
  return c[name];
}

var _mtimeCheckInterval = 5000; // 5 seconds

function _getMTime(f) {
  var mcache = _getCache('mtimes');
  var now = +(new Date);
  if (appjet.config.devMode ||
      !(mcache[f] && (now - mcache[f].lastCheck < _mtimeCheckInterval))) {
    var jfile = new net.appjet.oui.JarVirtualFile(f);
    if (jfile.exists() && !jfile.isDirectory()) {
      mcache[f] = {
	lastCheck:  now,
	mtime: jfile.lastModified()
      };
    } else {
      mcache[f] = null;
    }
  }
  if (mcache[f]) {
    return +mcache[f].mtime;
  } else {
    return null;
  }
}

function _wrapFile(localFile) {
  return {
    getPath: function() { return localFile; },
    getMTime: function() { return _getMTime(localFile); },
    getContents: function() { return _readFileAndProcess(localFile, 'string'); }
  };
}

function _readFileAndProcess(fileName, type) {
  if (fileName.slice(-8) == "_ejs.css") {
    // run CSS through EJS
    var template = readFile(fileName);
    var ejs = new EJS({text:template, name:fileName});
    var resultString = ejs.render({});
    if (type == 'bytes') {
      return new java.lang.String(resultString).getBytes("UTF-8");
    }
    else {
      return resultString;
    }
  }
  else if (type == 'string') {
    return readFile(fileName);
  }
  else if (type == 'bytes') {
    return readFileBytes(fileName);
  }
}

function _cachedFileBytes(f) {
  var mtime = _getMTime(f);
  if (!mtime) { return null; }
  var fcache = _getCache('file-bytes-cache');
  if (!(fcache[f] && (fcache[f].mtime == mtime))) {
    varz.incrementInt("faststatic-file-bytes-cache-miss");
    var bytes = _readFileAndProcess(f, 'bytes');
    if (bytes) {
      fcache[f] = {mtime: mtime, bytes: bytes};
    };
  }
  if (fcache[f] && fcache[f].bytes) {
    return fcache[f].bytes;
  } else {
    return null;
  }
}

function _shouldGzip(contentType) {
  var userAgent = request.headers["User-Agent"];
  if (! userAgent) return false;
  if (! (/Firefox/.test(userAgent) || /webkit/i.test(userAgent))) return false;
  if (! _gzipableTypes[contentType]) return false;

	return request.acceptsGzip;
}

function _getCachedGzip(original, key) {
  var c = _getCache("gzipped");
  if (! c[key] || ! java.util.Arrays.equals(c[key].original, original)) {
    c[key] = {original: original,
              gzip: stringutils.gzip(original)};
  }
  return c[key].gzip;
}

function _setGzipHeader() {
  response.setHeader("Content-Encoding", "gzip");
}

//----------------------------------------------------------------

/**
 * Function for serving a single static file.
 */
function singleFileServer(localPath, opts) {
  var contentType = _guessContentType(localPath);

  return function() {
    (opts.cache ? response.alwaysCache() : response.neverCache());
    response.setContentType(contentType);
    var bytes = _cachedFileBytes(localPath);
    if (bytes) {
      if (_shouldGzip(contentType)) {
        bytes = _getCachedGzip(bytes, "file:"+localPath);
        _setGzipHeader();
      }
      response.writeBytes(bytes);
      return true;
    } else {
      return false;
    }
  };
}

/**
 * valid opts:
 *   alwaysCache: default false
 */
function directoryServer(localDir, opts) {
  if (stringutils.endsWith(localDir, "/")) {
    localDir = localDir.substr(0, localDir.length-1);
  }
  return function(relpath) {
    if (stringutils.startsWith(relpath, "/")) {
      relpath = relpath.substr(1);
    }
    if (relpath.indexOf('..') != -1) {
      response.forbid();
    }
    (opts.cache ? response.alwaysCache() : response.neverCache());
    var contentType = _guessContentType(relpath);
    response.setContentType(contentType);
    var fullPath = localDir + "/" + relpath;
    var bytes = _cachedFileBytes(fullPath);

    if (bytes) {
      if (_shouldGzip(contentType)) {
        bytes = _getCachedGzip(bytes, "file:"+fullPath);
        _setGzipHeader();
      }
      response.writeBytes(bytes);
      return true;
    } else {
      return false;
    }
  };
}

/**
 * Serves cat files, which are concatenated versions of many files.
 */
function compressedFileServer(opts) {
  var cfcache = _getCache('compressed-files');
  return function() {
    var key = request.path.split('/').slice(-1)[0];
    var contentType = _guessContentType(request.path);
    response.setContentType(contentType);
    response.alwaysCache();
    var data = cfcache[key];
    if (data) {
      if (_shouldGzip(contentType)) {
        data = _getCachedGzip((new java.lang.String(data)).getBytes(response.getCharacterEncoding()), "comp:"+key);
        _setGzipHeader();
        response.writeBytes(data);
      } else {
        response.write(data);
      }
      return true;
    } else {
      return false;
    }
  };
}

function getCompressedFilesKey(type, baseLocalDir, localFileList) {
  if (stringutils.endsWith(baseLocalDir, '/')) {
    baseLocalDir = baseLocalDir.substr(0, baseLocalDir.length-1);
  }

  var fileList = [];
  // convert passed-in file list into list of our file objects
  localFileList.forEach(function(f) {
    if (typeof(f) == 'string') {
      fileList.push(_wrapFile(baseLocalDir+'/'+f));
    } else {
      fileList.push(f);
    }
  });

  // have we seen this exact fileset before?
  var fsId = fileList.map(function(f) { return f.getPath(); }).join('|');
  var fsMTime = Math.max.apply(this,
			       fileList.map(function(f) { return f.getMTime(); }));

  var kdcache = _getCache('fileset-keydata-cache');
  if (!(kdcache[fsId] && (kdcache[fsId].mtime == fsMTime))) {
    //println("cache miss for fileset: "+fsId);
    //println("compressing fileset...");
    kdcache[fsId] = {
      mtime: fsMTime,
      keyString: _compressFilesAndMakeKey(type, fileList)
    };
  }
  return kdcache[fsId].keyString;
}

function _compressFilesAndMakeKey(type, fileList) {
  function _compress(s) {
    if (type == 'css') {
      varz.incrementInt("faststatic-yuicompressor-compressCSS");
      return yuicompressor.compressCSS(s);
    } else if (type == 'js') {
      varz.incrementInt("faststatic-yuicompressor-compressJS");
      return yuicompressor.compressJS(s);
    } else {
      throw Error('Dont know how to compress this filetype: '+type);
    }
  }

  var fullstr = "";
  fileList.forEach(function(f) {
    fullstr += _compress(f.getContents());
  });

  fullstr = _compress(fullstr);

  var key = stringutils.md5(fullstr) + '.' + type;
  var cfcache = _getCache('compressed-files');
  cfcache[key] = fullstr;
  return key;
}