annotate src/settings.sml @ 2307:6ae9a2784a45

Return to working version mode
author Adam Chlipala <adam@chlipala.net>
date Sun, 20 Dec 2015 14:39:50 -0500
parents 6fb9232ade99
children
rev   line source
adam@1478 1 (* Copyright (c) 2008-2011, 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
rmbruijn@1597 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
ezyang@1739 30 val configBin = ref Config.bin
ezyang@1739 31 val configLib = ref Config.lib
ezyang@1739 32 val configSrcLib = ref Config.srclib
ezyang@1739 33 val configInclude = ref Config.includ
ezyang@1739 34 val configSitelisp = ref Config.sitelisp
ezyang@1739 35
grrwlf@1871 36 val configCCompiler = ref Config.ccompiler
grrwlf@1871 37
grrwlf@1871 38 fun getCCompiler () = !configCCompiler
grrwlf@1871 39 fun setCCompiler cc = configCCompiler := cc
grrwlf@1871 40
ezyang@1739 41 fun libUr () = OS.Path.joinDirFile {dir = !configSrcLib,
ezyang@1739 42 file = "ur"}
ezyang@1739 43 fun libC () = OS.Path.joinDirFile {dir = !configSrcLib,
ezyang@1739 44 file = "c"}
ezyang@1739 45 fun libJs () = OS.Path.joinDirFile {dir = !configSrcLib,
ezyang@1739 46 file = "js"}
ezyang@1739 47
ezyang@1739 48 fun libFile s = OS.Path.joinDirFile {dir = libUr (),
ezyang@1739 49 file = s}
ezyang@1739 50
adam@1637 51 val urlPrefixFull = ref "/"
adamc@764 52 val urlPrefix = ref "/"
adam@1370 53 val urlPrePrefix = ref ""
adamc@764 54 val timeout = ref 0
adamc@764 55 val headers = ref ([] : string list)
adamc@766 56 val scripts = ref ([] : string list)
adamc@764 57
adam@1637 58 fun getUrlPrefixFull () = !urlPrefixFull
adamc@764 59 fun getUrlPrefix () = !urlPrefix
adam@1370 60 fun getUrlPrePrefix () = !urlPrePrefix
adamc@764 61 fun setUrlPrefix p =
adam@1370 62 let
adam@1370 63 val prefix = if p = "" then
adam@1370 64 "/"
adam@1370 65 else if String.sub (p, size p - 1) <> #"/" then
adam@1370 66 p ^ "/"
adam@1370 67 else
adam@1370 68 p
adam@1370 69
adam@1470 70 fun findPrefix n =
adam@1470 71 let
adam@1470 72 val (befor, after) = Substring.splitl (fn ch => ch <> #"/") (Substring.extract (prefix, n, NONE))
adam@1470 73 in
adam@1470 74 if Substring.isEmpty after then
adam@1470 75 ("", prefix)
adam@1470 76 else
adam@1470 77 (String.substring (prefix, 0, n) ^ Substring.string befor, Substring.string after)
rmbruijn@1597 78 end
adam@1470 79
adam@1370 80 val (prepre, prefix) =
adam@1370 81 if String.isPrefix "http://" prefix then
adam@1470 82 findPrefix 7
adam@1470 83 else if String.isPrefix "https://" prefix then
adam@1470 84 findPrefix 8
adam@1370 85 else
adam@1370 86 ("", prefix)
adam@1370 87 in
adam@1637 88 urlPrefixFull := p;
adam@1370 89 urlPrePrefix := prepre;
adam@1370 90 urlPrefix := prefix
adam@1370 91 end
adamc@764 92
adamc@764 93 fun getTimeout () = !timeout
adamc@764 94 fun setTimeout n = timeout := n
adamc@764 95
adamc@764 96 fun getHeaders () = !headers
adamc@764 97 fun setHeaders ls = headers := ls
adamc@764 98
adamc@766 99 fun getScripts () = !scripts
adamc@766 100 fun setScripts ls = scripts := ls
adamc@766 101
adamc@765 102 type ffi = string * string
adamc@765 103
adamc@765 104 structure K = struct
adamc@765 105 type ord_key = ffi
adamc@765 106 fun compare ((m1, x1), (m2, x2)) =
adamc@765 107 Order.join (String.compare (m1, m2),
adamc@765 108 fn () => String.compare (x1, x2))
adamc@764 109 end
adamc@765 110
adamc@765 111 structure S = BinarySetFn(K)
adamc@765 112 structure M = BinaryMapFn(K)
adamc@765 113
adamc@765 114 fun basis x = S.addList (S.empty, map (fn x : string => ("Basis", x)) x)
adamc@765 115
adamc@765 116 val clientToServerBase = basis ["int",
adamc@765 117 "float",
adamc@765 118 "string",
adamc@765 119 "time",
adamc@765 120 "file",
adamc@765 121 "unit",
adamc@765 122 "option",
adamc@765 123 "list",
adam@1288 124 "bool",
adam@1288 125 "variant"]
adamc@765 126 val clientToServer = ref clientToServerBase
adamc@765 127 fun setClientToServer ls = clientToServer := S.addList (clientToServerBase, ls)
adamc@765 128 fun mayClientToServer x = S.member (!clientToServer, x)
adamc@765 129
adamc@779 130 val effectfulBase = basis ["dml",
adamc@779 131 "nextval",
adamc@1073 132 "setval",
adamc@779 133 "set_cookie",
adamc@1050 134 "clear_cookie",
adamc@765 135 "new_channel",
adamc@1200 136 "send",
adamc@1200 137 "htmlifyInt_w",
adamc@1200 138 "htmlifyFloat_w",
adamc@1200 139 "htmlifyString_w",
adamc@1200 140 "htmlifyBool_w",
adamc@1200 141 "htmlifyTime_w",
adamc@1200 142 "attrifyInt_w",
adamc@1200 143 "attrifyFloat_w",
adamc@1200 144 "attrifyString_w",
adamc@1200 145 "attrifyChar_w",
adamc@1200 146 "urlifyInt_w",
adamc@1200 147 "urlifyFloat_w",
adamc@1200 148 "urlifyString_w",
adamc@1200 149 "urlifyBool_w",
adamc@1200 150 "urlifyChannel_w"]
adamc@765 151
adamc@765 152 val effectful = ref effectfulBase
adamc@765 153 fun setEffectful ls = effectful := S.addList (effectfulBase, ls)
ziv@2265 154 fun isEffectful ("Sqlcache", _) = true
ziv@2265 155 | isEffectful x = S.member (!effectful, x)
adam@1878 156 fun addEffectful x = effectful := S.add (!effectful, x)
adamc@765 157
adamc@1171 158 val benignBase = basis ["get_cookie",
adamc@1171 159 "new_client_source",
adamc@1171 160 "get_client_source",
adamc@1171 161 "set_client_source",
adamc@1171 162 "current",
adamc@1171 163 "alert",
adam@1290 164 "confirm",
adamc@1171 165 "onError",
adamc@1171 166 "onFail",
adamc@1171 167 "onConnectFail",
adamc@1171 168 "onDisconnect",
adamc@1171 169 "onServerError",
adam@1783 170 "mouseEvent",
adam@1783 171 "keyEvent",
adamc@1250 172 "debug",
adam@1422 173 "rand",
adam@1465 174 "now",
adam@1465 175 "getHeader",
adam@1555 176 "setHeader",
adam@1555 177 "spawn",
adam@1555 178 "onClick",
adam@1555 179 "onDblclick",
ziv@2130 180 "onContextmenu",
adam@1555 181 "onKeydown",
adam@1555 182 "onKeypress",
adam@1555 183 "onKeyup",
adam@1555 184 "onMousedown",
ziv@2130 185 "onMouseenter",
ziv@2130 186 "onMouseleave",
adam@1791 187 "onMousemove",
adam@1791 188 "onMouseout",
adam@1791 189 "onMouseover",
adam@1556 190 "onMouseup",
adam@1559 191 "preventDefault",
adam@1559 192 "stopPropagation",
adam@1785 193 "fresh",
adam@1952 194 "giveFocus",
adam@1952 195 "currentUrlHasPost",
adam@1952 196 "currentUrlHasQueryString",
adam@1952 197 "currentUrl"]
adamc@1171 198
adamc@1171 199 val benign = ref benignBase
adamc@1171 200 fun setBenignEffectful ls = benign := S.addList (benignBase, ls)
adam@2010 201 fun addBenignEffectful x = benign := S.add (!benign, x)
adamc@1171 202 fun isBenignEffectful x = S.member (!benign, x)
adamc@1171 203
adam@1595 204 val clientBase = basis ["get_client_source",
adamc@841 205 "current",
adamc@765 206 "alert",
adam@1290 207 "confirm",
adamc@765 208 "recv",
adamc@765 209 "sleep",
adamc@765 210 "spawn",
adamc@765 211 "onError",
adamc@765 212 "onFail",
adamc@765 213 "onConnectFail",
adamc@765 214 "onDisconnect",
adamc@895 215 "onServerError",
adam@1783 216 "mouseEvent",
adam@1783 217 "keyEvent",
adam@1555 218 "onClick",
ziv@2130 219 "onContextmenu",
adam@1555 220 "onDblclick",
adam@1555 221 "onKeydown",
adam@1555 222 "onKeypress",
adam@1555 223 "onKeyup",
adam@1555 224 "onMousedown",
ziv@2130 225 "onMouseenter",
ziv@2130 226 "onMouseleave",
adam@1791 227 "onMousemove",
adam@1791 228 "onMouseout",
adam@1791 229 "onMouseover",
adam@1559 230 "onMouseup",
adam@1559 231 "preventDefault",
adam@1785 232 "stopPropagation",
adam@1785 233 "giveFocus"]
adamc@765 234 val client = ref clientBase
adamc@765 235 fun setClientOnly ls = client := S.addList (clientBase, ls)
adam@2010 236 fun addClientOnly x = client := S.add (!client, x)
adamc@765 237 fun isClientOnly x = S.member (!client, x)
adamc@765 238
adamc@765 239 val serverBase = basis ["requestHeader",
adamc@765 240 "query",
adamc@765 241 "dml",
adamc@765 242 "nextval",
adamc@1073 243 "setval",
adamc@765 244 "channel",
adam@1787 245 "send",
adam@1787 246 "fieldName",
adam@1787 247 "fieldValue",
adam@1787 248 "remainingFields",
adam@1787 249 "firstFormField"]
adamc@765 250 val server = ref serverBase
adamc@765 251 fun setServerOnly ls = server := S.addList (serverBase, ls)
adam@2010 252 fun addServerOnly x = server := S.add (!server, x)
adamc@765 253 fun isServerOnly x = S.member (!server, x)
adamc@765 254
adamc@765 255 val basisM = foldl (fn ((k, v : string), m) => M.insert (m, ("Basis", k), v)) M.empty
adamc@765 256
adamc@765 257 val jsFuncsBase = basisM [("alert", "alert"),
adam@1599 258 ("stringToTime", "stringToTime"),
adam@1599 259 ("stringToTime_error", "stringToTime_error"),
adam@1609 260 ("timef", "strftime"),
adam@1290 261 ("confirm", "confrm"),
adamc@765 262 ("get_client_source", "sg"),
adamc@841 263 ("current", "scur"),
adamc@765 264 ("htmlifyBool", "bs"),
adamc@765 265 ("htmlifyFloat", "ts"),
adamc@765 266 ("htmlifyInt", "ts"),
adamc@765 267 ("htmlifyString", "eh"),
adamc@765 268 ("new_client_source", "sc"),
adamc@765 269 ("set_client_source", "sv"),
adamc@838 270 ("stringToFloat", "pflo"),
adamc@838 271 ("stringToInt", "pio"),
adamc@765 272 ("stringToFloat_error", "pfl"),
adamc@765 273 ("stringToInt_error", "pi"),
adamc@765 274 ("urlifyInt", "ts"),
adamc@765 275 ("urlifyFloat", "ts"),
adam@1360 276 ("urlifyTime", "ts"),
adamc@765 277 ("urlifyString", "uf"),
adamc@912 278 ("urlifyBool", "ub"),
adamc@765 279 ("recv", "rv"),
adamc@765 280 ("strcat", "cat"),
adamc@765 281 ("intToString", "ts"),
adamc@765 282 ("floatToString", "ts"),
adamc@821 283 ("charToString", "ts"),
adamc@765 284 ("onError", "onError"),
adamc@765 285 ("onFail", "onFail"),
adamc@765 286 ("onConnectFail", "onConnectFail"),
adamc@765 287 ("onDisconnect", "onDisconnect"),
adamc@798 288 ("onServerError", "onServerError"),
adamc@1108 289 ("attrifyString", "atr"),
adamc@798 290 ("attrifyInt", "ts"),
adamc@798 291 ("attrifyFloat", "ts"),
adamc@820 292 ("attrifyBool", "bs"),
adamc@821 293 ("boolToString", "ts"),
adamc@1057 294 ("str1", "id"),
adamc@821 295 ("strsub", "sub"),
adamc@828 296 ("strsuffix", "suf"),
adamc@829 297 ("strlen", "slen"),
adamc@829 298 ("strindex", "sidx"),
dukhovni@2045 299 ("strsindex", "ssidx"),
adamc@829 300 ("strchr", "schr"),
adamc@831 301 ("substring", "ssub"),
adamc@895 302 ("strcspn", "sspn"),
adam@1624 303 ("strlenGe", "strlenGe"),
adam@1783 304 ("mouseEvent", "uw_mouseEvent"),
adam@1783 305 ("keyEvent", "uw_keyEvent"),
adam@1404 306 ("minTime", "0"),
adam@2097 307 ("stringToBool_error", "s2be"),
adam@2097 308 ("stringToBool", "s2b"),
adamc@1061 309
adamc@1061 310 ("islower", "isLower"),
adamc@1061 311 ("isupper", "isUpper"),
adamc@1061 312 ("isalpha", "isAlpha"),
adamc@1061 313 ("isdigit", "isDigit"),
adamc@1061 314 ("isalnum", "isAlnum"),
adamc@1061 315 ("isblank", "isBlank"),
adamc@1061 316 ("isspace", "isSpace"),
adamc@1061 317 ("isxdigit", "isXdigit"),
adam@1927 318 ("isprint", "isPrint"),
adamc@1061 319 ("tolower", "toLower"),
adamc@1323 320 ("toupper", "toUpper"),
adam@1927 321 ("ord", "ord"),
adamc@1323 322
adamc@1323 323 ("checkUrl", "checkUrl"),
adam@1366 324 ("bless", "bless"),
adam@2008 325 ("blessData", "blessData"),
adam@1366 326
adam@1366 327 ("eq_time", "eq"),
adam@1366 328 ("lt_time", "lt"),
adam@1430 329 ("le_time", "le"),
adam@1430 330
adam@1625 331 ("debug", "uw_debug"),
adam@1625 332 ("naughtyDebug", "uw_debug"),
adam@1487 333
adam@1571 334 ("floatFromInt", "float"),
adam@1571 335 ("ceil", "ceil"),
adam@1571 336 ("trunc", "trunc"),
adam@1571 337 ("round", "round"),
adam@1571 338
adam@1487 339 ("now", "now"),
adam@1487 340 ("timeToString", "showTime"),
adam@1629 341 ("htmlifyTime", "showTimeHtml"),
adam@1514 342 ("toSeconds", "toSeconds"),
adam@1518 343 ("addSeconds", "addSeconds"),
adam@1555 344 ("diffInSeconds", "diffInSeconds"),
adam@1685 345 ("toMilliseconds", "toMilliseconds"),
adam@2187 346 ("fromMilliseconds", "fromMilliseconds"),
adam@1685 347 ("diffInMilliseconds", "diffInMilliseconds"),
adam@1555 348
phurst@1986 349 ("fromDatetime", "fromDatetime"),
phurst@1986 350 ("datetimeYear", "datetimeYear"),
phurst@1986 351 ("datetimeMonth", "datetimeMonth"),
phurst@1986 352 ("datetimeDay", "datetimeDay"),
phurst@1986 353 ("datetimeHour", "datetimeHour"),
phurst@1986 354 ("datetimeMinute", "datetimeMinute"),
phurst@1986 355 ("datetimeSecond", "datetimeSecond"),
phurst@1986 356 ("datetimeDayOfWeek", "datetimeDayOfWeek"),
phurst@1986 357
phurst@1986 358
adam@1555 359 ("onClick", "uw_onClick"),
ziv@2130 360 ("onContextmenu", "uw_onContextmenu"),
adam@1555 361 ("onDblclick", "uw_onDblclick"),
adam@1555 362 ("onKeydown", "uw_onKeydown"),
adam@1555 363 ("onKeypress", "uw_onKeypress"),
adam@1555 364 ("onKeyup", "uw_onKeyup"),
adam@1555 365 ("onMousedown", "uw_onMousedown"),
ziv@2130 366 ("onMouseenter", "uw_onMouseenter"),
ziv@2130 367 ("onMouseleave", "uw_onMouseleave"),
adam@1791 368 ("onMousemove", "uw_onMousemove"),
adam@1791 369 ("onMouseout", "uw_onMouseout"),
adam@1791 370 ("onMouseover", "uw_onMouseover"),
adam@1556 371 ("onMouseup", "uw_onMouseup"),
adam@1559 372 ("preventDefault", "uw_preventDefault"),
adam@1559 373 ("stopPropagation", "uw_stopPropagation"),
adam@1556 374
adam@1755 375 ("fresh", "fresh"),
adam@1755 376
adam@1755 377 ("atom", "atom"),
adam@1755 378 ("css_url", "css_url"),
adam@1785 379 ("property", "property"),
adam@2189 380 ("giveFocus", "giveFocus"),
adam@2189 381
adam@2189 382 ("htmlifySpecialChar", "htmlifySpecialChar"),
adam@2189 383 ("chr", "chr")]
adamc@765 384 val jsFuncs = ref jsFuncsBase
adamc@765 385 fun setJsFuncs ls = jsFuncs := foldl (fn ((k, v), m) => M.insert (m, k, v)) jsFuncsBase ls
adamc@765 386 fun jsFunc x = M.find (!jsFuncs, x)
adam@2010 387 fun addJsFunc (k, v) = jsFuncs := M.insert (!jsFuncs, k, v)
adam@1433 388 fun allJsFuncs () = M.listItemsi (!jsFuncs)
adamc@765 389
adamc@768 390 datatype pattern_kind = Exact | Prefix
adamc@768 391 datatype action = Allow | Deny
adamc@768 392 type rule = { action : action, kind : pattern_kind, pattern : string }
adamc@768 393
adamc@768 394 datatype path_kind = Any | Url | Table | Sequence | View | Relation | Cookie | Style
adam@1752 395 type rewrite = { pkind : path_kind, kind : pattern_kind, from : string, to : string, hyphenate : bool }
adamc@768 396
adam@2096 397 fun pak2s pak =
adam@2096 398 case pak of
adam@2096 399 Exact => "Exact"
adam@2096 400 | Prefix => "Prefix"
adam@2096 401 fun pk2s pk =
adam@2096 402 case pk of
adam@2096 403 Any => "Any"
adam@2096 404 | Url => "Url"
adam@2096 405 | Table => "Table"
adam@2096 406 | Sequence => "Sequence"
adam@2096 407 | View => "View"
adam@2096 408 | Relation => "Relation"
adam@2096 409 | Cookie => "Cookie"
adam@2096 410 | Style => "Style"
adam@2096 411 fun r2s (r : rewrite) = pak2s (#kind r) ^ " " ^ pk2s (#pkind r) ^ ", from<" ^ #from r ^ ">, to<" ^ #to r ^ ">"
adam@2096 412
adamc@768 413 val rewrites = ref ([] : rewrite list)
adamc@768 414
adamc@768 415 fun subsume (pk1, pk2) =
adamc@768 416 pk1 = pk2
adamc@768 417 orelse pk2 = Any
adamc@768 418 orelse pk2 = Relation andalso (pk1 = Table orelse pk1 = Sequence orelse pk1 = View)
adamc@768 419
adamc@768 420 fun setRewriteRules ls = rewrites := ls
adamc@768 421 fun rewrite pk s =
adamc@768 422 let
adamc@768 423 fun rew (ls : rewrite list) =
adamc@768 424 case ls of
adamc@768 425 [] => s
adamc@768 426 | rewr :: ls =>
adamc@768 427 let
adamc@768 428 fun match () =
adamc@768 429 case #kind rewr of
adamc@768 430 Exact => if #from rewr = s then
adamc@768 431 SOME (size s)
adamc@768 432 else
adamc@768 433 NONE
adamc@768 434 | Prefix => if String.isPrefix (#from rewr) s then
adamc@768 435 SOME (size (#from rewr))
adamc@768 436 else
adamc@768 437 NONE
adamc@768 438 in
adamc@768 439 if subsume (pk, #pkind rewr) then
adamc@768 440 case match () of
adamc@768 441 NONE => rew ls
adam@1752 442 | SOME suffixStart =>
adam@1752 443 let
adam@1752 444 val s = #to rewr ^ String.extract (s, suffixStart, NONE)
adam@1752 445 in
adam@1752 446 if #hyphenate rewr then
adam@1752 447 String.translate (fn #"_" => "-" | ch => str ch) s
adam@1752 448 else
adam@1752 449 s
adam@1752 450 end
adamc@768 451 else
adamc@768 452 rew ls
adamc@768 453 end
adamc@768 454 in
adamc@768 455 rew (!rewrites)
adamc@768 456 end
adamc@768 457
adamc@769 458 val url = ref ([] : rule list)
adamc@769 459 val mime = ref ([] : rule list)
adam@1465 460 val request = ref ([] : rule list)
adam@1465 461 val response = ref ([] : rule list)
adam@1799 462 val env = ref ([] : rule list)
adamc@769 463
adamc@769 464 fun setUrlRules ls = url := ls
adamc@769 465 fun setMimeRules ls = mime := ls
adam@1465 466 fun setRequestHeaderRules ls = request := ls
adam@1465 467 fun setResponseHeaderRules ls = response := ls
adam@1799 468 fun setEnvVarRules ls = env := ls
adamc@769 469
adamc@770 470 fun getUrlRules () = !url
adamc@770 471 fun getMimeRules () = !mime
adam@1465 472 fun getRequestHeaderRules () = !request
adam@1465 473 fun getResponseHeaderRules () = !response
adam@1799 474 fun getEnvVarRules () = !env
adamc@770 475
adamc@769 476 fun check f rules s =
adamc@769 477 let
adamc@769 478 fun chk (ls : rule list) =
adamc@769 479 case ls of
adamc@769 480 [] => false
adamc@769 481 | rule :: ls =>
adamc@769 482 let
adamc@769 483 val matches =
adamc@769 484 case #kind rule of
adamc@769 485 Exact => #pattern rule = s
adamc@769 486 | Prefix => String.isPrefix (#pattern rule) s
adamc@769 487 in
adamc@769 488 if matches then
adamc@769 489 case #action rule of
adamc@769 490 Allow => true
adamc@769 491 | Deny => false
adamc@769 492 else
adamc@769 493 chk ls
adamc@769 494 end
adamc@769 495 in
adamc@769 496 f s andalso chk (!rules)
adamc@769 497 end
adamc@769 498
adamc@769 499 val checkUrl = check (fn _ => true) url
adam@1465 500
grrwlf@2024 501 val validMime = CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"/" orelse ch = #"-" orelse ch = #"." orelse ch = #"+")
adam@1799 502 val validEnv = CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"_" orelse ch = #".")
adam@1465 503
adam@1465 504 val checkMime = check validMime mime
adam@1465 505 val checkRequestHeader = check validMime request
adam@1465 506 val checkResponseHeader = check validMime response
adam@1799 507 val checkEnvVar = check validEnv env
adamc@769 508
adamc@855 509
adamc@855 510 type protocol = {
adamc@855 511 name : string,
adamc@1096 512 compile : string,
adamc@1095 513 linkStatic : string,
adamc@1095 514 linkDynamic : string,
adamc@1164 515 persistent : bool,
adamc@1164 516 code : unit -> Print.PD.pp_desc
adamc@855 517 }
adamc@855 518 val protocols = ref ([] : protocol list)
adamc@855 519 fun addProtocol p = protocols := p :: !protocols
adamc@855 520 fun getProtocol s = List.find (fn p => #name p = s) (!protocols)
adamc@855 521
ezyang@1739 522 fun clibFile s = OS.Path.joinDirFile {dir = libC (),
adamc@855 523 file = s}
adamc@855 524
adamc@865 525 val curProto = ref {name = "",
adamc@1096 526 compile = "",
adamc@1095 527 linkStatic = "",
adamc@1095 528 linkDynamic = "",
adamc@1164 529 persistent = false,
adamc@1164 530 code = fn () => Print.box []}
adamc@856 531 fun setProtocol name =
adamc@856 532 case getProtocol name of
adamc@856 533 NONE => raise Fail ("Unknown protocol " ^ name)
adamc@856 534 | SOME p => curProto := p
adamc@855 535 fun currentProtocol () = !curProto
adamc@855 536
adamc@857 537 val debug = ref false
adamc@857 538 fun setDebug b = debug := b
adamc@857 539 fun getDebug () = !debug
adamc@857 540
adamc@867 541 datatype sql_type =
adamc@867 542 Int
adamc@867 543 | Float
adamc@867 544 | String
adamc@1011 545 | Char
adamc@867 546 | Bool
adamc@867 547 | Time
adamc@867 548 | Blob
adamc@867 549 | Channel
adamc@867 550 | Client
adamc@867 551 | Nullable of sql_type
adamc@867 552
adamc@873 553 fun p_sql_ctype t =
adamc@867 554 let
adamc@867 555 open Print.PD
adamc@867 556 open Print
adamc@867 557 in
adamc@867 558 case t of
adamc@870 559 Int => "uw_Basis_int"
adamc@870 560 | Float => "uw_Basis_float"
adamc@870 561 | String => "uw_Basis_string"
adamc@1011 562 | Char => "uw_Basis_char"
adamc@870 563 | Bool => "uw_Basis_bool"
adamc@870 564 | Time => "uw_Basis_time"
adamc@870 565 | Blob => "uw_Basis_blob"
adamc@870 566 | Channel => "uw_Basis_channel"
adamc@870 567 | Client => "uw_Basis_client"
adamc@870 568 | Nullable String => "uw_Basis_string"
adamc@873 569 | Nullable t => p_sql_ctype t ^ "*"
adamc@867 570 end
adamc@867 571
adamc@867 572 fun isBlob Blob = true
adamc@867 573 | isBlob (Nullable t) = isBlob t
adamc@867 574 | isBlob _ = false
adamc@867 575
adamc@870 576 fun isNotNull (Nullable _) = false
adamc@870 577 | isNotNull _ = true
adamc@870 578
adam@1293 579 datatype failure_mode = Error | None
adam@1293 580
adamc@866 581 type dbms = {
adamc@866 582 name : string,
adam@1682 583 randomFunction : string,
adamc@866 584 header : string,
adamc@866 585 link : string,
adamc@873 586 p_sql_type : sql_type -> string,
adamc@870 587 init : {dbstring : string,
adamc@870 588 prepared : (string * int) list,
adamc@870 589 tables : (string * (string * sql_type) list) list,
adamc@872 590 views : (string * (string * sql_type) list) list,
adamc@870 591 sequences : string list} -> Print.PD.pp_desc,
adamc@873 592 query : {loc : ErrorMsg.span, cols : sql_type list,
adamc@880 593 doCols : ({loc : ErrorMsg.span, wontLeakStrings : bool, col : int, typ : sql_type} -> Print.PD.pp_desc)
adamc@867 594 -> Print.PD.pp_desc}
adamc@867 595 -> Print.PD.pp_desc,
adamc@867 596 queryPrepared : {loc : ErrorMsg.span, id : int, query : string,
adamc@873 597 inputs : sql_type list, cols : sql_type list,
adamc@880 598 doCols : ({loc : ErrorMsg.span, wontLeakStrings : bool, col : int,
adamc@880 599 typ : sql_type} -> Print.PD.pp_desc)
adamc@879 600 -> Print.PD.pp_desc,
adamc@879 601 nested : bool}
adamc@868 602 -> Print.PD.pp_desc,
adam@1293 603 dml : ErrorMsg.span * failure_mode -> Print.PD.pp_desc,
adamc@868 604 dmlPrepared : {loc : ErrorMsg.span, id : int, dml : string,
adam@1293 605 inputs : sql_type list, mode : failure_mode} -> Print.PD.pp_desc,
adamc@878 606 nextval : {loc : ErrorMsg.span, seqName : string option, seqE : Print.PD.pp_desc} -> Print.PD.pp_desc,
adamc@874 607 nextvalPrepared : {loc : ErrorMsg.span, id : int, query : string} -> Print.PD.pp_desc,
adamc@1073 608 setval : {loc : ErrorMsg.span, seqE : Print.PD.pp_desc, count : Print.PD.pp_desc} -> Print.PD.pp_desc,
adamc@874 609 sqlifyString : string -> string,
adamc@874 610 p_cast : string * sql_type -> string,
adamc@874 611 p_blank : int * sql_type -> string,
adamc@877 612 supportsDeleteAs : bool,
adamc@886 613 supportsUpdateAs : bool,
adamc@877 614 createSequence : string -> string,
adamc@878 615 textKeysNeedLengths : bool,
adamc@879 616 supportsNextval : bool,
adamc@882 617 supportsNestedPrepared : bool,
adamc@890 618 sqlPrefix : string,
adamc@1014 619 supportsOctetLength : bool,
adamc@1014 620 trueString : string,
adamc@1196 621 falseString : string,
adamc@1196 622 onlyUnion : bool,
adam@1777 623 nestedRelops : bool,
adam@1778 624 windowFunctions: bool
adamc@866 625 }
adamc@866 626
adamc@866 627 val dbmses = ref ([] : dbms list)
adamc@866 628 val curDb = ref ({name = "",
adam@1682 629 randomFunction = "",
adamc@866 630 header = "",
adamc@866 631 link = "",
adamc@873 632 p_sql_type = fn _ => "",
adamc@867 633 init = fn _ => Print.box [],
adamc@867 634 query = fn _ => Print.box [],
adamc@868 635 queryPrepared = fn _ => Print.box [],
adamc@868 636 dml = fn _ => Print.box [],
adamc@869 637 dmlPrepared = fn _ => Print.box [],
adamc@869 638 nextval = fn _ => Print.box [],
adamc@874 639 nextvalPrepared = fn _ => Print.box [],
adamc@1073 640 setval = fn _ => Print.box [],
adamc@874 641 sqlifyString = fn s => s,
adamc@874 642 p_cast = fn _ => "",
adamc@874 643 p_blank = fn _ => "",
adamc@877 644 supportsDeleteAs = false,
adamc@886 645 supportsUpdateAs = false,
adamc@877 646 createSequence = fn _ => "",
adamc@878 647 textKeysNeedLengths = false,
adamc@879 648 supportsNextval = false,
adamc@882 649 supportsNestedPrepared = false,
adamc@890 650 sqlPrefix = "",
adamc@1014 651 supportsOctetLength = false,
adamc@1014 652 trueString = "",
adamc@1196 653 falseString = "",
adamc@1196 654 onlyUnion = false,
adam@1777 655 nestedRelops = false,
adam@1777 656 windowFunctions = false} : dbms)
adamc@866 657
adamc@866 658 fun addDbms v = dbmses := v :: !dbmses
adamc@866 659 fun setDbms s =
adamc@866 660 case List.find (fn db => #name db = s) (!dbmses) of
adamc@866 661 NONE => raise Fail ("Unknown DBMS " ^ s)
adamc@866 662 | SOME db => curDb := db
adamc@866 663 fun currentDbms () = !curDb
adamc@866 664
adamc@891 665 val dbstring = ref (NONE : string option)
adamc@891 666 fun setDbstring so = dbstring := so
adamc@891 667 fun getDbstring () = !dbstring
adamc@891 668
adamc@891 669 val exe = ref (NONE : string option)
adamc@891 670 fun setExe so = exe := so
adamc@891 671 fun getExe () = !exe
adamc@891 672
adamc@891 673 val sql = ref (NONE : string option)
adamc@891 674 fun setSql so = sql := so
adamc@891 675 fun getSql () = !sql
adamc@891 676
adam@1820 677 val coreInline = ref 5
adamc@1016 678 fun setCoreInline n = coreInline := n
adamc@1016 679 fun getCoreInline () = !coreInline
adamc@1016 680
adam@1820 681 val monoInline = ref 5
adamc@1016 682 fun setMonoInline n = monoInline := n
adamc@1016 683 fun getMonoInline () = !monoInline
adamc@1016 684
adamc@1095 685 val staticLinking = ref false
adamc@1095 686 fun setStaticLinking b = staticLinking := b
adamc@1095 687 fun getStaticLinking () = !staticLinking
adamc@1095 688
adamc@1114 689 val deadlines = ref false
adamc@1114 690 fun setDeadlines b = deadlines := b
adamc@1114 691 fun getDeadlines () = !deadlines
adamc@1114 692
adamc@1164 693 val sigFile = ref (NONE : string option)
adamc@1164 694 fun setSigFile v = sigFile := v
adamc@1164 695 fun getSigFile () = !sigFile
adamc@1164 696
adamc@1183 697 structure SS = BinarySetFn(struct
adamc@1183 698 type ord_key = string
adamc@1183 699 val compare = String.compare
adamc@1183 700 end)
adamc@1183 701
adamc@1183 702 val safeGet = ref SS.empty
adamc@1183 703 fun setSafeGets ls = safeGet := SS.addList (SS.empty, ls)
adamc@1183 704 fun isSafeGet x = SS.member (!safeGet, x)
adamc@1183 705
adam@1294 706 val onError = ref (NONE : (string * string list * string) option)
adam@1294 707 fun setOnError x = onError := x
adam@1294 708 fun getOnError () = !onError
adam@1294 709
adam@1307 710 val limits = ["messages", "clients", "headers", "page", "heap", "script",
adam@1307 711 "inputs", "subinputs", "cleanup", "deltas", "transactionals",
adam@1308 712 "globals", "database", "time"]
adam@1307 713
adam@1307 714 val limitsList = ref ([] : (string * int) list)
adam@1307 715 fun addLimit (v as (name, _)) =
adam@1307 716 if List.exists (fn name' => name' = name) limits then
adam@1308 717 (limitsList := v :: !limitsList;
adam@1308 718 if name = "time" then
adam@1308 719 setDeadlines true
adam@1308 720 else
adam@1308 721 ())
adam@1307 722 else
adam@1307 723 raise Fail ("Unknown limit category '" ^ name ^ "'")
adam@1307 724 fun limits () = !limitsList
adam@1307 725
adam@1332 726 val minHeap = ref 0
adam@1332 727 fun setMinHeap n = if n >= 0 then minHeap := n else raise Fail "Trying to set negative minHeap"
adam@1332 728 fun getMinHeap () = !minHeap
adam@1332 729
adam@1393 730 val alwaysInline = ref SS.empty
adam@1393 731 fun addAlwaysInline s = alwaysInline := SS.add (!alwaysInline, s)
adam@1393 732 fun checkAlwaysInline s = SS.member (!alwaysInline, s)
adam@1393 733
adam@1966 734 val neverInline = ref SS.empty
adam@1966 735 fun addNeverInline s = neverInline := SS.add (!neverInline, s)
adam@1966 736 fun checkNeverInline s = SS.member (!neverInline, s)
adam@1966 737
adam@1478 738 val noXsrfProtection = ref SS.empty
adam@1478 739 fun addNoXsrfProtection s = noXsrfProtection := SS.add (!noXsrfProtection, s)
adam@1478 740 fun checkNoXsrfProtection s = SS.member (!noXsrfProtection, s)
adam@1478 741
adam@1629 742 val timeFormat = ref "%c"
adam@1629 743 fun setTimeFormat v = timeFormat := v
adam@1629 744 fun getTimeFormat () = !timeFormat
adam@1629 745
adam@1953 746 fun lowercase s =
adam@1953 747 case s of
adam@1953 748 "" => ""
adam@1953 749 | _ => str (Char.toLower (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adam@1953 750
adam@1953 751 fun capitalize s =
adam@1953 752 case s of
adam@1953 753 "" => ""
adam@1953 754 | _ => str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adam@1953 755
adam@2108 756 val allLower = CharVector.map Char.toLower
adam@2108 757
adam@1953 758 val mangle = ref true
adam@1953 759 fun setMangleSql x = mangle := x
adam@2108 760
adam@2108 761 fun mangleSqlTable s =
adam@2108 762 if #name (currentDbms ()) = "mysql" then
adam@2108 763 if !mangle then
adam@2108 764 "uw_" ^ allLower s
adam@2108 765 else
adam@2108 766 allLower s
adam@2108 767 else
adam@2108 768 if !mangle then
adam@2108 769 "uw_" ^ capitalize s
adam@2108 770 else
adam@2108 771 lowercase s
adam@2108 772
adam@2108 773 fun mangleSql s =
adam@2108 774 if #name (currentDbms ()) = "mysql" then
adam@2108 775 if !mangle then
ziv@2130 776 "uw_" ^ allLower s
adam@2108 777 else
adam@2108 778 allLower s
adam@2108 779 else
adam@2108 780 if !mangle then
adam@2108 781 "uw_" ^ s
adam@2108 782 else
adam@2108 783 lowercase s
adam@2108 784
adam@2108 785 fun mangleSqlCatalog s =
adam@2108 786 if #name (currentDbms ()) = "mysql" then
adam@2108 787 if !mangle then
adam@2108 788 "uw_" ^ allLower s
adam@2108 789 else
adam@2108 790 allLower s
adam@2108 791 else
adam@2108 792 if !mangle then
adam@2108 793 "uw_" ^ s
adam@2108 794 else
adam@2108 795 lowercase s
adam@1953 796
adam@1956 797 val html5 = ref false
adam@1956 798 fun setIsHtml5 b = html5 := b
adam@1956 799 fun getIsHtml5 () = !html5
adam@1956 800
adam@2010 801 val less = ref false
adam@2010 802 fun setLessSafeFfi b = less := b
adam@2010 803 fun getLessSafeFfi () = !less
adam@2010 804
ziv@2213 805 val sqlcache = ref false
ziv@2213 806 fun setSqlcache b = sqlcache := b
ziv@2213 807 fun getSqlcache () = !sqlcache
ziv@2213 808
adam@2046 809 structure SM = BinaryMapFn(struct
adam@2046 810 type ord_key = string
adam@2046 811 val compare = String.compare
adam@2046 812 end)
adam@2046 813
adam@2046 814 val noMimeFile = ref false
adam@2046 815
adam@2046 816 fun noMime () =
adam@2046 817 (TextIO.output (TextIO.stdErr, "WARNING: Error opening /etc/mime.types. Static files will be served with no suggested MIME types.\n");
adam@2046 818 noMimeFile := true;
adam@2046 819 SM.empty)
adam@2046 820
adam@2046 821 fun readMimeTypes () =
adam@2046 822 let
adam@2046 823 val inf = TextIO.openIn "/etc/mime.types"
adam@2046 824
adam@2046 825 fun loop m =
adam@2046 826 case TextIO.inputLine inf of
adam@2046 827 NONE => m
adam@2046 828 | SOME line =>
adam@2046 829 if size line > 0 andalso String.sub (line, 0) = #"#" then
adam@2046 830 loop m
adam@2046 831 else
adam@2046 832 case String.tokens Char.isSpace line of
adam@2046 833 typ :: exts =>
adam@2046 834 loop (foldl (fn (ext, m) => SM.insert (m, ext, typ)) m exts)
adam@2046 835 | _ => loop m
adam@2046 836 in
adam@2046 837 loop SM.empty
adam@2046 838 before TextIO.closeIn inf
adam@2046 839 end handle IO.Io _ => noMime ()
adam@2046 840 | OS.SysErr _ => noMime ()
adam@2046 841
adam@2046 842 val mimeTypes = ref (NONE : string SM.map option)
adam@2046 843
adam@2046 844 fun getMimeTypes () =
adam@2046 845 case !mimeTypes of
adam@2046 846 SOME m => m
adam@2046 847 | NONE =>
adam@2046 848 let
adam@2046 849 val m = readMimeTypes ()
adam@2046 850 in
adam@2046 851 mimeTypes := SOME m;
adam@2046 852 m
adam@2046 853 end
adam@2046 854
adam@2046 855 fun mimeTypeOf filename =
adam@2046 856 case OS.Path.ext filename of
adam@2046 857 NONE => (if !noMimeFile then
adam@2046 858 ()
adam@2046 859 else
adam@2046 860 TextIO.output (TextIO.stdErr, "WARNING: No extension found in filename '" ^ filename ^ "'. Header 'Content-Type' will be omitted in HTTP responses.\n");
adam@2046 861 NONE)
adam@2046 862 | SOME ext =>
adam@2046 863 let
adam@2046 864 val to = SM.find (getMimeTypes (), ext)
adam@2046 865 in
adam@2046 866 case to of
adam@2046 867 NONE => if !noMimeFile then
adam@2046 868 ()
adam@2046 869 else
adam@2046 870 TextIO.output (TextIO.stdErr, "WARNING: No MIME type known for extension '" ^ ext ^ "'. Header 'Content-Type' will be omitted in HTTP responses.\n")
adam@2046 871 | _ => ();
adam@2046 872 to
adam@2046 873 end
adam@2046 874
adam@2046 875 val files = ref (SM.empty : (string * {Uri : string, ContentType : string option, LastModified : Time.time, Bytes : Word8Vector.vector}) SM.map)
adam@2046 876
adam@2046 877 val filePath = ref "."
adam@2046 878
adam@2046 879 fun setFilePath path = filePath := path
adam@2046 880
adam@2046 881 fun addFile {Uri, LoadFromFilename} =
adam@2046 882 let
julian@2135 883 val path = OS.Path.concat (!filePath, LoadFromFilename)
adam@2046 884 in
adam@2046 885 case SM.find (!files, Uri) of
adam@2046 886 SOME (path', _) =>
adam@2183 887 if OS.Path.mkCanonical path' = OS.Path.mkCanonical path then
adam@2046 888 ()
adam@2046 889 else
adam@2172 890 ErrorMsg.error ("Two different files requested for URI " ^ Uri ^ " ( " ^ path' ^ " vs. " ^ path ^ ")")
adam@2046 891 | NONE =>
adam@2046 892 let
adam@2046 893 val inf = BinIO.openIn path
adam@2046 894 in
adam@2046 895 files := SM.insert (!files,
adam@2046 896 Uri,
adam@2046 897 (path,
adam@2046 898 {Uri = Uri,
adam@2046 899 ContentType = mimeTypeOf path,
adam@2046 900 LastModified = OS.FileSys.modTime path,
adam@2046 901 Bytes = BinIO.inputAll inf}));
adam@2046 902 BinIO.closeIn inf
adam@2046 903 end
adam@2046 904 end handle IO.Io _ =>
adam@2046 905 ErrorMsg.error ("Error loading file " ^ LoadFromFilename)
adam@2046 906 | OS.SysErr (s, _) =>
adam@2046 907 ErrorMsg.error ("Error loading file " ^ LoadFromFilename ^ " (" ^ s ^ ")")
adam@2046 908
adam@2046 909 fun listFiles () = map #2 (SM.listItems (!files))
adam@2046 910
adam@2197 911 val jsFiles = ref (SM.empty : {Filename : string, Content : string} SM.map)
adam@2197 912
adam@2197 913 fun addJsFile LoadFromFilename =
adam@2197 914 let
adam@2197 915 val path = OS.Path.concat (!filePath, LoadFromFilename)
adam@2197 916 val inf = TextIO.openIn path
adam@2197 917 in
adam@2197 918 jsFiles := SM.insert (!jsFiles,
adam@2197 919 path,
adam@2197 920 {Filename = LoadFromFilename,
adam@2197 921 Content = TextIO.inputAll inf});
adam@2197 922 TextIO.closeIn inf
adam@2197 923 end handle IO.Io _ =>
adam@2197 924 ErrorMsg.error ("Error loading file " ^ LoadFromFilename)
adam@2197 925 | OS.SysErr (s, _) =>
adam@2197 926 ErrorMsg.error ("Error loading file " ^ LoadFromFilename ^ " (" ^ s ^ ")")
adam@2197 927
adam@2197 928 fun listJsFiles () = SM.listItems (!jsFiles)
adam@2197 929
adam@2192 930 fun reset () =
adam@2192 931 (urlPrefixFull := "/";
adam@2192 932 urlPrefix := "/";
adam@2192 933 urlPrePrefix := "";
adam@2192 934 timeout := 0;
adam@2192 935 headers := [];
adam@2192 936 scripts := [];
adam@2192 937 clientToServer := clientToServerBase;
adam@2192 938 effectful := effectfulBase;
adam@2192 939 benign := benignBase;
adam@2192 940 client := clientBase;
adam@2192 941 server := serverBase;
adam@2192 942 jsFuncs := jsFuncsBase;
adam@2192 943 rewrites := [];
adam@2192 944 url := [];
adam@2192 945 mime := [];
adam@2192 946 request := [];
adam@2192 947 response := [];
adam@2192 948 env := [];
adam@2192 949 debug := false;
adam@2192 950 dbstring := NONE;
adam@2192 951 exe := NONE;
adam@2192 952 sql := NONE;
adam@2192 953 coreInline := 5;
adam@2192 954 monoInline := 5;
adam@2192 955 staticLinking := false;
adam@2192 956 deadlines := false;
adam@2192 957 sigFile := NONE;
adam@2192 958 safeGet := SS.empty;
adam@2192 959 onError := NONE;
adam@2192 960 limitsList := [];
adam@2192 961 minHeap := 0;
adam@2192 962 alwaysInline := SS.empty;
adam@2192 963 neverInline := SS.empty;
adam@2192 964 noXsrfProtection := SS.empty;
adam@2192 965 timeFormat := "%c";
adam@2192 966 mangle := true;
adam@2192 967 html5 := false;
adam@2192 968 less := false;
adam@2192 969 noMimeFile := false;
adam@2192 970 mimeTypes := NONE;
adam@2192 971 files := SM.empty;
adam@2197 972 jsFiles := SM.empty;
adam@2192 973 filePath := ".")
adam@2192 974
adamc@765 975 end