From eadb2b86e0d6dacb883f8f0b978dc1541fd9e759 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sat, 8 Jul 2017 15:58:09 +0200 Subject: [PATCH] Added the 'Custom Method' example --- examples/v2/customMethod/README.md | 8 +++++ examples/v2/customMethod/index.js | 43 +++++++++++++++++++++++++++ examples/v2/customMethod/index.ts | 43 +++++++++++++++++++++++++++ examples/v2/customMethod/package.json | 11 +++++++ 4 files changed, 105 insertions(+) create mode 100644 examples/v2/customMethod/README.md create mode 100644 examples/v2/customMethod/index.js create mode 100644 examples/v2/customMethod/index.ts create mode 100644 examples/v2/customMethod/package.json diff --git a/examples/v2/customMethod/README.md b/examples/v2/customMethod/README.md new file mode 100644 index 00000000..15c05f6b --- /dev/null +++ b/examples/v2/customMethod/README.md @@ -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. diff --git a/examples/v2/customMethod/index.js b/examples/v2/customMethod/index.js new file mode 100644 index 00000000..597bb143 --- /dev/null +++ b/examples/v2/customMethod/index.js @@ -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)); diff --git a/examples/v2/customMethod/index.ts b/examples/v2/customMethod/index.ts new file mode 100644 index 00000000..0a0d097a --- /dev/null +++ b/examples/v2/customMethod/index.ts @@ -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)); diff --git a/examples/v2/customMethod/package.json b/examples/v2/customMethod/package.json new file mode 100644 index 00000000..6b673e72 --- /dev/null +++ b/examples/v2/customMethod/package.json @@ -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 ", + "license": "Unlicense", + "dependencies": { + "webdav-server": "^2.0.0" + } +}