Skip to content

Commit eadb2b8

Browse files
committed
Added the 'Custom Method' example
1 parent 4b1cb25 commit eadb2b8

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

examples/v2/customMethod/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Custom method
2+
3+
This is an example to show how to make a custom method.
4+
5+
This example focus on the `TRACE` method, to send to a remote user all actions to a resource or its children.
6+
It uses the headers `Trace-Depth`, `Trace-Separator` and `Trace-Method` to customize the answer and the observed resources.
7+
8+
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.

examples/v2/customMethod/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const webdav = require('webdav-server').v2;
2+
3+
// Server instantiation
4+
const server = new webdav.WebDAVServer();
5+
6+
server.method('TRACE', {
7+
unchunked(wctx, data, next)
8+
{
9+
const path = wctx.requested.path.toString(true);
10+
const nbPaths = wctx.requested.path.paths.length;
11+
const method = wctx.headers.find('trace-method', '*').toLowerCase();
12+
const separator = wctx.headers.find('trace-separator', '\r\n');
13+
const iDepth = parseInt(wctx.headers.find('trace-depth', 'infinity').toLowerCase());
14+
const depth = isNaN(iDepth) ? -1 : iDepth;
15+
wctx.setCode(webdav.HTTPCodes.OK);
16+
17+
server.afterRequest((ctx, next) => {
18+
const ctxMethod = ctx.request.method.toLowerCase();
19+
const ctxPath = ctx.requested.path;
20+
const sCtxPath = ctxPath.toString(true);
21+
22+
if((method === '*' || ctxMethod === method) && ((depth === -1 || ctxPath.paths.length <= depth + nbPaths) && sCtxPath.indexOf(path) === 0))
23+
{
24+
wctx.response.write(JSON.stringify({
25+
request: {
26+
method: ctxMethod,
27+
path: ctxPath.toString()
28+
},
29+
response: {
30+
status: {
31+
code: ctx.response.statusCode,
32+
message: ctx.response.statusMessage
33+
}
34+
}
35+
}));
36+
wctx.response.write(separator);
37+
}
38+
next();
39+
})
40+
}
41+
})
42+
43+
server.start((s) => console.log('Ready on port', s.address().port));

examples/v2/customMethod/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { v2 as webdav } from 'webdav-server'
2+
3+
// Server instantiation
4+
const server = new webdav.WebDAVServer();
5+
6+
server.method('TRACE', {
7+
unchunked(wctx : webdav.HTTPRequestContext, data : Buffer, next : () => void)
8+
{
9+
const path = wctx.requested.path.toString(true);
10+
const nbPaths = wctx.requested.path.paths.length;
11+
const method = wctx.headers.find('trace-method', '*').toLowerCase();
12+
const separator = wctx.headers.find('trace-separator', '\r\n');
13+
const iDepth = parseInt(wctx.headers.find('trace-depth', 'infinity').toLowerCase());
14+
const depth = isNaN(iDepth) ? -1 : iDepth;
15+
wctx.setCode(webdav.HTTPCodes.OK);
16+
17+
server.afterRequest((ctx, next) => {
18+
const ctxMethod = ctx.request.method.toLowerCase();
19+
const ctxPath = ctx.requested.path;
20+
const sCtxPath = ctxPath.toString(true);
21+
22+
if((method === '*' || ctxMethod === method) && ((depth === -1 || ctxPath.paths.length <= depth + nbPaths) && sCtxPath.indexOf(path) === 0))
23+
{
24+
wctx.response.write(JSON.stringify({
25+
request: {
26+
method: ctxMethod,
27+
path: ctxPath.toString()
28+
},
29+
response: {
30+
status: {
31+
code: ctx.response.statusCode,
32+
message: ctx.response.statusMessage
33+
}
34+
}
35+
}));
36+
wctx.response.write(separator);
37+
}
38+
next();
39+
})
40+
}
41+
})
42+
43+
server.start((s) => console.log('Ready on port', s.address().port));

examples/v2/customMethod/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "custom-method",
3+
"version": "1.0.0",
4+
"description": "Example to show how to make a custom method.",
5+
"main": "index.js",
6+
"author": "Adrien Castex <[email protected]>",
7+
"license": "Unlicense",
8+
"dependencies": {
9+
"webdav-server": "^2.0.0"
10+
}
11+
}

0 commit comments

Comments
 (0)