forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.ts
35 lines (33 loc) · 1.15 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import http from 'http'
import { Context, WebDAVDepth } from './types'
import { parseXML } from './operations/shared'
import { URL } from 'url'
export default (req: http.IncomingMessage, base: string, allows: Array<string>): Context => {
const authorization = req.headers?.authorization?.split(' ')[1]
const path = new URL(req.url as string, `http:https://${req.headers.host}`).pathname
const ctx: Context = {
req: req,
depth: req.headers?.depth as WebDAVDepth,
method: (req.method as string || '').toLowerCase(),
path: path.replace(base, ''),
base,
config: {},
auth: { user: undefined, pass: undefined },
allows,
get(field: string): string | undefined {
const req: http.IncomingMessage = this.req
switch (field = field.toLowerCase()) {
case 'referer':
case 'referrer':
return req.headers.referrer as string || req.headers.referer || ''
default:
return req.headers[field] as string || ''
}
}
}
if (authorization) {
const pairs = Buffer.from(authorization, "base64").toString("utf8").split(':')
ctx.auth = { user: pairs[0], pass: pairs[1] }
}
return ctx
}