Skip to content

Commit

Permalink
net/url: add #:method argument to get-pure-port/headers
Browse files Browse the repository at this point in the history
Supports HEAD with redirections, for example.
  • Loading branch information
mflatt committed Apr 12, 2020
1 parent 7f97847 commit 1b48cd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 8 additions & 5 deletions pkgs/net-doc/net/scribblings/url.scrbl
Expand Up @@ -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.
Expand All @@ -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?]
Expand Down
4 changes: 3 additions & 1 deletion racket/collects/net/url.rkt
Expand Up @@ -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])
Expand All @@ -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)
Expand Down Expand Up @@ -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?))
Expand Down

0 comments on commit 1b48cd8

Please sign in to comment.