response.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. ;;; HTTP response objects
  2. ;; Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 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 textual-ports)
  22. #:use-module (ice-9 rdelim)
  23. #:use-module (ice-9 match)
  24. #:use-module (srfi srfi-9)
  25. #:use-module (web http)
  26. #:export (response?
  27. response-version
  28. response-code
  29. response-reason-phrase
  30. response-headers
  31. response-port
  32. read-response
  33. build-response
  34. adapt-response-version
  35. write-response
  36. response-must-not-include-body?
  37. response-body-port
  38. read-response-body
  39. write-response-body
  40. ;; General headers
  41. ;;
  42. response-cache-control
  43. response-connection
  44. response-date
  45. response-pragma
  46. response-trailer
  47. response-transfer-encoding
  48. response-upgrade
  49. response-via
  50. response-warning
  51. ;; Entity headers
  52. ;;
  53. response-allow
  54. response-content-encoding
  55. response-content-language
  56. response-content-length
  57. response-content-location
  58. response-content-md5
  59. response-content-range
  60. response-content-type
  61. text-content-type?
  62. response-expires
  63. response-last-modified
  64. ;; Response headers
  65. ;;
  66. response-accept-ranges
  67. response-age
  68. response-etag
  69. response-location
  70. response-proxy-authenticate
  71. response-retry-after
  72. response-server
  73. response-vary
  74. response-www-authenticate))
  75. (define-record-type <response>
  76. (make-response version code reason-phrase headers port)
  77. response?
  78. (version response-version)
  79. (code response-code)
  80. (reason-phrase %response-reason-phrase)
  81. (headers response-headers)
  82. (port response-port))
  83. (define (bad-response message . args)
  84. (throw 'bad-response message args))
  85. (define (non-negative-integer? n)
  86. (and (number? n) (>= n 0) (exact? n) (integer? n)))
  87. (define (validate-headers headers)
  88. (if (pair? headers)
  89. (let ((h (car headers)))
  90. (if (pair? h)
  91. (let ((k (car h)) (v (cdr h)))
  92. (if (valid-header? k v)
  93. (validate-headers (cdr headers))
  94. (bad-response "Bad value for header ~a: ~s" k v)))
  95. (bad-response "Header not a pair: ~a" h)))
  96. (if (not (null? headers))
  97. (bad-response "Headers not a list: ~a" headers))))
  98. (define* (build-response #:key (version '(1 . 1)) (code 200) reason-phrase
  99. (headers '()) port (validate-headers? #t))
  100. "Construct an HTTP response object. If VALIDATE-HEADERS? is true,
  101. the headers are each run through their respective validators."
  102. (cond
  103. ((not (and (pair? version)
  104. (non-negative-integer? (car version))
  105. (non-negative-integer? (cdr version))))
  106. (bad-response "Bad version: ~a" version))
  107. ((not (and (non-negative-integer? code) (< code 600)))
  108. (bad-response "Bad code: ~a" code))
  109. ((and reason-phrase (not (string? reason-phrase)))
  110. (bad-response "Bad reason phrase" reason-phrase))
  111. (else
  112. (if validate-headers?
  113. (validate-headers headers))))
  114. (make-response version code reason-phrase headers port))
  115. (define *reason-phrases*
  116. '((100 . "Continue")
  117. (101 . "Switching Protocols")
  118. (200 . "OK")
  119. (201 . "Created")
  120. (202 . "Accepted")
  121. (203 . "Non-Authoritative Information")
  122. (204 . "No Content")
  123. (205 . "Reset Content")
  124. (206 . "Partial Content")
  125. (300 . "Multiple Choices")
  126. (301 . "Moved Permanently")
  127. (302 . "Found")
  128. (303 . "See Other")
  129. (304 . "Not Modified")
  130. (305 . "Use Proxy")
  131. (307 . "Temporary Redirect")
  132. (400 . "Bad Request")
  133. (401 . "Unauthorized")
  134. (402 . "Payment Required")
  135. (403 . "Forbidden")
  136. (404 . "Not Found")
  137. (405 . "Method Not Allowed")
  138. (406 . "Not Acceptable")
  139. (407 . "Proxy Authentication Required")
  140. (408 . "Request Timeout")
  141. (409 . "Conflict")
  142. (410 . "Gone")
  143. (411 . "Length Required")
  144. (412 . "Precondition Failed")
  145. (413 . "Request Entity Too Large")
  146. (414 . "Request-URI Too Long")
  147. (415 . "Unsupported Media Type")
  148. (416 . "Requested Range Not Satisfiable")
  149. (417 . "Expectation Failed")
  150. (500 . "Internal Server Error")
  151. (501 . "Not Implemented")
  152. (502 . "Bad Gateway")
  153. (503 . "Service Unavailable")
  154. (504 . "Gateway Timeout")
  155. (505 . "HTTP Version Not Supported")))
  156. (define (code->reason-phrase code)
  157. (or (assv-ref *reason-phrases* code)
  158. "(Unknown)"))
  159. (define (response-reason-phrase response)
  160. "Return the reason phrase given in RESPONSE, or the standard
  161. reason phrase for the response's code."
  162. (or (%response-reason-phrase response)
  163. (code->reason-phrase (response-code response))))
  164. (define (text-content-type? type)
  165. "Return #t if TYPE, a symbol as returned by `response-content-type',
  166. represents a textual type such as `text/plain'."
  167. (let ((type (symbol->string type)))
  168. (or (string-prefix? "text/" type)
  169. (string-suffix? "/xml" type)
  170. (string-suffix? "+xml" type))))
  171. (define (read-response port)
  172. "Read an HTTP response from PORT.
  173. As a side effect, sets the encoding on PORT to
  174. ISO-8859-1 (latin-1), so that reading one character reads one byte. See
  175. the discussion of character sets in \"HTTP Responses\" in the manual,
  176. for more information."
  177. (set-port-encoding! port "ISO-8859-1")
  178. (call-with-values (lambda () (read-response-line port))
  179. (lambda (version code reason-phrase)
  180. (make-response version code reason-phrase (read-headers port) port))))
  181. (define (adapt-response-version response version)
  182. "Adapt the given response to a different HTTP version. Returns a new
  183. HTTP response.
  184. The idea is that many applications might just build a response for the
  185. default HTTP version, and this method could handle a number of
  186. programmatic transformations to respond to older HTTP versions (0.9 and
  187. 1.0). But currently this function is a bit heavy-handed, just updating
  188. the version field."
  189. (build-response #:code (response-code response)
  190. #:version version
  191. #:headers (response-headers response)
  192. #:port (response-port response)))
  193. (define (write-response r port)
  194. "Write the given HTTP response to PORT.
  195. Returns a new response, whose ‘response-port’ will continue writing
  196. on PORT, perhaps using some transfer encoding."
  197. (write-response-line (response-version r) (response-code r)
  198. (response-reason-phrase r) port)
  199. (write-headers (response-headers r) port)
  200. (put-string port "\r\n")
  201. (if (eq? port (response-port r))
  202. r
  203. (make-response (response-version r) (response-code r)
  204. (response-reason-phrase r) (response-headers r) port)))
  205. (define (response-must-not-include-body? r)
  206. "Returns ‘#t’ if the response R is not permitted to have a body.
  207. This is true for some response types, like those with code 304."
  208. ;; RFC 2616, section 4.3.
  209. (or (<= 100 (response-code r) 199)
  210. (= (response-code r) 204)
  211. (= (response-code r) 304)))
  212. (define (make-delimited-input-port port len keep-alive?)
  213. "Return an input port that reads from PORT, and makes sure that
  214. exactly LEN bytes are available from PORT. Closing the returned port
  215. closes PORT, unless KEEP-ALIVE? is true."
  216. (define bytes-read 0)
  217. (define (fail)
  218. (bad-response "EOF while reading response body: ~a bytes of ~a"
  219. bytes-read len))
  220. (define (read! bv start count)
  221. ;; Read at most LEN bytes in total. HTTP/1.1 doesn't say what to do
  222. ;; when a server provides more than the Content-Length, but it seems
  223. ;; wise to just stop reading at LEN.
  224. (let ((count (min count (- len bytes-read))))
  225. (let loop ((ret (get-bytevector-n! port bv start count)))
  226. (cond ((eof-object? ret)
  227. (if (= bytes-read len)
  228. 0 ; EOF
  229. (fail)))
  230. ((and (zero? ret) (> count 0))
  231. ;; Do not return zero since zero means EOF, so try again.
  232. (loop (get-bytevector-n! port bv start count)))
  233. (else
  234. (set! bytes-read (+ bytes-read ret))
  235. ret)))))
  236. (define close
  237. (and (not keep-alive?)
  238. (lambda ()
  239. (close-port port))))
  240. (make-custom-binary-input-port "delimited input port" read! #f #f close))
  241. (define* (response-body-port r #:key (decode? #t) (keep-alive? #t))
  242. "Return an input port from which the body of R can be read. The
  243. encoding of the returned port is set according to R's ‘content-type’
  244. header, when it's textual, except if DECODE? is ‘#f’. Return #f when
  245. no body is available.
  246. When KEEP-ALIVE? is ‘#f’, closing the returned port also closes R's
  247. response port."
  248. (define port
  249. (cond
  250. ((member '(chunked) (response-transfer-encoding r))
  251. (make-chunked-input-port (response-port r)
  252. #:keep-alive? keep-alive?))
  253. ((response-content-length r)
  254. => (lambda (len)
  255. (make-delimited-input-port (response-port r)
  256. len keep-alive?)))
  257. ((response-must-not-include-body? r)
  258. #f)
  259. ((or (memq 'close (response-connection r))
  260. (and (equal? (response-version r) '(1 . 0))
  261. (not (memq 'keep-alive (response-connection r)))))
  262. (response-port r))
  263. (else
  264. ;; Here we have a message with no transfer encoding, no
  265. ;; content-length, and a response that won't necessarily be closed
  266. ;; by the server. Not much we can do; assume that the client
  267. ;; knows how to handle it.
  268. (response-port r))))
  269. (when (and decode? port)
  270. (match (response-content-type r)
  271. (((? text-content-type?) . props)
  272. (set-port-encoding! port
  273. (or (assq-ref props 'charset)
  274. "ISO-8859-1")))
  275. (_ #f)))
  276. port)
  277. (define (read-response-body r)
  278. "Reads the response body from R, as a bytevector. Returns
  279. ‘#f’ if there was no response body."
  280. (let ((body (and=> (response-body-port r #:decode? #f)
  281. get-bytevector-all)))
  282. ;; Reading a body of length 0 will result in get-bytevector-all
  283. ;; returning the EOF object.
  284. (if (eof-object? body)
  285. #vu8()
  286. body)))
  287. (define (write-response-body r bv)
  288. "Write BV, a bytevector, to the port corresponding to the HTTP
  289. response R."
  290. (put-bytevector (response-port r) bv))
  291. (define-syntax define-response-accessor
  292. (lambda (x)
  293. (syntax-case x ()
  294. ((_ field)
  295. #'(define-response-accessor field #f))
  296. ((_ field def) (identifier? #'field)
  297. #`(define* (#,(datum->syntax
  298. #'field
  299. (symbol-append 'response- (syntax->datum #'field)))
  300. response
  301. #:optional (default def))
  302. (cond
  303. ((assq 'field (response-headers response)) => cdr)
  304. (else default)))))))
  305. ;; General headers
  306. ;;
  307. (define-response-accessor cache-control '())
  308. (define-response-accessor connection '())
  309. (define-response-accessor date #f)
  310. (define-response-accessor pragma '())
  311. (define-response-accessor trailer '())
  312. (define-response-accessor transfer-encoding '())
  313. (define-response-accessor upgrade '())
  314. (define-response-accessor via '())
  315. (define-response-accessor warning '())
  316. ;; Entity headers
  317. ;;
  318. (define-response-accessor allow '())
  319. (define-response-accessor content-encoding '())
  320. (define-response-accessor content-language '())
  321. (define-response-accessor content-length #f)
  322. (define-response-accessor content-location #f)
  323. (define-response-accessor content-md5 #f)
  324. (define-response-accessor content-range #f)
  325. (define-response-accessor content-type #f)
  326. (define-response-accessor expires #f)
  327. (define-response-accessor last-modified #f)
  328. ;; Response headers
  329. ;;
  330. (define-response-accessor accept-ranges #f)
  331. (define-response-accessor age #f)
  332. (define-response-accessor etag #f)
  333. (define-response-accessor location #f)
  334. (define-response-accessor proxy-authenticate #f)
  335. (define-response-accessor retry-after #f)
  336. (define-response-accessor server #f)
  337. (define-response-accessor vary '())
  338. (define-response-accessor www-authenticate #f)