Permalink
Browse files

handle put requests synchronously

  • Loading branch information...
1 parent 4ea4dff commit 070e0212e27a973a3a529d467f01edd8e1ac6586 @nightwing nightwing committed Mar 31, 2013
Showing with 20 additions and 13 deletions.
  1. +1 −1 server/js/package.json
  2. +19 −12 server/js/server.js
View
@@ -1,7 +1,7 @@
{
"name": "node-webfs",
+ "version": "0.1.0",
"dependencies": {
- "nak": "~0.2.4",
"mkdirp": "~0.3.5"
},
"scripts": {}
View
@@ -28,7 +28,7 @@ if (arg[0])
if (arg[1])
PORT = parseInt(arg[1]);
-ROOT = pathlib.normalize(ROOT);
+ROOT = pathlib.resolve(ROOT);
var webfs = {
@@ -78,23 +78,31 @@ var webfs = {
});
},
doPUT: function(req, res, filePath) {
- var parentDir = pathlib.dirname(filePath);
- mkdirp(parentDir, function(err, made) {
- var writeStream = fs.createWriteStream(filePath);
- req.pipe(writeStream);
- req.on('end', function () {
+ var data = "";
+ req.on("data", function(chunk) {
+ data += chunk;
+ });
+ req.on("error", function() {
+ webfs.error(res, 404, "Could't save file");
+ });
+ req.on("end", function() {
+ var parentDir = pathlib.dirname(filePath);
+ mkdirp(parentDir, function(err, made) {
+ try {
+ fs.writeFileSync(filePath, data);
+ } catch(e) {
+ console.log(e);
+ return webfs.error(res, 404, "Could't save file");
+ }
fs.stat(filePath, function(err, stat){
if (err)
return webfs.error(res, 404, "Path not found");
-
+
res.statusCode = 200;
webfs.sendEtagHeader(res, stat);
res.end("OK");
});
});
- writeStream.on('error', function (err) {
- webfs.error(res, 404, "Can't save file");
- });
});
},
doDELETE: function(req, res, filePath) {
@@ -130,9 +138,8 @@ var webfs = {
http.createServer(function(req, res) {
var filePath = decodeURIComponent(urllib.parse(req.url).path);
- filePath = pathlib.join(ROOT, filePath);
-
console.log(req.method, filePath);
+ filePath = pathlib.join(ROOT, filePath);
if (filePath.lastIndexOf(ROOT, 0) != 0)
return webfs.error(res, 500, "Hacker attempt?");

0 comments on commit 070e021

Please sign in to comment.