-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b1cb25
commit eadb2b8
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |