aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/query-parameters.scm
blob: 66edb64d963a87c82dd51046e2e5bb7ce3e67cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2016, 2017  Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2014  David Thompson <davet@gnu.org>
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-data-service web query-parameters)
  #:use-module (guix-data-service web util)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (srfi srfi-19)
  #:use-module (web request)
  #:use-module (web uri)
  #:export (parse-query-string

            <invalid-query-parameter>
            make-invalid-query-parameter
            invalid-query-parameter?
            invalid-query-parameter-value
            invalid-query-parameter-message

            any-invalid-query-parameters?

            parse-query-parameters
            guard-against-mutually-exclusive-query-parameters
            query-parameters->string

            parse-datetime
            parse-result-limit))

(define (parse-query-string query)
  "Parse and decode the URI query string QUERY and return an alist."
  (let lp ((lst (map uri-decode (string-split query (char-set #\& #\=)))))
    (match lst
      ((key value . rest)
       (cons (cons key value) (lp rest)))
      (("") '())
      (() '()))))

(define-immutable-record-type <invalid-query-parameter>
  (make-invalid-query-parameter value message)
  invalid-query-parameter?
  (value   invalid-query-parameter-value)
  (message invalid-query-parameter-message))

(define (guard-against-mutually-exclusive-query-parameters
         parsed-query-parameters
         mutually-exclusive-groups)
  (map (match-lambda
         ((name . value)
          (if (invalid-query-parameter? value)
              (cons name value)
              (or
               (any (lambda (group)
                       (if (memq name group)
                           (let ((other-names
                                  (filter (lambda (other-name)
                                            (and (not (eq? name other-name))
                                                 (memq other-name group)))
                                          (map car parsed-query-parameters))))
                             (if (not (null? other-names))
                                 (cons
                                  name
                                  (make-invalid-query-parameter
                                   value
                                   (string-append
                                    "cannot be specified along with "
                                    (string-join (map symbol->string
                                                      other-names)
                                                 ", "))))
                                 #f))
                           #f))
                     mutually-exclusive-groups)
               (cons name value)))))
       parsed-query-parameters))

(define (parse-query-parameters request
                                accepted-query-parameters)
  (define request-query-parameters
    (let ((query (uri-query (request-uri request))))
      (if (and query
               (not (string-null? query)))
          (map (match-lambda
                 ((name . value)
                  (cons (string->symbol name)
                        value)))
               (parse-query-string query))
          '())))

  (filter-map
   (match-lambda
     ((name processor)
      (match (assq name request-query-parameters)
        (#f #f)
        ((_ . "") #f)
        ((_ . value) (cons name
                           (processor value)))))

     ((name processor #:required)
      (match (assq name request-query-parameters)
        (#f (cons name
                  (make-invalid-query-parameter
                   #f "this value is required.")))
        ((_ . "") (cons name
                        (make-invalid-query-parameter
                         #f "this value is required.")))
        ((_ . value) (cons name
                           (processor value)))))

     ((name processor #:multi-value)
      (match (filter-map
              (match-lambda
                ((k . value)
                 (and
                  (eq? k name)
                  (match value
                    (#f #f)
                    ("" #f)
                    (value (processor value))))))
              request-query-parameters)
        (() #f)
        (x (cons name x))))

     ((name processor #:default default)
      (match (assq name request-query-parameters)
        (#f (cons name default))
        ((_ . "") (cons name default))
        ((_ . value) (cons name
                           (processor value))))))
   accepted-query-parameters))

(define (query-parameters->string query-parameters)
  (define (value->text value)
    (match value
      (#f "")
      ((? date? date)
       (date->string date "~1 ~3"))
      (other other)))

  (string-join
   (concatenate
    (map
     (match-lambda
       ((key . ($ <invalid-query-parameter>))
        '())
       ((key . value)
        (list (simple-format #f "~A=~A"
                             key (value->text value))))
       ((key values ...)
        (map (lambda (value)
               (simple-format #f "~A=~A"
                              key (value->text value)))
             values)))
     query-parameters))
   "&"))

(define (parse-datetime s)
  (catch
    'misc-error
    (lambda ()
      (string->date s "~Y-~m-~d ~H:~M:~S"))
    (lambda (key . args)
      (make-invalid-query-parameter s #f))))

(define (parse-result-limit s)
  (match (string->number s)
    (#f (make-invalid-query-parameter s #f))
    ((? number? num) num)))

(define (any-invalid-query-parameters? query-parameters)
  (->bool (any invalid-query-parameter? (map cdr query-parameters))))