| 1 |
;;; |
| 2 |
;;; cgi.scm - CGI utility |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-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 |
;; Surprisingly, there's no ``formal'' definition of CGI. |
| 35 |
;; The most reliable document I found is in <http://CGI-Spec.Golux.Com/> |
| 36 |
|
| 37 |
;; TODO: a method should provided to return HTTP error condition to |
| 38 |
;; the httpd. |
| 39 |
|
| 40 |
(define-module www.cgi |
| 41 |
(use scheme.list) |
| 42 |
(use srfi.13) |
| 43 |
(use rfc.uri) |
| 44 |
(use rfc.mime) |
| 45 |
(use rfc.822) |
| 46 |
(use gauche.charconv) |
| 47 |
(use gauche.collection) |
| 48 |
(use gauche.uvector) |
| 49 |
(use text.tree) |
| 50 |
(use text.html-lite) |
| 51 |
(use file.util) |
| 52 |
(export cgi-metavariables |
| 53 |
cgi-get-metavariable |
| 54 |
cgi-output-character-encoding |
| 55 |
cgi-parse-parameters |
| 56 |
cgi-get-parameter |
| 57 |
cgi-header |
| 58 |
cgi-temporary-files |
| 59 |
cgi-add-temporary-file |
| 60 |
cgi-main |
| 61 |
<cgi-error> |
| 62 |
<cgi-request-size-error> |
| 63 |
<cgi-content-type-error> |
| 64 |
<cgi-request-method-error>) |
| 65 |
) |
| 66 |
(select-module www.cgi) |
| 67 |
|
| 68 |
(autoload rfc.cookie parse-cookie-string) |
| 69 |
(autoload gauche.vport open-input-limited-length-port) |
| 70 |
|
| 71 |
;;---------------------------------------------------------------- |
| 72 |
;; A parameter cgi-metavariables can be used to supply metavariables, |
| 73 |
;; instead of via environment variables. It is handy for debugging, |
| 74 |
;; or call CGI routine from other Scheme module. |
| 75 |
(define cgi-metavariables (make-parameter #f)) |
| 76 |
|
| 77 |
;; Internal routine to get metavariable |
| 78 |
(define (get-meta name) |
| 79 |
(or (and-let* ([mv (cgi-metavariables)] |
| 80 |
[p (assoc name mv)]) |
| 81 |
(cadr p)) |
| 82 |
(sys-getenv name))) |
| 83 |
|
| 84 |
;; rename to export |
| 85 |
(define cgi-get-metavariable get-meta) |
| 86 |
|
| 87 |
;;---------------------------------------------------------------- |
| 88 |
;; Error conditions |
| 89 |
;; |
| 90 |
|
| 91 |
(define-condition-type <cgi-error> <error> |
| 92 |
#f) |
| 93 |
|
| 94 |
(define-condition-type <cgi-request-size-error> <cgi-error> |
| 95 |
#f |
| 96 |
(name) ;; part name or #f |
| 97 |
(size)) |
| 98 |
|
| 99 |
(define-condition-type <cgi-content-type-error> <cgi-error> |
| 100 |
#f |
| 101 |
(content-type)) |
| 102 |
|
| 103 |
(define-condition-type <cgi-request-method-error> <cgi-error> |
| 104 |
#f |
| 105 |
(request-method)) |
| 106 |
|
| 107 |
;;---------------------------------------------------------------- |
| 108 |
;; The output character encoding. Used to generate the default |
| 109 |
;; output generator. |
| 110 |
|
| 111 |
(define cgi-output-character-encoding |
| 112 |
(make-parameter (gauche-character-encoding))) |
| 113 |
|
| 114 |
;;---------------------------------------------------------------- |
| 115 |
;; The list of temporary files created to save uploaded files. |
| 116 |
;; The files will be unlinked when cgi-main exits. |
| 117 |
|
| 118 |
(define cgi-temporary-files |
| 119 |
(make-parameter '())) |
| 120 |
|
| 121 |
(define (cgi-add-temporary-file path) |
| 122 |
(cgi-temporary-files (cons path (cgi-temporary-files)))) |
| 123 |
|
| 124 |
;;---------------------------------------------------------------- |
| 125 |
;; Get query string. (internal) |
| 126 |
;; If the request is multipart/mime, it just returns a symbol 'mime |
| 127 |
;; without retrieving input. cgi-parse-parameters handles the mime |
| 128 |
;; message. |
| 129 |
(define (cgi-get-query content-length) |
| 130 |
(let* ([type (mime-parse-content-type (get-meta "CONTENT_TYPE"))] |
| 131 |
[typesig (and type (list (car type) (cadr type)))] |
| 132 |
[method (get-meta "REQUEST_METHOD")]) |
| 133 |
(unless (or (not type) |
| 134 |
(member typesig |
| 135 |
'(("application" "x-www-form-urlencoded") |
| 136 |
("multipart" "form-data")))) |
| 137 |
(errorf <cgi-content-type-error> |
| 138 |
:content-type type |
| 139 |
"Unsupported CONTENT_TYPE: ~a" type)) |
| 140 |
(cond [(not method) ;; interactive use. |
| 141 |
(if (sys-isatty (current-input-port)) |
| 142 |
(begin |
| 143 |
(display "Enter parameters (name=value). ^D to stop.\n") |
| 144 |
(flush) |
| 145 |
(let loop ((line (read-line)) |
| 146 |
(params '())) |
| 147 |
(if (eof-object? line) |
| 148 |
(string-join (reverse params) "&") |
| 149 |
(loop (read-line) |
| 150 |
(if (string-null? line) |
| 151 |
params |
| 152 |
(cons line params)))))) |
| 153 |
(error <cgi-request-method-error> |
| 154 |
:request-method #f |
| 155 |
"REQUEST_METHOD not defined"))] |
| 156 |
[(or (string-ci=? method "GET") |
| 157 |
(string-ci=? method "HEAD")) |
| 158 |
(or (get-meta "QUERY_STRING") "")] |
| 159 |
[(string-ci=? method "POST") |
| 160 |
(if (equal? typesig '("multipart" "form-data")) |
| 161 |
'mime |
| 162 |
(or (and-let* ([lenp (or content-length |
| 163 |
(get-meta "CONTENT_LENGTH"))] |
| 164 |
[len (x->integer lenp)] |
| 165 |
[ (<= 0 len) ] |
| 166 |
[data (read-complete-block len)]) |
| 167 |
;; NB: When we're here, we know content-type is |
| 168 |
;; application/x-www-form-urlencoded, so buf should consist |
| 169 |
;; of ASCII characters. |
| 170 |
(u8vector->string data)) |
| 171 |
(port->string (current-input-port))))] |
| 172 |
[else |
| 173 |
(errorf <cgi-request-method-error> |
| 174 |
:request-method method |
| 175 |
"Unknown REQUEST_METHOD: ~a" method)] |
| 176 |
))) |
| 177 |
|
| 178 |
;; Read LEN octets from the current input port and returns u8vector. |
| 179 |
;; The port buffering may not be :full, so we should keep reading |
| 180 |
;; until all data is retrieved. |
| 181 |
(define (read-complete-block len) |
| 182 |
(if (zero? len) |
| 183 |
'#u8() |
| 184 |
(let ([buf (make-u8vector len)] |
| 185 |
[inp (current-input-port)]) |
| 186 |
(let loop ([i 0]) |
| 187 |
(let1 nread (read-block! buf inp i) |
| 188 |
(cond [(eof-object? nread) |
| 189 |
(errorf <cgi-error> "POST request ends prematurely. Expected content-length: ~a, but read only ~a octets." len i)] |
| 190 |
[(< (+ nread i) len) (loop (+ nread i))] |
| 191 |
[else buf])))))) |
| 192 |
|
| 193 |
;;---------------------------------------------------------------- |
| 194 |
;; API: cgi-parse-parameters &keyword query-string merge-cookies |
| 195 |
;; part-handlers content-type |
| 196 |
;; mime-input |
| 197 |
(define (cgi-parse-parameters :key |
| 198 |
(query-string #f) |
| 199 |
(merge-cookies #f) |
| 200 |
(part-handlers '()) |
| 201 |
(content-type #f) |
| 202 |
(content-length #f) |
| 203 |
(mime-input #f)) |
| 204 |
(let* ([input (cond [ query-string ] |
| 205 |
[(or mime-input content-type) 'mime] |
| 206 |
[else (cgi-get-query content-length)])] |
| 207 |
[cookies (if-let1 s (and merge-cookies (get-meta "HTTP_COOKIE")) |
| 208 |
(parse-cookie-string s) |
| 209 |
'())]) |
| 210 |
(define (group alist) |
| 211 |
(group-collection->alist alist :key car :value cadr :test equal?)) |
| 212 |
($ alist-merge equal? append |
| 213 |
(cond |
| 214 |
[(eq? input 'mime) |
| 215 |
(group (get-mime-parts part-handlers |
| 216 |
(or content-type (get-meta "CONTENT_TYPE")) |
| 217 |
(or content-length (get-meta "CONTENT_LENGTH")) |
| 218 |
(or mime-input (current-input-port))))] |
| 219 |
[(string-null? input) '()] |
| 220 |
[else (group (uri-decompose-query input))]) |
| 221 |
(map (^[cookie] (list (car cookie) (cadr cookie))) cookies)))) |
| 222 |
|
| 223 |
;; part-handlers: ((<name-list> <action> <options> ...) ...) |
| 224 |
;; <name-list> : list of field names (string, symbol, or regexp) |
| 225 |
;; <action> : #f ;; return as string (default) |
| 226 |
;; : file ;; save content to a tmpfile, |
| 227 |
;; ;; returns filename. |
| 228 |
;; : file+name ;; save content to a tmpfile, |
| 229 |
;; ;; returns a list (tmpfile origfile) |
| 230 |
;; ;; where origfile is the filename given |
| 231 |
;; ;; from the client. |
| 232 |
;; : ignore ;; discard the value. |
| 233 |
;; : <procedure> ;; calls <procedure> with |
| 234 |
;; ;; name, part-info, input-port. |
| 235 |
;; <options> keyword-value list. |
| 236 |
;; :prefix "prefix" - use "prefix" for temporary file |
| 237 |
;; (file and file+name only) |
| 238 |
;; :mode <mode> - set tmpfile's mode to <mode> |
| 239 |
;; (file and file+name only) |
| 240 |
;; :max-length integer - limit the content-length of the |
| 241 |
;; part. if it is exceeded, |
| 242 |
;; <cgi-request-size-error> is |
| 243 |
;; thrown. (NOT SUPPORTED) |
| 244 |
;; |
| 245 |
;; Returns ((<part-name> <value>) ...) |
| 246 |
;; |
| 247 |
(define (get-mime-parts part-handlers ctype clength inp) |
| 248 |
(define (part-ref info name) |
| 249 |
(rfc822-header-ref (ref info 'headers) name)) |
| 250 |
|
| 251 |
(define (make-file-handler prefix honor-origfile? mode) |
| 252 |
(^[name filename part-info inp] |
| 253 |
(receive (outp tmpfile) (sys-mkstemp prefix) |
| 254 |
(cgi-add-temporary-file tmpfile) |
| 255 |
(mime-retrieve-body part-info inp outp) |
| 256 |
(close-output-port outp) |
| 257 |
(when mode (sys-chmod tmpfile mode)) |
| 258 |
(if honor-origfile? |
| 259 |
(list tmpfile filename) |
| 260 |
tmpfile)))) |
| 261 |
|
| 262 |
(define (string-handler name filename part-info inp) |
| 263 |
(mime-body->string part-info inp)) |
| 264 |
|
| 265 |
(define (ignore-handler name filename part-info inp) |
| 266 |
(let loop ([ignore (read-line inp #t)]) |
| 267 |
(if (eof-object? ignore) #f (loop (read-line inp #t))))) |
| 268 |
|
| 269 |
(define (get-action&opts part-name) |
| 270 |
(let1 clause (find (^[entry] |
| 271 |
(or (eq? (car entry) #t) |
| 272 |
(and (regexp? (car entry)) |
| 273 |
(rxmatch (car entry) part-name)) |
| 274 |
(and (list? (car entry)) |
| 275 |
(member part-name (map x->string (car entry)))) |
| 276 |
(string=? (x->string (car entry)) part-name))) |
| 277 |
part-handlers) |
| 278 |
(cond |
| 279 |
[(or (not clause) |
| 280 |
(not (pair? (cdr clause)))) |
| 281 |
(list string-handler)] ;; default action |
| 282 |
[(and (= (length clause) 3) |
| 283 |
(memq (cadr clause) '(file file+name)) |
| 284 |
(string? (caddr clause))) |
| 285 |
;; backward compatibility - will be deleted soon |
| 286 |
(list (cadr clause) :prefix (caddr clause))] |
| 287 |
[else (cdr clause)]))) |
| 288 |
|
| 289 |
(define (get-handler action . opts) |
| 290 |
(cond |
| 291 |
[(not action) string-handler] |
| 292 |
[(memq action '(file file+name)) |
| 293 |
(make-file-handler (get-keyword* :prefix opts |
| 294 |
(build-path (temporary-directory) |
| 295 |
"gauche-cgi-")) |
| 296 |
(eq? action 'file+name) |
| 297 |
(get-keyword :mode opts #f))] |
| 298 |
[(eq? action 'ignore) ignore-handler] |
| 299 |
[else action])) |
| 300 |
|
| 301 |
;; The value of content-disposition must be a properly quoted string |
| 302 |
;; according to RFC2183 and RFC2045. However, IE sends a pathname including |
| 303 |
;; backslashes without quoting them. As a compromise, we only consider |
| 304 |
;; backslash escape if the following character is either #\" or #\\. |
| 305 |
;; (This fix is provided by Tatsuya BIZENN). |
| 306 |
(define (content-disposition-string input) |
| 307 |
(read-char input) ; discard beginning DQUOTE |
| 308 |
(let1 r (open-output-string :private? #t) |
| 309 |
(define (finish) (get-output-string r)) |
| 310 |
(let loop ([c (read-char input)]) |
| 311 |
(cond [(eof-object? c) (finish)] ; tolerate missing closing DQUOTE |
| 312 |
[(char=? c #\") (finish)] ; discard ending DQUOTE |
| 313 |
[(char=? c #\\) |
| 314 |
(let1 c (read-char input) |
| 315 |
(cond [(eof-object? c) (finish)] ;; tolerate stray backslash |
| 316 |
[else (unless (char-set-contains? #[\\\"] c) |
| 317 |
(write-char #\\ r)) |
| 318 |
(write-char c r) |
| 319 |
(loop (read-char input))]))] |
| 320 |
[else (write-char c r) (loop (read-char input))])))) |
| 321 |
|
| 322 |
(define (parse-content-disposition field) |
| 323 |
(if field |
| 324 |
(rfc822-field->tokens field |
| 325 |
`((#[\"] . ,content-disposition-string) |
| 326 |
(,*rfc822-atext-chars* . ,rfc822-dot-atom))) |
| 327 |
'())) |
| 328 |
|
| 329 |
;; NB: This is not an ideal solution. There could be a malformed |
| 330 |
;; option string (e.g. filename=c:\foo\bar\baz, in which the option |
| 331 |
;; value should be quoted but we have to accept those non-conforming |
| 332 |
;; clients). |
| 333 |
(define (get-option optname optregex opts) |
| 334 |
(cond [(member optname opts) => cadr] |
| 335 |
[(any (^t (and (string? t) (rxmatch optregex t))) opts) |
| 336 |
=> (cut rxmatch-after <>)] |
| 337 |
[else #f])) |
| 338 |
|
| 339 |
(define (handle-part part-info inp) |
| 340 |
(let* ([cd (part-ref part-info "content-disposition")] |
| 341 |
[opts (parse-content-disposition cd)] |
| 342 |
[name (get-option "name=" #/^name=/ opts)] |
| 343 |
[filename (get-option "filename=" #/^filename=/ opts)]) |
| 344 |
(cond |
| 345 |
[(not name) ;; if no name is given, just ignore this part. |
| 346 |
(ignore-handler name filename part-info inp) |
| 347 |
#f] |
| 348 |
[(not filename) ;; this is not a file uploading field. |
| 349 |
(list name (string-handler name filename part-info inp))] |
| 350 |
[(string-null? filename) ;; file field is empty |
| 351 |
(list name (ignore-handler name filename part-info inp))] |
| 352 |
[else |
| 353 |
(let* ([action&opts (get-action&opts name)] |
| 354 |
[handler (apply get-handler action&opts)] |
| 355 |
[result (handler name filename part-info inp)]) |
| 356 |
(list name result))]))) |
| 357 |
|
| 358 |
(let* ([inp (if (and clength (<= 0 (x->integer clength))) |
| 359 |
(open-input-limited-length-port inp (x->integer clength)) |
| 360 |
inp)] |
| 361 |
[result (mime-parse-message inp `(("content-type" ,ctype)) |
| 362 |
handle-part)]) |
| 363 |
(filter-map (cut ~ <>'content) (~ result'content)))) |
| 364 |
|
| 365 |
;;---------------------------------------------------------------- |
| 366 |
;; API: cgi-get-parameter key params &keyword list default convert |
| 367 |
;; |
| 368 |
(define (cgi-get-parameter key params :key ((:list lis) #f) |
| 369 |
(default (if lis '() #f)) |
| 370 |
((:convert cv) identity)) |
| 371 |
(cond [(assoc key params) => (^p (if lis (map cv (cdr p)) (cv (cadr p))))] |
| 372 |
[else default])) |
| 373 |
|
| 374 |
;;---------------------------------------------------------------- |
| 375 |
;; API: cgi-header &keyword content-type cookies location &allow-other-keys |
| 376 |
;; |
| 377 |
(define (cgi-header :key |
| 378 |
(content-type #f) |
| 379 |
(location #f) |
| 380 |
(status #f) |
| 381 |
(cookies '()) |
| 382 |
:allow-other-keys rest) |
| 383 |
(let ([ct (or content-type |
| 384 |
(and (not location) "text/html"))] |
| 385 |
[r '()]) |
| 386 |
(when status (push! r #"Status: ~status\r\n")) |
| 387 |
(when ct (push! r #"Content-type: ~ct\r\n")) |
| 388 |
(when location (push! r #"Location: ~location\r\n")) |
| 389 |
(dolist [cookie cookies] |
| 390 |
(push! r #"Set-cookie: ~cookie\r\n")) |
| 391 |
(dolist [p (slices rest 2)] |
| 392 |
(when (pair? (cdr p)) |
| 393 |
(let1 hdrname (if (keyword? (car p)) (keyword->string (car p)) (car p)) |
| 394 |
(push! r #"~|hdrname|: ~(cadr p)\r\n")))) |
| 395 |
(push! r "\r\n") |
| 396 |
(reverse r))) |
| 397 |
|
| 398 |
;;---------------------------------------------------------------- |
| 399 |
;; API: cgi-main proc &keyword on-error merge-cookies |
| 400 |
;; output-proc part-handlers |
| 401 |
(define (cgi-main proc :key |
| 402 |
(on-error cgi-default-error-proc) |
| 403 |
(output-proc cgi-default-output) |
| 404 |
(merge-cookies #f) |
| 405 |
(part-handlers '())) |
| 406 |
(set! (port-buffering (current-error-port)) :line) |
| 407 |
(guard (e [else (output-proc (on-error e))]) |
| 408 |
(let1 params (cgi-parse-parameters :merge-cookies merge-cookies |
| 409 |
:part-handlers part-handlers) |
| 410 |
(output-proc (proc params)))) |
| 411 |
;; remove any temporary files |
| 412 |
(for-each sys-unlink (cgi-temporary-files)) |
| 413 |
0) |
| 414 |
|
| 415 |
;; aux fns |
| 416 |
(define (cgi-default-error-proc e) |
| 417 |
`(,(cgi-header) |
| 418 |
,(html-doctype) |
| 419 |
,(html:html |
| 420 |
(html:head (html:title "Error")) |
| 421 |
(html:body (html:h1 "Error") |
| 422 |
(html:p (html-escape-string (condition-message e e))))))) |
| 423 |
|
| 424 |
(define (cgi-default-output tree) |
| 425 |
(if (ces-equivalent? (gauche-character-encoding) |
| 426 |
(cgi-output-character-encoding)) |
| 427 |
(write-tree tree) |
| 428 |
;; NB: we avoid using wrap-with-output-conversion, for we want |
| 429 |
;; to make sure output is flushed each time in case cgi-main |
| 430 |
;; is used in a persistent process. |
| 431 |
(with-output-conversion |
| 432 |
(current-output-port) |
| 433 |
(cut write-tree tree) |
| 434 |
:encoding (cgi-output-character-encoding)))) |