Skip to content

Commit

Permalink
Fixed PROPPATCH and PROPFIND xml parsing errors when the request body…
Browse files Browse the repository at this point in the history
… was not valid
  • Loading branch information
AdrienCastex committed Jun 11, 2017
1 parent 759befc commit e7fed58
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 82 deletions.
2 changes: 1 addition & 1 deletion lib/server/commands/Propfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function lockDiscovery(lockDiscoveryCache, arg, path, resource, callback) {
});
}
function parseRequestBody(arg) {
var xml = XML_1.XML.parse(arg.data);
var allTrue = {
leftElements: [],
mustDisplay: function () { return true; },
Expand All @@ -57,6 +56,7 @@ function parseRequestBody(arg) {
if (arg.contentLength <= 0)
return allTrue;
try {
var xml = XML_1.XML.parse(arg.data);
var propfind = xml.find('DAV:propfind');
if (propfind.findIndex('DAV:propname') !== -1)
return onlyName;
Expand Down
84 changes: 46 additions & 38 deletions lib/server/commands/Proppatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,58 @@ function default_1(arg, callback) {
});
var response = multistatus.ele('D:response');
response.ele('D:href').add(arg.fullUri());
var xml = XML_1.XML.parse(arg.data);
var root = xml.find('DAV:propertyupdate');
var finalize = function () {
finalize = function () {
arg.setCode(WebDAVRequest_1.HTTPCodes.MultiStatus);
arg.writeXML(multistatus);
callback();
try {
var xml = XML_1.XML.parse(arg.data);
var root_1 = xml.find('DAV:propertyupdate');
var finalize_1 = function () {
finalize_1 = function () {
arg.setCode(WebDAVRequest_1.HTTPCodes.MultiStatus);
arg.writeXML(multistatus);
callback();
};
};
};
function notify(el, error) {
var code = error ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.OK;
var propstat = response.ele('D:propstat');
propstat.ele('D:prop').ele(el.name);
propstat.ele('D:status').add('HTTP/1.1 ' + code + ' ' + http_1.STATUS_CODES[code]);
}
execute('DAV:set', function (el, callback) {
r.setProperty(el.name, el.elements, callback);
});
execute('DAV:remove', function (el, callback) {
r.removeProperty(el.name, callback);
});
function execute(name, fnProp) {
var list = root.findMany(name);
if (list.length === 0) {
finalize();
return;
}
list.forEach(function (el) {
var els = el.find('DAV:prop').elements;
if (els.length === 0) {
finalize();
var notify_1 = function (el, error) {
var code = error ? WebDAVRequest_1.HTTPCodes.Conflict : WebDAVRequest_1.HTTPCodes.OK;
var propstat = response.ele('D:propstat');
propstat.ele('D:prop').ele(el.name);
propstat.ele('D:status').add('HTTP/1.1 ' + code + ' ' + http_1.STATUS_CODES[code]);
};
var execute = function (name, eventName, fnProp) {
var list = root_1.findMany(name);
if (list.length === 0) {
finalize_1();
return;
}
var nb = els.length;
els.forEach(function (el) {
fnProp(el, function (e) { return process.nextTick(function () {
notify(el, e);
--nb;
if (nb === 0)
finalize();
}); });
list.forEach(function (el) {
var els = el.find('DAV:prop').elements;
if (els.length === 0) {
finalize_1();
return;
}
var nb = els.length;
els.forEach(function (el) {
fnProp(el, function (e) { return process.nextTick(function () {
if (!e)
arg.invokeEvent(eventName, r, el);
notify_1(el, e);
--nb;
if (nb === 0)
finalize_1();
}); });
});
});
};
execute('DAV:set', 'setProperty', function (el, callback) {
r.setProperty(el.name, el.elements, callback);
});
execute('DAV:remove', 'removeProperty', function (el, callback) {
r.removeProperty(el.name, callback);
});
}
catch (ex) {
arg.setCode(WebDAVRequest_1.HTTPCodes.BadRequest);
callback();
}
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/server/commands/Propfind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ interface PropertyRule

function parseRequestBody(arg : MethodCallArgs) : PropertyRule
{
const xml = XML.parse(arg.data);

const allTrue : PropertyRule = {
leftElements: [],
mustDisplay: () => true,
Expand All @@ -79,6 +77,8 @@ function parseRequestBody(arg : MethodCallArgs) : PropertyRule

try
{
const xml = XML.parse(arg.data);

const propfind = xml.find('DAV:propfind');

if(propfind.findIndex('DAV:propname') !== -1)
Expand Down
93 changes: 52 additions & 41 deletions src/server/commands/Proppatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'
import { STATUS_CODES } from 'http'
import { EventsName } from '../../server/webDAVServer/events'
import { IResource } from '../../resource/IResource'
import { XML } from '../../helper/XML'

Expand All @@ -21,62 +22,72 @@ export default function(arg : MethodCallArgs, callback)
const response = multistatus.ele('D:response');
response.ele('D:href').add(arg.fullUri());

const xml = XML.parse(arg.data);
const root = xml.find('DAV:propertyupdate');

let finalize = function()
try
{
finalize = function()
const xml = XML.parse(arg.data);
const root = xml.find('DAV:propertyupdate');

let finalize = function()
{
arg.setCode(HTTPCodes.MultiStatus);
arg.writeXML(multistatus);
callback();
finalize = function()
{
arg.setCode(HTTPCodes.MultiStatus);
arg.writeXML(multistatus);
callback();
}
}
}

function notify(el : any, error : any)
{
const code = error ? HTTPCodes.Conflict : HTTPCodes.OK;
const propstat = response.ele('D:propstat');
propstat.ele('D:prop').ele(el.name);
propstat.ele('D:status').add('HTTP/1.1 ' + code + ' ' + STATUS_CODES[code]);
}

execute('DAV:set', (el, callback) => {
r.setProperty(el.name, el.elements, callback)
})
execute('DAV:remove', (el, callback) => {
r.removeProperty(el.name, callback)
})

function execute(name, fnProp)
{
const list = root.findMany(name);
if(list.length === 0)
const notify = function(el : any, error : any)
{
finalize();
return;
const code = error ? HTTPCodes.Conflict : HTTPCodes.OK;
const propstat = response.ele('D:propstat');
propstat.ele('D:prop').ele(el.name);
propstat.ele('D:status').add('HTTP/1.1 ' + code + ' ' + STATUS_CODES[code]);
}

list.forEach(function(el) {
const els = el.find('DAV:prop').elements;
if(els.length === 0)
const execute = function(name : string, eventName : EventsName, fnProp)
{
const list = root.findMany(name);
if(list.length === 0)
{
finalize();
return;
}

let nb = els.length;
els.forEach(function(el) {
fnProp(el, (e) => process.nextTick(() => {
notify(el, e);
--nb;
if(nb === 0)
finalize();
}))
list.forEach(function(el) {
const els = el.find('DAV:prop').elements;
if(els.length === 0)
{
finalize();
return;
}

let nb = els.length;
els.forEach(function(el) {
fnProp(el, (e) => process.nextTick(() => {
if(!e)
arg.invokeEvent(eventName, r, el);
notify(el, e);
--nb;
if(nb === 0)
finalize();
}))
})
})
}

execute('DAV:set', 'setProperty', (el, callback) => {
r.setProperty(el.name, el.elements, callback)
})
execute('DAV:remove', 'removeProperty', (el, callback) => {
r.removeProperty(el.name, callback)
})
}
catch(ex)
{
arg.setCode(HTTPCodes.BadRequest);
callback();
}
})
})
})
Expand Down

0 comments on commit e7fed58

Please sign in to comment.