net/url: add #:method argument to get-pure-port/headers

Supports HEAD with redirections, for example.
This commit is contained in:
Matthew Flatt 2020-04-11 08:56:46 -06:00
parent 7f9784775f
commit 1b48cd86f9
2 changed files with 11 additions and 6 deletions

View File

@ -417,17 +417,19 @@ empty string, or it will be a string matching the following regexp:
@defproc[(get-pure-port/headers
[url url?]
[headers (listof string?) '()]
[#:method method (or/c #"GET" #"HEAD" #"DELETE" #"OPTIONS") #"GET"]
[#:redirections redirections exact-nonnegative-integer? 0]
[#:status? status? boolean? #f]
[#:connection connection (or/c #f http-connection?)])
(values input-port? string?)]{
This function is an alternative to calling @racket[get-impure-port] and
@racket[purify-port] when needing to follow redirections. It also
This function is an alternative to calling @racket[get-impure-port]
(or @racket[head-impure-port], @racket[delete-impure-port], or @racket[options-impure-port])
and @racket[purify-port] when needing to follow redirections. It also
supports HTTP/1.1 connections, which are used when the @racket[connection]
argument is not @racket[#f].
The @racket[get-pure-port/headers] function performs a GET request
on @racket[url], follows up to @racket[redirections] redirections
The @racket[get-pure-port/headers] function performs a request specified by
@racket[method] (GET by default) on @racket[url], follows up to @racket[redirections] redirections,
and returns a port containing the data as well as the headers for
the final connection. If @racket[status?] is true, then the status
line is included in the result string.
@ -438,7 +440,8 @@ empty string, or it will be a string matching the following regexp:
If @racket[connection] is provided, read all data from the result port
before making a new request with the same @racket[connection]. (Reusing
a @racket[connection] without reading all data may or may not work.)
}
@history[#:changed "7.7.0.1" @elem{Added the @racket[#:method] argument.}]}
@deftogether[(
@defproc[(http-connection? [v any/c]) boolean?]

View File

@ -302,6 +302,7 @@
(hc:http-conn-close! hc))
(define (get-pure-port/headers url [strings '()]
#:method [method #"GET"]
#:redirections [redirections 0]
#:status? [status? #f]
#:connection [conn #f])
@ -316,7 +317,7 @@
make-ports)
(and conn #t)))
(define-values (status headers response-port)
(hc:http-conn-recv! hc #:method #"GET" #:close? (not conn) #:content-decode '()))
(hc:http-conn-recv! hc #:method method #:close? (not conn) #:content-decode '()))
(define new-url
(ormap (λ (h)
@ -495,6 +496,7 @@
(purify-port (input-port? . -> . string?))
(get-pure-port/headers (->* (url?)
((listof string?)
#:method (or/c #"GET" #"HEAD" #"DELETE" #"OPTIONS")
#:redirections exact-nonnegative-integer?
#:status? boolean?
#:connection (or/c #f hc:http-conn?))