;;; Web client
-;; Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
+;; Copyright (C) 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
#:use-module (web uri)
#:use-module (web http)
#:use-module (srfi srfi-1)
+ #:use-module (srfi srfi-9)
+ #:use-module (srfi srfi-9 gnu)
#:export (current-http-proxy
open-socket-for-uri
http-get
(delete-duplicates
(getaddrinfo (uri-host uri)
(cond (port => number->string)
- (else (symbol->string (uri-scheme uri))))
+ ((uri-scheme uri) => symbol->string)
+ (else (error "Not an absolute URI" uri)))
(if port
AI_NUMERICSERV
0))
;; Buffer input and output on this port.
(setvbuf s _IOFBF)
- ;; Enlarge the receive buffer.
- (setsockopt s SOL_SOCKET SO_RCVBUF (* 12 1024))
;; If we're using a proxy, make a note of that.
(when http-proxy (set-http-proxy-port?! s #t))
s)
(loop (cdr addresses))))))))
(define (extend-request r k v . additional)
- (let ((r (build-request (request-uri r) #:version (request-version r)
- #:headers
- (assoc-set! (copy-tree (request-headers r))
- k v)
- #:port (request-port r))))
+ (let ((r (set-field r (request-headers)
+ (assoc-set! (copy-tree (request-headers r))
+ k v))))
(if (null? additional)
r
(apply extend-request r additional))))
((not body)
(let ((length (request-content-length request)))
(if length
+ ;; FIXME make this stricter: content-length header should be
+ ;; prohibited if there's no body, even if the content-length
+ ;; is 0.
(unless (zero? length)
(error "content-length, but no body"))
(when (assq 'transfer-encoding (request-headers request))
(rlen (if (= rlen blen)
request
(error "bad content-length" rlen blen)))
- ((zero? blen) request)
(else (extend-request request 'content-length blen))))
body))))
(define* (request uri #:key
(body #f)
(port (open-socket-for-uri uri))
- (method "GET")
+ (method 'GET)
(version '(1 . 1))
(keep-alive? #f)
(headers '())
(force-output (request-port request))
(let ((response (read-response port)))
(cond
- ((equal? (request-method request) "HEAD")
+ ((eq? (request-method request) 'HEAD)
(unless keep-alive?
(close-port port))
(values response #f))
(issue-deprecation-warning
"The #:extra-headers argument to http-get has been renamed to #:headers. "
"Please update your code."))
- (request uri #:method "GET" #:body body
+ (request uri #:method 'GET #:body body
#:port port #:version version #:keep-alive? keep-alive?
#:headers headers #:decode-body? decode-body?
#:streaming? streaming?))
#:streaming? streaming?)))
(define-http-verb http-head
- "HEAD"
+ 'HEAD
"Fetch message headers for the given URI using the HTTP \"HEAD\"
method.
other procedures can treat all of the http-foo verbs identically.")
(define-http-verb http-post
- "POST"
+ 'POST
"Post data to the given URI using the HTTP \"POST\" method.
This function is similar to ‘http-get’, except it uses the \"POST\"
Returns two values: the resulting response, and the response body.")
(define-http-verb http-put
- "PUT"
+ 'PUT
"Put data at the given URI using the HTTP \"PUT\" method.
This function is similar to ‘http-get’, except it uses the \"PUT\"
Returns two values: the resulting response, and the response body.")
(define-http-verb http-delete
- "DELETE"
+ 'DELETE
"Delete data at the given URI using the HTTP \"DELETE\" method.
This function is similar to ‘http-get’, except it uses the \"DELETE\"
Returns two values: the resulting response, and the response body.")
(define-http-verb http-trace
- "TRACE"
+ 'TRACE
"Send an HTTP \"TRACE\" request.
This function is similar to ‘http-get’, except it uses the \"TRACE\"
Returns two values: the resulting response, and the response body.")
(define-http-verb http-options
- "OPTIONS"
+ 'OPTIONS
"Query characteristics of an HTTP resource using the HTTP \"OPTIONS\"
method.