Skip to content

Commit

Permalink
Install certificates and add examples. (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Jun 21, 2024
1 parent b585c8f commit dd1d9c0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
9 changes: 6 additions & 3 deletions examples/client-get-tls-custom.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import http
import net
import net.x509
import tls

main:
network := net.open

certificate := tls.RootCertificate SERVER-CERT
certificate.install

client := http.Client.tls network
--root-certificates=[SERVER-CERT]

response := client.get "localhost:8080" "/json"
while data := response.body.read:
print data.to-string

client.close

SERVER-CERT ::= x509.Certificate.parse """
SERVER-CERT ::= """
-----BEGIN CERTIFICATE-----
MIIDkzCCAnugAwIBAgIUb3nSgGzXBdgsDhg8shods8EHszAwDQYJKoZIhvcNAQEL
BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
Expand Down
3 changes: 1 addition & 2 deletions examples/server-tls.toit
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ ITEMS := ["FOO", "BAR", "BAZ"]

main:
network := net.open
server := http.Server.tls
--certificate=TLS-SERVER-CERT
server := http.Server.tls --certificate=TLS-SERVER-CERT
server.listen network 8080:: | request/http.RequestIncoming writer/http.ResponseWriter |
if request.path == "/empty":
else if request.path == "/json":
Expand Down
33 changes: 33 additions & 0 deletions src/server.toit
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@ import .request
import .status-codes
import .web-socket

/**
An HTTP server.
# Examples
```
import http
import net
main:
network := net.open
// Listen on a free port.
tcp-socket := network.tcp-listen 0
print "Server on http://localhost:$tcp-socket.local-address.port/"
server := http.Server
server.listen tcp-socket:: | request/http.RequestIncoming writer/http.ResponseWriter |
resource := request.query.resource
if resource == "/empty":
else if resource == "/":
writer.headers.set "Content-Type" "text/html"
writer.out.write """
<html>
<body>
<p>Hello world</p>
</body>
</html>
"""
else:
writer.headers.set "Content-Type" "text/plain"
writer.write-headers 404
writer.out.write "Not found\n"
writer.close
```
*/
class Server:
static DEFAULT-READ-TIMEOUT/Duration ::= Duration --s=30

Expand Down
3 changes: 1 addition & 2 deletions tests/google-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import http
import net
import net.x509
import certificate-roots

main:
network := net.open
security-store := http.SecurityStoreInMemory
certificate-roots.install-common-trusted-roots
client := http.Client.tls network
--security-store=security-store
--root-certificates=[certificate-roots.GTS-ROOT-R1]
response := client.get "script.google.com" "/"
while data := response.body.read:
response = client.get "www.google.com" "/"
Expand Down
5 changes: 3 additions & 2 deletions tests/package.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sdk: ^2.0.0-alpha.91
prefixes:
certificate_roots: toit-cert-roots
http: ..
Expand All @@ -7,5 +8,5 @@ packages:
toit-cert-roots:
url: github.com/toitware/toit-cert-roots
name: certificate_roots
version: 1.3.2
hash: 288547039d8a3797330064e91d8c79ad16313545
version: 1.6.1
hash: 55d3be82ed53d8d332338b2de931865cf69fe48b
2 changes: 1 addition & 1 deletion tests/package.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
certificate_roots:
url: github.com/toitware/toit-cert-roots
version: ^1.3.2
version: ^1.6.1
http:
path: ..

0 comments on commit dd1d9c0

Please sign in to comment.