annotate src/settings.sml @ 1121:0cee0c8d8c37

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