request.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ;;; HTTP request objects
  2. ;; Copyright (C) 2010, 2011, 2012 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 request)
  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 (srfi srfi-9)
  24. #:use-module (web uri)
  25. #:use-module (web http)
  26. #:export (request?
  27. request-method
  28. request-uri
  29. request-version
  30. request-headers
  31. request-meta
  32. request-port
  33. read-request
  34. build-request
  35. write-request
  36. read-request-body
  37. write-request-body
  38. ;; General headers
  39. ;;
  40. request-cache-control
  41. request-connection
  42. request-date
  43. request-pragma
  44. request-trailer
  45. request-transfer-encoding
  46. request-upgrade
  47. request-via
  48. request-warning
  49. ;; Entity headers
  50. ;;
  51. request-allow
  52. request-content-encoding
  53. request-content-language
  54. request-content-length
  55. request-content-location
  56. request-content-md5
  57. request-content-range
  58. request-content-type
  59. request-expires
  60. request-last-modified
  61. ;; Request headers
  62. ;;
  63. request-accept
  64. request-accept-charset
  65. request-accept-encoding
  66. request-accept-language
  67. request-authorization
  68. request-expect
  69. request-from
  70. request-host
  71. request-if-match
  72. request-if-modified-since
  73. request-if-none-match
  74. request-if-range
  75. request-if-unmodified-since
  76. request-max-forwards
  77. request-proxy-authorization
  78. request-range
  79. request-referer
  80. request-te
  81. request-user-agent
  82. ;; Misc
  83. request-absolute-uri))
  84. ;;; {Character Encodings, Strings, and Bytevectors}
  85. ;;;
  86. ;;; Requests are read from over the wire, and as such have to be treated
  87. ;;; very carefully.
  88. ;;;
  89. ;;; The header portion of the message is defined to be in a subset of
  90. ;;; ASCII, and may be processed either byte-wise (using bytevectors and
  91. ;;; binary I/O) or as characters in a single-byte ASCII-compatible
  92. ;;; encoding.
  93. ;;;
  94. ;;; We choose the latter, processing as strings in the latin-1
  95. ;;; encoding. This allows us to use all the read-delimited machinery,
  96. ;;; character sets, and regular expressions, shared substrings, etc.
  97. ;;;
  98. ;;; The characters in the header values may themselves encode other
  99. ;;; bytes or characters -- basically each header has its own parser. We
  100. ;;; leave that as a header-specific topic.
  101. ;;;
  102. ;;; The body is present if the content-length header is present. Its
  103. ;;; format and, if textual, encoding is determined by the headers, but
  104. ;;; its length is encoded in bytes. So we just slurp that number of
  105. ;;; characters in latin-1, knowing that the number of characters
  106. ;;; corresponds to the number of bytes, and then convert to a
  107. ;;; bytevector, perhaps for later decoding.
  108. ;;;
  109. (define-record-type <request>
  110. (make-request method uri version headers meta port)
  111. request?
  112. (method request-method)
  113. (uri request-uri)
  114. (version request-version)
  115. (headers request-headers)
  116. (meta request-meta)
  117. (port request-port))
  118. (define (bad-request message . args)
  119. (throw 'bad-request message args))
  120. (define (bad-request-printer port key args default-printer)
  121. (apply (case-lambda
  122. ((msg args)
  123. (display "Bad request: " port)
  124. (apply format port msg args)
  125. (newline port))
  126. (_ (default-printer)))
  127. args))
  128. (set-exception-printer! 'bad-request bad-request-printer)
  129. (define (non-negative-integer? n)
  130. (and (number? n) (>= n 0) (exact? n) (integer? n)))
  131. (define (validate-headers headers)
  132. (if (pair? headers)
  133. (let ((h (car headers)))
  134. (if (pair? h)
  135. (let ((k (car h)) (v (cdr h)))
  136. (if (valid-header? k v)
  137. (validate-headers (cdr headers))
  138. (bad-request "Bad value for header ~a: ~s" k v)))
  139. (bad-request "Header not a pair: ~a" h)))
  140. (if (not (null? headers))
  141. (bad-request "Headers not a list: ~a" headers))))
  142. (define* (build-request uri #:key (method 'GET) (version '(1 . 1))
  143. (headers '()) port (meta '())
  144. (validate-headers? #t))
  145. "Construct an HTTP request object. If VALIDATE-HEADERS? is true,
  146. the headers are each run through their respective validators."
  147. (let ((needs-host? (and (equal? version '(1 . 1))
  148. (not (assq-ref headers 'host)))))
  149. (cond
  150. ((not (and (pair? version)
  151. (non-negative-integer? (car version))
  152. (non-negative-integer? (cdr version))))
  153. (bad-request "Bad version: ~a" version))
  154. ((not (uri-reference? uri))
  155. (bad-request "Bad uri: ~a" uri))
  156. ((and (not port) (memq method '(POST PUT)))
  157. (bad-request "Missing port for message ~a" method))
  158. ((not (list? meta))
  159. (bad-request "Bad metadata alist" meta))
  160. ((and needs-host? (not (uri-host uri)))
  161. (bad-request "HTTP/1.1 request without Host header and no host in URI: ~a"
  162. uri))
  163. (else
  164. (if validate-headers?
  165. (validate-headers headers))))
  166. (make-request method uri version
  167. (if needs-host?
  168. (acons 'host (cons (uri-host uri) (uri-port uri))
  169. headers)
  170. headers)
  171. meta port)))
  172. (define* (read-request port #:optional (meta '()))
  173. "Read an HTTP request from PORT, optionally attaching the given
  174. metadata, META.
  175. As a side effect, sets the encoding on PORT to
  176. ISO-8859-1 (latin-1), so that reading one character reads one byte. See
  177. the discussion of character sets in \"HTTP Requests\" in the manual, for
  178. more information.
  179. Note that the body is not part of the request. Once you have read a
  180. request, you may read the body separately, and likewise for writing
  181. requests."
  182. (set-port-encoding! port "ISO-8859-1")
  183. (call-with-values (lambda () (read-request-line port))
  184. (lambda (method uri version)
  185. (make-request method uri version (read-headers port) meta port))))
  186. ;; FIXME: really return a new request?
  187. (define (write-request r port)
  188. "Write the given HTTP request to PORT.
  189. Return a new request, whose ‘request-port’ will continue writing
  190. on PORT, perhaps using some transfer encoding."
  191. (write-request-line (request-method r) (request-uri r)
  192. (request-version r) port)
  193. (write-headers (request-headers r) port)
  194. (put-string port "\r\n")
  195. (if (eq? port (request-port r))
  196. r
  197. (make-request (request-method r) (request-uri r) (request-version r)
  198. (request-headers r) (request-meta r) port)))
  199. (define (read-request-body r)
  200. "Reads the request body from R, as a bytevector. Return ‘#f’
  201. if there was no request body."
  202. (let ((nbytes (request-content-length r)))
  203. (and nbytes
  204. (let ((bv (get-bytevector-n (request-port r) nbytes)))
  205. (if (= (bytevector-length bv) nbytes)
  206. bv
  207. (bad-request "EOF while reading request body: ~a bytes of ~a"
  208. (bytevector-length bv) nbytes))))))
  209. (define (write-request-body r bv)
  210. "Write BV, a bytevector, to the port corresponding to the HTTP
  211. request R."
  212. (put-bytevector (request-port r) bv))
  213. (define-syntax define-request-accessor
  214. (lambda (x)
  215. (syntax-case x ()
  216. ((_ field)
  217. #'(define-request-accessor field #f))
  218. ((_ field def) (identifier? #'field)
  219. #`(define* (#,(datum->syntax
  220. #'field
  221. (symbol-append 'request- (syntax->datum #'field)))
  222. request
  223. #:optional (default def))
  224. (cond
  225. ((assq 'field (request-headers request)) => cdr)
  226. (else default)))))))
  227. ;; General headers
  228. ;;
  229. (define-request-accessor cache-control '())
  230. (define-request-accessor connection '())
  231. (define-request-accessor date #f)
  232. (define-request-accessor pragma '())
  233. (define-request-accessor trailer '())
  234. (define-request-accessor transfer-encoding '())
  235. (define-request-accessor upgrade '())
  236. (define-request-accessor via '())
  237. (define-request-accessor warning '())
  238. ;; Entity headers
  239. ;;
  240. (define-request-accessor allow '())
  241. (define-request-accessor content-encoding '())
  242. (define-request-accessor content-language '())
  243. (define-request-accessor content-length #f)
  244. (define-request-accessor content-location #f)
  245. (define-request-accessor content-md5 #f)
  246. (define-request-accessor content-range #f)
  247. (define-request-accessor content-type #f)
  248. (define-request-accessor expires #f)
  249. (define-request-accessor last-modified #f)
  250. ;; Request headers
  251. ;;
  252. (define-request-accessor accept '())
  253. (define-request-accessor accept-charset '())
  254. (define-request-accessor accept-encoding '())
  255. (define-request-accessor accept-language '())
  256. (define-request-accessor authorization #f)
  257. (define-request-accessor expect '())
  258. (define-request-accessor from #f)
  259. (define-request-accessor host #f)
  260. ;; Absence of an if-directive appears to be different from `*'.
  261. (define-request-accessor if-match #f)
  262. (define-request-accessor if-modified-since #f)
  263. (define-request-accessor if-none-match #f)
  264. (define-request-accessor if-range #f)
  265. (define-request-accessor if-unmodified-since #f)
  266. (define-request-accessor max-forwards #f)
  267. (define-request-accessor proxy-authorization #f)
  268. (define-request-accessor range #f)
  269. (define-request-accessor referer #f)
  270. (define-request-accessor te '())
  271. (define-request-accessor user-agent #f)
  272. ;; Misc accessors
  273. (define* (request-absolute-uri r #:optional default-host default-port
  274. default-scheme)
  275. "A helper routine to determine the absolute URI of a request, using the
  276. ‘host’ header and the default host and port."
  277. (let ((uri (request-uri r)))
  278. (if (uri-host uri)
  279. uri
  280. (let ((host
  281. (or (request-host r)
  282. (if default-host
  283. (cons default-host default-port)
  284. (bad-request
  285. "URI not absolute, no Host header, and no default: ~s"
  286. uri)))))
  287. (build-uri (or (uri-scheme uri)
  288. default-scheme
  289. (bad-request "URI not absolute and no default-port"
  290. uri))
  291. #:host (car host)
  292. #:port (cdr host)
  293. #:path (uri-path uri)
  294. #:query (uri-query uri)
  295. #:fragment (uri-fragment uri))))))