1 Request
(require (planet gh/http:1:=0/request)) |
1.1 Why not just use net/url?
Racket’s net/url functions are very convenient for doing simple requests using HTTP 1.0. Use them when you can.
Sometimes you need to use HTTP 1.1. For example you may need the speed
of making multiple requests to a server over one persistent
connection. Or you may want to use the Expect: 100-continue
request header—
This library is intended to help you with those kinds of applications. It provides basic building blocks to let you do any kind of HTTP request flow, with help for some of the gory details.
Also it supplies some higher-level support. The call/requests functions are in the style of call/input-url. These take care of some standard setup and teardown, including ensuring the ports are closed even if an exception is raised.
It even provides variations of net/url’s functions like get-impure-port. You can use those when you want to use Expect: 100-continue but do not need to make multiple requests over one connection.
1.2 Connections and requests
procedure
(connect scheme host port) →
input-port? output-port? scheme : (or/c "http" "https") host : string? port : exact-positive-integer?
procedure
(disconnect in out) → any
in : input-port? out : output-port?
Examples: | ||
|
procedure
(connect-uri uri) →
input-port? output-port? uri : string?
Examples: | ||
|
procedure
(uri->scheme&host&port uri) →
(or/c "http" "https") string? exact-positive-integer? uri : string?
scheme, "http" (default if unspecified) or "https"
host, such as "www.foo.com"
port as specified by uri. Defaults to 80 if scheme is "http" or 443 if "https".
Examples: | ||||||||||||||||
|
procedure
(uri&headers->path&header uri heads) →
string? string? uri : string? heads : dict?
Examples: | |||||||||||||||
|
procedure
(start-request in out http-version method path heads) → boolean? in : input-port? out : output-port? http-version : (or/c "1.0" "1.1") method : string? path : string? heads : string?
When method is "PUT" or "POST", you should pay attention to the returned boolean? value:
#t: You should go ahead and transmit the entity (the data for the put or post request), after which you should call flush-output and call purify-port/log-debug to read the response headers.
#f: You should not transmit the entity data. Instead just call purify-port/log-debug to read the response headers. (The reason for this is to support the Expect: 100-continue request header, and the case where the server says not to continue because it will fail or redirect the request. This lets you avoid sending a large entity body that won’t be used.)
For other methods (such as "GET" or "HEAD"), this will always return #t. You should always just go ahead and call purify-port/log-debug to read the response headers.
1.3 Reading response headers
procedure
(purify-port/log-debug in) → string?
in : input-port?
The other functions in this library use log-debug, so using purify-port/log-debug will give a complete/consistent log output.
procedure
(redirect-uri h) → (or/c #f string?)
h : string?
procedure
(same-connection? old new) → boolean?
old : url? new : url?
procedure
(close-connection? h) → boolean?
h : string?
1.4 Reading response entities
1.4.1 Building blocks
procedure
in : input-port? h : string?
Used by read-entity/bytes.
procedure
(entity-content-decoder/ports heads)
→ (input-port? output-port? -> any) heads : (or/c string? bytes?)
Note: I wanted this to return an input-port? like read-entity/transfer-decoding-port and to be named read-entity/content-decoding-port. Unfortunately, the gzip and deflate functions provided by Racket don’t seem to make that possible, as they use a copy-port pattern.
Examines the Content-Encoding header, if any, and returns a function (input-port? output-port? -> void?) which will copy the bytes and decode them. If there no content encoding specified, or an unsupported encoding, this simply returns copy-port.
procedure
(entity-content-decode-bytes bytes heads) → bytes?
bytes : bytes? heads : (or/c string? bytes?)
1.4.2 Conveniences
procedure
(read-entity/bytes in h) → bytes?
in : input-port? h : string?
procedure
(read-entity/xexpr in h) → xexpr?
in : input-port? h : string?
How the xexpr is created depends on the Content-Type response header present in h:
The xexpr is created using read-xml and xml->xexpr if the Content-Type is text/xml or application/xml, or if there is no Content-Type specified and the first bytes of the entity are "<?xml".
The xexpr is created using read-html-as-xml and xml->xexpr if the Content-Type is text/html.
If the Content-Type is text/plain then the xexpr is simply bytes?.
If the Content-Type is application/x-www-form-urlencoded then the xexpr is `(data ,(form-url-encoded->alist (bytes->string/utf-8 bytes))).
Otherwise the xexpr is `(entity () ,(format "~a" by)).
1.5 call/input-url style
Like call/input-url, these procedures help you make HTTP requests and ensure that the ports are closed even if there is an exception.
procedure
(call/requests scheme host port func) → any/c
scheme : (or/c "http" "https") host : string? port : exact-positive-integer? func : (input-port? output-port? . -> . any/c)
Your func function is called with two arguments, the input and output ports. The value you return from func is returned as the value of call/requests.
Tip: Remember to call flush-output after writing data to out, otherwise the server may not get the data and respond, while you wait to read from in.
(define-values (scheme host port) (uri->scheme&host&port uri)) (call/requests scheme host port (lambda (in out) (define-values (path rh) (uri&headers->path&header uri '("Expect: 100-continue"))) (define tx-data? (start-request in out method path rh "1.0")) (when tx-data? (display data out) (flush-output out)) ;Important! (define h (purify-port/log-debug in)) (read-entity/bytes in h)))
For a single request, you may prefer to use call/input-request or call/output-request.
procedure
(call/input-request http-version method uri heads entity-reader #:redirects redirects) → any/c http-version : (or/c "1.0" "1.1") method : string? uri : string? heads : dict? entity-reader : (input-port? string? . -> . any/c) redirects : 10
Like call/requests, this guarantees the ports will be closed.
Your entity-reader function is called after the response header has been read (the header is passed to you as a string), giving you the ability to read the response entity from the supplied input-port?. For example you may read the response using read-entity/bytes or read-entity/xexpr.
If redirects is non-zero, up to that number of redirects will be followed automatically. Whether the response is a redirect is determined using redirect-uri. If the redirected location can be reached on the existing HTTP connection (as determined by close-connection? and same-connection?), then the existing connection will be used (which is faster than disconnecting and reconnecting.)
procedure
(call/output-request http-version method uri data data-length heads entity-reader #:redirects redirects) → any/c http-version : (or/c "1.0" "1.1") method : string? uri : string? data : (or/c bytes? (output-port? . -> . void?)) data-length : (or/c #f exact-nonnegative-integer?) heads : dict? entity-reader : (input-port? string? . -> . any/c) redirects : 10
Like call/requests, this guarantees the ports will be closed.
The data and data-length arguments depend on whether you want to provide the data as bytes? or as a function that will write the data:
If data is bytes?, you may set data-length to #f. A Content-Length header will be created for you using (bytes-length data).
If you pass a writer function for data, then data-length should be the number of bytes that you will write. This allows a Content-Length header to be created for you. Otherwise, if you supply #f for data-length, there will be no Content-Length header (unless you supply your own in heads).
Your entity-reader function is called after the response header has been read (the header is passed to you as a string), giving you the ability to read the response entity from the supplied input-port?. For example you may read the response using read-entity/bytes or read-entity/xexpr.
If redirects is non-zero, up to that number of redirects will be followed automatically. Whether the response is a redirect is determined using redirect-uri. If the redirected location can be reached on the existing HTTP connection (as determined by close-connection? and same-connection?), then the existing connection will be used (which is faster than disconnecting and reconnecting.)
If you supply "1.1" for http-version and the request header Expect: 100-continue, then redirects or failures can be handled much more efficiently. If the server supports Expect: 100-continue, then it can respond with the redirect or failure before you transmit all of the entity data.
1.6 net/url impure port style
procedure
(head-impure-port* url heads) → input-port?
url : url? heads : (listof string? '())
procedure
(get-impure-port* url heads) → input-port?
url : url? heads : (listof string? '())
procedure
(delete-impure-port* url heads) → input-port?
url : url? heads : (listof string? '())
procedure
(put-impure-port* url data heads) → input-port?
url : url? data : bytes? heads : (listof string? '())
procedure
(post-impure-port* url data heads) → input-port?
url : url? data : bytes? heads : (listof string? '())
parameter
(http-ver version) → void? version : string?
These variations add the ability to do HTTP 1.1, and support for you including an Expect: 100-continue request header in heads. This allows a PUT or POST request to fail or redirect, before data is sent to the server.
To make these functions do HTTP 1.1 requests, you should set the http-ver parameter to "1.1".
Because HTTP 1.1 defaults to persistent connections, it would be nice for you to include a Connection: close request header in heads.
1.7 Date string conversion
procedure
(seconds->gmt-string [s]) → string?
s : exact-integer? = (current-seconds)
Examples: | ||||
|
procedure
(seconds->gmt-8601-string [style s]) → string?
style : (or/c 'plain 'T/Z 'T/.00Z) = 'T/Z s : exact-integer? = (current-seconds)
Examples: | |||||||
|
procedure
s : string?
Examples: | ||||||
|