From dd1d9c044da5d8f5ad000e754ac33e2a0411c62c Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Fri, 21 Jun 2024 12:27:58 +0200 Subject: [PATCH] Install certificates and add examples. (#142) --- examples/client-get-tls-custom.toit | 9 +++++--- examples/server-tls.toit | 3 +-- src/server.toit | 33 +++++++++++++++++++++++++++++ tests/google-test.toit | 3 +-- tests/package.lock | 5 +++-- tests/package.yaml | 2 +- 6 files changed, 45 insertions(+), 10 deletions(-) diff --git a/examples/client-get-tls-custom.toit b/examples/client-get-tls-custom.toit index cb5f056..058ec96 100644 --- a/examples/client-get-tls-custom.toit +++ b/examples/client-get-tls-custom.toit @@ -4,12 +4,15 @@ 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: @@ -17,7 +20,7 @@ main: client.close -SERVER-CERT ::= x509.Certificate.parse """ +SERVER-CERT ::= """ -----BEGIN CERTIFICATE----- MIIDkzCCAnugAwIBAgIUb3nSgGzXBdgsDhg8shods8EHszAwDQYJKoZIhvcNAQEL BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM diff --git a/examples/server-tls.toit b/examples/server-tls.toit index 9b11517..81fd7d1 100644 --- a/examples/server-tls.toit +++ b/examples/server-tls.toit @@ -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": diff --git a/src/server.toit b/src/server.toit index 941a1ae..fe2bb06 100644 --- a/src/server.toit +++ b/src/server.toit @@ -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 """ + + +

Hello world

+ + + """ + 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 diff --git a/tests/google-test.toit b/tests/google-test.toit index 3df7132..66a875b 100644 --- a/tests/google-test.toit +++ b/tests/google-test.toit @@ -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" "/" diff --git a/tests/package.lock b/tests/package.lock index 598cf1f..2193b27 100644 --- a/tests/package.lock +++ b/tests/package.lock @@ -1,3 +1,4 @@ +sdk: ^2.0.0-alpha.91 prefixes: certificate_roots: toit-cert-roots http: .. @@ -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 diff --git a/tests/package.yaml b/tests/package.yaml index 8916ec1..02d3522 100644 --- a/tests/package.yaml +++ b/tests/package.yaml @@ -1,6 +1,6 @@ dependencies: certificate_roots: url: github.com/toitware/toit-cert-roots - version: ^1.3.2 + version: ^1.6.1 http: path: ..