| 1 |
;;; |
| 2 |
;;; www.cgi.test - framework to test CGI scripts |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2003-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module www.cgi.test |
| 35 |
(use gauche.process) |
| 36 |
(use rfc.uri) |
| 37 |
(use rfc.822) |
| 38 |
(use sxml.ssax) |
| 39 |
(export cgi-test-environment-ref |
| 40 |
call-with-cgi-script |
| 41 |
run-cgi-script->header&body |
| 42 |
run-cgi-script->sxml |
| 43 |
run-cgi-script->string |
| 44 |
run-cgi-script->string-list |
| 45 |
) |
| 46 |
) |
| 47 |
(select-module www.cgi.test) |
| 48 |
|
| 49 |
;; Default environments |
| 50 |
(define *cgi-test-env* |
| 51 |
(hash-table 'string=? |
| 52 |
'("SERVER_SOFTWARE" . "cgitest/1.0") |
| 53 |
'("SERVER_NAME" . "localhost") |
| 54 |
'("GATEWAY_INTERFACE" . "CGI/1.1") |
| 55 |
'("SERVER_PROTOCOL" . "HTTP/1.1") |
| 56 |
'("SERVER_PORT" . "80") |
| 57 |
'("REQUEST_METHOD" . "GET") |
| 58 |
'("REMOTE_HOST" . "remote") |
| 59 |
'("REMOTE_ADDR" . "127.0.0.1") |
| 60 |
)) |
| 61 |
|
| 62 |
(define cgi-test-environment-ref |
| 63 |
(getter-with-setter |
| 64 |
(^[key . maybe-default] |
| 65 |
(apply hash-table-get *cgi-test-env* (x->string key) maybe-default)) |
| 66 |
(^[key value] |
| 67 |
(hash-table-put! *cgi-test-env* (x->string key) (x->string value))))) |
| 68 |
|
| 69 |
;; Runs CGI script under specified environments. |
| 70 |
;; Calls proc with a port connected to cgi process's stdout. |
| 71 |
(define (call-with-cgi-script script proc :key (environment '()) (parameters #f)) |
| 72 |
;; set up environment |
| 73 |
(let1 envtab (make-hash-table 'string=?) |
| 74 |
(hash-table-for-each *cgi-test-env* |
| 75 |
(cut hash-table-put! envtab <> <>)) |
| 76 |
(dolist [p environment] |
| 77 |
(hash-table-put! envtab (x->string (car p)) (x->string (cdr p)))) |
| 78 |
(let ([method (hash-table-get envtab "REQUEST_METHOD" "GET")] |
| 79 |
[query (and parameters (build-query-string parameters))]) |
| 80 |
(cond |
| 81 |
[(and parameters (member method '("GET" "HEAD"))) |
| 82 |
(hash-table-put! envtab "QUERY_STRING" query)] |
| 83 |
[(and parameters (equal? method "POST")) |
| 84 |
;; TODO: support multipart/form-data |
| 85 |
(hash-table-put! envtab "CONTENT_TYPE" |
| 86 |
"application/x-www-form-urlencoded") |
| 87 |
(hash-table-put! envtab "CONTENT_LENGTH" |
| 88 |
(x->string (string-size query)))]) |
| 89 |
(call-with-process-io `("env" ,@(build-env envtab) ,script) |
| 90 |
(^[inp outp] |
| 91 |
(when (and query (equal? method "POST")) |
| 92 |
(display query outp) |
| 93 |
(newline outp) |
| 94 |
(close-output-port outp)) |
| 95 |
(proc inp)) |
| 96 |
:on-abnormal-exit :ignore) |
| 97 |
))) |
| 98 |
|
| 99 |
;; Convenience procedurs |
| 100 |
(define (run-cgi-script->header&body script reader . args) |
| 101 |
(apply call-with-cgi-script script |
| 102 |
(^p (let* ([header (rfc822-header->list p)] |
| 103 |
[body (reader p)]) |
| 104 |
(values header body))) |
| 105 |
args)) |
| 106 |
|
| 107 |
(define (run-cgi-script->sxml script . args) |
| 108 |
(apply run-cgi-script->header&body script (cut ssax:xml->sxml <> '()) args)) |
| 109 |
|
| 110 |
(define (run-cgi-script->string script . args) |
| 111 |
(apply run-cgi-script->header&body script port->string args)) |
| 112 |
|
| 113 |
(define (run-cgi-script->string-list script . args) |
| 114 |
(apply run-cgi-script->header&body script port->string-list args)) |
| 115 |
|
| 116 |
;; Utils |
| 117 |
|
| 118 |
(define (build-query-string param-alist) |
| 119 |
(string-join (map (^p (string-join |
| 120 |
(map (^x (uri-encode-string (x->string x))) |
| 121 |
(list (car p) (cdr p))) |
| 122 |
"=")) |
| 123 |
param-alist) |
| 124 |
"&")) |
| 125 |
|
| 126 |
(define (build-env env-table) |
| 127 |
(hash-table-map env-table (^[k v] #"~|k|=~|v|"))) |