Skip to content

Commit

Permalink
Added the 'Custom Method' example
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jul 8, 2017
1 parent 4b1cb25 commit eadb2b8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/v2/customMethod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Custom method

This is an example to show how to make a custom method.

This example focus on the `TRACE` method, to send to a remote user all actions to a resource or its children.
It uses the headers `Trace-Depth`, `Trace-Separator` and `Trace-Method` to customize the answer and the observed resources.

The `ts` file and the `js` file are the same thing. The `js` file displays the example in JavaScript while the `ts` file displays the example in TypeScript.
43 changes: 43 additions & 0 deletions examples/v2/customMethod/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const webdav = require('webdav-server').v2;

// Server instantiation
const server = new webdav.WebDAVServer();

server.method('TRACE', {
unchunked(wctx, data, next)
{
const path = wctx.requested.path.toString(true);
const nbPaths = wctx.requested.path.paths.length;
const method = wctx.headers.find('trace-method', '*').toLowerCase();
const separator = wctx.headers.find('trace-separator', '\r\n');
const iDepth = parseInt(wctx.headers.find('trace-depth', 'infinity').toLowerCase());
const depth = isNaN(iDepth) ? -1 : iDepth;
wctx.setCode(webdav.HTTPCodes.OK);

server.afterRequest((ctx, next) => {
const ctxMethod = ctx.request.method.toLowerCase();
const ctxPath = ctx.requested.path;
const sCtxPath = ctxPath.toString(true);

if((method === '*' || ctxMethod === method) && ((depth === -1 || ctxPath.paths.length <= depth + nbPaths) && sCtxPath.indexOf(path) === 0))
{
wctx.response.write(JSON.stringify({
request: {
method: ctxMethod,
path: ctxPath.toString()
},
response: {
status: {
code: ctx.response.statusCode,
message: ctx.response.statusMessage
}
}
}));
wctx.response.write(separator);
}
next();
})
}
})

server.start((s) => console.log('Ready on port', s.address().port));
43 changes: 43 additions & 0 deletions examples/v2/customMethod/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { v2 as webdav } from 'webdav-server'

// Server instantiation
const server = new webdav.WebDAVServer();

server.method('TRACE', {
unchunked(wctx : webdav.HTTPRequestContext, data : Buffer, next : () => void)
{
const path = wctx.requested.path.toString(true);
const nbPaths = wctx.requested.path.paths.length;
const method = wctx.headers.find('trace-method', '*').toLowerCase();
const separator = wctx.headers.find('trace-separator', '\r\n');
const iDepth = parseInt(wctx.headers.find('trace-depth', 'infinity').toLowerCase());
const depth = isNaN(iDepth) ? -1 : iDepth;
wctx.setCode(webdav.HTTPCodes.OK);

server.afterRequest((ctx, next) => {
const ctxMethod = ctx.request.method.toLowerCase();
const ctxPath = ctx.requested.path;
const sCtxPath = ctxPath.toString(true);

if((method === '*' || ctxMethod === method) && ((depth === -1 || ctxPath.paths.length <= depth + nbPaths) && sCtxPath.indexOf(path) === 0))
{
wctx.response.write(JSON.stringify({
request: {
method: ctxMethod,
path: ctxPath.toString()
},
response: {
status: {
code: ctx.response.statusCode,
message: ctx.response.statusMessage
}
}
}));
wctx.response.write(separator);
}
next();
})
}
})

server.start((s) => console.log('Ready on port', s.address().port));
11 changes: 11 additions & 0 deletions examples/v2/customMethod/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "custom-method",
"version": "1.0.0",
"description": "Example to show how to make a custom method.",
"main": "index.js",
"author": "Adrien Castex <[email protected]>",
"license": "Unlicense",
"dependencies": {
"webdav-server": "^2.0.0"
}
}

0 comments on commit eadb2b8

Please sign in to comment.