1
;; cgi-get.jl -- return the parameters from a CGI GET request
2
;; Copyright (C) 1999 John Harper <john@dcs.warwick.ac.uk>
4
;; $Id: cgi-get.jl,v 1.9 2001/09/15 21:43:12 jsh Exp $
6
;; This file is part of librep.
8
;; librep is free software; you can redistribute it and/or modify it
9
;; under the terms of the GNU General Public License as published by
10
;; the Free Software Foundation; either version 2, or (at your option)
13
;; librep is distributed in the hope that it will be useful, but
14
;; WITHOUT ANY WARRANTY; without even the implied warranty of
15
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
;; GNU General Public License for more details.
18
;; You should have received a copy of the GNU General Public License
19
;; along with librep; see the file COPYING. If not, write to
20
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
(declare (unsafe-for-call/cc))
24
(define-structure rep.www.cgi-get
26
(export cgi-get-params)
33
(define-structure-alias cgi-get rep.www.cgi-get)
35
(define unquote-plus-map (let ((map (make-string (1+ ?+)))
43
(defun cgi-get-params (#!optional query-string)
45
(setq query-string (getenv "QUERY_STRING")))
50
(while (string-looking-at "([^=]+)=([^&]*)(&|$)" query-string point)
51
(setq point (match-end))
54
(substring query-string (match-start 1) (match-end 1)))))
56
(substring query-string (match-start 2) (match-end 2))))
57
(when (string= value "")
59
(setq params (cons (cons name value) params)))
62
(defsubst hexdigit (char)
63
(if (and (>= char ?0) (<= char ?9))
65
(+ (- (char-upcase char) ?A) 10)))
67
(defun unquote (string)
71
(setq string (translate-string string unquote-plus-map))
72
(while (string-match "%.." string point)
73
(setq frags (cons (substring string point (match-start)) frags))
74
(setq point (match-end))
75
(setq frags (cons (+ (* (hexdigit (aref string (- point 2))) 16)
76
(hexdigit (aref string (1- point)))) frags)))
79
(setq frags (cons (substring string point) frags))
80
(apply concat (nreverse frags)))))
86
(test (equal (cgi-get-params "")
88
(test (equal (cgi-get-params "foo=bar")
90
(test (equal (cgi-get-params "foo=bar&baz=quux")
91
'((foo . "bar") (baz . "quux"))))
92
(test (equal (cgi-get-params "foo=&baz=quux")
93
'((foo . ()) (baz . "quux"))))
94
(test (equal (cgi-get-params "foo=%3A%2F%3D")
96
(test (equal (cgi-get-params "foo=+bar+")
100
(define-self-test 'rep.www.cgi-get self-test))