mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-20 11:40:18 +02:00
web: Add `http-get*'.
* module/web/client.scm (http-get*): New procedure. * doc/ref/web.texi (Web Client): Document it.
This commit is contained in:
parent
75d6c59fc2
commit
91e693a8e8
2 changed files with 31 additions and 1 deletions
|
@ -1400,6 +1400,13 @@ response will be decoded to string, if it is a textual content-type.
|
||||||
Otherwise it will be returned as a bytevector.
|
Otherwise it will be returned as a bytevector.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
|
@deffn {Scheme Procedure} http-get* uri [#:port=(open-socket-for-uri uri)] [#:version='(1 . 1)] [#:keep-alive?=#f] [#:extra-headers='()] [#:decode-body?=#t]
|
||||||
|
Like @code{http-get}, but return an input port from which to read. When
|
||||||
|
@var{decode-body?} is true, as is the default, the returned port has its
|
||||||
|
encoding set appropriately if the data at @var{uri} is textual. Closing the
|
||||||
|
returned port closes @var{port}, unless @var{keep-alive?} is true.
|
||||||
|
@end deffn
|
||||||
|
|
||||||
@code{http-get} is useful for making one-off requests to web sites. If
|
@code{http-get} is useful for making one-off requests to web sites. If
|
||||||
you are writing a web spider or some other client that needs to handle a
|
you are writing a web spider or some other client that needs to handle a
|
||||||
number of requests in parallel, it's better to build an event-driven URL
|
number of requests in parallel, it's better to build an event-driven URL
|
||||||
|
|
|
@ -39,7 +39,8 @@
|
||||||
#:use-module (web response)
|
#:use-module (web response)
|
||||||
#:use-module (web uri)
|
#:use-module (web uri)
|
||||||
#:export (open-socket-for-uri
|
#:export (open-socket-for-uri
|
||||||
http-get))
|
http-get
|
||||||
|
http-get*))
|
||||||
|
|
||||||
(define (open-socket-for-uri uri)
|
(define (open-socket-for-uri uri)
|
||||||
"Return an open input/output port for a connection to URI."
|
"Return an open input/output port for a connection to URI."
|
||||||
|
@ -135,3 +136,25 @@ Otherwise it will be returned as a bytevector."
|
||||||
(if decode-body?
|
(if decode-body?
|
||||||
(decode-response-body res body)
|
(decode-response-body res body)
|
||||||
body)))))
|
body)))))
|
||||||
|
|
||||||
|
(define* (http-get* uri #:key (port (open-socket-for-uri uri))
|
||||||
|
(version '(1 . 1)) (keep-alive? #f) (extra-headers '())
|
||||||
|
(decode-body? #t))
|
||||||
|
"Like ‘http-get’, but return an input port from which to read. When
|
||||||
|
DECODE-BODY? is true, as is the default, the returned port has its
|
||||||
|
encoding set appropriately if the data at URI is textual. Closing the
|
||||||
|
returned port closes PORT, unless KEEP-ALIVE? is true."
|
||||||
|
(let ((req (build-request uri #:version version
|
||||||
|
#:headers (if keep-alive?
|
||||||
|
extra-headers
|
||||||
|
(cons '(connection close)
|
||||||
|
extra-headers)))))
|
||||||
|
(write-request req port)
|
||||||
|
(force-output port)
|
||||||
|
(unless keep-alive?
|
||||||
|
(shutdown port 1))
|
||||||
|
(let* ((res (read-response port))
|
||||||
|
(body (response-body-port res
|
||||||
|
#:keep-alive? keep-alive?
|
||||||
|
#:decode? decode-body?)))
|
||||||
|
(values res body))))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue