Overview
Comment: | converted vector to defstruct |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | stml2 |
Files: | files | file ages | folders |
SHA1: |
605397d08c8b5ae424ded79db368798d |
User & Date: | matt on 2018-09-09 01:19:22 |
Other Links: | branch diff | manifest | tags |
Context
2018-09-09
| ||
12:52 | Pulled sugar.scm into stml2.scm check-in: 4856914104 user: matt tags: stml2 | |
01:19 | converted vector to defstruct check-in: 605397d08c user: matt tags: stml2 | |
2018-09-08
| ||
23:04 | Added ability to specify config file check-in: e954e3db42 user: matt tags: stml2 | |
Changes
Modified stml2.scm from [23503f7143] to [91fb608a44].
︙ | ︙ | |||
12 13 14 15 16 17 18 | ;; (declare (unit stml)) (module stml2 * (import chicken scheme data-structures extras srfi-13 ports posix srfi-69 files srfi-1) | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | ;; (declare (unit stml)) (module stml2 * (import chicken scheme data-structures extras srfi-13 ports posix srfi-69 files srfi-1) (use cookie (prefix dbi dbi:) (prefix crypt c:) typed-records) ;; (declare (uses misc-stml)) (use regex) ;; The (usually global) sdat contains everything about the session ;; (defstruct sdat (dbtype 'pg) (dbinit #f) (conn #f) (page "home") (page-type 'html) (toppage "index") (content-type "Content-type: text/html; charset=iso-8859-1\n\n") (formdat #f) (params '()) (path-params '()) (session-key #f) (pagedat '()) (curr-page "home") (alt-page-dat #f) (sroot "./") (session-cookie #f) (curr-err #f) (log-port (current-error-port)) (logfile "/tmp/stml.log") (seen-pages '()) (page-dir-style #t) (debug-mode #f) (session-id #f) (pagevars (make-hash-table)) (pagevars-before (make-hash-table)) (sessionvars (make-hash-table)) (sessionvars-before (make-hash-table)) (globalvars (make-hash-table)) (globalvars-before (make-hash-table)) (request-method #f) (domain "localhost") (twikidir #f) (script #f) (force-ssl #f) (shared-hash (make-hash-table))) (define (apply-config-file session #!optional (configf #f)) (let* ((rawconfigdat (session:read-config session configf)) (configdat (if rawconfigdat (eval rawconfigdat) '())) (sroot (s:find-param 'sroot configdat)) (logfile (s:find-param 'logfile configdat)) (dbtype (s:find-param 'dbtype configdat)) (dbinit (s:find-param 'dbinit configdat)) (domain (s:find-param 'domain configdat)) (twikidir (s:find-param 'twikidir configdat)) (page-dir (s:find-param 'page-dir-style configdat)) (debugmode (s:find-param 'debugmode configdat)) (script (s:find-param 'script configdat)) (force-ssl (s:find-param 'force-ssl configdat))) (if sroot (sdat-sroot-set! session sroot)) (if logfile (sdat-logfile-set! session logfile)) (if dbtype (sdat-dbtype-set! session dbtype)) (if dbinit (sdat-dbinit-set! session dbinit)) (if domain (sdat-domain-set! session domain)) (if twikidir (sdat-twikidir-set! session twikidir)) (if debugmode (sdat-debug-mode-set! session debugmode)) (if script (sdat-script-set! session script)) (if force-ssl (sdat-force-ssl-set! session force-ssl)) (sdat-page-dir-style-set! session page-dir) ;; (print "configdat: ")(pp configdat) (if debugmode (session:log session "sroot: " sroot " logfile: " logfile " dbtype: " dbtype " dbinit: " dbinit " domain: " domain " page-dir-style: " page-dir)) )) ;; extract various tokens from the parameter list ;; 'key val => put in the params list ;; strings => maintain order and add to the datalist <<== IMPORTANT (define (s:extract inlst) (if (null? inlst) inlst (let loop ((data '()) |
︙ | ︙ | |||
143 144 145 146 147 148 149 | (define (s:form . args) ;; create a link for calling back into the current page and calling a specified ;; function (let* ((action (let ((v (s:find-param 'action args))) (if v v "default"))) (id (let ((i (s:find-param 'id args))) (if i i #f))) | | | 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | (define (s:form . args) ;; create a link for calling back into the current page and calling a specified ;; function (let* ((action (let ((v (s:find-param 'action args))) (if v v "default"))) (id (let ((i (s:find-param 'id args))) (if i i #f))) (page (let ((p (sdat-page s:session))) (if p p "home"))) ;; (link (session:link-to s:session page (if id ;; (list 'action action 'id id) ;; (list 'action action))))) (link (if (string=? (substring action 0 5) "http:") ;; if first part of string is http: action (session:link-to s:session |
︙ | ︙ | |||
401 402 403 404 405 406 407 | #;(define (s:model-path model) (session:model-path s:session model)) ;; share data between pages calls. NOTE: This is not persistent ;; between cgi calls. Use sessionvars for that. ;; (define (s:shared-hash) | | | | | | | | 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | #;(define (s:model-path model) (session:model-path s:session model)) ;; share data between pages calls. NOTE: This is not persistent ;; between cgi calls. Use sessionvars for that. ;; (define (s:shared-hash) (sdat-shared-hash s:session)) (define (s:shared-set! key val) (hash-table-set! (sdat-shared-hash s:session) key val)) ;; What to return when no value for key? ;; (define (s:shared-get key) (hash-table-ref/default (sdat-shared-hash s:session) key #f)) ;; http://foo.bar.com/pagename/p1/p2 => '("p1" "p2") ;; #### DEPRECATED #### (define (s:get-page-params) (sdat-path-params s:session)) (define (s:get-path-params) (sdat-path-params s:session)) (define (s:db) (sdat-conn s:session)) ;;====================================================================== ;; cgi and session stuff ;;====================================================================== ;;(declare (uses cookie)) ;;(declare (uses html-filter)) |
︙ | ︙ | |||
1436 1437 1438 1439 1440 1441 1442 | ;; TODO ;; Concept of order num incremented with each page access ;; if a branch is taken then a new session would need to be created ;; ;; make-vector-record session session dbtype dbinit conn params path-params session-key session-id domain toppage page curr-page content-type page-type sroot twikidir pagedat alt-page-dat pagevars pagevars-before sessionvars sessionvars-before globalvars globalvars-before logpt formdat request-method session-cookie curr-err log-port logfile seen-pages page-dir-style debugmode | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 | ;; TODO ;; Concept of order num incremented with each page access ;; if a branch is taken then a new session would need to be created ;; ;; make-vector-record session session dbtype dbinit conn params path-params session-key session-id domain toppage page curr-page content-type page-type sroot twikidir pagedat alt-page-dat pagevars pagevars-before sessionvars sessionvars-before globalvars globalvars-before logpt formdat request-method session-cookie curr-err log-port logfile seen-pages page-dir-style debugmode ;; (define (make-sdat)(make-vector 36)) ;; (define (sdat-dbtype vec) (vector-ref vec 0)) ;; (define (sdat-dbinit vec) (vector-ref vec 1)) ;; (define (sdat-conn vec) (vector-ref vec 2)) ;; (define (sdat-pgconn vec) (vector-ref (vector-ref vec 2) 1)) ;; (define (sdat-params vec) (vector-ref vec 3)) ;; (define (sdat-path-params vec) (vector-ref vec 4)) ;; (define (sdat-session-key vec) (vector-ref vec 5)) ;; (define (sdat-session-id vec) (vector-ref vec 6)) ;; (define (sdat-domain vec) (vector-ref vec 7)) ;; (define (sdat-toppage vec) (vector-ref vec 8)) ;; (define (sdat-page vec) (vector-ref vec 9)) ;; (define (sdat-curr-page vec) (vector-ref vec 10)) ;; (define (sdat-content-type vec) (vector-ref vec 11)) ;; (define (sdat-page-type vec) (vector-ref vec 12)) ;; (define (sdat-sroot vec) (vector-ref vec 13)) ;; (define (sdat-twikidir vec) (vector-ref vec 14)) ;; (define (sdat-pagedat vec) (vector-ref vec 15)) ;; (define (sdat-alt-page-dat vec) (vector-ref vec 16)) ;; (define (sdat-pagevars vec) (vector-ref vec 17)) ;; (define (sdat-pagevars-before vec) (vector-ref vec 18)) ;; (define (sdat-sessionvars vec) (vector-ref vec 19)) ;; (define (sdat-sessionvars-before vec) (vector-ref vec 20)) ;; (define (sdat-globalvars vec) (vector-ref vec 21)) ;; (define (sdat-globalvars-before vec) (vector-ref vec 22)) ;; (define (sdat-logpt vec) (vector-ref vec 23)) ;; (define (sdat-formdat vec) (vector-ref vec 24)) ;; (define (sdat-request-method vec) (vector-ref vec 25)) ;; (define (sdat-session-cookie vec) (vector-ref vec 26)) ;; (define (sdat-curr-err vec) (vector-ref vec 27)) ;; (define (sdat-log-port vec) (vector-ref vec 28)) ;; (define (sdat-logfile vec) (vector-ref vec 29)) ;; (define (sdat-seen-pages vec) (vector-ref vec 30)) ;; (define (sdat-page-dir-style vec) (vector-ref vec 31)) ;; (define (sdat-debugmode vec) (vector-ref vec 32)) ;; (define (sdat-shared-hash vec) (vector-ref vec 33)) ;; (define (sdat-script vec) (vector-ref vec 34)) ;; (define (sdat-force-ssl vec) (vector-ref vec 35)) ;; ;; (define (session:get-shared vec varname) ;; (hash-table-ref/default (vector-ref vec 33) varname #f)) ;; ;; (define (sdat-dbtype-set! vec val)(vector-set! vec 0 val)) ;; (define (sdat-dbinit-set! vec val)(vector-set! vec 1 val)) ;; (define (sdat-conn-set! vec val)(vector-set! vec 2 val)) ;; (define (sdat-params-set! vec val)(vector-set! vec 3 val)) ;; (define (sdat-path-set-params! vec val)(vector-set! vec 4 val)) ;; (define (sdat-session-set-key! vec val)(vector-set! vec 5 val)) ;; (define (sdat-session-set-id! vec val)(vector-set! vec 6 val)) ;; (define (sdat-domain-set! vec val)(vector-set! vec 7 val)) ;; (define (sdat-toppage-set! vec val)(vector-set! vec 8 val)) ;; (define (sdat-page-set! vec val)(vector-set! vec 9 val)) ;; (define (sdat-curr-set-page! vec val)(vector-set! vec 10 val)) ;; (define (sdat-content-set-type! vec val)(vector-set! vec 11 val)) ;; (define (sdat-page-set-type! vec val)(vector-set! vec 12 val)) ;; (define (sdat-sroot-set! vec val)(vector-set! vec 13 val)) ;; (define (sdat-twikidir-set! vec val)(vector-set! vec 14 val)) ;; (define (sdat-pagedat-set! vec val)(vector-set! vec 15 val)) ;; (define (sdat-alt-set-page-dat! vec val)(vector-set! vec 16 val)) ;; (define (sdat-pagevars-set! vec val)(vector-set! vec 17 val)) ;; (define (sdat-pagevars-set-before! vec val)(vector-set! vec 18 val)) ;; (define (sdat-sessionvars-set! vec val)(vector-set! vec 19 val)) ;; (define (sdat-sessionvars-set-before! vec val)(vector-set! vec 20 val)) ;; (define (sdat-globalvars-set! vec val)(vector-set! vec 21 val)) ;; (define (sdat-globalvars-set-before! vec val)(vector-set! vec 22 val)) ;; (define (sdat-logpt-set! vec val)(vector-set! vec 23 val)) ;; (define (sdat-formdat-set! vec val)(vector-set! vec 24 val)) ;; (define (sdat-request-set-method! vec val)(vector-set! vec 25 val)) ;; (define (sdat-session-set-cookie! vec val)(vector-set! vec 26 val)) ;; (define (sdat-curr-set-err! vec val)(vector-set! vec 27 val)) ;; (define (sdat-log-set-port! vec val)(vector-set! vec 28 val)) ;; (define (sdat-logfile-set! vec val)(vector-set! vec 29 val)) ;; (define (sdat-seen-set-pages! vec val)(vector-set! vec 30 val)) ;; (define (sdat-page-set-dir-style! vec val)(vector-set! vec 31 val)) ;; (define (sdat-debugmode-set! vec val)(vector-set! vec 32 val)) ;; (define (sdat-shared-set-hash! vec val)(vector-set! vec 33 val)) ;; (define (sdat-script-set! vec val)(vector-set! vec 34 val)) ;; (define (sdat-force-set-ssl! vec val)(vector-set! vec 35 val)) ;; ;; (define (session:set-shared! vec varname val) ;; (hash-table-set! (vector-ref vec 33) varname val)) ;; The global session (define s:session (make-sdat)) ;; SPLIT INTO STRAIGHT FORWARD INIT AND COMPLEX INIT #;(define (session:initialize self #!optional (configf #f)) (sdat-dbtype-set! self 'pg) (sdat-page-set! self "home") ;; these are defaults (sdat-curr-set-page! self "home") (sdat-content-set-type! self "Content-type: text/html; charset=iso-8859-1\n\n") (sdat-page-set-type! self 'html) (sdat-toppage-set! self "index") (sdat-params-set! self '()) ;; (sdat-path-set-params! self '()) (sdat-session-set-key! self #f) (sdat-pagedat-set! self '()) (sdat-alt-set-page-dat! self #f) (sdat-sroot-set! self "./") (sdat-session-set-cookie! self #f) (sdat-curr-set-err! self #f) (sdat-log-set-port! self (current-error-port)) (sdat-seen-set-pages! self '()) (sdat-page-set-dir-style! self #t) ;; #t : pages/<pagename>_(view|cntl).scm ;; #f : pages/<pagename>/(view|control).scm (sdat-debugmode-set! self #f) (sdat-pagevars-set! self (make-hash-table)) (sdat-sessionvars-set! self (make-hash-table)) (sdat-globalvars-set! self (make-hash-table)) (sdat-pagevars-set-before! self (make-hash-table)) (sdat-sessionvars-set-before! self (make-hash-table)) (sdat-globalvars-set-before! self (make-hash-table)) (sdat-domain-set! self "locahost") ;; end of defaults (sdat-script-set! self #f) (sdat-force-set-ssl! self #f) (let* ((rawconfigdat (session:read-config self configf)) (configdat (if rawconfigdat (eval rawconfigdat) '())) (sroot (s:find-param 'sroot configdat)) (logfile (s:find-param 'logfile configdat)) (dbtype (s:find-param 'dbtype configdat)) (dbinit (s:find-param 'dbinit configdat)) (domain (s:find-param 'domain configdat)) (twikidir (s:find-param 'twikidir configdat)) (page-dir (s:find-param 'page-dir-style configdat)) (debugmode (s:find-param 'debugmode configdat)) (script (s:find-param 'script configdat)) (force-ssl (s:find-param 'force-ssl configdat))) (if sroot (sdat-sroot-set! self sroot)) (if logfile (sdat-logfile-set! self logfile)) (if dbtype (sdat-dbtype-set! self dbtype)) (if dbinit (sdat-dbinit-set! self dbinit)) (if domain (sdat-domain-set! self domain)) (if twikidir (sdat-twikidir-set! self twikidir)) (if debugmode (sdat-debugmode-set! self debugmode)) (if script (sdat-script-set! self script)) (if force-ssl (sdat-force-set-ssl! self force-ssl)) (sdat-page-set-dir-style! self page-dir) ;; (print "configdat: ")(pp configdat) (if debugmode (session:log self "sroot: " sroot " logfile: " logfile " dbtype: " dbtype " dbinit: " dbinit " domain: " domain " page-dir-style: " page-dir)) ) (sdat-shared-set-hash! self (make-hash-table)) ) ;; Used for the strangely inconsistent handling of the config file. A better way is needed. ;; ;; (let ((dbtype (sdat-dbtype self))) ;; (print "dbtype: " dbtype) ;; (sdat-dbtype-set! self (eval dbtype)))) (define (session:setup self) (let ((dbtype (sdat-dbtype self)) (debugmode (sdat-debug-mode self)) (dbinit (eval (sdat-dbinit self))) (dbexists #f)) (let ((dbfname (alist-ref 'dbname dbinit))) (if debugmode (session:log self "session:setup dbfname=" dbfname ", dbtype=" dbtype ", dbinit=" dbinit)) (if (eq? dbtype 'sqlite3) ;; The 'auto method will distribute dbs across the disk using hash ;; of user host and user. TODO ;; (if (eq? dbfname 'auto) ;; This is the auto assignment of a db based on hash of IP (let ((dbpath (pathname-directory dbfname))) ;; do a couple sanity checks here to make setting up easier (if debugmode (session:log self "INFO: setting up for sqlite3 db access to " dbfname)) (if (not (file-write-access? dbpath)) (session:log self "WARNING: Cannot write to " dbpath) (if debugmode (session:log self "INFO: " dbpath " is writeable"))) (if (file-exists? dbfname) (begin ;; (session:log self "setting dbexists to #t") (set! dbexists #t)))) (if debugmode (session:log self "INFO: setting up for pg db access to account info " dbinit))) (if debugmode (session:log self "dbtype: " dbtype " dbfname: " dbfname " dbexists: " dbexists))) (sdat-conn-set! self (dbi:open dbtype dbinit)) (set! *db* (sdat-conn self)) (if (and (not dbexists)(eq? dbtype 'sqlite3)) (begin (print "WARNING: Setting up session db with sqlite3") (session:setup-db self))) (session:process-url-path self) (session:setup-session-key self) ;; capture stdin if this is a POST (sdat-request-method-set! self (get-environment-variable "REQUEST_METHOD")) (sdat-formdat-set! self (formdat:load-all)))) ;; setup the db with session tables, works for sqlite only right now (define (session:setup-db self) (let ((conn (sdat-conn self))) (for-each (lambda (stmt) (dbi:exec conn stmt)) (list "CREATE TABLE session_vars (id INTEGER PRIMARY KEY,session_id INTEGER,page TEXT,key TEXT,value TEXT);" "CREATE TABLE sessions (id INTEGER PRIMARY KEY,session_key TEXT,last_used TIMESTAMP);" "CREATE TABLE metadata (id INTEGER PRIMARY KEY,key TEXT,value TEXT);")))) ;; ;; if we have a session_key look up the session-id and store it ;; (sdat-session-set-id! self (session:get-id self))) ;; only set session-cookie when a new session is created (define (session:setup-session-key self) (let* ((sk (session:extract-session-key self)) (sid (if sk (session:get-id self sk) #f))) (if (not sid) ;; need a new key (let* ((new-key (session:get-new-key self)) (new-sid (session:get-id self new-key))) (sdat-session-key-set! self new-key) (sdat-session-id-set! self new-sid) (sdat-session-cookie-set! self (session:make-cookie self))) (sdat-session-id-set! self sid)))) (define (session:make-cookie self) ;; (list (conc "session_key=" (sdat-session-key self) "; Path=/; Domain=." (sdat-domain self) "; Max-Age=" (* 86400 14) "; Version=1"))) ;; According to ;; http://www.codemarvels.com/2010/11/apache-rewriterule-set-a-cookie-on-localhost/ ;; Here are the 2 (often left out) requirements to set a cookie using ;; httpd-F�s rewrite rule (mod_rewrite), while working on localhost:-A ;; ;; Use the IP 127.0.0.1 instead of localhost/machine-name as the |
︙ | ︙ | |||
1672 1673 1674 1675 1676 1677 1678 | ;; header, which makes the cookie a persistent one (not really ;; persistent, as the expires value set is the current server time ;; %G–%@ so you don-F-F�t even get to see your cookie!)-A (list (string-substitute ";" "; " (car (construct-cookie-string ;; warning! messing up this itty bitty bit of code will cost much time! | | | | | | | | | | 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 | ;; header, which makes the cookie a persistent one (not really ;; persistent, as the expires value set is the current server time ;; %G–%@ so you don-F-F�t even get to see your cookie!)-A (list (string-substitute ";" "; " (car (construct-cookie-string ;; warning! messing up this itty bitty bit of code will cost much time! `(("session_key" ,(sdat-session-key self) expires: ,(+ (current-seconds) (* 14 86400)) ;; max-age: (* 14 86400) path: "/" ;; domain: ,(string-append "." (sdat-domain self)) version: 1)) 0))))) ;; look up a given session key and return the id if found, #f if not found (define (session:get-id self session-key) ;; (let ((session-key (sdat-session-key self))) (if session-key (let ((query (string-append "SELECT id FROM sessions WHERE session_key='" session-key "'")) (conn (sdat-conn self)) (result #f)) (dbi:for-each-row (lambda (tuple) (set! result (vector-ref tuple 0))) conn query) (if result (dbi:exec conn (conc "UPDATE sessions SET last_used=" (dbi:now conn) " WHERE session_key=?;") session-key)) result) #f)) ;; (define (session:process-url-path self) (let ((path-info (get-environment-variable "PATH_INFO")) (query-string (get-environment-variable "QUERY_STRING"))) ;; (session:log self "path-info=" path-info " query-string=" query-string) (if path-info (let* ((parts (string-split path-info "/")) (numparts (length parts))) (if (> numparts 0) (sdat-page-set! self (car parts))) ;; (session:log self "url-path=" url-path " parts=" parts) (if (> numparts 1) (sdat-path-params-set! self (cdr parts))) (if query-string (sdat-params-set! self (string-split query-string "&"))))))) ;; BUGGY! (define (session:get-new-key self) (let ((conn (sdat-conn self)) (tmpkey (session:make-rand-string 20)) (status #f)) (dbi:for-each-row (lambda (tuple) (set! status #t)) conn (string-append "INSERT INTO sessions (session_key) VALUES ('" tmpkey "')")) tmpkey)) |
︙ | ︙ | |||
1734 1735 1736 1737 1738 1739 1740 | (define (session:get-session-id self session-key) (let ((query "SELECT id FROM sessions WHERE session_key=?;") (result #f)) ;; (pg:query-for-each (lambda (tuple) ;; (set! result (vector-ref tuple 0))) ;; (vector-ref tuple 0))) ;; (s:sqlparam query session-key) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 | (define (session:get-session-id self session-key) (let ((query "SELECT id FROM sessions WHERE session_key=?;") (result #f)) ;; (pg:query-for-each (lambda (tuple) ;; (set! result (vector-ref tuple 0))) ;; (vector-ref tuple 0))) ;; (s:sqlparam query session-key) ;; (sdat-conn self)) ;; conn) (dbi:for-each-row (lambda (tuple) (set! result (vector-ref tuple 0))) ;; (vector-ref tuple 0))) (sdat-conn self) (s:sqlparam query session-key)) result)) ;; delete all records for a session ;; ;; NEEDS TO BE TRANSACTIONIZED! ;; (define (session:delete-session self session-key) (let ((session-id (session:get-session-id self session-key)) (qry1 ;; (conc "BEGIN;" "DELETE FROM session_vars WHERE session_id=?;") (qry2 "DELETE FROM sessions WHERE id=?;") ;; "COMMIT;")) (conn (sdat-conn self))) (if session-id (begin (dbi:exec conn qry1 session-id) ;; session-id) (dbi:exec conn qry2 session-id) ;; (session:initialize self) (session:setup self))) (not (session:get-session-id self session-key)))) ;; (define (session:delete-session self session-key) ;; (let ((session-id (session:get-session-id self session-key)) ;; (queries (list "BEGIN;" ;; "DELETE FROM session_vars WHERE session_id=?;" ;; "DELETE FROM sessions WHERE id=?;" ;; "COMMIT;")) ;; (conn (sdat-conn self))) ;; (if session-id ;; (begin ;; (for-each ;; (lambda (query) ;; (dbi:exec conn query session-id)) ;; queries) ;; (initialize self '()) ;; (session:setup self))) ;; (not (session:get-session-id self session-key)))) (define (session:extract-key self key) (let ((params (sdat-params self))) (session:extract-key-from-param self params key))) (define (session:extract-key-from-param self params key) (let ((r1 (regexp (string-append "^" key "=([^=]+)$")))) (err:log "INFO: Looking for " key " in " params) (if (< (length params) 1) #f (let loop ((head (car params)) (tail (cdr params))) (let ((match (string-match r1 head))) (cond (match (let ((session-key (list-ref match 1))) (err:log "INFO: Found session key=" session-key) (sdat-session-key-set! self (list-ref match 1)) session-key)) ((null? tail) #f) (else (loop (car tail) (cdr tail))))))))) (define (session:set-page! self page_name) (sdat-page-set! self page_name)) (define (session:close self) (dbi:close (sdat-conn self))) ;; (close-output-port (sdat-logpt self)) (define (session:err-msg self msg) (hash-table-set! (sdat-sessionvars self) "ERROR_MSG" (string-intersperse (map s:any->string msg) " "))) (define (session:prev-err self) (let ((prev-err (hash-table-ref/default (sdat-sessionvars-before self) "ERROR_MSG" #f)) (curr-err (hash-table-ref/default (sdat-sessionvars self) "ERROR_MSG" #f))) (if prev-err prev-err (if curr-err curr-err #f)))) ;; session vars ;; 1. keys are always a string NOT a symbol ;; 2. values are always a string conversion is the responsibility of the ;; consuming function (at least for now, I'd like to change this) ;; set a session var for the current page ;; (define (session:curr-page-set! self key value) (hash-table-set! (sdat-pagevars self) (s:any->string key) (s:any->string value))) ;; del a var for the current page ;; (define (session:page-var-del! self key) (hash-table-delete! (sdat-pagevars self) (s:any->string key))) ;; get the appropriate hash given a page "*sessionvars*, *globalvars* or page ;; (define (session:get-page-hash self page) (if (string=? page "*sessionvars*") (sdat-sessionvars self) (if (string=? page "*globalvars*") (sdat-globalvars self) (sdat-pagevars self)))) ;; set a session var for a given page ;; (define (session:set! self page key value) (let ((ht (session:get-page-hash self page))) (hash-table-set! ht (s:any->string key) (s:any->string value)))) ;; get session vars for the current page ;; (define (session:page-get self key) (hash-table-ref/default (sdat-pagevars self) key #f)) ;; get session vars for a specified page ;; (define (session:get self page key params) (let* ((ht (session:get-page-hash self page)) (res (hash-table-ref/default ht (s:any->string key) #f))) (session:apply-type-preference res params))) ;; delete a session var for a specified page ;; (define (session:del! self page key) (let ((ht (session:get-page-hash self page))) (hash-table-delete! ht (s:any->string key)))) ;; get ALL keys for this page and store in the session pagevars hash ;; (define (session:get-vars self) (let ((session-id (sdat-session-id self))) (if (not session-id) (err:log "ERROR: No session id in session object! session:get-vars") (let* ((result #f) (conn (sdat-conn self)) (pagevars-before (sdat-pagevars-before self)) (sessionvars-before (sdat-sessionvars-before self)) (globalvars-before (sdat-globalvars-before self)) (pagevars (sdat-pagevars self)) (sessionvars (sdat-sessionvars self)) (globalvars (sdat-globalvars self)) (page-name (sdat-page self)) (session-key (sdat-session-key self)) (query (string-append "SELECT key,value FROM session_vars INNER JOIN sessions ON session_vars.session_id=sessions.id " "WHERE session_key=? AND page=?;"))) ;; first the page specific vars (dbi:for-each-row (lambda (tuple) (let ((k (vector-ref tuple 0)) (v (vector-ref tuple 1))) |
︙ | ︙ | |||
1912 1913 1914 1915 1916 1917 1918 | (hash-table-set! globalvars-before k v) (hash-table-set! globalvars k v))) conn (s:sqlparam query session-key "*globalvars")) )))) (define (session:save-vars self) | | | | | | | | | | | 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 | (hash-table-set! globalvars-before k v) (hash-table-set! globalvars k v))) conn (s:sqlparam query session-key "*globalvars")) )))) (define (session:save-vars self) (let ((session-id (sdat-session-id self))) (if (not session-id) (err:log "ERROR: No session id in session object! session:get-vars") (let* ((status #f) (conn (sdat-conn self)) (page-name (sdat-page self)) (del-query "DELETE FROM session_vars WHERE session_id=? AND page=? AND key=?;") (ins-query "INSERT INTO session_vars (session_id,page,key,value) VALUES(?,?,?,?);") (upd-query "UPDATE session_vars set value=? WHERE key=? AND session_id=? AND page=?;") (changed-count 0)) ;; save the delta only (for-each (lambda (page) ;; page is: "*globalvars*" "*sessionvars*" or otherstring (let* ((before-after-ht (cond ((string=? page "*sessionvars*") (vector (sdat-sessionvars self) (sdat-sessionvars-before self))) ((string=? page "*globalvars*") (vector (sdat-globalvars self) (sdat-globalvars-before self))) (else (vector (sdat-pagevars self) (sdat-pagevars-before self))))) (master-ht (vector-ref before-after-ht 0)) (before-ht (vector-ref before-after-ht 1)) (master-keys (hash-table-keys master-ht)) (before-keys (hash-table-keys before-ht)) (all-keys (delete-duplicates (append master-keys before-keys)))) (for-each (lambda (key) |
︙ | ︙ | |||
2032 2033 2034 2035 2036 2037 2038 | ;; 'flat => pages flat directory ;; 'dir => directory tree pages/<pagename>/{view,control}.scm ;; parts: ;; 'both => load control and view (anything other than view or control and the default) ;; 'view => load view only ;; 'control => load control only (define (session:call-parts self page #!key (parts 'both)) | | | | | | | | | 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 | ;; 'flat => pages flat directory ;; 'dir => directory tree pages/<pagename>/{view,control}.scm ;; parts: ;; 'both => load control and view (anything other than view or control and the default) ;; 'view => load view only ;; 'control => load control only (define (session:call-parts self page #!key (parts 'both)) (sdat-curr-page-set! self page) (let* ((dir-style (sdat-page-dir-style self));; (equal? (sdat-page-dir-style self) "onedir")) ;; flag #t for onedir, #f for old style (dir (string-append (sdat-sroot self) (if dir-style (conc "/pages/") (conc "/pages/" page))))) (case dir-style ;; NB// Stored always loads both control and view ((stored) ((eval (string->symbol (conc "pages:" page))) self ;; the session (sdat-conn self) ;; the db connection (sdat-shared-hash self) ;; a shared hash table for passing data to/from page calls )) ((flat) (let* ((so-file (conc dir page ".so")) (scm-file (conc dir page ".scm")) (src-file (or (file-exists? so-file) (file-exists? scm-file)))) (if src-file (begin (load src-file) ((eval (string->symbol (conc "pages:" page))) self ;; the session (sdat-conn self) ;; the db connection (sdat-shared-hash self) ;; a shared hash table for passing data to/from page calls )) (list "<p>Page not found " page " </p>")))) ;; first the control ;; (let ((control-file (conc "pages/" page "_ctrl.scm")) ;; (view-file (conc "pages/" page "_view.scm"))) ;; (if (and (file-exists? control-file) ;; (not (eq? parts 'view))) |
︙ | ︙ | |||
2080 2081 2082 2083 2084 2085 2086 | (else (list "ERROR: page-dir-style must be stored, dir or flat, got " dir-style))))) (define (session:call self page parts) (session:call-parts self page 'both)) ;; (define (session:load-model self model) | | | | | | | | | | | | | | 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 | (else (list "ERROR: page-dir-style must be stored, dir or flat, got " dir-style))))) (define (session:call self page parts) (session:call-parts self page 'both)) ;; (define (session:load-model self model) ;; (let ((model.scm (string-append (sdat-sroot self) "/models/" model ".scm")) ;; (model.so (string-append (sdat-sroot self) "/models/" model ".so"))) ;; (if (file-exists? model.so) ;; (load model.so) ;; (if (file-exists? model.scm) ;; (load model.scm) ;; (s:log "ERROR: model " model.scm " not found"))))) ;; (define (session:model-path self model) ;; (string-append (sdat-sroot self) "/models/" model ".scm")) (define (session:pp-formdat self) (let ((dat (formdat:all->strings (sdat-formdat self)))) (string-intersperse dat "<br> "))) (define (session:param->string params) ;; (err:log "params=" params) (if (< (length params) 1) "" (let loop ((key (car params)) (val (cadr params)) (tail (cddr params)) (result '())) (let ((newresult (cons (string-append (s:any->string key) "=" (s:any->string val)) result))) (if (< (length tail) 1) ;; true if done (string-intersperse newresult "&") (loop (car tail)(cadr tail)(cddr tail) newresult)))))) (define (session:link-to self page params) (let* ((https-host (get-environment-variable "HTTPS_HOST")) (force-ssl (sdat-force-ssl self)) (server (or https-host ;; Assuming HTTPS_HOST is only set if available (get-environment-variable "HTTP_HOST") (get-environment-variable "SERVER_NAME") (sdat-domain self))) (force-script (sdat-script self)) (script (or force-script (let ((script-name (string-split (get-environment-variable "SCRIPT_NAME") "/"))) (if (> (length script-name) 1) (string-append (car script-name) "/" (cadr script-name)) (get-environment-variable "SCRIPT_NAME"))))) ;; build script name from first two elements. This is a hangover from before I used ? in the URL.) (session-key (sdat-session-key self)) (paramstr (session:param->string params))) (session:log self "server=" server " script=" script " page=" page) (string-append (if (or https-host force-ssl) "https://" "http://") server "/" script "/" page "?" paramstr))) ;; "/sn=" session-key))) (define (session:cgi-out self) (let* ((content (list (sdat-content-type self))) ;; '("Content-type: text/html; charset=iso-8859-1\n\n")) (header (let ((cookie (sdat-session-cookie self))) (if cookie (cons (string-append "Set-Cookie: " (car cookie)) content) content))) (pagedat (sdat-pagedat self))) (s:cgi-out (cons header pagedat)))) (define (session:log self . msg) (with-output-to-port (sdat-log-port self) ;; (sdat-logpt self) (lambda () (apply print msg)))) ;; escape, convert or return raw when given user input data that potentially ;; could be malicious ;; (define (session:apply-type-preference res params) |
︙ | ︙ | |||
2190 2191 2192 2193 2194 2195 2196 | (if (null? tail) #f (loop (car tail)(cdr tail))))))))) ;; params are stored as list of key=val ;; (define (session:get-param self key type-params) ;; (session:log s:session "params=" (slot-ref s:session 'params)) | | | | 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 | (if (null? tail) #f (loop (car tail)(cdr tail))))))))) ;; params are stored as list of key=val ;; (define (session:get-param self key type-params) ;; (session:log s:session "params=" (slot-ref s:session 'params)) (let* ((params (sdat-params self)) (res (session:get-param-from params key))) (session:apply-type-preference res type-params))) ;; This one will get the first value found regardless of form ;; param: (dtype [tag1 tag2 ...]) ;; dtype: ;; 'raw : do no conversion ;; 'number : convert to number, return #f if fails ;; 'escaped : use html-escape to protect the input -- this is the default ;; (define (session:get-input self key params) (let* ((dtype (if (null? params) 'escaped (car params))) (tags (if (null? params) '() (cdr params))) (formdat (sdat-formdat self)) (res (if (not formdat) #f (if (or (string? key)(number? key)(symbol? key)) (if (and (vector? formdat)(eq? (vector-length formdat) 1)(hash-table? (vector-ref formdat 0))) (formdat:get formdat key) (begin (session:log self "ERROR: formdat: " formdat " is not of class <formdat>") #f)) |
︙ | ︙ | |||
2231 2232 2233 2234 2235 2236 2237 | res)) (else (if (string? res) (s:html-filter->string res '()) res))))) ;; This one will get the first value found regardless of form (define (session:get-input-keys self) | | | | 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 | res)) (else (if (string? res) (s:html-filter->string res '()) res))))) ;; This one will get the first value found regardless of form (define (session:get-input-keys self) (let* ((formdat (sdat-formdat self))) (if (not formdat) #f (if (and (vector? formdat)(eq? (vector-length formdat) 1)(hash-table? (vector-ref formdat 0))) (formdat:keys formdat) (begin (session:log self "ERROR: formdat: " formdat " is not of class <formdat>") #f))))) (define (session:run-actions self) (let* ((action (session:get-param self 'action '(raw))) (page (sdat-page self))) ;; (print "action=" action " page=" page) (if action (let ((action-lst (string-split action "."))) ;; (print "action-lst=" action-lst) (if (not (= (length action-lst) 2)) (err:log "Action should be of form: module.action") (let* ((targ-page (car action-lst)) |
︙ | ︙ | |||
2268 2269 2270 2271 2272 2273 2274 | ((exn file) (s:log "file error")) ((exn i/o) (s:log "i/o error")) ((exn ) (s:log "Action not implemented: " proc-name " action: " targ-action)) (var () (s:log "Unknown Error")))))))))) (define (session:never-called-page? self page) (session:log self "Checking for page: " page) | | | | | | 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 | ((exn file) (s:log "file error")) ((exn i/o) (s:log "i/o error")) ((exn ) (s:log "Action not implemented: " proc-name " action: " targ-action)) (var () (s:log "Unknown Error")))))))))) (define (session:never-called-page? self page) (session:log self "Checking for page: " page) (not (member page (sdat-seen-pages self)))) (define (session:set-called! self page) (sdat-seen-pages-set! self (cons page (sdat-seen-pages self)))) ;;====================================================================== ;; Alternative data type delivery ;;====================================================================== (define (session:alt-out self) (let ((dat (sdat-alt-page-dat self))) ;; (s:log "dat is: " dat) ;; (print "HTTP/1.1 200 OK") (print "Date: " (time->string (seconds->utc-time (current-seconds)))) (print "Content-Type: " (sdat-content-type self)) (print "Accept-Ranges: bytes") (print "Content-Length: " (if (blob? dat) (blob-size dat) 0)) (print "Keep-Alive: timeout=15, max=100") (print "Connection: Keep-Alive") (print "") |
︙ | ︙ | |||
2304 2305 2306 2307 2308 2309 2310 | ;; (define (s:log . msg) (apply session:log s:session msg)) ;; Usage: (s:get-err s:big) (define (s:get-err wrapperfunc) | | | | | | | | | | 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 | ;; (define (s:log . msg) (apply session:log s:session msg)) ;; Usage: (s:get-err s:big) (define (s:get-err wrapperfunc) (let ((errmsg (sdat-curr-err s:session))) (if errmsg ((if wrapperfunc wrapperfunc s:strong) errmsg) '()))) (define (stml:cgi-session session) ;; (session:initialize session) (session:setup session) (session:get-vars session) (sdat-log-port-set! session ;; (current-error-port)) (open-output-file (sdat-logfile session) #:append)) (s:validate-inputs) (session:run-actions session) (sdat-pagedat-set! session (append (sdat-pagedat session) (s:call (sdat-toppage session)))) (if (eq? (sdat-page-type session) 'html) ;; default is html. (session:cgi-out session) (session:alt-out session)) (session:save-vars session) (session:close session)) (define (s:validate-inputs) |
︙ | ︙ | |||
2347 2348 2349 2350 2351 2352 2353 | (s:h1 "ERROR") (s:p err))))))) (define (stml:main proc) (handle-exceptions exn | | | 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 | (s:h1 "ERROR") (s:p err))))))) (define (stml:main proc) (handle-exceptions exn (if (sdat-debug-mode s:session) (begin (print "Content-type: text/html") (print "") (print "<html> <head> <title>EXCEPTION</title> </head> <body>") (print " QUERY_STRING is: <b> " (get-environment-variable "QUERY_STRING") " </b> <br>") (print "<pre>") ;; (print " EXCEPTION: " ((condition-property-accessor 'exn 'message) exn)) |
︙ | ︙ | |||
2382 2383 2384 2385 2386 2387 2388 | (print (car var) "\t" (cdr var))) (get-environment-variables)))) ;; return something useful to the user (print "Content-type: text/html") (print "") (print "<html> <head> <title>EXCEPTION</title> </head> <body>") (print "<h1>CRASH!</h1>") | | | | | | | 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 | (print (car var) "\t" (cdr var))) (get-environment-variables)))) ;; return something useful to the user (print "Content-type: text/html") (print "") (print "<html> <head> <title>EXCEPTION</title> </head> <body>") (print "<h1>CRASH!</h1>") (print " Please notify support at " (sdat-domain s:session) " that the error log is stml-crash-" (current-process-id) ".log</b> <br>") ;; (print "<pre>") ;; ;; (print " EXCEPTION: " ((condition-property-accessor 'exn 'message) exn)) ;; ;; (print-error-message exn) ;; ;; (print-call-chain) ;; (print "</pre>") ;; (print "<table>") ;; (for-each (lambda (var) ;; (print "<tr><td>" (car var) "</td><td>" (cdr var) "</td></tr>")) ;; (get-environment-variables)) ;; (print "</table>") (print "</body></html>"))) (if proc (proc s:session) (stml:cgi-session s:session)) ;; (raise-error) ;; (exit) )) ;; find out if we are in debugmode (define (s:debug-mode?) (sdat-debug-mode s:session)) (define (s:never-called-page? page) (session:never-called-page? s:session page)) (define (s:set-err . args) (sdat-curr-err-set! s:session args)) (define (s:current-page) (sdat-page s:session)) (define (s:delete-session) (session:delete-session s:session (sdat-session-key s:session))) (define (s:call page . partsl) (if (null? partsl) (session:call s:session page #f) (session:call s:session page (car partsl)))) (define (s:link-to page . params) |
︙ | ︙ | |||
2460 2461 2462 2463 2464 2465 2466 | (define (s:session-var-del! key) (session:del! s:session "*sessionvars*" key)) (define s:session-var-delete! s:session-var-del!) ;; utility to get all vars as hash table (define (s:session-get-sessionvars) | | | < < | 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 | (define (s:session-var-del! key) (session:del! s:session "*sessionvars*" key)) (define s:session-var-delete! s:session-var-del!) ;; utility to get all vars as hash table (define (s:session-get-sessionvars) (sdat-sessionvars s:session)) ) |