Skip to content

Commit

Permalink
Implemented the 'If' header check in all methods where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 23, 2017
1 parent e90274f commit fab8388
Show file tree
Hide file tree
Showing 25 changed files with 1,149 additions and 959 deletions.
1 change: 1 addition & 0 deletions lib/server/MethodCallArgs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class MethodCallArgs {
user: IUser;
protected constructor(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, exit: () => void, callback: () => void);
static create(server: WebDAVServer, request: http.IncomingMessage, response: http.ServerResponse, callback: (error: Error, mca: MethodCallArgs) => void): void;
checkIfHeader(defaultResource: IResource, callback: () => void): void;
requireCustomPrivilege(privileges: string | string[], resource: IResource, callback: () => void): void;
requirePrivilege(privileges: BasicPrivilege | BasicPrivilege[], resource: IResource, callback: () => void): void;
requireErCustomPrivilege(privileges: string | string[], resource: IResource, callback: (error: Error, can: boolean) => void): void;
Expand Down
21 changes: 21 additions & 0 deletions lib/server/MethodCallArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
var IPrivilegeManager_1 = require("../user/privilege/IPrivilegeManager");
var XML_1 = require("../helper/XML");
var IfParser_1 = require("../helper/IfParser");
var HTTPCodes_1 = require("./HTTPCodes");
var FSManager_1 = require("../manager/FSManager");
var Errors_1 = require("../Errors");
Expand Down Expand Up @@ -37,6 +38,26 @@ var MethodCallArgs = (function () {
});
});
};
MethodCallArgs.prototype.checkIfHeader = function (defaultResource, callback) {
var _this = this;
var ifHeader = this.findHeader('If');
if (!ifHeader) {
callback();
return;
}
IfParser_1.parseIfHeader(ifHeader)(this, defaultResource, function (e, passed) {
if (e) {
_this.setCode(HTTPCodes_1.HTTPCodes.InternalServerError);
_this.exit();
}
else if (!passed) {
_this.setCode(HTTPCodes_1.HTTPCodes.PreconditionFailed);
_this.exit();
}
else
callback();
});
};
MethodCallArgs.prototype.requireCustomPrivilege = function (privileges, resource, callback) {
var _this = this;
IPrivilegeManager_1.requirePrivilege(privileges, this, resource, function (e, can) {
Expand Down
146 changes: 74 additions & 72 deletions lib/server/commands/Copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,100 +102,102 @@ function default_1(arg, callback) {
callback();
return;
}
var overwrite = arg.findHeader('overwrite') !== 'F';
var destination = arg.findHeader('destination');
if (!destination) {
arg.setCode(WebDAVRequest_1.HTTPCodes.BadRequest);
callback();
return;
}
destination = destination.substring(destination.indexOf('://') + '://'.length);
destination = destination.substring(destination.indexOf('/'));
destination = new FSManager_1.FSPath(destination);
arg.server.getResourceFromPath(destination.getParent(), function (e, rDest) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
arg.checkIfHeader(source, function () {
var overwrite = arg.findHeader('overwrite') !== 'F';
var destination = arg.findHeader('destination');
if (!destination) {
arg.setCode(WebDAVRequest_1.HTTPCodes.BadRequest);
callback();
return;
}
arg.requirePrivilege(['canGetType'], source, function () {
arg.requirePrivilege(['canGetChildren'], rDest, function () {
source.type(function (e, type) { return process.nextTick(function () {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
function done(overridded) {
copy(arg, source, rDest, destination, function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else if (overridded)
arg.setCode(WebDAVRequest_1.HTTPCodes.NoContent);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
callback();
});
}
var nb = 0;
function go(error, destCollision) {
if (nb <= 0)
return;
if (error) {
nb = -1;
destination = destination.substring(destination.indexOf('://') + '://'.length);
destination = destination.substring(destination.indexOf('/'));
destination = new FSManager_1.FSPath(destination);
arg.server.getResourceFromPath(destination.getParent(), function (e, rDest) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
arg.requirePrivilege(['canGetType'], source, function () {
arg.requirePrivilege(['canGetChildren'], rDest, function () {
source.type(function (e, type) { return process.nextTick(function () {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
if (destCollision) {
nb = -1;
if (!overwrite) {
function done(overridded) {
copy(arg, source, rDest, destination, function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else if (overridded)
arg.setCode(WebDAVRequest_1.HTTPCodes.NoContent);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
callback();
});
}
var nb = 0;
function go(error, destCollision) {
if (nb <= 0)
return;
if (error) {
nb = -1;
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
destCollision.type(function (e, destType) { return process.nextTick(function () {
if (e) {
callback(e);
return;
}
if (destType !== type) {
if (destCollision) {
nb = -1;
if (!overwrite) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
destCollision.delete(function (e) { return process.nextTick(function () {
destCollision.type(function (e, destType) { return process.nextTick(function () {
if (e) {
callback(e);
return;
}
done(true);
if (destType !== type) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
destCollision.delete(function (e) { return process.nextTick(function () {
if (e) {
callback(e);
return;
}
done(true);
}); });
}); });
}); });
return;
}
--nb;
if (nb === 0) {
done(false);
}
}
rDest.getChildren(function (e, children) { return process.nextTick(function () {
if (e) {
go(e, null);
return;
}
nb += children.length;
if (nb === 0) {
done(false);
return;
return;
}
--nb;
if (nb === 0) {
done(false);
}
}
children.forEach(function (child) {
child.webName(function (e, name) { return process.nextTick(function () {
go(e, name === destination.fileName() ? child : null);
}); });
});
rDest.getChildren(function (e, children) { return process.nextTick(function () {
if (e) {
go(e, null);
return;
}
nb += children.length;
if (nb === 0) {
done(false);
return;
}
children.forEach(function (child) {
child.webName(function (e, name) { return process.nextTick(function () {
go(e, name === destination.fileName() ? child : null);
}); });
});
}); });
}); });
}); });
});
});
});
});
Expand Down
18 changes: 10 additions & 8 deletions lib/server/commands/Delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ function default_1(arg, callback) {
callback();
return;
}
arg.requirePrivilege(['canDelete'], r, function () {
r.delete(function (e) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
}); });
arg.checkIfHeader(r, function () {
arg.requirePrivilege(['canDelete'], r, function () {
r.delete(function (e) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
}); });
});
});
});
}
Expand Down
38 changes: 20 additions & 18 deletions lib/server/commands/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ function default_1(arg, callback) {
callback();
return;
}
var targetSource = arg.findHeader('source', 'F').toUpperCase() === 'T';
arg.requirePrivilege(targetSource ? ['canRead', 'canSource'] : ['canRead'], r, function () {
r.read(targetSource, function (e, c) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
else {
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
var content = c;
if (c === undefined || c === null)
content = new Buffer(0);
else if (c.constructor === Boolean || c.constructor === Number)
content = c.toString();
else
content = c;
arg.response.write(content);
}
callback();
}); });
arg.checkIfHeader(r, function () {
var targetSource = arg.findHeader('source', 'F').toUpperCase() === 'T';
arg.requirePrivilege(targetSource ? ['canRead', 'canSource'] : ['canRead'], r, function () {
r.read(targetSource, function (e, c) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
else {
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
var content = c;
if (c === undefined || c === null)
content = new Buffer(0);
else if (c.constructor === Boolean || c.constructor === Number)
content = c.toString();
else
content = c;
arg.response.write(content);
}
callback();
}); });
});
});
});
}
Expand Down
18 changes: 10 additions & 8 deletions lib/server/commands/Head.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ function default_1(arg, callback) {
return;
}
var targetSource = arg.findHeader('source', 'F').toUpperCase() === 'T';
arg.requirePrivilege(targetSource ? ['canRead', 'canSource'] : ['canRead'], r, function () {
r.read(targetSource, function (e, c) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
}); });
arg.checkIfHeader(r, function () {
arg.requirePrivilege(targetSource ? ['canRead', 'canSource'] : ['canRead'], r, function () {
r.read(targetSource, function (e, c) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
}); });
});
});
});
}
Expand Down
Loading

0 comments on commit fab8388

Please sign in to comment.