Skip to content

Commit

Permalink
Implemented the gateway check + Added the 'totalSize' to the 'write(.…
Browse files Browse the repository at this point in the history
…..)' method of resources when available + Added the 'MustIgnore' error skip
  • Loading branch information
AdrienCastex committed Jun 19, 2017
1 parent 2c89532 commit a58d38f
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 63 deletions.
2 changes: 1 addition & 1 deletion lib/server/MethodCallArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var MethodCallArgs = (function () {
var mca = new MethodCallArgs(server, request, response, null, null);
response.setHeader('DAV', '1,2');
response.setHeader('Server', server.options.serverName + '/' + server.options.version);
server.getResourceFromPath(mca.uri, function (e, r) {
server.getResourceFromPath(mca, mca.uri, function (e, r) {
if (e || !r) {
setAllowHeader();
return;
Expand Down
26 changes: 14 additions & 12 deletions lib/server/commands/Copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ function copy(arg, source, rDest, destination, callback) {
next();
return;
}
source.read(true, function (e, rstream) { return _(e, function () {
dest.write(true, function (e, wstream) { return _(e, function () {
rstream.on('end', function () {
arg.invokeEvent('read', source);
next();
});
wstream.on('finish', function () {
arg.invokeEvent('write', dest);
});
rstream.pipe(wstream);
source.size(true, function (e, size) {
source.read(true, function (e, rstream) { return _(e, function () {
dest.write(true, function (e, wstream) { return _(e, function () {
rstream.on('end', function () {
arg.invokeEvent('read', source);
next();
});
wstream.on('finish', function () {
arg.invokeEvent('write', dest);
});
rstream.pipe(wstream);
}); }, size);
}); });
}); });
});
function next() {
if (!type.isDirectory) {
arg.invokeEvent('copy', source, dest);
Expand Down Expand Up @@ -103,7 +105,7 @@ function method(arg, callback) {
destination = destination.substring(destination.indexOf('/'));
}
destination = new FSManager_1.FSPath(destination);
arg.server.getResourceFromPath(destination.getParent(), function (e, rDest) {
arg.server.getResourceFromPath(arg, destination.getParent(), function (e, rDest) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
Expand Down
2 changes: 1 addition & 1 deletion lib/server/commands/Lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function createLock(arg, callback) {
arg.getResource(function (e, r) {
if (e === Errors_1.Errors.ResourceNotFound) {
arg.checkIfHeader(undefined, function () {
arg.server.getResourceFromPath(arg.path.getParent(), function (e, r) {
arg.server.getResourceFromPath(arg, arg.path.getParent(), function (e, r) {
if (e) {
arg.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
Expand Down
2 changes: 1 addition & 1 deletion lib/server/commands/Mkcol.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function method(arg, callback) {
callback();
return;
}
arg.server.getResourceFromPath(arg.path.getParent(), function (e, r) {
arg.server.getResourceFromPath(arg, arg.path.getParent(), function (e, r) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Conflict);
callback();
Expand Down
6 changes: 5 additions & 1 deletion lib/server/commands/Move.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ function method(arg, callback) {
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) {
arg.server.getResourceFromPath(arg, destination.getParent(), function (e, rDest) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
return;
}
arg.requirePrivilege(['canAddChild'], rDest, function () {
r.moveTo(rDest, destination.fileName(), overwrite, function (e) { return process.nextTick(function () {
if (e)
Expand Down
12 changes: 11 additions & 1 deletion lib/server/commands/Propfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ function lockDiscovery(lockDiscoveryCache, arg, path, resource, callback) {
return;
}
resource.getLocks(function (e, locks) {
if (e === Errors_1.Errors.MustIgnore) {
locks = [];
}
else if (e) {
callback(e, null);
return;
}
if (resource.parent) {
var parentPath = path.getParent();
lockDiscovery(lockDiscoveryCache, arg, parentPath, resource.parent, function (e, l) {
Expand Down Expand Up @@ -104,6 +111,7 @@ function method(arg, callback) {
if (!e)
done(multistatus);
else {
console.log(e);
if (e === Errors_1.Errors.BadAuthentication)
arg.setCode(WebDAVRequest_1.HTTPCodes.Unauthorized);
else
Expand All @@ -116,6 +124,7 @@ function method(arg, callback) {
arg.requirePrivilege('canGetChildren', resource, function () {
resource.getChildren(function (e, children) { return process.nextTick(function () {
function err(e) {
console.log(e);
if (e === Errors_1.Errors.BadAuthentication)
arg.setCode(WebDAVRequest_1.HTTPCodes.Unauthorized);
else
Expand Down Expand Up @@ -246,7 +255,8 @@ function method(arg, callback) {
return;
}
var p = arg.fullUri(path).replace(' ', '%20');
response.ele('D:href', undefined, true).add(p.lastIndexOf('/') !== p.length - 1 && type.isDirectory ? p + '/' : p);
var href = p.lastIndexOf('/') !== p.length - 1 && type.isDirectory ? p + '/' : p;
response.ele('D:href', undefined, true).add(href);
response.ele('D:location').ele('D:href', undefined, true).add(p);
if (tags.resourcetype.value && type.isDirectory)
tags.resourcetype.el.ele('D:collection');
Expand Down
16 changes: 10 additions & 6 deletions lib/server/commands/Put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var IResource_1 = require("../../resource/IResource");
var Errors_1 = require("../../Errors");
var path = require("path");
function createResource(arg, callback, validCallback) {
arg.server.getResourceFromPath(arg.path.getParent(), function (e, r) {
arg.server.getResourceFromPath(arg, arg.path.getParent(), function (e, r) {
if (e) {
arg.setCode(e === Errors_1.Errors.ResourceNotFound ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
Expand Down Expand Up @@ -63,7 +63,7 @@ function unchunkedMethod(arg, callback) {
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
}
callback();
}); });
}); }, arg.contentLength);
});
return;
}
Expand Down Expand Up @@ -121,7 +121,7 @@ function unchunkedMethod(arg, callback) {
}
callback();
});
}); });
}); }, arg.contentLength);
}); });
});
}
Expand All @@ -136,6 +136,7 @@ unchunkedMethod.chunked = function (arg, callback) {
var targetSource = arg.isSource;
arg.getResource(function (e, r) {
if (e && e !== Errors_1.Errors.ResourceNotFound) {
console.log(e);
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
Expand All @@ -145,21 +146,23 @@ unchunkedMethod.chunked = function (arg, callback) {
createResource(arg, callback, function (r) {
r.write(targetSource, function (e, stream) { return process.nextTick(function () {
if (e) {
console.log(e);
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
return;
}
arg.request.pipe(stream);
stream.on('finish', function (e) {
stream.on('finish', function () {
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
arg.invokeEvent('write', r);
callback();
});
stream.on('error', function (e) {
console.log(e);
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
});
}); });
}); }, arg.contentLength);
});
return;
}
Expand Down Expand Up @@ -188,10 +191,11 @@ unchunkedMethod.chunked = function (arg, callback) {
callback();
});
stream.on('error', function (e) {
console.log(e);
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
});
}); });
}); }, arg.contentLength);
}); });
});
});
Expand Down
27 changes: 19 additions & 8 deletions lib/server/commands/Unlock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var LockScope_1 = require("../../resource/lock/LockScope");
var LockKind_1 = require("../../resource/lock/LockKind");
var LockType_1 = require("../../resource/lock/LockType");
var Errors_1 = require("../../Errors");
var Lock_1 = require("../../resource/lock/Lock");
function method(arg, callback) {
if (!arg.user) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Forbidden);
Expand Down Expand Up @@ -35,15 +40,21 @@ function method(arg, callback) {
return;
}
r.getLock(token, function (e, lock) {
if (e || !lock) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Conflict);
callback();
return;
if (e !== Errors_1.Errors.MustIgnore) {
if (e || !lock) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Conflict);
callback();
return;
}
if (!!lock.userUid && lock.userUid !== arg.user.uid) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Forbidden);
callback();
return;
}
}
if (lock.userUid !== arg.user.uid) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Forbidden);
callback();
return;
else {
lock = new Lock_1.Lock(new LockKind_1.LockKind(LockScope_1.LockScope.Exclusive, LockType_1.LockType.Write), undefined, undefined);
lock.uuid = token;
}
r.removeLock(lock.uuid, function (e, done) {
if (e || !done)
Expand Down
2 changes: 1 addition & 1 deletion src/server/MethodCallArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class MethodCallArgs
response.setHeader('DAV', '1,2');
response.setHeader('Server', server.options.serverName + '/' + server.options.version);

server.getResourceFromPath(mca.uri, (e, r) => {
server.getResourceFromPath(mca, mca.uri, (e, r) => {
if(e || !r)
{
setAllowHeader();
Expand Down
26 changes: 14 additions & 12 deletions src/server/commands/Copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,20 @@ function copy(arg : MethodCallArgs, source : IResource, rDest : IResource, desti
return;
}

source.read(true, (e, rstream) => _(e, () => {
dest.write(true, (e, wstream) => _(e, () => {
rstream.on('end', () => {
arg.invokeEvent('read', source);
next();
});
wstream.on('finish', () => {
arg.invokeEvent('write', dest);
})
rstream.pipe(wstream);
source.size(true, (e, size) => {
source.read(true, (e, rstream) => _(e, () => {
dest.write(true, (e, wstream) => _(e, () => {
rstream.on('end', () => {
arg.invokeEvent('read', source);
next();
});
wstream.on('finish', () => {
arg.invokeEvent('write', dest);
})
rstream.pipe(wstream);
}), size)
}))
}))
})

function next()
{
Expand Down Expand Up @@ -130,7 +132,7 @@ export function method(arg : MethodCallArgs, callback)
}
destination = new FSPath(destination)

arg.server.getResourceFromPath(destination.getParent(), (e, rDest) => {
arg.server.getResourceFromPath(arg, destination.getParent(), (e, rDest) => {
if(e)
{
arg.setCode(HTTPCodes.InternalServerError);
Expand Down
2 changes: 1 addition & 1 deletion src/server/commands/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function createLock(arg : MethodCallArgs, callback)
{ // create the resource

arg.checkIfHeader(undefined, () => {
arg.server.getResourceFromPath(arg.path.getParent(), (e, r) => {
arg.server.getResourceFromPath(arg, arg.path.getParent(), (e, r) => {
if(e)
{
arg.setCode(e === Errors.ResourceNotFound ? HTTPCodes.Conflict : HTTPCodes.InternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion src/server/commands/Mkcol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function method(arg : MethodCallArgs, callback)
return;
}

arg.server.getResourceFromPath(arg.path.getParent(), (e, r) => {
arg.server.getResourceFromPath(arg, arg.path.getParent(), (e, r) => {
if(e)
{
arg.setCode(HTTPCodes.Conflict)
Expand Down
8 changes: 7 additions & 1 deletion src/server/commands/Move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ export function method(arg : MethodCallArgs, callback)
destination = destination.substring(destination.indexOf('/'))
destination = new FSPath(destination)

arg.server.getResourceFromPath(destination.getParent(), (e, rDest) => {
arg.server.getResourceFromPath(arg, destination.getParent(), (e, rDest) => {
if(e)
{
arg.setCode(HTTPCodes.InternalServerError);
return;
}

arg.requirePrivilege([ 'canAddChild' ], rDest, () => {
r.moveTo(rDest, destination.fileName(), overwrite, (e) => process.nextTick(() => {
if(e)
Expand Down
15 changes: 14 additions & 1 deletion src/server/commands/Propfind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ function lockDiscovery(lockDiscoveryCache : any, arg : MethodCallArgs, path : FS
}

resource.getLocks((e, locks) => {
if(e === Errors.MustIgnore)
{
locks = [];
}
else if(e)
{
callback(e, null);
return;
}

if(resource.parent)
{
const parentPath = path.getParent();
Expand Down Expand Up @@ -141,6 +151,7 @@ export function method(arg : MethodCallArgs, callback)
done(multistatus);
else
{
console.log(e);
if(e === Errors.BadAuthentication)
arg.setCode(HTTPCodes.Unauthorized);
else
Expand All @@ -156,6 +167,7 @@ export function method(arg : MethodCallArgs, callback)

function err(e)
{
console.log(e);
if(e === Errors.BadAuthentication)
arg.setCode(HTTPCodes.Unauthorized);
else
Expand Down Expand Up @@ -327,7 +339,8 @@ export function method(arg : MethodCallArgs, callback)
}

const p = arg.fullUri(path).replace(' ', '%20');
response.ele('D:href', undefined, true).add(p.lastIndexOf('/') !== p.length - 1 && type.isDirectory ? p + '/' : p);
const href = p.lastIndexOf('/') !== p.length - 1 && type.isDirectory ? p + '/' : p;
response.ele('D:href', undefined, true).add(href);
response.ele('D:location').ele('D:href', undefined, true).add(p);

if(tags.resourcetype.value && type.isDirectory)
Expand Down

0 comments on commit a58d38f

Please sign in to comment.