cookie.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;; guile-webutils -- Web application utilities for Guile
  2. ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
  3. ;;;
  4. ;;; This program is free software: you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU General Public License
  6. ;;; as published by the Free Software Foundation, either version 3 of
  7. ;;; the License, or (at your option) any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with this program. If not, see
  16. ;;; <http://www.gnu.org/licenses/>.
  17. (define-module (webutils cookie)
  18. #:use-module (srfi srfi-1)
  19. #:use-module (srfi srfi-19)
  20. #:use-module (ice-9 control)
  21. #:use-module (ice-9 match)
  22. #:use-module (web http)
  23. #:use-module (webutils date)
  24. #:export (set-cookie delete-cookie))
  25. ;;; HTTP Cookie support
  26. ;;; Cookie, including utils used by Set-Cookie
  27. ;;; ==========================================
  28. ;; Valid characters for cookie values
  29. ;; (all printable ascii characters, excepting "," and ";")
  30. (define cookie-val-char-set
  31. (char-set-difference (char-set-delete char-set:ascii #\, #\;)
  32. char-set:iso-control))
  33. ;; Valid characters for cookie names
  34. ;; (same as cookie-val-char-set, minus "=")
  35. (define cookie-name-char-set
  36. (char-set-delete cookie-val-char-set #\=))
  37. ;; TODO: Not the best implementation. On known cookie-av pairs
  38. ;; (see rfc6265) we should do proper parsing.
  39. (define (parse-cookie cookie-text)
  40. (let ((parts (string-split cookie-text #\;)))
  41. (define (split-cookie-pair cookie-pair)
  42. (let* ((trimmed (string-trim cookie-pair))
  43. (delim (string-index trimmed #\=))
  44. (attrib (if delim
  45. (substring trimmed 0 delim)
  46. trimmed))
  47. (val (if delim
  48. (substring trimmed (+ delim 1))
  49. #t)))
  50. (cons attrib val)))
  51. (map split-cookie-pair parts)))
  52. (define (write-cookie cookie-alist port)
  53. (let ((cookie-str
  54. (string-join
  55. (map (match-lambda
  56. ;; If the value is a string, we join it with =
  57. ((name . (? string? val))
  58. (string-append name "=" val))
  59. ;; If the value is a date, we convert it to
  60. ;; an HTTP-style date string, then join with =
  61. ((name . (? date? val))
  62. (string-append name "=" (date->http-date-string val)))
  63. ;; If the value is #t, we just use the name from the pair
  64. ((name . #t)
  65. name))
  66. cookie-alist)
  67. "; ")))
  68. (display cookie-str port)))
  69. (define (valid-cookie-name? str)
  70. "Check if STR is a valid cookie name"
  71. (and (string? str)
  72. (string-every cookie-name-char-set str)))
  73. (define (valid-cookie-val? str)
  74. "Check if STR is a valid cookie value"
  75. (or (eq? str #t)
  76. (and (string? str)
  77. (string-every cookie-val-char-set str))
  78. (date? str)))
  79. (define (validate-cookie cookie-alist)
  80. (match cookie-alist
  81. ((((? valid-cookie-name? name) . (or #t (? valid-cookie-val? _))) ...)
  82. #t)
  83. (_ #f)))
  84. ;; ;; @@: We might never need to do this unless we're a client...
  85. ;; (define* (cookie-valid? cookie request #:optional (date (current-date)))
  86. ;; "See whether or not a cookie is valid (applies to domain/path/date)
  87. ;; Not the same as cookie-validator, this isn't about syntax."
  88. ;; 'TODO)
  89. (declare-header! "Cookie"
  90. parse-cookie validate-cookie write-cookie)
  91. ;;; Set-Cookie
  92. ;;; ==========
  93. (define (parse-set-cookie str)
  94. ;; We can utilize the parse-cookie code here.
  95. ;; The difference between Set-Cookie representation and Cookie
  96. ;; representation is that the Set-Cookie isn't *just* an alist,
  97. ;; because the first pair is special (the actual cookie pair name
  98. ;; and value) whereas the rest are just attributes (properties
  99. ;; about the cookie that the browser looks at)
  100. (match (parse-cookie str)
  101. (((name . val) attrs ...)
  102. (list name val attrs))))
  103. (define (validate-set-cookie obj)
  104. (match obj
  105. ;; See comment in parse-set-cookie.
  106. ((name val (attrs ...))
  107. (validate-cookie
  108. (cons (cons name val)
  109. attrs)))
  110. (_ #f)))
  111. (define (write-set-cookie obj port)
  112. (match obj
  113. ;; See comment in parse-set-cookie.
  114. ((name val (attrs ...))
  115. (write-cookie
  116. (cons (cons name val)
  117. attrs)
  118. port))))
  119. (declare-header! "Set-Cookie"
  120. parse-set-cookie validate-set-cookie write-set-cookie
  121. #:multiple? #t)
  122. ;;; Utility for users to construct Set-Cookie headers easily.
  123. (define* (set-cookie name #:optional (val "")
  124. #:key expires max-age domain
  125. path secure http-only
  126. (extensions '())) ; extensions is its own alist
  127. "Produce a Set-Cookie header.
  128. Includes the 'set-cookie symbol, so if you don't need that, just take the cdr."
  129. (define (maybe-append name val)
  130. (lambda (prev)
  131. (if val
  132. (cons (cons name val)
  133. prev)
  134. prev)))
  135. (define basic-prop-alist
  136. ((compose
  137. (maybe-append "Expires" expires)
  138. (maybe-append "Max-Age" max-age)
  139. (maybe-append "Domain" domain)
  140. (maybe-append "Path" path)
  141. (maybe-append "Secure" secure)
  142. (maybe-append "HttpOnly" http-only))
  143. '()))
  144. (define prop-alist
  145. (append basic-prop-alist
  146. extensions))
  147. (cons 'set-cookie (list name val prop-alist)))
  148. (define %the-epoch
  149. (time-monotonic->date (make-time 'time-monotonic 0 0) 0))
  150. (define (delete-cookie name)
  151. "Inform the client that we would like to delete the cookie by setting
  152. the Expires field in the past."
  153. (set-cookie name #:expires %the-epoch))