-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ol
310 lines (264 loc) · 7.79 KB
/
main.ol
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright 2008-2022 Fabrizio Montesi <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
from console import Console
from file import File
from protocols.http import DefaultOperationHttpRequest
from runtime import Runtime
from string-utils import StringUtils
from types.Binding import Binding
from .hooks import PreResponseHookIface, PostResponseHookIface
type LeonardoBinding {
location:any
protocol?:string { ? }
}
/// Configuration parameters
type Params {
location:string //< location on which the web server should be exposed.
wwwDir?:string //< path to the directory containing the web files. Default: /var/lib/leonardo/www/
defaultPage:string //< default page to be served when clients ask for a directory. Default: "index.html"
/// configuration parameters for the HTTP input port
httpConfig? {
/// default = false
debug?:bool {
showContent?:bool //< default = false
}
}
preResponseHook?:LeonardoBinding //< Binding to a custom PreResponseHook
postResponseHook?:LeonardoBinding //< Binding to a custom PostResponseHook
/// Redirections to sub-services
redirection* {
name:string //< name of the service
binding:LeonardoBinding //< Binding to the target service
}
}
interface HTTPInterface {
RequestResponse:
default( DefaultOperationHttpRequest )( undefined )
}
service Leonardo( params:Params ) {
execution: concurrent
inputPort HTTPInput {
location: params.location
protocol: http {
keepAlive = true // Keep connections open
debug = is_defined( params.httpConfig.debug ) && params.httpConfig.debug
debug.showContent = is_defined( params.httpConfig.debug.showContent ) && params.httpConfig.debug.showContent
format -> httpParams.format
contentType -> httpParams.contentType
statusCode -> statusCode
redirect -> redirect
cacheControl.maxAge -> httpParams.cacheControl.maxAge
default = "default"
}
interfaces: HTTPInterface
}
outputPort preResponseHook {
interfaces: PreResponseHookIface
}
outputPort postResponseHook {
interfaces: PostResponseHookIface
}
embed Console as console
embed StringUtils as stringUtils
embed File as file
embed Runtime as runtime
embed WebFiles as webFiles
define loadHooks {
if( is_defined( params.preResponseHook ) ) {
preResponseHook << params.preResponseHook
} else {
loadEmbeddedService@runtime( {
filepath = "internal/hooks/pre-response.ol"
type = "Jolie"
} )( preResponseHook.location )
}
if( is_defined( params.postResponseHook ) ) {
postResponseHook << params.postResponseHook
} else {
loadEmbeddedService@runtime( {
filepath = "internal/hooks/post-response.ol"
type = "Jolie"
} )( postResponseHook.location )
}
}
define setRedirections {
for( redirection in params.redirection ) {
with( request ) {
.name = "#" + redirection.name;
.location = redirection.binding.location;
if ( is_defined( redirection.binding.protocol ) ) {
.protocol << redirection.binding.protocol
}
}
setOutputPort@runtime( request )()
undef( request )
setRedirection@runtime( {
inputPortName = "HTTPInput"
outputPortName = "#" + redirection.name
resourceName = redirection.name
} )()
}
}
init {
if( !is_defined( params.wwwDir ) ) {
params.wwwDir = "/var/lib/leonardo/www/"
}
setRedirections
loadHooks
toAbsolutePath@file( params.wwwDir )( params.wwwDir )
getFileSeparator@file()( fs )
params.wwwDir += fs
undef( fs )
format = "html"
println@console( "Leonardo started\n\tLocation: " + global.inputPorts.HTTPInput.location + "\n\tWeb directory: " + params.wwwDir )()
}
main {
[ default( request )( response ) {
runPostResponseHook = false
scope( computeResponse ) {
install(
FileNotFound =>
println@console( "File not found: " + computeResponse.FileNotFound )()
statusCode = 404,
MovedPermanently =>
redirect = computeResponse.MovedPermanently
statusCode = 301
)
get@webFiles( {
target = request.operation
wwwDir = params.wwwDir
defaultPage = params.defaultPage
} )( getResult )
httpParams -> getResult.httpParams
runPostResponseHook = true
install( PreResponseFault =>
response = computeResponse.PreResponseFault.response
statusCode = computeResponse.PreResponseFault.statusCode
runPostResponseHook = false
)
with( decoratedResponse ) {
.config.wwwDir = params.wwwDir;
.request.path = getResult.path;
if( getResult.format == "html" ) {
.content -> getResult.content
}
}
run@preResponseHook( decoratedResponse )( newResponse )
if( !(newResponse instanceof void) ) {
response -> newResponse
} else {
response -> getResult.content
}
}
} ] {
if( runPostResponseHook ) {
run@postResponseHook( decoratedResponse )()
}
}
}
}
type GetRequest {
target:string
wwwDir:string
defaultPage?:string ///< default: index.html
}
type GetResponse {
content:string | raw
path:string
httpParams {
format:string
contentType:string
cacheControl? {
maxAge:int
}
}
}
interface WebFilesInterface {
RequestResponse:
get( GetRequest )( GetResponse ) throws FileNotFound(string) MovedPermanently(string)
}
service WebFiles {
execution: concurrent
inputPort Input {
location: "local"
interfaces: WebFilesInterface
}
embed StringUtils as stringUtils
embed File as file
embed Console as console
init {
getFileSeparator@file()( fs )
getRealServiceDirectory@file()( dir )
setMimeTypeFile@file( dir + fs + "internal" + fs + "mime.types" )()
undef( fs )
undef( dir )
maliciousSubstrings[0] = ".."
maliciousSubstrings[1] = ".svn"
maliciousSubstrings[2] = ".git"
install(
FileNotFound => nullProcess,
MovedPermanently => nullProcess
)
}
define setCacheHeaders {
if( s.result[0] == "image"
|| endsWith@stringUtils( f.filename { suffix = ".js" } )
|| endsWith@stringUtils( f.filename { suffix = ".css" } )
|| endsWith@stringUtils( f.filename { suffix = ".woff" } ) ) {
response.httpParams.cacheControl.maxAge = 60 * 60 * 2 // 2 hours
}
}
define checkForMaliciousPath {
for( maliciousSubstring in maliciousSubstrings ) {
if( contains@stringUtils( s.result[0] { substring = maliciousSubstring } ) ) {
throw( FileNotFound )
}
}
}
main {
get( request )( response ) {
getFileSeparator@file()( fs )
if( !endsWith@stringUtils( request.wwwDir { suffix = fs } ) ) {
request.wwwDir += fs
}
split@stringUtils( request.target { regex = "\\?" } )( s )
// DefaultPage
if( s.result[0] == "" || endsWith@stringUtils( s.result[0] { suffix = "/" } ) ) {
s.result[0] +=
if( is_defined( request.defaultPage ) )
request.defaultPage
else "index.html"
}
checkForMaliciousPath
userPath = s.result[0]
response.path = request.wwwDir + userPath
f.filename = response.path
isDirectory@file( f.filename )( isDirectory )
if( isDirectory ) {
redirect = userPath + "/"
throw( MovedPermanently, redirect )
}
getMimeType@file( f.filename )( response.httpParams.contentType )
split@stringUtils( response.httpParams.contentType { regex = "/" } )( s )
if( s.result[0] == "text" ) {
f.format = "text"
response.httpParams.format = "html"
} else {
f.format = "binary"
response.httpParams.format = "binary"
}
setCacheHeaders
readFile@file( f )( response.content )
}
}
}