Add methods to retrieve cookies and to get request method, protocol and host.
This commit is contained in:
parent
8f14f2f2a3
commit
f8830635bf
|
@ -1,11 +1,17 @@
|
|||
#lang racket/base
|
||||
|
||||
(require
|
||||
web-server/http)
|
||||
web-server/http
|
||||
web-server/http/cookie-parse
|
||||
net/url-structs)
|
||||
|
||||
(provide
|
||||
make-weblet-parameter
|
||||
weblet-parameter-ref
|
||||
weblet-parameter-cookie-ref
|
||||
weblet-parameter-method
|
||||
weblet-parameter-protocol
|
||||
weblet-parameter-host
|
||||
; Fields of struct
|
||||
weblet-parameter-request
|
||||
weblet-parameter-path
|
||||
|
@ -20,7 +26,11 @@
|
|||
))
|
||||
|
||||
; Constructor
|
||||
(define (make-weblet-parameter #:request request #:path path #:match [match (make-hash)])
|
||||
(define (make-weblet-parameter
|
||||
#:request request
|
||||
#:path path
|
||||
#:match [match (make-hash)]
|
||||
#:cookies [cookies (make-hash)])
|
||||
(weblet-parameter
|
||||
request
|
||||
path
|
||||
|
@ -35,3 +45,37 @@
|
|||
; Priority: from request, from match
|
||||
(or (and req (bytes->string/utf-8 (binding:form-value req)))
|
||||
mat)))
|
||||
|
||||
; Get a cookie from the request. Return the cookie value as a string or #f if the asked value was not passed.
|
||||
; Key is a string
|
||||
(define (weblet-parameter-cookie-ref wp key (default #f))
|
||||
(define cookie
|
||||
(findf
|
||||
(lambda (c) (equal? key (client-cookie-name c)))
|
||||
(request-cookies (weblet-parameter-request wp))))
|
||||
(and cookie (client-cookie-value cookie)))
|
||||
|
||||
; Get the method used for the request.
|
||||
; Method is returned as a symbol
|
||||
(define (weblet-parameter-method wp)
|
||||
(define method (request-method (weblet-parameter-request wp)))
|
||||
(case method
|
||||
((#"GET") 'get)
|
||||
((#"HEAD") 'head)
|
||||
((#"POST") 'post)
|
||||
((#"PUT") 'put)
|
||||
((#"DELETE") 'delete)
|
||||
((#"TRACE") 'trace)
|
||||
((#"OPTIONS") 'options)
|
||||
((#"CONNECT") 'connect)
|
||||
((#"PATCH") 'patch)
|
||||
(else 'other-method)))
|
||||
|
||||
; Get the protocol used for the request, as a symbol
|
||||
(define (weblet-parameter-protocol wp)
|
||||
(define scheme (url-scheme (request-uri (weblet-parameter-request wp))))
|
||||
(and scheme (string->symbol scheme)))
|
||||
|
||||
; Get the host used for the request
|
||||
(define (weblet-parameter-host wp)
|
||||
(url-host (request-uri (weblet-parameter-request wp))))
|
||||
|
|
Loading…
Reference in New Issue