source: project/release/4/uri-dispatch/trunk/uri-dispatch.scm @ 15221

Last change on this file since 15221 was 15221, checked in by certainty, 10 years ago

replaced uri? with uri-reference?

File size: 4.4 KB
Line 
1;;; uri-dispatch.scm ---
2;;
3;; Filename: uri-dispatch.scm
4;; Description:
5;; Author: David Krentzlin <david@lisp-unleashed.de>
6;; Maintainer:
7;; Created: Mi Jul 15 18:58:29 2009 (CEST)
8;; Version: $Id$
9;; Version:
10;; Last-Updated: Mi Jul 15 21:34:53 2009 (CEST)
11;;           By: David Krentzlin <david@lisp-unleashed.de>
12;;     Update #: 91
13;; URL:
14;; Keywords:
15;; Compatibility:
16;;
17;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18;;
19;;; Commentary:
20;;
21;;
22;;
23;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24;;
25;;; Change log:
26;;
27;;
28;; RCS $Log$
29;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30;;
31;; Copyright (c) <2009> David Krentzlin <david@lisp-unleashed.de>
32;;
33;;   Permission is hereby granted, free of charge, to any person
34;;   obtaining a copy of this software and associated documentation
35;;   files (the "Software"), to deal in the Software without
36;;   restriction, including without limitation the rights to use,
37;;   copy, modify, merge, publish, distribute, sublicense, and/or sell
38;;   copies of the Software, and to permit persons to whom the
39;;   Software is furnished to do so, subject to the following
40;;   conditions:
41;;
42;;   The above copyright notice and this permission notice shall be
43;;   included in all copies or substantial portions of the Software.
44;;
45;;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46;;   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
47;;   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48;;   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
49;;   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
50;;   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51;;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
52;;   OTHER DEALINGS IN THE SOFTWARE.
53;;
54;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55;;
56;;; Code:
57
58(module uri-dispatch
59  (dispatch-error dispatch-environment
60   enable-checks  whitelist! default-dispatch-target
61   dispatch-uri)
62  (import scheme chicken)
63  (require-library uri-common environments srfi-13)
64  (import (only uri-common uri-reference? uri-path)
65          (only srfi-13 string-append)
66          (only environments environment-ref environment-has-binding?))
67
68  (define dispatch-error (make-parameter (lambda args #f)))
69  (define procedure-whitelist (list))
70  (define module-whitelist  (list))
71  (define dispatch-environment interaction-environment)
72  (define enable-checks (make-parameter #f))
73  (define default-dispatch-target (make-parameter (lambda args #f)))
74
75  (define (dispatch-uri uri)
76    (unless (uri-reference? uri)
77      (error "Supplied argument is must be an uri"))
78    (let ((path (cdr (uri-path uri))))
79      (cond
80       ((null? path)
81        (apply (or (default-dispatch-target)
82                   (dispatch-error)) '()))
83       ((= 1 (length path))
84        (if (equal? (car path) "")
85            (apply (or (default-dispatch-target) (dispatch-error)) '())
86            (apply (or (handler-ref (string->symbol (car path)))
87                       (dispatch-error))
88                   (cdr path))))
89       (else
90        (let ((mod/proc (handler-ref (string->symbol (string-append (car path) "#" (cadr path))) (string->symbol (car path)))))
91          (if mod/proc
92              (apply mod/proc (cddr path))
93              (apply (or (handler-ref (string->symbol (car path)))
94                         (dispatch-error))
95                     (cdr path))))))))
96
97  (define (handler-ref symbol #!optional (mod #f))
98    (and (environment-has-binding? (dispatch-environment) symbol)
99         (let ((binding (environment-ref (dispatch-environment) symbol)))
100            (and (procedure? binding) (whitelisted? symbol mod) binding))))
101 
102  (define (whitelisted? symbol mod)   
103    (or (not (enable-checks))
104        (and (not mod) (member symbol procedure-whitelist))
105        (and mod (member mod module-whitelist))))
106
107  (define (whitelist! def)
108    (let loop ((def def) (pwl procedure-whitelist) (mwl module-whitelist))
109      (cond
110       ((null? def)
111        (set! procedure-whitelist pwl)
112        (set! module-whitelist mwl))
113       ((and (list? (car def)) (eq? (caar def) 'module) (positive? (length (cdar def))))
114        (loop (cdr def) pwl (append mwl (cdar def))))
115       (else
116        (loop (cdr def) (cons (car def) pwl) mwl)))))
117)           
118
119
120;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121;;; uri-dispatch.scm ends here
Note: See TracBrowser for help on using the repository browser.