Skip to content

Commit

Permalink
Added support to HEAD method
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 13, 2017
1 parent d7c5192 commit 105580f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

This module is not complete yet.
It implements the following methods :
* HEAD (resource ping)
* GET (file get content)
* MKCOL (directory creation)
* PROPFIND (get file information)
Expand Down
2 changes: 2 additions & 0 deletions lib/server/commands/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var NotImplemented_1 = require("./NotImplemented");
var Propfind_1 = require("./Propfind");
var Options_1 = require("./Options");
var Mkcol_1 = require("./Mkcol");
var Head_1 = require("./Head");
var Post_1 = require("./Post");
var Put_1 = require("./Put");
var Get_1 = require("./Get");
Expand All @@ -12,6 +13,7 @@ exports.default = {
Propfind: Propfind_1.default,
Options: Options_1.default,
Mkcol: Mkcol_1.default,
Head: Head_1.default,
Post: Post_1.default,
Put: Put_1.default,
Get: Get_1.default
Expand Down
2 changes: 2 additions & 0 deletions lib/server/commands/Head.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { MethodCallArgs } from '../WebDAVRequest';
export default function (arg: MethodCallArgs, callback: any): void;
20 changes: 20 additions & 0 deletions lib/server/commands/Head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
function default_1(arg, callback) {
arg.getResource(function (e, r) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.NotFound);
callback();
return;
}
r.read(function (e, c) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.MethodNotAllowed);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
callback();
});
});
}
exports.default = default_1;
2 changes: 2 additions & 0 deletions src/server/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import NotImplemented from './NotImplemented'
import Propfind from './Propfind'
import Options from './Options'
import Mkcol from './Mkcol'
import Head from './Head'
import Post from './Post'
import Put from './Put'
import Get from './Get'
Expand All @@ -11,6 +12,7 @@ export default {
Propfind,
Options,
Mkcol,
Head,
Post,
Put,
Get
Expand Down
22 changes: 22 additions & 0 deletions src/server/commands/Head.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'
import { IResource } from '../../resource/Resource'

export default function(arg : MethodCallArgs, callback)
{
arg.getResource((e, r) => {
if(e)
{
arg.setCode(HTTPCodes.NotFound)
callback();
return;
}

r.read((e, c) => {
if(e)
arg.setCode(HTTPCodes.MethodNotAllowed)
else
arg.setCode(HTTPCodes.OK);
callback();
})
})
}
31 changes: 31 additions & 0 deletions test/tests/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var webdav = require('../../lib/index.js'),
request = require('request')

module.exports = (test, options, index) => test('HEAD method', isValid =>
{
var server = new webdav.WebDAVServer();
server.start(options.port + index);
isValid = isValid.multiple(2, server);

request({
url: 'http://localhost:' + (options.port + index),
method: 'HEAD'
}, (e, res, body) => {
isValid(!e && res.statusCode !== 200);
})

server.rootResource.addChild(new webdav.VirtualFile('file'), e => {
if(e)
{
isValid(false, e)
return;
}

request({
url: 'http://localhost:' + (options.port + index) + '/file',
method: 'HEAD'
}, (e, res, body) => {
isValid(!e && res.statusCode === 200);
})
})
})

0 comments on commit 105580f

Please sign in to comment.