annotate src/settings.sml @ 1050:93315ac00394

More fun with cookies
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Nov 2009 14:20:00 -0500
parents 065ce3252090
children eaba663fd6aa
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@1050 83 "clear_cookie",
adamc@765 84 "new_client_source",
adamc@765 85 "get_client_source",
adamc@765 86 "set_client_source",
adamc@845 87 "current",
adamc@765 88 "alert",
adamc@765 89 "new_channel",
adamc@765 90 "send",
adamc@765 91 "onError",
adamc@765 92 "onFail",
adamc@765 93 "onConnectFail",
adamc@765 94 "onDisconnect",
adamc@895 95 "onServerError",
adamc@895 96 "kc"]
adamc@765 97
adamc@765 98 val effectful = ref effectfulBase
adamc@765 99 fun setEffectful ls = effectful := S.addList (effectfulBase, ls)
adamc@765 100 fun isEffectful x = S.member (!effectful, x)
adamc@765 101
adamc@765 102 val clientBase = basis ["get",
adamc@765 103 "set",
adamc@841 104 "current",
adamc@765 105 "alert",
adamc@765 106 "recv",
adamc@765 107 "sleep",
adamc@765 108 "spawn",
adamc@765 109 "onError",
adamc@765 110 "onFail",
adamc@765 111 "onConnectFail",
adamc@765 112 "onDisconnect",
adamc@895 113 "onServerError",
adamc@895 114 "kc"]
adamc@765 115 val client = ref clientBase
adamc@765 116 fun setClientOnly ls = client := S.addList (clientBase, ls)
adamc@765 117 fun isClientOnly x = S.member (!client, x)
adamc@765 118
adamc@765 119 val serverBase = basis ["requestHeader",
adamc@765 120 "query",
adamc@765 121 "dml",
adamc@765 122 "nextval",
adamc@765 123 "channel",
adamc@765 124 "send"]
adamc@765 125 val server = ref serverBase
adamc@765 126 fun setServerOnly ls = server := S.addList (serverBase, ls)
adamc@765 127 fun isServerOnly x = S.member (!server, x)
adamc@765 128
adamc@765 129 val basisM = foldl (fn ((k, v : string), m) => M.insert (m, ("Basis", k), v)) M.empty
adamc@765 130
adamc@765 131 val jsFuncsBase = basisM [("alert", "alert"),
adamc@765 132 ("get_client_source", "sg"),
adamc@841 133 ("current", "scur"),
adamc@765 134 ("htmlifyBool", "bs"),
adamc@765 135 ("htmlifyFloat", "ts"),
adamc@765 136 ("htmlifyInt", "ts"),
adamc@765 137 ("htmlifyString", "eh"),
adamc@765 138 ("new_client_source", "sc"),
adamc@765 139 ("set_client_source", "sv"),
adamc@838 140 ("stringToFloat", "pflo"),
adamc@838 141 ("stringToInt", "pio"),
adamc@765 142 ("stringToFloat_error", "pfl"),
adamc@765 143 ("stringToInt_error", "pi"),
adamc@765 144 ("urlifyInt", "ts"),
adamc@765 145 ("urlifyFloat", "ts"),
adamc@765 146 ("urlifyString", "uf"),
adamc@912 147 ("urlifyBool", "ub"),
adamc@765 148 ("recv", "rv"),
adamc@765 149 ("strcat", "cat"),
adamc@765 150 ("intToString", "ts"),
adamc@765 151 ("floatToString", "ts"),
adamc@821 152 ("charToString", "ts"),
adamc@765 153 ("onError", "onError"),
adamc@765 154 ("onFail", "onFail"),
adamc@765 155 ("onConnectFail", "onConnectFail"),
adamc@765 156 ("onDisconnect", "onDisconnect"),
adamc@798 157 ("onServerError", "onServerError"),
adamc@798 158 ("attrifyString", "escape"),
adamc@798 159 ("attrifyInt", "ts"),
adamc@798 160 ("attrifyFloat", "ts"),
adamc@820 161 ("attrifyBool", "bs"),
adamc@821 162 ("boolToString", "ts"),
adamc@821 163 ("strsub", "sub"),
adamc@828 164 ("strsuffix", "suf"),
adamc@829 165 ("strlen", "slen"),
adamc@829 166 ("strindex", "sidx"),
adamc@829 167 ("strchr", "schr"),
adamc@831 168 ("substring", "ssub"),
adamc@895 169 ("strcspn", "sspn"),
adamc@895 170 ("kc", "kc")]
adamc@765 171 val jsFuncs = ref jsFuncsBase
adamc@765 172 fun setJsFuncs ls = jsFuncs := foldl (fn ((k, v), m) => M.insert (m, k, v)) jsFuncsBase ls
adamc@765 173 fun jsFunc x = M.find (!jsFuncs, x)
adamc@765 174
adamc@768 175 datatype pattern_kind = Exact | Prefix
adamc@768 176 datatype action = Allow | Deny
adamc@768 177 type rule = { action : action, kind : pattern_kind, pattern : string }
adamc@768 178
adamc@768 179 datatype path_kind = Any | Url | Table | Sequence | View | Relation | Cookie | Style
adamc@768 180 type rewrite = { pkind : path_kind, kind : pattern_kind, from : string, to : string }
adamc@768 181
adamc@768 182 val rewrites = ref ([] : rewrite list)
adamc@768 183
adamc@768 184 fun subsume (pk1, pk2) =
adamc@768 185 pk1 = pk2
adamc@768 186 orelse pk2 = Any
adamc@768 187 orelse pk2 = Relation andalso (pk1 = Table orelse pk1 = Sequence orelse pk1 = View)
adamc@768 188
adamc@768 189 fun setRewriteRules ls = rewrites := ls
adamc@768 190 fun rewrite pk s =
adamc@768 191 let
adamc@768 192 fun rew (ls : rewrite list) =
adamc@768 193 case ls of
adamc@768 194 [] => s
adamc@768 195 | rewr :: ls =>
adamc@768 196 let
adamc@768 197 fun match () =
adamc@768 198 case #kind rewr of
adamc@768 199 Exact => if #from rewr = s then
adamc@768 200 SOME (size s)
adamc@768 201 else
adamc@768 202 NONE
adamc@768 203 | Prefix => if String.isPrefix (#from rewr) s then
adamc@768 204 SOME (size (#from rewr))
adamc@768 205 else
adamc@768 206 NONE
adamc@768 207 in
adamc@768 208 if subsume (pk, #pkind rewr) then
adamc@768 209 case match () of
adamc@768 210 NONE => rew ls
adamc@768 211 | SOME suffixStart => #to rewr ^ String.extract (s, suffixStart, NONE)
adamc@768 212 else
adamc@768 213 rew ls
adamc@768 214 end
adamc@768 215 in
adamc@768 216 rew (!rewrites)
adamc@768 217 end
adamc@768 218
adamc@769 219 val url = ref ([] : rule list)
adamc@769 220 val mime = ref ([] : rule list)
adamc@769 221
adamc@769 222 fun setUrlRules ls = url := ls
adamc@769 223 fun setMimeRules ls = mime := ls
adamc@769 224
adamc@770 225 fun getUrlRules () = !url
adamc@770 226 fun getMimeRules () = !mime
adamc@770 227
adamc@769 228 fun check f rules s =
adamc@769 229 let
adamc@769 230 fun chk (ls : rule list) =
adamc@769 231 case ls of
adamc@769 232 [] => false
adamc@769 233 | rule :: ls =>
adamc@769 234 let
adamc@769 235 val matches =
adamc@769 236 case #kind rule of
adamc@769 237 Exact => #pattern rule = s
adamc@769 238 | Prefix => String.isPrefix (#pattern rule) s
adamc@769 239 in
adamc@769 240 if matches then
adamc@769 241 case #action rule of
adamc@769 242 Allow => true
adamc@769 243 | Deny => false
adamc@769 244 else
adamc@769 245 chk ls
adamc@769 246 end
adamc@769 247 in
adamc@769 248 f s andalso chk (!rules)
adamc@769 249 end
adamc@769 250
adamc@769 251 val checkUrl = check (fn _ => true) url
adamc@769 252 val checkMime = check
adamc@769 253 (CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"/" orelse ch = #"-" orelse ch = #"."))
adamc@769 254 mime
adamc@769 255
adamc@855 256
adamc@855 257 type protocol = {
adamc@855 258 name : string,
adamc@855 259 link : string,
adamc@858 260 persistent : bool
adamc@855 261 }
adamc@855 262 val protocols = ref ([] : protocol list)
adamc@855 263 fun addProtocol p = protocols := p :: !protocols
adamc@855 264 fun getProtocol s = List.find (fn p => #name p = s) (!protocols)
adamc@855 265
adamc@855 266 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@855 267 file = s}
adamc@855 268
adamc@865 269 val curProto = ref {name = "",
adamc@865 270 link = "",
adamc@865 271 persistent = false}
adamc@856 272 fun setProtocol name =
adamc@856 273 case getProtocol name of
adamc@856 274 NONE => raise Fail ("Unknown protocol " ^ name)
adamc@856 275 | SOME p => curProto := p
adamc@855 276 fun currentProtocol () = !curProto
adamc@855 277
adamc@857 278 val debug = ref false
adamc@857 279 fun setDebug b = debug := b
adamc@857 280 fun getDebug () = !debug
adamc@857 281
adamc@867 282 datatype sql_type =
adamc@867 283 Int
adamc@867 284 | Float
adamc@867 285 | String
adamc@1011 286 | Char
adamc@867 287 | Bool
adamc@867 288 | Time
adamc@867 289 | Blob
adamc@867 290 | Channel
adamc@867 291 | Client
adamc@867 292 | Nullable of sql_type
adamc@867 293
adamc@873 294 fun p_sql_ctype t =
adamc@867 295 let
adamc@867 296 open Print.PD
adamc@867 297 open Print
adamc@867 298 in
adamc@867 299 case t of
adamc@870 300 Int => "uw_Basis_int"
adamc@870 301 | Float => "uw_Basis_float"
adamc@870 302 | String => "uw_Basis_string"
adamc@1011 303 | Char => "uw_Basis_char"
adamc@870 304 | Bool => "uw_Basis_bool"
adamc@870 305 | Time => "uw_Basis_time"
adamc@870 306 | Blob => "uw_Basis_blob"
adamc@870 307 | Channel => "uw_Basis_channel"
adamc@870 308 | Client => "uw_Basis_client"
adamc@870 309 | Nullable String => "uw_Basis_string"
adamc@873 310 | Nullable t => p_sql_ctype t ^ "*"
adamc@867 311 end
adamc@867 312
adamc@867 313 fun isBlob Blob = true
adamc@867 314 | isBlob (Nullable t) = isBlob t
adamc@867 315 | isBlob _ = false
adamc@867 316
adamc@870 317 fun isNotNull (Nullable _) = false
adamc@870 318 | isNotNull _ = true
adamc@870 319
adamc@866 320 type dbms = {
adamc@866 321 name : string,
adamc@866 322 header : string,
adamc@866 323 link : string,
adamc@873 324 p_sql_type : sql_type -> string,
adamc@870 325 init : {dbstring : string,
adamc@870 326 prepared : (string * int) list,
adamc@870 327 tables : (string * (string * sql_type) list) list,
adamc@872 328 views : (string * (string * sql_type) list) list,
adamc@870 329 sequences : string list} -> Print.PD.pp_desc,
adamc@873 330 query : {loc : ErrorMsg.span, cols : sql_type list,
adamc@880 331 doCols : ({loc : ErrorMsg.span, wontLeakStrings : bool, col : int, typ : sql_type} -> Print.PD.pp_desc)
adamc@867 332 -> Print.PD.pp_desc}
adamc@867 333 -> Print.PD.pp_desc,
adamc@867 334 queryPrepared : {loc : ErrorMsg.span, id : int, query : string,
adamc@873 335 inputs : sql_type list, cols : sql_type list,
adamc@880 336 doCols : ({loc : ErrorMsg.span, wontLeakStrings : bool, col : int,
adamc@880 337 typ : sql_type} -> Print.PD.pp_desc)
adamc@879 338 -> Print.PD.pp_desc,
adamc@879 339 nested : bool}
adamc@868 340 -> Print.PD.pp_desc,
adamc@868 341 dml : ErrorMsg.span -> Print.PD.pp_desc,
adamc@868 342 dmlPrepared : {loc : ErrorMsg.span, id : int, dml : string,
adamc@869 343 inputs : sql_type list} -> Print.PD.pp_desc,
adamc@878 344 nextval : {loc : ErrorMsg.span, seqName : string option, seqE : Print.PD.pp_desc} -> Print.PD.pp_desc,
adamc@874 345 nextvalPrepared : {loc : ErrorMsg.span, id : int, query : string} -> Print.PD.pp_desc,
adamc@874 346 sqlifyString : string -> string,
adamc@874 347 p_cast : string * sql_type -> string,
adamc@874 348 p_blank : int * sql_type -> string,
adamc@877 349 supportsDeleteAs : bool,
adamc@886 350 supportsUpdateAs : bool,
adamc@877 351 createSequence : string -> string,
adamc@878 352 textKeysNeedLengths : bool,
adamc@879 353 supportsNextval : bool,
adamc@882 354 supportsNestedPrepared : bool,
adamc@890 355 sqlPrefix : string,
adamc@1014 356 supportsOctetLength : bool,
adamc@1014 357 trueString : string,
adamc@1014 358 falseString : string
adamc@866 359 }
adamc@866 360
adamc@866 361 val dbmses = ref ([] : dbms list)
adamc@866 362 val curDb = ref ({name = "",
adamc@866 363 header = "",
adamc@866 364 link = "",
adamc@873 365 p_sql_type = fn _ => "",
adamc@867 366 init = fn _ => Print.box [],
adamc@867 367 query = fn _ => Print.box [],
adamc@868 368 queryPrepared = fn _ => Print.box [],
adamc@868 369 dml = fn _ => Print.box [],
adamc@869 370 dmlPrepared = fn _ => Print.box [],
adamc@869 371 nextval = fn _ => Print.box [],
adamc@874 372 nextvalPrepared = fn _ => Print.box [],
adamc@874 373 sqlifyString = fn s => s,
adamc@874 374 p_cast = fn _ => "",
adamc@874 375 p_blank = fn _ => "",
adamc@877 376 supportsDeleteAs = false,
adamc@886 377 supportsUpdateAs = false,
adamc@877 378 createSequence = fn _ => "",
adamc@878 379 textKeysNeedLengths = false,
adamc@879 380 supportsNextval = false,
adamc@882 381 supportsNestedPrepared = false,
adamc@890 382 sqlPrefix = "",
adamc@1014 383 supportsOctetLength = false,
adamc@1014 384 trueString = "",
adamc@1014 385 falseString = ""} : dbms)
adamc@866 386
adamc@866 387 fun addDbms v = dbmses := v :: !dbmses
adamc@866 388 fun setDbms s =
adamc@866 389 case List.find (fn db => #name db = s) (!dbmses) of
adamc@866 390 NONE => raise Fail ("Unknown DBMS " ^ s)
adamc@866 391 | SOME db => curDb := db
adamc@866 392 fun currentDbms () = !curDb
adamc@866 393
adamc@891 394 val dbstring = ref (NONE : string option)
adamc@891 395 fun setDbstring so = dbstring := so
adamc@891 396 fun getDbstring () = !dbstring
adamc@891 397
adamc@891 398 val exe = ref (NONE : string option)
adamc@891 399 fun setExe so = exe := so
adamc@891 400 fun getExe () = !exe
adamc@891 401
adamc@891 402 val sql = ref (NONE : string option)
adamc@891 403 fun setSql so = sql := so
adamc@891 404 fun getSql () = !sql
adamc@891 405
adamc@1016 406 val coreInline = ref 20
adamc@1016 407 fun setCoreInline n = coreInline := n
adamc@1016 408 fun getCoreInline () = !coreInline
adamc@1016 409
adamc@1016 410 val monoInline = ref 20
adamc@1016 411 fun setMonoInline n = monoInline := n
adamc@1016 412 fun getMonoInline () = !monoInline
adamc@1016 413
adamc@765 414 end