Skip to content

Commit

Permalink
Update for v0.1.94
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 10, 2010
1 parent 7935af4 commit f2d65d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
47 changes: 20 additions & 27 deletions fu.js
Expand Up @@ -9,11 +9,10 @@ var fu = exports;
var NOT_FOUND = "Not Found\n";

function notFound(req, res) {
res.sendHeader(404, [ ["Content-Type", "text/plain"]
, ["Content-Length", NOT_FOUND.length]
]);
res.write(NOT_FOUND);
res.close();
res.writeHead(404, { "Content-Type": "text/plain"
, "Content-Length": NOT_FOUND.length
});
res.end(NOT_FOUND);
}

var getMap = {};
Expand All @@ -26,20 +25,18 @@ var server = createServer(function (req, res) {
var handler = getMap[url.parse(req.url).pathname] || notFound;

res.simpleText = function (code, body) {
res.sendHeader(code, [ ["Content-Type", "text/plain"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
res.writeHead(code, { "Content-Type": "text/plain"
, "Content-Length": body.length
});
res.end(body);
};

res.simpleJSON = function (code, obj) {
var body = JSON.stringify(obj);
res.sendHeader(code, [ ["Content-Type", "text/json"]
, ["Content-Length", body.length]
]);
res.write(body);
res.close();
res.writeHead(code, { "Content-Type": "text/json"
, "Content-Length": body.length
});
res.end(body);
};

handler(req, res);
Expand All @@ -61,7 +58,6 @@ function extname (path) {
fu.staticHandler = function (filename) {
var body, headers;
var content_type = fu.mime.lookupExtension(extname(filename));
var encoding = (content_type.slice(0,4) === "text" ? "utf8" : "binary");

function loadResponseData(callback) {
if (body && headers && !DEBUG) {
Expand All @@ -70,17 +66,15 @@ fu.staticHandler = function (filename) {
}

sys.puts("loading " + filename + "...");
readFile(filename, encoding, function (err, data) {
readFile(filename, function (err, data) {
if (err) {
sys.puts("Error loading " + filename);
} else {
body = data;
headers = [ [ "Content-Type" , content_type ]
, [ "Content-Length" , body.length ]
];
if (!DEBUG)
headers.push(["Cache-Control", "public"]);

headers = { "Content-Type": content_type
, "Content-Length": body.length
};
if (!DEBUG) headers["Cache-Control"] = "public";
sys.puts("static file " + filename + " loaded");
callback();
}
Expand All @@ -89,9 +83,8 @@ fu.staticHandler = function (filename) {

return function (req, res) {
loadResponseData(function () {
res.sendHeader(200, headers);
res.write(body, encoding);
res.close();
res.writeHead(200, headers);
res.end(body);
});
}
};
Expand All @@ -102,7 +95,7 @@ fu.mime = {
lookupExtension : function(ext, fallback) {
return fu.mime.TYPES[ext.toLowerCase()] || fallback || 'application/octet-stream';
},

// List of most common mime-types, stolen from Rack.
TYPES : { ".3gp" : "video/3gpp"
, ".a" : "application/octet-stream"
Expand Down
2 changes: 1 addition & 1 deletion server.js
Expand Up @@ -64,7 +64,7 @@ var channel = new function () {
while (callbacks.length > 0 && now - callbacks[0].timestamp > 30*1000) {
callbacks.shift().callback([]);
}
}, 1000);
}, 3000);
};

var sessions = {};
Expand Down

0 comments on commit f2d65d9

Please sign in to comment.