@@ -18,28 +18,12 @@ import .status-codes
1818import .web-socket
1919
2020/**
21- An HTTP v1.1 client.
22-
23- This class provides methods to fetch data from HTTP servers.
24-
25- When the client is no longer needed, resources should be freed up with the
26- $close method. This is an API incompatibility with version one of the
27- package, which would automatically close the client after one request.
28- See the documentation of the constructor for details.
29-
30- This client has built-in websocket support. The separate websockets package
31- should no longer be used.
32-
33- The client caches connections to servers, so a second request to the same
34- server will use the same socket and may save a lot of CPU time for setting
35- up TLS connections. It also caches session state for TLS connections, so
36- subsequent connections to a server will use the session state to speed up
37- the TLS handshake from about 1000ms to about 150ms (on ESP32).
21+ HTTP Client.
3822
3923# Get
40- Use the $get method to fetch data using a $GET request.
24+ Use the $Client. get method to fetch data using a $GET request.
4125
42- The $get method keeps track of the underlying resources and is thus
26+ The $Client. get method keeps track of the underlying resources and is thus
4327 very easy to use.
4428
4529Example that takes the incoming data and reads it as JSON:
@@ -53,7 +37,7 @@ PATH ::= "/get"
5337
5438main:
5539 network := net.open
56- client := null
40+ client/http.Client := null
5741 try:
5842 client = http.Client network
5943 response := client.get URI PATH
@@ -62,24 +46,53 @@ main:
6246 if client: client.close
6347```
6448
65- For https connection use the $Client.tls constructor with the certificate of
66- the server:
67- ```
68- client := http.Client.tls network
69- --root_certificates=[CERTIFICATE]
70- ```
71- The certificate is server dependendent, and must be obtained before-hand.
72- Most commonly one uses a combination of inspecting the certificate in Google Chrome,
73- and the package `certificate_roots`:
74- https://pkg.toit.io/package/github.com%2Ftoitware%2Ftoit-cert-roots
49+ For https connections either use the $Client.tls constructor or provide
50+ a uri with the https scheme. You need to make sure that the server's
51+ root certificate is installed or provided in the root-certificates list.
52+ For public root certificates see the `certificate_roots` package.
53+
54+ On embedded devices we recommend to provide a $SecurityStore if the server
55+ supports TLS resume. See the tls-resume.toit example.
7556
76- For example, the `httpbin.org` server uses the `AMAZON_ROOT_CA_1` certificate:
7757```
78- import certificate_roots
58+ import certificate-roots
59+ import http
60+ import net
7961
80- CERTIFICATE ::= certificate_roots.AMAZON_ROOT_CA_1
62+ URI ::= "https://www.example.com"
63+
64+ main:
65+ certificate-roots.install-common-trusted-roots
66+ network := net.open
67+ // On embedded devices consider providing a SecurityStore to speed up
68+ // subsequent connections to the same server.
69+ client := http.Client network
70+ response := client.get --uri=URI
71+ while data := response.body.read:
72+ print data.to-string
73+ client.close
8174```
8275*/
76+
77+ /**
78+ An HTTP v1.1 client.
79+
80+ This class provides methods to fetch data from HTTP servers.
81+
82+ When the client is no longer needed, resources should be freed up with the
83+ $close method. This is an API incompatibility with version one of the
84+ package, which would automatically close the client after one request.
85+ See the documentation of the constructor for details.
86+
87+ This client has built-in websocket support. The separate websockets package
88+ should no longer be used.
89+
90+ The client caches connections to servers, so a second request to the same
91+ server will use the same socket and may save a lot of CPU time for setting
92+ up TLS connections. It also caches session state for TLS connections, so
93+ subsequent connections to a server will use the session state to speed up
94+ the TLS handshake from about 1000ms to about 150ms (on ESP32).
95+ */
8396class Client :
8497 /**
8598 The maximum number of redirects to follow if 'follow_redirect' is true for $get and $post requests.
@@ -96,39 +109,53 @@ class Client:
96109 security-store_ / SecurityStore
97110
98111 /**
99- Constructs a new client instance over the given interface.
112+ Constructs a new client instance over the given $network.
113+
114+ Use `net.open` to obtain an $network.
115+
116+ Clients that connect to secure servers may provide a $security-store. This
117+ is used to store session state for TLS connections, which can speed up
118+ the TLS handshake from about 1000ms to about 150ms (on ESP32). The
119+ handshake then also uses less memory.
120+
121+ The $root-certificates parameter is nowadays mostly unused, as the
122+ recommended way to provide root certificates is to `install` them
123+ instead.
124+
100125 The client will default to an insecure HTTP connection, but this can be
101- overridden by a redirect or a URI specifying a secure scheme.
102- Therefore it can be meaningful to provide certificate roots despite the
126+ overridden by a redirect or a URI specifying a secure scheme. Therefore
127+ it can be meaningful to provide certificate roots despite the
103128 insecure default.
104- If the client is used for secure connections, the $root-certificates must
105- contain the root certificate of the server.
129+
130+ If the client is used for secure connections, its root certificate must
131+ be installed or provided in the $root-certificates list.
132+
106133 A client will try to keep a connection open to the last server it
107134 contacted, in the hope that the next request will connect to the same
108135 server. This can save a lot of CPU time for TLS connections which are
109136 expensive to set up, but it also reserves a fairly large amount of
110137 buffer memory for the TLS connection. Call $close (perhaps in a finally
111138 clause) to release the connection.
139+
112140 See the `certificate_roots` package for common roots:
113141 https://pkg.toit.io/package/github.com%2Ftoitware%2Ftoit-cert-roots
114- Use `net.open` to obtain an interface.
115142 */
116- constructor . interface_
143+ constructor network / tcp.Interface
117144 --root-certificates / List= []
118145 --security-store / SecurityStore= SecurityStoreInMemory :
146+ interface_ = network
119147 security-store_ = security-store
120148 root-certificates_ = root-certificates
121149 add-finalizer this :: this .finalize_
122150
123151 /**
124- Constructs a new client.
125- The client will default to a secure HTTPS connection, but this can be
126- overridden by a redirect or a URI specifying an insecure scheme.
127- The $root-certificates must contain the root certificate of the server.
128- See the `certificate_roots` package for common roots:
129- https://pkg.toit.io/package/github.com%2Ftoitware%2Ftoit-cert-roots
152+ Variant of $constructor.
153+
154+ Constructs a client that defaults to a secure HTTPS connection.
155+
130156 A client $certificate can be specified for the rare case where the client
131157 authenticates itself.
158+
132159 The $server-name can be specified for verifying the TLS certificate. This is
133160 for the rare case where we wish to verify the TLS connections with a
134161 different server name from the one used to establish the connection.
0 commit comments