response.scm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ;;; HTTP response objects
  2. ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (web response)
  19. #:use-module (rnrs bytevectors)
  20. #:use-module (ice-9 binary-ports)
  21. #:use-module (ice-9 rdelim)
  22. #:use-module (srfi srfi-9)
  23. #:use-module (web http)
  24. #:export (response?
  25. response-version
  26. response-code
  27. response-reason-phrase
  28. response-headers
  29. response-port
  30. read-response
  31. build-response
  32. adapt-response-version
  33. write-response
  34. read-response-body
  35. write-response-body
  36. ;; General headers
  37. ;;
  38. response-cache-control
  39. response-connection
  40. response-date
  41. response-pragma
  42. response-trailer
  43. response-transfer-encoding
  44. response-upgrade
  45. response-via
  46. response-warning
  47. ;; Entity headers
  48. ;;
  49. response-allow
  50. response-content-encoding
  51. response-content-language
  52. response-content-length
  53. response-content-location
  54. response-content-md5
  55. response-content-range
  56. response-content-type
  57. response-expires
  58. response-last-modified
  59. ;; Response headers
  60. ;;
  61. response-accept-ranges
  62. response-age
  63. response-etag
  64. response-location
  65. response-proxy-authenticate
  66. response-retry-after
  67. response-server
  68. response-vary
  69. response-www-authenticate))
  70. (define-record-type <response>
  71. (make-response version code reason-phrase headers port)
  72. response?
  73. (version response-version)
  74. (code response-code)
  75. (reason-phrase %response-reason-phrase)
  76. (headers response-headers)
  77. (port response-port))
  78. (define (bad-response message . args)
  79. (throw 'bad-response message args))
  80. (define (non-negative-integer? n)
  81. (and (number? n) (>= n 0) (exact? n) (integer? n)))
  82. (define (validate-headers headers)
  83. (if (pair? headers)
  84. (let ((h (car headers)))
  85. (if (pair? h)
  86. (let ((k (car h)) (v (cdr h)))
  87. (if (valid-header? k v)
  88. (validate-headers (cdr headers))
  89. (bad-response "Bad value for header ~a: ~s" k v)))
  90. (bad-response "Header not a pair: ~a" h)))
  91. (if (not (null? headers))
  92. (bad-response "Headers not a list: ~a" headers))))
  93. (define* (build-response #:key (version '(1 . 1)) (code 200) reason-phrase
  94. (headers '()) port (validate-headers? #t))
  95. "Construct an HTTP response object. If @var{validate-headers?} is true,
  96. the headers are each run through their respective validators."
  97. (cond
  98. ((not (and (pair? version)
  99. (non-negative-integer? (car version))
  100. (non-negative-integer? (cdr version))))
  101. (bad-response "Bad version: ~a" version))
  102. ((not (and (non-negative-integer? code) (< code 600)))
  103. (bad-response "Bad code: ~a" code))
  104. ((and reason-phrase (not (string? reason-phrase)))
  105. (bad-response "Bad reason phrase" reason-phrase))
  106. (else
  107. (if validate-headers?
  108. (validate-headers headers))))
  109. (make-response version code reason-phrase headers port))
  110. (define *reason-phrases*
  111. '((100 . "Continue")
  112. (101 . "Switching Protocols")
  113. (200 . "OK")
  114. (201 . "Created")
  115. (202 . "Accepted")
  116. (203 . "Non-Authoritative Information")
  117. (204 . "No Content")
  118. (205 . "Reset Content")
  119. (206 . "Partial Content")
  120. (300 . "Multiple Choices")
  121. (301 . "Moved Permanently")
  122. (302 . "Found")
  123. (303 . "See Other")
  124. (304 . "Not Modified")
  125. (305 . "Use Proxy")
  126. (307 . "Temporary Redirect")
  127. (400 . "Bad Request")
  128. (401 . "Unauthorized")
  129. (402 . "Payment Required")
  130. (403 . "Forbidden")
  131. (404 . "Not Found")
  132. (405 . "Method Not Allowed")
  133. (406 . "Not Acceptable")
  134. (407 . "Proxy Authentication Required")
  135. (408 . "Request Timeout")
  136. (409 . "Conflict")
  137. (410 . "Gone")
  138. (411 . "Length Required")
  139. (412 . "Precondition Failed")
  140. (413 . "Request Entity Too Large")
  141. (414 . "Request-URI Too Long")
  142. (415 . "Unsupported Media Type")
  143. (416 . "Requested Range Not Satisfiable")
  144. (417 . "Expectation Failed")
  145. (500 . "Internal Server Error")
  146. (501 . "Not Implemented")
  147. (502 . "Bad Gateway")
  148. (503 . "Service Unavailable")
  149. (504 . "Gateway Timeout")
  150. (505 . "HTTP Version Not Supported")))
  151. (define (code->reason-phrase code)
  152. (or (assv-ref *reason-phrases* code)
  153. "(Unknown)"))
  154. (define (response-reason-phrase response)
  155. "Return the reason phrase given in @var{response}, or the standard
  156. reason phrase for the response's code."
  157. (or (%response-reason-phrase response)
  158. (code->reason-phrase (response-code response))))
  159. (define (read-response port)
  160. "Read an HTTP response from @var{port}.
  161. As a side effect, sets the encoding on @var{port} to
  162. ISO-8859-1 (latin-1), so that reading one character reads one byte. See
  163. the discussion of character sets in \"HTTP Responses\" in the manual,
  164. for more information."
  165. (set-port-encoding! port "ISO-8859-1")
  166. (call-with-values (lambda () (read-response-line port))
  167. (lambda (version code reason-phrase)
  168. (make-response version code reason-phrase (read-headers port) port))))
  169. (define (adapt-response-version response version)
  170. "Adapt the given response to a different HTTP version. Returns a new
  171. HTTP response.
  172. The idea is that many applications might just build a response for the
  173. default HTTP version, and this method could handle a number of
  174. programmatic transformations to respond to older HTTP versions (0.9 and
  175. 1.0). But currently this function is a bit heavy-handed, just updating
  176. the version field."
  177. (build-response #:code (response-code response)
  178. #:version version
  179. #:headers (response-headers response)
  180. #:port (response-port response)))
  181. (define (write-response r port)
  182. "Write the given HTTP response to @var{port}.
  183. Returns a new response, whose @code{response-port} will continue writing
  184. on @var{port}, perhaps using some transfer encoding."
  185. (write-response-line (response-version r) (response-code r)
  186. (response-reason-phrase r) port)
  187. (write-headers (response-headers r) port)
  188. (display "\r\n" port)
  189. (if (eq? port (response-port r))
  190. r
  191. (make-response (response-version r) (response-code r)
  192. (response-reason-phrase r) (response-headers r) port)))
  193. (define (read-response-body r)
  194. "Reads the response body from @var{r}, as a bytevector. Returns
  195. @code{#f} if there was no response body."
  196. (let ((nbytes (response-content-length r)))
  197. (and nbytes
  198. (let ((bv (get-bytevector-n (response-port r) nbytes)))
  199. (if (= (bytevector-length bv) nbytes)
  200. bv
  201. (bad-response "EOF while reading response body: ~a bytes of ~a"
  202. (bytevector-length bv) nbytes))))))
  203. (define (write-response-body r bv)
  204. "Write @var{body}, a bytevector, to the port corresponding to the HTTP
  205. response @var{r}."
  206. (put-bytevector (response-port r) bv))
  207. (define-syntax define-response-accessor
  208. (lambda (x)
  209. (syntax-case x ()
  210. ((_ field)
  211. #'(define-response-accessor field #f))
  212. ((_ field def) (identifier? #'field)
  213. #`(define* (#,(datum->syntax
  214. #'field
  215. (symbol-append 'response- (syntax->datum #'field)))
  216. response
  217. #:optional (default def))
  218. (cond
  219. ((assq 'field (response-headers response)) => cdr)
  220. (else default)))))))
  221. ;; General headers
  222. ;;
  223. (define-response-accessor cache-control '())
  224. (define-response-accessor connection '())
  225. (define-response-accessor date #f)
  226. (define-response-accessor pragma '())
  227. (define-response-accessor trailer '())
  228. (define-response-accessor transfer-encoding '())
  229. (define-response-accessor upgrade '())
  230. (define-response-accessor via '())
  231. (define-response-accessor warning '())
  232. ;; Entity headers
  233. ;;
  234. (define-response-accessor allow '())
  235. (define-response-accessor content-encoding '())
  236. (define-response-accessor content-language '())
  237. (define-response-accessor content-length #f)
  238. (define-response-accessor content-location #f)
  239. (define-response-accessor content-md5 #f)
  240. (define-response-accessor content-range #f)
  241. (define-response-accessor content-type #f)
  242. (define-response-accessor expires #f)
  243. (define-response-accessor last-modified #f)
  244. ;; Response headers
  245. ;;
  246. (define-response-accessor accept-ranges #f)
  247. (define-response-accessor age #f)
  248. (define-response-accessor etag #f)
  249. (define-response-accessor location #f)
  250. (define-response-accessor proxy-authenticate #f)
  251. (define-response-accessor retry-after #f)
  252. (define-response-accessor server #f)
  253. (define-response-accessor vary '())
  254. (define-response-accessor www-authenticate #f)