This repository was archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathweb-util.js
More file actions
122 lines (117 loc) · 3.26 KB
/
Copy pathweb-util.js
File metadata and controls
122 lines (117 loc) · 3.26 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict'
/**
* Provides misc utility functions for the web client
* @module web-util
*/
module.exports.parseAllowedMethods = parseAllowedMethods
module.exports.parseLinkHeader = parseLinkHeader
module.exports.absoluteUrl = absoluteUrl
module.exports.hostname = hostname
/**
* Extracts the allowed HTTP methods from the 'Allow' and 'Accept-Patch'
* headers, and returns a hashmap of verbs allowed by the server
* @method parseAllowedMethods
* @param allowMethodsHeader {String} `Access-Control-Allow-Methods` response
* header
* @param acceptPatchHeader {String} `Accept-Patch` response header
* @return {Object} Hashmap of verbs (in lowercase) allowed by the server for
* the current user. Example:
* ```
* {
* 'get': true,
* 'put': true
* }
* ```
*/
function parseAllowedMethods (allowMethodsHeader, acceptPatchHeader) {
var allowedMethods = {}
if (allowMethodsHeader) {
var verbs = allowMethodsHeader.split(',')
verbs.forEach(function (methodName) {
if (methodName && allowMethodsHeader.indexOf(methodName) >= 0) {
allowedMethods[methodName.trim().toLowerCase()] = true
}
})
}
if (acceptPatchHeader &&
acceptPatchHeader.indexOf('application/sparql-update') >= 0) {
allowedMethods.patch = true
}
return allowedMethods
}
/**
* Parses a Link header from an XHR HTTP Request.
* @method parseLinkHeader
* @param link {String} Contents of the Link response header
* @return {Object}
*/
function parseLinkHeader (link) {
if (!link) {
return {}
}
var linkexp = /<[^>]*>\s*(\s*;\s*[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g
var paramexp = /[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g
var matches = link.match(linkexp)
var rels = {}
for (var i = 0; i < matches.length; i++) {
var split = matches[i].split('>')
var href = split[0].substring(1)
var ps = split[1]
var s = ps.match(paramexp)
for (var j = 0; j < s.length; j++) {
var p = s[j]
var paramsplit = p.split('=')
// var name = paramsplit[0]
var rel = paramsplit[1].replace(/["']/g, '')
if (!rels[rel]) {
rels[rel] = []
}
rels[rel].push(href)
if (rels[rel].length > 1) {
rels[rel].sort()
}
}
}
return rels
}
function hostname (url) {
var protocol, hostname, result, pathSegments
var fragments = url.split('//')
if (fragments.length === 2) {
protocol = fragments[0]
hostname = fragments[1]
} else {
hostname = url
}
pathSegments = hostname.split('/')
if (protocol) {
result = protocol + '//' + pathSegments[0]
} else {
result = pathSegments[0]
}
if (url.startsWith('//')) {
result = '//' + result
}
return result
}
/**
* Return an absolute URL
* @method absoluteUrl
* @param baseUrl {String} URL to be used as base
* @param pathUrl {String} Absolute or relative URL
* @return {String}
*/
function absoluteUrl (baseUrl, pathUrl) {
if (pathUrl && pathUrl.slice(0, 4) !== 'http') {
return [baseUrl, pathUrl].map(function (path) {
if (path[0] === '/') {
path = path.slice(1)
}
if (path[path.length - 1] === '/') {
path = path.slice(0, path.length - 1)
}
return path
}).join('/')
}
return pathUrl
}