File: bindings.rkt

package info (click to toggle)
racket 6.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,344 kB
  • ctags: 39,484
  • sloc: ansic: 277,847; sh: 33,512; asm: 13,558; lisp: 7,113; cpp: 2,872; makefile: 2,421; pascal: 2,262; exp: 499; python: 274; xml: 11
file content (52 lines) | stat: -rw-r--r-- 2,017 bytes parent folder | download | duplicates (7)
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
#lang racket/base
(require racket/list
         racket/contract
         racket/match
         web-server/private/util
         web-server/http/request-structs)

(define (request-headers request)
  (map (match-lambda
         [(struct header (field value))
          (cons (lowercase-symbol! (bytes->string/utf-8 field))
                (bytes->string/utf-8 value))])
       (request-headers/raw request)))
(define (request-bindings request)
  (map (match-lambda
         [(struct binding:form (id value))
          (cons (lowercase-symbol! (bytes->string/utf-8 id))
                (bytes->string/utf-8 value))]
         [(struct binding:file (id fname headers value))
          (cons (lowercase-symbol! (bytes->string/utf-8 id))
                value)])
       (request-bindings/raw request)))

; extract-binding/single : sym (listof (cons str str)) -> str
(define (extract-binding/single name bindings)
  (define lst (extract-bindings name bindings))
  (cond
    [(empty? lst)
     (error 'extract-binding/single "~e not found in ~e" name bindings)]
    [(empty? (rest lst))
     (first lst)]
    [else 
     (error 'extract-binding/single "~e occurs multiple times in ~e" name bindings)]))

; extract-bindings : sym (listof (cons str str)) -> (listof str)
(define (extract-bindings name bindings)
  (map cdr (filter (lambda (x) (equal? name (car x))) bindings)))

; exists-binding? : sym (listof (cons sym str)) -> bool
; for checkboxes
(define (exists-binding? name bindings)
  (if (assq name bindings)
      #t
      #f))

(provide/contract
 [extract-binding/single (symbol? (listof (cons/c symbol? any/c)) . -> . any/c)]
 [extract-bindings (symbol? (listof (cons/c symbol? any/c)) . -> . (listof any/c))]
 [exists-binding? (symbol? (listof (cons/c symbol? any/c)) . -> . boolean?)]   
 [request-bindings (request? . -> . (listof (or/c (cons/c symbol? string?)
                                                  (cons/c symbol? bytes?))))]
 [request-headers (request? . -> . (listof (cons/c symbol? string?)))])