File: request-structs.rkt

package info (click to toggle)
racket 7.9%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 178,684 kB
  • sloc: ansic: 282,112; lisp: 234,887; pascal: 70,954; sh: 27,112; asm: 16,268; makefile: 4,613; cpp: 2,715; ada: 1,681; javascript: 1,244; cs: 879; exp: 499; csh: 422; python: 274; xml: 106; perl: 104
file content (351 lines) | stat: -rw-r--r-- 13,047 bytes parent folder | download
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#lang racket/base

(require racket/contract
         racket/serialize
         racket/match
         racket/port
         racket/promise
         net/url-structs
         web-server/private/util
         (for-syntax racket/base
                     racket/struct-info
                     racket/match
                     syntax/transformer))

;; Most serializable structs are protected
;; with struct-guard/c rather than contract-out.

(provide
 ;; header
 (struct-out header)
 (contract-out
  [headers-assq
   (-> bytes? (listof header?) (or/c #f header?))]
  [headers-assq*
   (-> bytes? (listof header?) (or/c #f header?))])
 ;; ----------------------------------------
 ;; request
 (contract-out
  [struct request ([method bytes?]
                   [uri url?]
                   [headers/raw (listof header?)]
                   [bindings/raw-promise (promise/c (listof binding?))]
                   [post-data/raw (or/c #f bytes?)]
                   [host-ip string?]
                   [host-port number?]
                   [client-ip string?])]
  [request-bindings/raw
   (-> request? (listof binding?))])
 ;; ----------------------------------------
 ;; binding
 (struct-out binding)
 (struct-out binding:form)
 binding:file/port?
 (contract-out
  [struct (binding:file binding)
    ;; enforce the `binding:file-content` contract to be extra careful
    ([id bytes?]
     [filename bytes?]
     [headers (listof header?)]
     [content bytes?])]
  [binding:file/port-in
   (-> binding:file/port? input-port?)]
  [make-binding:file/port
   (-> bytes? bytes? (listof header?) input-port? binding:file/port?)]
  [bindings-assq
   (-> bytes? (listof binding?) (or/c #f binding?))]
  [bindings-assq-all
   (-> bytes? (listof binding?) (listof binding?))]))

;                                        
;   ;;                      ;;           
;   ;;                      ;;           
;   ;;;;;   ;;    ;;     ;;;;;  ;;   ;; ;
;   ;;  ;  ;  ;  ;  ;   ;   ;; ;  ;  ;;; 
;   ;;  ;; ;  ;     ;;  ;   ;; ;  ;  ;;  
;   ;;  ;;;;;;;;  ;;;; ;;   ;;;;;;;; ;;  
;   ;;  ;; ;     ;  ;;  ;   ;; ;     ;;  
;   ;;  ;; ;    ;;  ;;  ;   ;; ;     ;;  
;   ;;  ;;  ;;;  ;;; ;   ;;; ;  ;;;  ;;  
;                                        

(define-serializable-struct header
  (field value)
  #:guard (struct-guard/c bytes? bytes?)
  #:transparent)

(define (headers-assq* f hs)
  (for/first ([h (in-list hs)]
              #:when (bytes-ci=? f (header-field h)))
    h))

(define (headers-assq f hs)
  (for/first ([h (in-list hs)]
              #:when (bytes=? f (header-field h)))
    h))

;                                          
;                                       ;; 
;                                       ;; 
;   ;; ;  ;;    ;;; ; ;; ;;   ;;    ;; ;;;;
;   ;;;  ;  ;  ;   ;; ;; ;;  ;  ; ;;  ; ;; 
;   ;;   ;  ;  ;   ;; ;; ;;  ;  ;  ;    ;; 
;   ;;  ;;;;;;;;   ;; ;; ;; ;;;;;;  ;;  ;; 
;   ;;   ;     ;   ;; ;; ;;  ;        ;;;; 
;   ;;   ;     ;   ;;  ; ;;  ;    ;   ;  ; 
;   ;;    ;;;   ;;;;;  ;;;;   ;;;  ;;;   ;;
;                  ;;                      
;                  ;;                      
;                  ;;                      
;                                          

(define-struct request
  (method uri headers/raw bindings/raw-promise post-data/raw
          host-ip host-port client-ip)
  #:transparent)

(define (request-bindings/raw r)
  (force (request-bindings/raw-promise r)))


;                                           
;                                           
;   ;;     ;;            ;; ;;              
;   ;;                   ;;                 
;   ;;;;   ;; ; ;;;   ;;;;; ;; ; ;;;   ;;;;;
;   ;;  ;  ;; ;;  ;  ;   ;; ;; ;;  ;  ;  ;  
;   ;;  ;  ;; ;;  ;; ;   ;; ;; ;;  ;;;;  ;; 
;   ;;  ;; ;; ;;  ;;;;   ;; ;; ;;  ;; ;  ;  
;   ;;  ;  ;; ;;  ;; ;   ;; ;; ;;  ;;  ;;   
;   ;;  ;  ;; ;;  ;; ;   ;; ;; ;;  ;;;;     
;   ; ;;   ;; ;;  ;;  ;;; ; ;; ;;  ;; ;;;;; 
;                                     ;   ;;
;                                    ;;   ; 
;                                      ;;;  
;                                           

(define-serializable-struct binding
  (id)
  #:guard (struct-guard/c bytes?)
  #:transparent)

(define (bindings-assq ti bs)
  (for/first ([b (in-list bs)]
              #:when (equal? ti (binding-id b)))
    b))

(define (bindings-assq-all ti bs)
  (for/list ([b (in-list bs)]
             #:when (equal? ti (binding-id b)))
    b))

(define-serializable-struct (binding:form binding)
  (value)
  #:guard (struct-guard/c bytes? bytes?)
  #:transparent)

;; The implementation of `binding:file` is complicated
;; to support the change in web-server-lib v1.6 to
;; offload large files to disk while preserving backwards-compatability.
;; In particular, `binding:file` was originally declared as
;; `#:transparent` and serializable (via `define-serializable-struct`).
;; Changing its nature as a struct would break `typed/web-server/http`.
;;
;; Our basic approach (suggested by Sam T-H:
;; <https://github.com/racket/web-server/pull/77#discussion_r338045221>)
;; is to construct new-style instances using a function `make-binding:file/port`.
;; This function returns an impersonator, and the wrapper for
;; for `binding:file-contents` access reads the port to a byte-string
;; and memoized it.
;;
;; The parent `binding:file` type gets particularly complex
;; because we need to keep the raw constructors and the mutator private
;; to this module, even in the face of reflective operations.
;; The details are explained in comments below.
;;
;; Another design consideration is the the port-based API is stateful,
;; whereas the bytes-based API is purely functional.
;; If a user program uses the port-based API, it must take responsibility for
;; its stateful nature. See docs for more details.


;; First, we create the parent type.
;; Doing this inside a `let` turns out to be the easiest way to get the
;; right names visible in the rest of this module,
;; and hapily also avoids defining names we don't want to accidently use.
(define-values [struct:binding:file/raw
                binding:file?
                binding:file-filename
                binding:file-headers
                binding:file-content
                set-binding:file-content!]
  (let ()
    (struct binding:file binding
      (filename headers [content #:mutable])
      #:property prop:serializable
      (make-serialize-info
       (match-lambda
         [(binding:file id filename headers content)
          (vector id filename headers content)])
       #'deserialize-info:binding:file-v0
       #f
       (or (current-load-relative-directory) (current-directory)))
      #:transparent)
    (values struct:binding:file
            binding:file?
            binding:file-filename
            binding:file-headers
            binding:file-content
            set-binding:file-content!)))

;; We chaperone `struct:binding:file/raw` to define the provided struct type.
(define struct:binding:file
  (chaperone-struct-type
   struct:binding:file/raw
   ;; Wrap struct-info to block use of the mutator:
   (λ (name init-field-cnt auto-field-cnt accessor-proc mutator-proc
            immutable-k-list super-type skipped?)
     (values name init-field-cnt auto-field-cnt accessor-proc
             (chaperone-procedure
              mutator-proc
              (λ (instance index new-value)
                (raise-arguments-error
                 'binding:file
                 "mutation not allowed\n illegal attempt to use mutator from struct-info"
                 "given" instance
                 "index" index
                 "value" new-value)))
             immutable-k-list super-type skipped?))
   ;; Wrap the constructor to check field contracts,
   ;; plus chaperone the resulting instance to block `set-binding:file-content!`
   ;; and make `struct-info` return this chaperoned struct type.
   (λ (ctor)
     (chaperone-procedure
      ctor
      (λ (id filename headers content)
        (define (bad-arg! pos [expected "bytes?"])
          (raise-argument-error
           'make-binding:file expected pos id filename headers content))
        (unless (bytes? id)
          (bad-arg! 0))
        (unless (bytes? filename)
          (bad-arg! 1))
        (unless (and (list? headers) (andmap header? headers))
          (bad-arg! 2 "(listof header?)"))
        (unless (bytes? content)
          (bad-arg! 3))
        (values (λ (instance)
                  (chaperone-struct
                   instance
                   struct:binding:file/raw
                   struct-info
                   (λ (type skipped?)
                     (values struct:binding:file skipped?))
                   set-binding:file-content!
                   (λ (self new-val)
                     (raise-arguments-error
                      'set-binding:file-content!
                      "mutation not allowed\n illegal attempt to mutate an immediate instance"
                      "instance" self
                      "value" new-val))))
                id filename headers content))))
   ;; It looks like we can't protect reflective information
   ;; for instances of subtypes, so we'll just ban instantiating subtypes for now.
   (λ (id filename headers content subtype-name)
     (raise-arguments-error
      'binging:file
      (string-append "attempt to construct an instance of a subtype;\n"
                     " subtyping binging:file is not allowed")
      "subtype name" subtype-name
      "id" id
      "filename" filename
      "headers" headers
      "content" content))))

;; Define a constructor using the chaperoned `struct:binding:file`:
(define make-binding:file
  ;; workaround for https://github.com/racket/racket/issues/2905
  ;; and https://github.com/racket/racket/issues/2924
  (if (eqv? 1 (procedure-arity struct-type-make-constructor))
      (struct-type-make-constructor struct:binding:file)
      (struct-type-make-constructor struct:binding:file
                                    'make-binding:file)))

;; Use the checked constructor for the deserialize info:
(define deserialize-info:binding:file-v0
  (make-deserialize-info
   make-binding:file
   (λ ()
     (error 'binding:file "cycles not allowed"))))

(module+ deserialize-info
  (provide deserialize-info:binding:file-v0))

;; At last, define `binding:file` to work as both static
;; struct information and the checked constructor:
(define-syntax binding:file
  (let ([info-list
         (list #'struct:binding:file
               #'make-binding:file
               #'binding:file?
               (list #'binding:file-content
                     #'binding:file-headers
                     #'binding:file-filename
                     #'binding-id)
               '(#f #f #f #f)
               #'binding)])
    (struct info (transformer)
      #:super struct:struct-info
      #:property prop:procedure (struct-field-index transformer)
      #:property prop:struct-auto-info (λ (__) '(() ())))
    (info (λ () info-list)
          (make-variable-like-transformer #'make-binding:file))))

;; The definition of `binding:file/port` is relatively normal,
;; except that we don't provide the standard constructor.

(struct binding:file/port (in sema)
  #:super struct:binding:file/raw ;; use raw to avoid subtyping & mutation bans
  #:constructor-name binding:file/port/raw)

(define make-binding:file/port
  (let ()
    (define-values [imp-prop:raw has-raw? get-raw]
      ;; store the instance w/o impersonator
      (make-impersonator-property 'impersonator-prop:raw-this))
    (define (make-binding:file/port id filename headers in)
      (define raw
        (binding:file/port/raw id filename headers #f in (make-semaphore 1)))
      (impersonate-struct
       (chaperone-struct raw
                         struct:binding:file/port
                         struct-info
                         (λ (type skipped?)
                           ;; Expose only the chaperoned struct:binding:file
                           ;; (works because binding:file/port is opaque).
                           (values struct:binding:file skipped?)))
       struct:binding:file/port
       binding:file-content
       (λ (this content)
         ;; avoid allocating any closures
         (or content
             (call-with-semaphore (binding:file/port-sema this)
               (λ (this)
                 (cond
                   ;; Could have been used in some other thread.
                   ;; Need to check w/o impersonator, or we diverge.
                   [(binding:file-content (get-raw this))]
                   [else
                    ;; will raise an exn if the port was closed
                    ;; (perhaps concurrently, by user code)
                    (define content
                      (port->bytes (binding:file/port-in this)))
                    (set-binding:file-content! this content)
                    (close-input-port in)
                    content]))
               #f
               this)))
       imp-prop:raw
       raw))
    make-binding:file/port))