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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
module HTML.CGI
( CgiServerMsg(..), runCgiServerCmd
, cgiServerRegistry, registerCgiServer, unregisterCgiServer
, readCgiServerMsg, noHandlerPage, submitForm
)
where
import Char
import Directory ( doesFileExist, getCurrentDirectory )
import IO
import IOExts ( exclusiveIO, connectToCommand )
import List
import ReadNumeric
import ReadShowTerm
import System
import Time
import HTML.CGI.Config
import Network.NamedSocket
import Network.CPNS ( unregisterPort )
withCgiLogging :: Bool
withCgiLogging = True
data CgiServerMsg = CgiSubmit [(String,String)] [(String,String)]
| GetLoad
| SketchStatus
| SketchHandlers
| ShowStatus
| CleanServer
| StopCgiServer
readCgiServerMsg :: Handle -> IO (Maybe CgiServerMsg)
readCgiServerMsg handle = do
line <- hGetLine handle
case readsQTerm line of
[(msg,rem)] -> return (if all isSpace rem then Just msg else Nothing)
_ -> return Nothing
submitForm :: [String] -> IO ()
submitForm sargs = do
let (serverargs,lb,rargs) = stripServerArgs "" NoBalance sargs
case rargs of
[url,cgikey,serverprog] -> cgiScript url serverargs lb
(cgikey2portname cgikey) serverprog
[portname] -> cgiInteractiveScript portname
_ -> putStrLn $ "ERROR: cgi script called with illegal arguments!"
where
stripServerArgs serverargs load args = case args of
("-servertimeout":tos:rargs) ->
stripServerArgs (" -servertimeout "++tos) load rargs
("-multipleservers":rargs) -> stripServerArgs serverargs Multiple rargs
("-loadbalance":lbt:rargs) ->
stripServerArgs serverargs
(if lbt=="no" then NoBalance else
if lbt=="multiple" then Multiple else Standard) rargs
_ -> (serverargs,load,args)
data LoadBalance = NoBalance | Standard | Multiple
deriving Eq
runCgiServerCmd :: String -> CgiServerMsg -> IO String
runCgiServerCmd portname cmd = case cmd of
StopCgiServer -> do
h <- trySendScriptServerMessage portname StopCgiServer
hClose h
unregisterPort portname
return $ "Server at port '" ++ portname ++ "' unregistered."
CleanServer -> do
h <- trySendScriptServerMessage portname CleanServer
hGetContents h
GetLoad -> do
h <- trySendScriptServerMessage portname GetLoad
hGetContents h
ShowStatus -> do
h <- trySendScriptServerMessage portname ShowStatus
getInputAndClose h
SketchStatus -> do
h <- trySendScriptServerMessage portname SketchStatus
getInputAndClose h
SketchHandlers -> do
h <- trySendScriptServerMessage portname SketchHandlers
getInputAndClose h
_ -> error "HtmlCgi.runCgiServerCmd: called with illegal command!"
cgikey2portname :: String -> String
cgikey2portname cgikey =
concatMap (\c->if isAlphaNum c then [c] else []) cgikey
cgiInteractiveScript :: String -> IO ()
cgiInteractiveScript portname = do
cgiServerEnvVals <- mapIO getEnviron cgiServerEnvVars
let cgiServerEnv = zip cgiServerEnvVars cgiServerEnvVals
formEnv <- getFormVariables
catch (sendToServerAndPrintOrFail cgiServerEnv formEnv)
(putStrLn . errorPage)
where
sendToServerAndPrintOrFail cgiEnviron newcenv = do
h <- trySendScriptServerMessage portname (CgiSubmit cgiEnviron newcenv)
copyOutputAndClose h
errorPage e =
"Content-type: text/html\n\n" ++
"<html>\n<head><title>Server Error</title></head>\n" ++
"<body>\n<h1>Server Error</h1>\n" ++ showError e ++ "</body>\n</html>"
cgiScript :: String -> String -> LoadBalance -> String -> String -> IO ()
cgiScript url serverargs loadbalance portname serverprog = do
cgiServerEnvVals <- mapIO getEnviron cgiServerEnvVars
let cgiServerEnv = zip cgiServerEnvVars cgiServerEnvVals
let urlparam = head cgiServerEnvVals
formEnv <- getFormVariables
if null formEnv
then do
scriptKey <- if loadbalance==Multiple then getFreshKey
else return ""
catch (submitToServerOrStart url serverargs loadbalance portname
scriptKey serverprog cgiServerEnv)
(\_ -> putStrLn (noHandlerPage url urlparam))
else do
let scriptKey = maybe "" id (lookup "SCRIPTKEY" formEnv)
cgiEnviron = ("SCRIPTKEY",scriptKey) : cgiServerEnv
newcenv = filter (\e -> fst e /= "SCRIPTKEY") formEnv
catch (sendToServerAndPrintOrFail scriptKey cgiEnviron newcenv)
(\_ -> putStrLn (noHandlerPage url urlparam))
where
sendToServerAndPrintOrFail scriptKey cgiEnviron newcenv = do
h <- trySendScriptServerMessage (portname++scriptKey)
(CgiSubmit cgiEnviron newcenv)
eof <- hIsEOF h
if eof then error "Html.cgiScript: unexpected EOF failure"
else copyOutputAndClose h
getFreshKey :: IO String
getFreshKey = do
ctime <- getClockTime
pid <- getPID
return (show (clockTimeToInt ctime) ++ '_' : show pid)
noHandlerPage :: String -> String -> String
noHandlerPage cgiurl urlparam =
"Content-type: text/html\n\n" ++
"<html>\n<head><title>Server Error</title></head>\n" ++
"<body>\n<h1>Error: no submission handler</h1>\n" ++
"<p>Your request cannot be processed due to one of the following reasons:</p>\n" ++
"<ul>\n" ++
"<li>You have not submitted the web form for a long period (timeout).</li>\n"++
"<li>You have used the 'back' button of your browser and submitted\n"++
" the web form again (which should not be done in order to avoid the\n"++
" double submission of data).</li>\n" ++
"<li>The web server has been rebooted.</li>\n" ++
"</ul>\n" ++
"<p>In any case, <a href=\"" ++
(cgiurl ++ if null urlparam then "" else '?':urlparam) ++
"\">please click here to restart.</a></p>\n" ++
"</body>\n</html>"
cgiServerEnvVars :: [String]
cgiServerEnvVars =
["PATH_INFO","QUERY_STRING","HTTP_COOKIE","REMOTE_HOST","REMOTE_ADDR",
"REQUEST_METHOD","SCRIPT_NAME","SERVER_NAME","SERVER_PORT"]
trySendScriptServerMessage :: String -> a -> IO Handle
trySendScriptServerMessage portname msg =
connectToSocketRepeat scriptServerTimeOut done 0 (portname++"@localhost") >>=
maybe failed (\h -> hPutStrLn h (showQTerm msg) >> hFlush h >> return h)
submitToServerOrStart :: String -> String -> LoadBalance -> String -> String
-> String -> [(String,String)] -> IO ()
submitToServerOrStart url serverargs loadbalance pname scriptkey
serverprog cgiServerEnv =
connectToSocketRepeat scriptServerTimeOut done 0 completeportname >>=
maybe (execAndCopyOutput servercmd)
(\h ->
if loadbalance/=Standard
then cgiSubmit h
else do
isbusy <- getLoadOfServer h
if isbusy
then submitToOtherServer
else connectToSocketRepeat scriptServerTimeOut done 0
completeportname >>=
maybe (execAndCopyOutput servercmd) cgiSubmit )
where
completeportname = pname++scriptkey++"@localhost"
cmd = serverprog ++ serverargs ++ " -port \"" ++ pname
++ "\" -scriptkey \"" ++ scriptkey ++ "\""
errout = if withCgiLogging then " 2>> "++url++".log" else ""
servercmd = cmd++errout++" &"
cgiSubmit h = do
let cgiEnviron = ("SCRIPTKEY",scriptkey) : cgiServerEnv
hPutStrLn h (showQTerm (CgiSubmit cgiEnviron []))
hFlush h
copyOutputAndClose h
getLoadOfServer h = do
hPutStrLn h (showQTerm GetLoad)
hFlush h
loadanswer <- hGetLine h
hClose h
return (take 4 loadanswer == "busy")
submitToOtherServer = do
other <- findOtherReadyServer
otherscriptkey <- maybe (getFreshKey >>= \k -> return (scriptkey++k))
return
other
submitToServerOrStart url serverargs loadbalance pname
otherscriptkey serverprog cgiServerEnv
findOtherReadyServer = do
regs <- readCgiServerRegistry
let otherports = map (\ (_,_,p)->p)
(filter (\ (_,prog,_) -> serverprog==prog) regs)
findOtherReadyServerInPorts otherports
findOtherReadyServerInPorts [] = return Nothing
findOtherReadyServerInPorts (p:ps) = do
let (ppname,pscriptkey) = splitAt (length pname) p
if ppname==pname
then connectToSocketRepeat scriptServerTimeOut done 0 (p++"@localhost") >>=
maybe (findOtherReadyServerInPorts ps)
(\h -> do
isbusy <- getLoadOfServer h
if isbusy
then findOtherReadyServerInPorts ps
else return (Just pscriptkey) )
else findOtherReadyServerInPorts ps
execAndCopyOutput :: String -> IO ()
execAndCopyOutput cmd = connectToCommand cmd >>= copyOutputAndClose
copyOutputAndClose :: Handle -> IO ()
copyOutputAndClose h = do
clen <- copyUntilEmptyLine 0
if clen==0 then copyOutputUntilEOF else copyOutputLength clen
hClose h
where
copyUntilEmptyLine clen = do
l <- hGetLine h
putStrLn l
let clen' = if "Content-Length:" `isPrefixOf` l
then maybe clen fst (readNat (drop 15 l))
else clen
if null l then return clen' else copyUntilEmptyLine clen'
copyOutputUntilEOF = do
eof <- hIsEOF h
if eof
then done
else hGetLine h >>= putStrLn >> copyOutputUntilEOF
copyOutputLength n = do
if n>0 then hGetChar h >>= putChar >> copyOutputLength (n-1)
else done
getInputAndClose :: Handle -> IO String
getInputAndClose h = do
(clen,s) <- getUntilEmptyLine 0
cs <- if clen==0 then getInputUntilEOF else getInputLength clen
hClose h
return (s ++ cs)
where
getUntilEmptyLine clen = do
l <- hGetLine h
let clen' = if "Content-Length:" `isPrefixOf` l
then maybe clen fst (readNat (drop 15 l))
else clen
if null l then return (clen',l)
else do (clen'',s) <- getUntilEmptyLine clen'
return (clen'', l ++ '\n' : s)
getInputUntilEOF = do
eof <- hIsEOF h
if eof
then return ""
else do ln <- hGetLine h
cs <- getInputUntilEOF
return (ln ++ '\n' : cs)
getInputLength n = do
if n>0 then do c <- hGetChar h
cs <- getInputLength (n-1)
return (c:cs)
else return ""
putErrLn :: String -> IO ()
putErrLn s = hPutStrLn stderr s >> hFlush stderr
getFormVariables :: IO [(String,String)]
getFormVariables = do
clen <- getEnviron "CONTENT_LENGTH"
cont <- getNChar (maybe 0 fst (readNat clen))
return (includeCoordinates (parseCgiEnv cont))
parseCgiEnv :: String -> [(String,String)]
parseCgiEnv s | s == "" = []
| otherwise = map ufield2field
(map (\(n,v)->(n,utf2latin (urlencoded2string v)))
(map (splitChar '=') (split (=='&') s)))
where
ufield2field (n,v) = if take 7 n == "UFIELD_"
then (tail n, utf2latin (urlencoded2string v))
else (n,v)
splitChar c xs = let (ys,zs) = break (==c) xs
in if zs==[] then (ys,zs) else (ys,tail zs)
split p xs =
let (ys,zs) = break p xs
in if zs==[] then [ys]
else ys : split p (tail zs)
urlencoded2string :: String -> String
urlencoded2string [] = []
urlencoded2string (c:cs)
| c == '+' = ' ' : urlencoded2string cs
| c == '%' = chr (maybe 0 fst (readHex (take 2 cs)))
: urlencoded2string (drop 2 cs)
| otherwise = c : urlencoded2string cs
utf2latin :: String -> String
utf2latin [] = []
utf2latin [c] = [c]
utf2latin (c1:c2:cs)
| ord c1 == 195 = chr (ord c2 + 64) : utf2latin cs
| otherwise = c1 : utf2latin (c2:cs)
includeCoordinates :: [(String,String)] -> [(String,String)]
includeCoordinates [] = []
includeCoordinates ((tag,val):cenv)
= case break (=='.') tag of
(_,[]) -> (tag,val):includeCoordinates cenv
(event,['.','x']) -> ("x",val):(event,val):includeCoordinates cenv
(_,['.','y']) -> ("y",val):includeCoordinates cenv
_ -> error "includeCoordinates: unexpected . in url parameter"
getNChar :: Int -> IO String
getNChar n = if n<=0 then return ""
else do c <- getChar
cs <- getNChar (n-1)
return (c:cs)
registerCgiServer :: String -> String -> IO ()
registerCgiServer eurl epname = do
registryfile <- cgiServerRegistry
((register $## registryfile) $## eurl) $## epname
where
register registryfile url pname = exclusiveIO (registryfile ++ ".lock") $ do
exreg <- doesFileExist registryfile
unless exreg $ do
writeFile registryfile ""
system ("chmod 666 " ++ registryfile) >> done
pid <- getPID
wd <- getCurrentDirectory
appendFile registryfile (show (pid,wd++"/"++url++".server",pname) ++ "\n")
unregisterCgiServer :: String -> IO ()
unregisterCgiServer epname = do
registryfile <- cgiServerRegistry
(unregister $## registryfile) $## epname
where
unregister registryfile pname = exclusiveIO (registryfile ++ ".lock") $ do
exreg <- doesFileExist registryfile
if not exreg then done else do
mypid <- getPID
regs <- readCgiServerRegistry
let uregs = filter (\ (pid,_,port) -> mypid/=pid || pname/=port) regs
writeFile registryfile (concatMap (\reg -> show reg ++ "\n") uregs)
readCgiServerRegistry :: IO [(Int,String,String)]
readCgiServerRegistry = do
registryfile <- cgiServerRegistry
regs <- readQTermListFile registryfile
seq (length regs) done
return regs
|