From 1b48cd86f99a8049c5aa989693c2f03326847e5f Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Sat, 11 Apr 2020 08:56:46 -0600 Subject: [PATCH] net/url: add `#:method` argument to `get-pure-port/headers` Supports HEAD with redirections, for example. --- pkgs/net-doc/net/scribblings/url.scrbl | 13 ++++++++----- racket/collects/net/url.rkt | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkgs/net-doc/net/scribblings/url.scrbl b/pkgs/net-doc/net/scribblings/url.scrbl index bfb7d20b2be..c59212ec09d 100644 --- a/pkgs/net-doc/net/scribblings/url.scrbl +++ b/pkgs/net-doc/net/scribblings/url.scrbl @@ -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?] diff --git a/racket/collects/net/url.rkt b/racket/collects/net/url.rkt index bb2f7b16081..665f9747505 100644 --- a/racket/collects/net/url.rkt +++ b/racket/collects/net/url.rkt @@ -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?))