Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix mimetype handling

  • Loading branch information...
commit 83a4059f17ff0854ffb78eca1f35ca7c67613d61 1 parent b892434
@kriszyp kriszyp authored
Showing with 51 additions and 45 deletions.
  1. +51 −45 lib/jsgi/static.js
View
96 lib/jsgi/static.js
@@ -44,51 +44,57 @@ exports.Static = function(options, nextApp){
.then(function (stat) {
var ifModHeader=request['headers']['if-modified-since'];
if(stat.isFile()){
- // file exists.
- var extension = filePath.match(/\.[^\.]+$/);
- extension = extension && extension[0];
- var mimeType = extension && mime.mimeType(extension);
- var modifiedTime=new Date(stat.mtime);
- if(ifModHeader && new Date(ifModHeader)>=modifiedTime){
- // If it hasn't changed, do no further work.
- var headers = {"Date": new Date().toUTCString()};
- if(cachePolicy[mimeType]){
- headers['Expires']=new Date(new Date().valueOf()+
- cachePolicy[mimeType]*1000).toUTCString();
- }
- responseDeferred.resolve({
- status: 304,
- headers: headers,
- body: []
- });
- } else {
- // File has changed since last served to this client.
- fs.open(filePath, "r", 0666)
- .then(function (file) {
- var headers = {"content-length": stat.size,
- "Vary":"Accept-Encoding",
- "Last-Modified": modifiedTime.toUTCString()};
- if(cachePolicy[mimeType]){
- headers['Expires']=new Date(new Date().valueOf()+
- cachePolicy[mimeType]*1000).toUTCString();
- }
- if(mimeType.substr(0,4)=='text' ||
- mimeType.indexOf('xml')>=0 ||
- mimeType.indexOf('json')>=0) {
- mimeType+='; charset=UTF-8';
- }
- headers['content-type']=mimeType;
- stat = fs.statSync(filePath); // re-retrieve it
- var bodyDeferred = defer();
- var write;
- file.encoding = "binary";
- responseDeferred.resolve({
- status: 200,
- headers: headers,
- body: file
- });
- }, onFail);
- }
+ if(request.method == "GET"){
+ // file exists.
+ var extension = filePath.match(/\.[^\.]+$/);
+ extension = extension && extension[0];
+ var mimeType = extension && mime.mimeType(extension);
+ var modifiedTime=new Date(stat.mtime);
+ if(ifModHeader && new Date(ifModHeader)>=modifiedTime){
+ // If it hasn't changed, do no further work.
+ var headers = {"Date": new Date().toUTCString()};
+ if(mimeType in cachePolicy || "*" in cachePolicy){
+ headers['Expires']=new Date(new Date().valueOf()+
+ (mimeType in cachePolicy ? cachePolicy[mimeType] : cachePolicy["*"]) *1000).toUTCString();
+ }
+ responseDeferred.resolve({
+ status: 304,
+ headers: headers,
+ body: []
+ });
+ } else {
+ // File has changed since last served to this client.
+ fs.open(filePath, "r", 0666)
+ .then(function (file) {
+ var headers = {"content-length": stat.size,
+ "Vary":"Accept-Encoding",
+ "Date": new Date().toUTCString(),
+ "Last-Modified": modifiedTime.toUTCString()};
+ if(mimeType in cachePolicy || "*" in cachePolicy){
+ headers['Expires']=new Date(new Date().valueOf()+
+ (mimeType in cachePolicy ? cachePolicy[mimeType] : cachePolicy["*"]) *1000).toUTCString();
+ }
+ if(mimeType && (mimeType.substr(0,4)=='text' ||
+ mimeType.indexOf('xml')>=0 ||
+ mimeType.indexOf('json')>=0)) {
+ mimeType+='; charset=UTF-8';
+ }
+ headers['content-type']=mimeType;
+ stat = fs.statSync(filePath); // re-retrieve it
+ var bodyDeferred = defer();
+ var write;
+ file.encoding = "binary";
+ responseDeferred.resolve({
+ status: 200,
+ headers: headers,
+ body: file
+ });
+ }, onFail);
+ }
+ }else{
+ request.filePath = filePath;
+ onFail();
+ }
}
else if(stat.isDirectory()){
var modifiedTime=new Date(stat.mtime);
Please sign in to comment.
Something went wrong with that request. Please try again.