2 * Dynamic web page generation
with Standard ML
3 * Copyright (C
) 2003 Adam Chlipala
5 * This library is free software
; you can redistribute it
and/or
6 * modify it under the terms
of the GNU Lesser General Public
7 * License
as published by the Free Software Foundation
; either
8 * version
2.1 of the License
, or (at your option
) any later version
.
10 * This library is distributed
in the hope that it will be useful
,
11 * but WITHOUT ANY WARRANTY
; without even the implied warranty
of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
. See the GNU
13 * Lesser General Public License for more details
.
15 * You should have received a copy
of the GNU Lesser General Public
16 * License along
with this library
; if not
, write to the Free Software
17 * Foundation
, Inc
., 59 Temple Place
, Suite
330, Boston
, MA
02111-1307 USA
22 structure Web
:> WEB
=
48 val params
: string list StringMap
.map ref
= ref StringMap
.empty
50 val paramStack
: string list StringMap
.map list ref
= ref
[]
52 fun setParam (n
, v
) = params
:= StringMap
.insert (!params
, n
, v
)
53 fun setSingleParam (n
, v
) = setParam (n
, [v
])
56 (case StringMap
.find (!params
, v
) of
62 (case StringMap
.find (!params
, v
) of
67 (paramStack
:= (!params
) :: (!paramStack
);
72 [] => raise Fail
"Empty params stack in popParams"
73 | h
::t
=> (params
:= h
;
76 fun withParams f nvs
=
78 ((f ()) handle ex
=> (popParams (); raise ex
))
81 val text
= ref ([] : string list
)
83 fun print x
= text
:= x
:: (!text
)
86 (TextIO.print
"Status: 200\nContent-type: text/html\n\n";
87 TextIO.print (String.concat (List.rev (!text
))))
89 val getCgi
= OS
.Process
.getEnv
96 | xch #
"\"" = """
99 foldr
op^
"" (map
xch (String.explode s
))
104 fun xch #
"<" = "<"
107 | xch #
"\"" = """
108 | xch #
"\n" = "<br />"
111 foldr
op^
"" (map
xch (String.explode s
))
114 exception Format
of string
116 fun stoiOpt s
= Int.fromString s
118 (case Int.fromString s
of
119 NONE
=> raise Format s
122 fun storOpt s
= Real.fromString s
124 (case Real.fromString s
of
125 NONE
=> raise Format s
129 StringMap
.foldli (fn (n
, vs
, s
) => foldl (fn (v
, s
) => s ^
" VALUE: " ^ v
) (s ^
" NAME: " ^ n
) vs
)