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 @defproc[(get-pure-port/headers
[url url?] [url url?]
[headers (listof string?) '()] [headers (listof string?) '()]
[#:method method (or/c #"GET" #"HEAD" #"DELETE" #"OPTIONS") #"GET"]
[#:redirections redirections exact-nonnegative-integer? 0] [#:redirections redirections exact-nonnegative-integer? 0]
[#:status? status? boolean? #f] [#:status? status? boolean? #f]
[#:connection connection (or/c #f http-connection?)]) [#:connection connection (or/c #f http-connection?)])
(values input-port? string?)]{ (values input-port? string?)]{
This function is an alternative to calling @racket[get-impure-port] and This function is an alternative to calling @racket[get-impure-port]
@racket[purify-port] when needing to follow redirections. It also (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] supports HTTP/1.1 connections, which are used when the @racket[connection]
argument is not @racket[#f]. argument is not @racket[#f].
The @racket[get-pure-port/headers] function performs a GET request The @racket[get-pure-port/headers] function performs a request specified by
on @racket[url], follows up to @racket[redirections] redirections @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 and returns a port containing the data as well as the headers for
the final connection. If @racket[status?] is true, then the status the final connection. If @racket[status?] is true, then the status
line is included in the result string. 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 If @racket[connection] is provided, read all data from the result port
before making a new request with the same @racket[connection]. (Reusing before making a new request with the same @racket[connection]. (Reusing
a @racket[connection] without reading all data may or may not work.) 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[( @deftogether[(
@defproc[(http-connection? [v any/c]) boolean?] @defproc[(http-connection? [v any/c]) boolean?]

View File

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