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

Last change on this file since 15217 was 15217, checked in by certainty, 11 years ago

simplified expression

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 20:21:49 2009 (CEST)
11;;           By: David Krentzlin <david@lisp-unleashed.de>
12;;     Update #: 85
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) <year> 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
59
60
61(module uri-dispatch
62  (dispatch-error dispatch-environment
63   enable-checks  whitelist! default-dispatch-target
64   dispatch-uri)
65  (import scheme chicken extras)
66  (require-library uri-common environments srfi-13)
67  (import (only uri-common uri? uri-path)
68          (only srfi-13 string-append)
69          (only environments environment-ref environment-has-binding?))
70
71  (define dispatch-error (make-parameter (lambda args #f)))
72  (define procedure-whitelist (list))
73  (define module-whitelist  (list))
74  (define dispatch-environment interaction-environment)
75  (define enable-checks (make-parameter #f))
76  (define default-dispatch-target (make-parameter (lambda args #f)))
77
78  (define (dispatch-uri uri)
79    (unless (uri? uri)
80      (error "Supplied argument is must be an uri"))
81    (let ((path (cdr (uri-path uri))))
82      (cond
83       ((null? path)
84        (apply (or (default-dispatch-target)
85                   (dispatch-error)) '()))
86       ((= 1 (length path))
87        (if (equal? (car path) "")
88            (apply (or (default-dispatch-target) (dispatch-error)) '())
89            (apply (or (handler-ref (string->symbol (car path)))
90                       (dispatch-error))
91                   (cdr path))))
92       (else
93        (let ((mod/proc (handler-ref (string->symbol (string-append (car path) "#" (cadr path))) (string->symbol (car path)))))
94          (if mod/proc
95              (apply mod/proc (cddr path))
96              (apply (or (handler-ref (string->symbol (car path)))
97                         (dispatch-error))
98                     (cdr path))))))))
99
100  (define (handler-ref symbol #!optional (mod #f))
101    (and (environment-has-binding? (dispatch-environment) symbol)
102         (let ((binding (environment-ref (dispatch-environment) symbol)))
103            (and (procedure? binding) (whitelisted? symbol mod) binding))))
104 
105  (define (whitelisted? symbol mod)   
106    (or (not (enable-checks))
107        (and (not mod) (member symbol procedure-whitelist))
108        (and mod (member mod module-whitelist))))
109
110  (define (whitelist! def)
111    (let loop ((def def) (pwl procedure-whitelist) (mwl module-whitelist))
112      (cond
113       ((null? def)
114        (set! procedure-whitelist pwl)
115        (set! module-whitelist mwl))
116       ((and (list? (car def)) (eq? (caar def) 'module) (positive? (length (cdar def))))
117        (loop (cdr def) pwl (append mwl (cdar def))))
118       (else
119        (loop (cdr def) (cons (car def) pwl) mwl)))))
120)           
121
122
123;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124;;; uri-dispatch.scm ends here
Note: See TracBrowser for help on using the repository browser.