chickadee » intarweb » get-param

header-contents NAME HEADERSprocedure
get-value VECTORprocedure
get-params VECTORprocedure
get-param PARAM VECTOR #!optional DEFAULTprocedure

Procedures such as header-values are just shortcuts; these are the underlying procedures to query the raw contents of a header.

Header contents are lists of 2-element vectors; the first value containing the value for the header and the second value containing an alist with "parameters" for that header value. Parameters are attribute/value pairs that define further specialization of a header's value. For example, the accept header consists of a list of mime-types, which optionally can have a quality parameter that defines the preference for that mime-type. All parameter names are downcased symbols, just like header names.

Here's a few examples on how to retrieve info from headers:

;; This would be returned by a server and retrieved via (response-headers r):
(define example-headers
  (headers '((accept #(text/html ((q . 0.1)))
                     #(text/xml ((q . 0.5)))
             (allow HEAD GET)
	     (content-type #(text/html ((charset . utf-8))))
             (max-forwards 2))))

;;; Basic procedures
(define c (header-contents 'accept example-headers))
c ; => (#(text/html ((q . 0.5))) #(text/xml ((q . 0.1))) #(text/plain ()))

(get-value (car c))
; => text/html
(get-params (car c))
; => ((q . 0.5))
(get-param 'q (car c))
; => 0.5

;;; Simplified helpers
(header-values 'accept example-headers)
; => (text/html text/xml text/plain)
(header-values 'max-forwards example-headers)
; => (2)
(header-values 'nonexistent-header example-headers)
; => ()

;; This assumes there's only one value (returns the first)
(header-value 'max-forwards example-headers)
; => 2
(header-value 'nonexistent-header example-headers)
; => #f
(header-value 'nonexistent-header example-headers 'not-here)
; => not-here
;; Tricky:
(header-value 'accept example-headers)
; => text/html

;; This is tricky: this just returns the first, which is not the preferred
(header-params 'accept example-headers)
; => ((q . 0.1))
;; Quick access
(header-param 'charset 'content-type example-headers)
; => utf-8