aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/query-parameters.scm
blob: 615cd8108f249be7ab6e8ad7a6914050b20cd43a (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
;;; 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 (guix-data-service model build-status)
  #: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-checkbox-value
            parse-number
            parse-result-limit
            parse-system
            parse-target
            parse-build-status
            parse-derivation-build-status

            valid-targets->options))

(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 #:multi-value #:default default)
      (match (filter-map
              (match-lambda
                ((k . value)
                 (and
                  (eq? k name)
                  (match value
                    (#f #f)
                    ("" #f)
                    (value (processor value))))))
              request-query-parameters)
        (() (cons name default))
        (x (cons name x))))

     ((name processor #:no-default-when fields #:default default)
      (let ((use-default?
             (every (lambda (field)
                      (not (memq field fields)))
                    (map car request-query-parameters))))
        (match (assq name request-query-parameters)
          (#f (if use-default?
                  (cons name default)
                  #f))
          ((_ . "") (if use-default?
                        (cons name default)
                        #f))
          ((_ . value) (cons name
                             (processor value))))))

     ((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 "")
      (#t "on")
      ((? date? date)
       (date->string date "~1 ~3"))
      ;; TODO Hardcoding none here is a bit of a hack, but it does make the
      ;; Next page link work for revision derivations.
      ("" "none")
      (other other)))

  (string-join
   (concatenate
    (map
     (match-lambda
       ((key . ($ <invalid-query-parameter>))
        '())
       ((key . value)
        (if (list? value)
            (map (lambda (value)
                   (simple-format #f "~A=~A"
                                  key (value->text value)))
                 value)
            (list (simple-format
                   #f "~A=~A"
                   key (value->text value))))))
     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
       "Unable to parse date, please use YYYY-MM-DD HH:MM:SS as the format."))))

(define (parse-checkbox-value s)
  (string=? s "on"))

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

(define parse-result-limit parse-number)

(define parse-system identity)

(define (parse-target target)
  (if (string=? target "none")
      ""
      target))

(define (parse-build-status status)
  (if (member status build-status-strings)
      status
      (make-invalid-query-parameter
       status
       (string-append "unknown build status: "
                      status))))

(define (parse-derivation-build-status status)
  (define options
    '("none" "working" "failing" "unknown"))

  (if (member status options)
      (if (string=? status "none")
          #f
          status)
      (make-invalid-query-parameter
       status
       (string-append "unknown derivation build status: "
                      status))))

(define (valid-targets->options targets)
  `(("(no target)" . "none")
    ,@(map (lambda (target)
             (cons target target))
           targets)))

(define (any-invalid-query-parameters? query-parameters)
  (->bool (any (lambda (val)
                 (if (list? val)
                     (any invalid-query-parameter? val)
                     (invalid-query-parameter? val)))
               (map cdr query-parameters))))