-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.xqm
207 lines (189 loc) · 7.2 KB
/
http.xqm
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
xquery version "3.1";
module namespace api = "http://acdh.oeaw.ac.at/vicav/api/http";
import module namespace request = "http://exquery.org/ns/request";
import module namespace jobs = "http://basex.org/modules/jobs";
import module namespace l = "http://basex.org/modules/admin";
import module namespace rest = "http://exquery.org/ns/restxq";
declare function api:get-base-uri-public() as xs:string {
let $forwarded-hostname := if (contains(request:header('X-Forwarded-Host'), ','))
then substring-before(request:header('X-Forwarded-Host'), ',')
else request:header('X-Forwarded-Host'),
$urlScheme := if ((lower-case(request:header('X-Forwarded-Proto')) = 'https') or
(lower-case(request:header('Front-End-Https')) = 'on')) then 'https' else 'http',
$port := if ($urlScheme eq 'http' and request:port() ne 80) then ':'||request:port()
else if ($urlScheme eq 'https' and not(request:port() eq 80 or request:port() eq 443)) then ':'||request:port()
else '',
(: FIXME: this is to naive. Works for ProxyPass / to /exist/apps/cr-xq-mets/project
but probably not for /x/y/z/ to /exist/apps/cr-xq-mets/project. Especially check the get module. :)
$xForwardBasedPath := replace((request:header('X-Forwarded-Request-Uri'), request:path())[1], '^([^?]*)(\?.*)$', '$1')
return $urlScheme||'://'||($forwarded-hostname, request:hostname())[1]||$port||$xForwardBasedPath
};
(:~
: Returns a html or related file.
: @param $file file or unknown path
: @return rest response and binary file
:)
declare
%rest:path("vicav/{$file=[^/]+}")
function api:file($file as xs:string) as item()+ {
let $path := api:base-dir()|| $file
return if (file:exists($path)) then
if (matches($file, '\.(htm|html|pdf|m4a|js|docx|map|css|png|gif|jpg|jpeg|woff|woff2|svg|ttf|mp4)$', 'i')) then
let $bin := file:read-binary($path)
, $hash := xs:string(xs:hexBinary(hash:md5($bin)))
, $hashBrowser := request:header('If-None-Match', '')
return if ($hash = $hashBrowser) then
web:response-header(map{}, map{}, map{'status': 304, 'message': 'Not Modified'})
else (
web:response-header(map { 'media-type': web:content-type($path),
'method': 'basex',
'binary': 'yes' },
map { 'X-UA-Compatible': 'IE=11'
, 'Cache-Control': 'max-age=3600,public'
, 'ETag': $hash }),
$bin
) else api:forbidden-file($file)
else
(
web:response-header(map{'media-type': 'text/html',
'method': 'html'},
map{'Content-Language': 'en',
'X-UA-Compatible': 'IE=11'},
map{'status': 404, 'message':$file||' was not found'}),
<html xmlns="http://www.w3.org/1999/xhtml">
<title>{$file||' was not found'}</title>
<body>
<h1>{$file||' was not found'}</h1>
</body>
</html>
)
};
declare
%rest:path("vicav/js/{$file=.+}")
function api:bower_components-file($file as xs:string) as item()+ {
api:file('js/'||$file)
};
declare
%rest:path("vicav/downloads/{$file=.+}")
function api:docs-file($file as xs:string) as item()+ {
api:file('downloads/'||$file)
};
declare
%rest:path("vicav/docs/{$file=.+}")
function api:pdf-file($file as xs:string) as item()+ {
api:file('docs/'||$file)
};
declare
%rest:path("vicav/css/{$file=.+}")
function api:css-file($file as xs:string) as item()+ {
api:file('css/'||$file)
};
declare
%rest:path("vicav/vendor/{$file=.+}")
function api:vendor-file($file as xs:string) as item()+ {
api:file('vendor/'||$file)
};
declare
%rest:path("vicav/images/{$file=.+}")
function api:images-file($file as xs:string) as item()+ {
api:file('images/'||$file)
};
declare
%rest:path("vicav/fonts/{$file=.+}")
function api:fonts-file($file as xs:string) as item()+ {
api:file('fonts/'||$file)
};
declare
%rest:path("vicav/favicons/{$file=.+}")
function api:favicons-file($file as xs:string) as item()+ {
api:file('favicons/'||$file)
};
declare %private function api:base-dir() as xs:string {
file:base-dir()
(: if this is in a subdirectory say "responses"
replace(file:base-dir(), '^(.+)responses.*$', '$1') :)
};
(:~
: Returns index.html on /.
: @param $file file or unknown path
: @return rest response and binary file
:)
declare
%rest:path("vicav")
function api:index-file() as item()+ {
let $index-html := api:base-dir()||'index.html',
$index-htm := api:base-dir()||'index.htm',
$uri := rest:uri(),
(: $log := l:write-log('api:index-file() $uri := '||$uri||' base-uri-public := '||api:get-base-uri-public(), 'DEBUG'),:)
$absolute-prefix := if (matches(api:get-base-uri-public(), '/$')) then () else api:get-base-uri-public()||'/'
return if (exists($absolute-prefix)) then
<rest:response>
<http:response status="302">
<http:header name="Location" value="{$absolute-prefix}"/>
</http:response>
</rest:response>
else if (file:exists($index-html)) then
<rest:forward>index.html</rest:forward>
else if (file:exists($index-htm)) then
<rest:forward>index.htm</rest:forward>
else api:forbidden-file($index-html)
};
(:~
: Return 403 on all other (forbidden files).
: @param $file file or unknown path
: @return rest response and binary file
:)
declare
%private
function api:forbidden-file($file as xs:string) as item()+ {
<rest:response>
<http:response status="403" message="{$file} forbidden.">
<http:header name="Content-Language" value="en"/>
<http:header name="Content-Type" value="text/html; charset=utf-8"/>
</http:response>
</rest:response>,
<html xmlns="http://www.w3.org/1999/xhtml">
<title>{$file||' forbidden'}</title>
<body>
<h1>{$file||' forbidden'}</h1>
</body>
</html>
};
declare
%rest:path("vicav/test-error.xqm")
function api:test-error() as item()+ {
api:test-error('api:test-error')
};
declare
%rest:path("vicav/test-error.xqm/{$error-qname}")
function api:test-error($error-qname as xs:string) as item()+ {
error(xs:QName($error-qname))
};
declare
%rest:path("vicav/runtime")
function api:runtime-info() as item()+ {
let $runtime-info := db:system(),
$xslt-runtime-info := xslt:transform(<_/>,
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/><xsl:template match='/'><_><product-name><xsl:value-of select="system-property('xsl:product-name')"/></product-name><product-version><xsl:value-of select="system-property('xsl:product-version')"/></product-version></_></xsl:template></xsl:stylesheet>)/*
return
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Runtime info</title>
<body>
<h1>Runtime info</h1>
<table>
{for $item in $runtime-info/*:generalinformation/*
return
<tr>
<td>{$item/local-name()}</td>
<td>{$item}</td>
</tr>
}
<tr>
<td>{$xslt-runtime-info/*:product-name/text()}</td>
<td>{$xslt-runtime-info/*:product-version/text()}</td>
</tr>
</table>
</body>
</html>
};