annotate src/settings.sml @ 828:14a6c0971d89

String.length
author Adam Chlipala <adamc@hcoop.net>
date Sat, 30 May 2009 09:59:10 -0400
parents 395a5d450cc0
children 20fe00fd81da
rev   line source
adamc@764 1 (* Copyright (c) 2008-2009, Adam Chlipala
adamc@764 2 * All rights reserved.
adamc@764 3 *
adamc@764 4 * Redistribution and use in source and binary forms, with or without
adamc@764 5 * modification, are permitted provided that the following conditions are met:
adamc@764 6 *
adamc@764 7 * - Redistributions of source code must retain the above copyright notice,
adamc@764 8 * this list of conditions and the following disclaimer.
adamc@764 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@764 10 * this list of conditions and the following disclaimer in the documentation
adamc@764 11 * and/or other materials provided with the distribution.
adamc@764 12 * - The names of contributors may not be used to endorse or promote products
adamc@764 13 * derived from this software without specific prior written permission.
adamc@764 14 *
adamc@764 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@764 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@764 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@764 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@764 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@764 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@764 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@764 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@764 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@764 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@764 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@764 26 *)
adamc@764 27
adamc@764 28 structure Settings :> SETTINGS = struct
adamc@764 29
adamc@764 30 val urlPrefix = ref "/"
adamc@764 31 val timeout = ref 0
adamc@764 32 val headers = ref ([] : string list)
adamc@766 33 val scripts = ref ([] : string list)
adamc@764 34
adamc@764 35 fun getUrlPrefix () = !urlPrefix
adamc@764 36 fun setUrlPrefix p =
adamc@764 37 urlPrefix := (if p = "" then
adamc@764 38 "/"
adamc@764 39 else if String.sub (p, size p - 1) <> #"/" then
adamc@764 40 p ^ "/"
adamc@764 41 else
adamc@764 42 p)
adamc@764 43
adamc@764 44 fun getTimeout () = !timeout
adamc@764 45 fun setTimeout n = timeout := n
adamc@764 46
adamc@764 47 fun getHeaders () = !headers
adamc@764 48 fun setHeaders ls = headers := ls
adamc@764 49
adamc@766 50 fun getScripts () = !scripts
adamc@766 51 fun setScripts ls = scripts := ls
adamc@766 52
adamc@765 53 type ffi = string * string
adamc@765 54
adamc@765 55 structure K = struct
adamc@765 56 type ord_key = ffi
adamc@765 57 fun compare ((m1, x1), (m2, x2)) =
adamc@765 58 Order.join (String.compare (m1, m2),
adamc@765 59 fn () => String.compare (x1, x2))
adamc@764 60 end
adamc@765 61
adamc@765 62 structure S = BinarySetFn(K)
adamc@765 63 structure M = BinaryMapFn(K)
adamc@765 64
adamc@765 65 fun basis x = S.addList (S.empty, map (fn x : string => ("Basis", x)) x)
adamc@765 66
adamc@765 67 val clientToServerBase = basis ["int",
adamc@765 68 "float",
adamc@765 69 "string",
adamc@765 70 "time",
adamc@765 71 "file",
adamc@765 72 "unit",
adamc@765 73 "option",
adamc@765 74 "list",
adamc@765 75 "bool"]
adamc@765 76 val clientToServer = ref clientToServerBase
adamc@765 77 fun setClientToServer ls = clientToServer := S.addList (clientToServerBase, ls)
adamc@765 78 fun mayClientToServer x = S.member (!clientToServer, x)
adamc@765 79
adamc@779 80 val effectfulBase = basis ["dml",
adamc@779 81 "nextval",
adamc@779 82 "set_cookie",
adamc@765 83 "new_client_source",
adamc@765 84 "get_client_source",
adamc@765 85 "set_client_source",
adamc@765 86 "alert",
adamc@765 87 "new_channel",
adamc@765 88 "send",
adamc@765 89 "onError",
adamc@765 90 "onFail",
adamc@765 91 "onConnectFail",
adamc@765 92 "onDisconnect",
adamc@765 93 "onServerError"]
adamc@765 94
adamc@765 95 val effectful = ref effectfulBase
adamc@765 96 fun setEffectful ls = effectful := S.addList (effectfulBase, ls)
adamc@765 97 fun isEffectful x = S.member (!effectful, x)
adamc@765 98
adamc@765 99 val clientBase = basis ["get",
adamc@765 100 "set",
adamc@765 101 "alert",
adamc@765 102 "recv",
adamc@765 103 "sleep",
adamc@765 104 "spawn",
adamc@765 105 "onError",
adamc@765 106 "onFail",
adamc@765 107 "onConnectFail",
adamc@765 108 "onDisconnect",
adamc@765 109 "onServerError"]
adamc@765 110 val client = ref clientBase
adamc@765 111 fun setClientOnly ls = client := S.addList (clientBase, ls)
adamc@765 112 fun isClientOnly x = S.member (!client, x)
adamc@765 113
adamc@765 114 val serverBase = basis ["requestHeader",
adamc@765 115 "query",
adamc@765 116 "dml",
adamc@765 117 "nextval",
adamc@765 118 "channel",
adamc@765 119 "send"]
adamc@765 120 val server = ref serverBase
adamc@765 121 fun setServerOnly ls = server := S.addList (serverBase, ls)
adamc@765 122 fun isServerOnly x = S.member (!server, x)
adamc@765 123
adamc@765 124 val basisM = foldl (fn ((k, v : string), m) => M.insert (m, ("Basis", k), v)) M.empty
adamc@765 125
adamc@765 126 val jsFuncsBase = basisM [("alert", "alert"),
adamc@765 127 ("get_client_source", "sg"),
adamc@765 128 ("htmlifyBool", "bs"),
adamc@765 129 ("htmlifyFloat", "ts"),
adamc@765 130 ("htmlifyInt", "ts"),
adamc@765 131 ("htmlifyString", "eh"),
adamc@765 132 ("new_client_source", "sc"),
adamc@765 133 ("set_client_source", "sv"),
adamc@765 134 ("stringToFloat_error", "pfl"),
adamc@765 135 ("stringToInt_error", "pi"),
adamc@765 136 ("urlifyInt", "ts"),
adamc@765 137 ("urlifyFloat", "ts"),
adamc@765 138 ("urlifyString", "uf"),
adamc@765 139 ("recv", "rv"),
adamc@765 140 ("strcat", "cat"),
adamc@765 141 ("intToString", "ts"),
adamc@765 142 ("floatToString", "ts"),
adamc@821 143 ("charToString", "ts"),
adamc@765 144 ("onError", "onError"),
adamc@765 145 ("onFail", "onFail"),
adamc@765 146 ("onConnectFail", "onConnectFail"),
adamc@765 147 ("onDisconnect", "onDisconnect"),
adamc@798 148 ("onServerError", "onServerError"),
adamc@798 149 ("attrifyString", "escape"),
adamc@798 150 ("attrifyInt", "ts"),
adamc@798 151 ("attrifyFloat", "ts"),
adamc@820 152 ("attrifyBool", "bs"),
adamc@821 153 ("boolToString", "ts"),
adamc@821 154 ("strsub", "sub"),
adamc@828 155 ("strsuffix", "suf"),
adamc@828 156 ("strlen", "slen")]
adamc@765 157 val jsFuncs = ref jsFuncsBase
adamc@765 158 fun setJsFuncs ls = jsFuncs := foldl (fn ((k, v), m) => M.insert (m, k, v)) jsFuncsBase ls
adamc@765 159 fun jsFunc x = M.find (!jsFuncs, x)
adamc@765 160
adamc@768 161 datatype pattern_kind = Exact | Prefix
adamc@768 162 datatype action = Allow | Deny
adamc@768 163 type rule = { action : action, kind : pattern_kind, pattern : string }
adamc@768 164
adamc@768 165 datatype path_kind = Any | Url | Table | Sequence | View | Relation | Cookie | Style
adamc@768 166 type rewrite = { pkind : path_kind, kind : pattern_kind, from : string, to : string }
adamc@768 167
adamc@768 168 val rewrites = ref ([] : rewrite list)
adamc@768 169
adamc@768 170 fun subsume (pk1, pk2) =
adamc@768 171 pk1 = pk2
adamc@768 172 orelse pk2 = Any
adamc@768 173 orelse pk2 = Relation andalso (pk1 = Table orelse pk1 = Sequence orelse pk1 = View)
adamc@768 174
adamc@768 175 fun setRewriteRules ls = rewrites := ls
adamc@768 176 fun rewrite pk s =
adamc@768 177 let
adamc@768 178 fun rew (ls : rewrite list) =
adamc@768 179 case ls of
adamc@768 180 [] => s
adamc@768 181 | rewr :: ls =>
adamc@768 182 let
adamc@768 183 fun match () =
adamc@768 184 case #kind rewr of
adamc@768 185 Exact => if #from rewr = s then
adamc@768 186 SOME (size s)
adamc@768 187 else
adamc@768 188 NONE
adamc@768 189 | Prefix => if String.isPrefix (#from rewr) s then
adamc@768 190 SOME (size (#from rewr))
adamc@768 191 else
adamc@768 192 NONE
adamc@768 193 in
adamc@768 194 if subsume (pk, #pkind rewr) then
adamc@768 195 case match () of
adamc@768 196 NONE => rew ls
adamc@768 197 | SOME suffixStart => #to rewr ^ String.extract (s, suffixStart, NONE)
adamc@768 198 else
adamc@768 199 rew ls
adamc@768 200 end
adamc@768 201 in
adamc@768 202 rew (!rewrites)
adamc@768 203 end
adamc@768 204
adamc@769 205 val url = ref ([] : rule list)
adamc@769 206 val mime = ref ([] : rule list)
adamc@769 207
adamc@769 208 fun setUrlRules ls = url := ls
adamc@769 209 fun setMimeRules ls = mime := ls
adamc@769 210
adamc@770 211 fun getUrlRules () = !url
adamc@770 212 fun getMimeRules () = !mime
adamc@770 213
adamc@769 214 fun check f rules s =
adamc@769 215 let
adamc@769 216 fun chk (ls : rule list) =
adamc@769 217 case ls of
adamc@769 218 [] => false
adamc@769 219 | rule :: ls =>
adamc@769 220 let
adamc@769 221 val matches =
adamc@769 222 case #kind rule of
adamc@769 223 Exact => #pattern rule = s
adamc@769 224 | Prefix => String.isPrefix (#pattern rule) s
adamc@769 225 in
adamc@769 226 if matches then
adamc@769 227 case #action rule of
adamc@769 228 Allow => true
adamc@769 229 | Deny => false
adamc@769 230 else
adamc@769 231 chk ls
adamc@769 232 end
adamc@769 233 in
adamc@769 234 f s andalso chk (!rules)
adamc@769 235 end
adamc@769 236
adamc@769 237 val checkUrl = check (fn _ => true) url
adamc@769 238 val checkMime = check
adamc@769 239 (CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"/" orelse ch = #"-" orelse ch = #"."))
adamc@769 240 mime
adamc@769 241
adamc@765 242 end