From 42c318281df8f605d84dab26b9206506e1a1130a Mon Sep 17 00:00:00 2001 From: qvisten999 Date: Fri, 10 Sep 2021 15:15:30 +0200 Subject: [PATCH 01/11] wip --- net/fetch.toit | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 net/fetch.toit diff --git a/net/fetch.toit b/net/fetch.toit new file mode 100644 index 0000000..1ab915b --- /dev/null +++ b/net/fetch.toit @@ -0,0 +1,64 @@ + +import net +import net.x509 as net +import http +import tls + +main: + network_interface := net.open + + host := "www.google.com" + tcp := network_interface.tcp_connect host 443 + + socket := tls.Socket.client tcp + --server_name=host + --root_certificates=[GLOBALSIGN_ROOT] + + connection := http.Connection socket host + request := connection.new_request "GET" "/" + response := request.send + + bytes := 0 + while data := response.read: + bytes += data.size + + print "Read $bytes bytes from https://$host/" + +// To find trusted roots like this: +// $ openssl s_client -showcerts -verify 5 -connect example.com:443 | grep i: +// The last line with i: shows the name of the root cert. You can find the +// trusted root certificate in Chrome at chrome://settings/certificates (the +// Authorities tab) and export it to a text file. + +GLOBALSIGN_ROOT ::= net.Certificate.parse """\ +-----BEGIN CERTIFICATE----- +MIIFYjCCBEqgAwIBAgIQd70NbNs2+RrqIQ/E8FjTDTANBgkqhkiG9w0BAQsFADBX +MQswCQYDVQQGEwJCRTEZMBcGA1UEChMQR2xvYmFsU2lnbiBudi1zYTEQMA4GA1UE +CxMHUm9vdCBDQTEbMBkGA1UEAxMSR2xvYmFsU2lnbiBSb290IENBMB4XDTIwMDYx +OTAwMDA0MloXDTI4MDEyODAwMDA0MlowRzELMAkGA1UEBhMCVVMxIjAgBgNVBAoT +GUdvb2dsZSBUcnVzdCBTZXJ2aWNlcyBMTEMxFDASBgNVBAMTC0dUUyBSb290IFIx +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAthECix7joXebO9y/lD63 +ladAPKH9gvl9MgaCcfb2jH/76Nu8ai6Xl6OMS/kr9rH5zoQdsfnFl97vuf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SkHrEoo9C0dhemMXoh6dFSPsjbdBZBiLg9 +NR3t5P+T4Vxfq7vqfM/b5A3Ri1fyJm9bvhdGaJQ3b2t6yMAYN/olUazsaL+yyEn9 +WprKASOshIArAoyZl+tJaox118fessmXn1hIVw41oeQa1v1vg4Fv74zPl6/AhSrw +9U5pCZEt4Wi4wStz6dTZ/CLANx8LZh1J7QJVj2fhMtfTJr9w4z30Z209fOU0iOMy ++qduBmpvvYuR7hZL6Dupszfnw0Skfths18dG9ZKb59UhvmaSGZRVbNQpsg3BZlvi +d0lIKO2d1xozclOzgjXPYovJJIultzkMu34qQb9Sz/yilrbCgj8= +-----END CERTIFICATE-----""" From a39ee98ee2380c5c69c3d70f411871802ac41721 Mon Sep 17 00:00:00 2001 From: qvisten999 Date: Tue, 14 Sep 2021 11:31:40 +0200 Subject: [PATCH 02/11] added tls example --- net/README.md | 15 +++++++++++++++ net/{fetch.toit => tls.toit} | 5 +++++ 2 files changed, 20 insertions(+) create mode 100644 net/README.md rename net/{fetch.toit => tls.toit} (96%) diff --git a/net/README.md b/net/README.md new file mode 100644 index 0000000..e51eddd --- /dev/null +++ b/net/README.md @@ -0,0 +1,15 @@ +# Fading example + +This example sends a request to Google.com and prints the size of the response. + +## Running the example + +To run the example on your ESP32, execute the following command: + +```bash +toit run tls.toit +``` + +## License + +[BSD0](https://choosealicense.com/licenses/0bsd/) diff --git a/net/fetch.toit b/net/tls.toit similarity index 96% rename from net/fetch.toit rename to net/tls.toit index 1ab915b..ac9d915 100644 --- a/net/fetch.toit +++ b/net/tls.toit @@ -1,3 +1,8 @@ +/** +Tls example code. + +This example sends a request to Google.com and prints the size of the response. +*/ import net import net.x509 as net From 2b12139cd3ad8df7275d7cdf494053f7732cba7b Mon Sep 17 00:00:00 2001 From: qvisten999 Date: Tue, 14 Sep 2021 11:32:10 +0200 Subject: [PATCH 03/11] fixing typo --- net/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/README.md b/net/README.md index e51eddd..a565c1b 100644 --- a/net/README.md +++ b/net/README.md @@ -1,4 +1,4 @@ -# Fading example +# Tls example This example sends a request to Google.com and prints the size of the response. From 2e82a584d167d4b849167cd139a3d8d330480c70 Mon Sep 17 00:00:00 2001 From: qvisten999 Date: Tue, 14 Sep 2021 11:42:11 +0200 Subject: [PATCH 04/11] added fibonacci example --- basics/language/README.md | 15 +++++++++++++++ basics/language/fibonacci.toit | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 basics/language/README.md create mode 100644 basics/language/fibonacci.toit diff --git a/basics/language/README.md b/basics/language/README.md new file mode 100644 index 0000000..fc8e681 --- /dev/null +++ b/basics/language/README.md @@ -0,0 +1,15 @@ +# Fibonacci example + +This example shows how to calculate a specific number in the fibonacci sequence using recursion. + +## Running the example + +To run the example on your ESP32, execute the following command: + +```bash +toit run fibonacci.toit +``` + +## License + +[BSD0](https://choosealicense.com/licenses/0bsd/) diff --git a/basics/language/fibonacci.toit b/basics/language/fibonacci.toit new file mode 100644 index 0000000..c5d27a9 --- /dev/null +++ b/basics/language/fibonacci.toit @@ -0,0 +1,16 @@ +/** +This example defines a top-level function called fib that is not a member of any class. +It prints the 10th number in the fibonacci sequence. + +The fib function is recursive, calling itself, and also makes use of a few new features. +The if-statement is well known from other languages. In Toit it works by taking an expression +and conditionally evaluating a block. Like other blocks we could have used indentation to group +multiple lines. +*/ + +fib n: + if n <= 1: return n + return (fib n-1) + (fib n-2) + +main: + print "The 10th Fibonacci number is $(fib 10)" From bceb4dd1c19322244a7719b2713e2402bf0b7ae9 Mon Sep 17 00:00:00 2001 From: Jacob Qvist Jensen Date: Tue, 14 Sep 2021 12:36:55 +0200 Subject: [PATCH 05/11] Update net/README.md Co-authored-by: Florian Loitsch --- net/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/README.md b/net/README.md index a565c1b..17d965d 100644 --- a/net/README.md +++ b/net/README.md @@ -1,4 +1,4 @@ -# Tls example +# TLS example This example sends a request to Google.com and prints the size of the response. From e3ab23a8898b8b9f8ce3de91973db2b0e4b3a470 Mon Sep 17 00:00:00 2001 From: Jacob Qvist Jensen Date: Tue, 14 Sep 2021 12:37:02 +0200 Subject: [PATCH 06/11] Update net/tls.toit Co-authored-by: Florian Loitsch --- net/tls.toit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/tls.toit b/net/tls.toit index ac9d915..b4c2a2c 100644 --- a/net/tls.toit +++ b/net/tls.toit @@ -1,5 +1,5 @@ /** -Tls example code. +TLS example code. This example sends a request to Google.com and prints the size of the response. */ From a1fdc38be86ecfbd6bca1afc2ad200b396d8d58a Mon Sep 17 00:00:00 2001 From: Jacob Qvist Jensen Date: Tue, 14 Sep 2021 12:37:21 +0200 Subject: [PATCH 07/11] Update net/tls.toit Co-authored-by: Florian Loitsch --- net/tls.toit | 1 + 1 file changed, 1 insertion(+) diff --git a/net/tls.toit b/net/tls.toit index b4c2a2c..50715c0 100644 --- a/net/tls.toit +++ b/net/tls.toit @@ -10,6 +10,7 @@ import http import tls main: + // Open the default network interface (typically WiFi). network_interface := net.open host := "www.google.com" From 8909dd34ed03756e0d23fe325ba7f79d4e764699 Mon Sep 17 00:00:00 2001 From: Jacob Qvist Jensen Date: Tue, 14 Sep 2021 12:37:32 +0200 Subject: [PATCH 08/11] Update net/tls.toit Co-authored-by: Florian Loitsch --- net/tls.toit | 1 + 1 file changed, 1 insertion(+) diff --git a/net/tls.toit b/net/tls.toit index 50715c0..8652d5d 100644 --- a/net/tls.toit +++ b/net/tls.toit @@ -14,6 +14,7 @@ main: network_interface := net.open host := "www.google.com" + // Establish a TCP connection with Google.com. tcp := network_interface.tcp_connect host 443 socket := tls.Socket.client tcp From f336b134835f36a15b9e63a5860e32d780fe5e70 Mon Sep 17 00:00:00 2001 From: Jacob Qvist Jensen Date: Tue, 14 Sep 2021 12:37:43 +0200 Subject: [PATCH 09/11] Update net/tls.toit Co-authored-by: Florian Loitsch --- net/tls.toit | 1 + 1 file changed, 1 insertion(+) diff --git a/net/tls.toit b/net/tls.toit index 8652d5d..1da6557 100644 --- a/net/tls.toit +++ b/net/tls.toit @@ -17,6 +17,7 @@ main: // Establish a TCP connection with Google.com. tcp := network_interface.tcp_connect host 443 + // Upgrade the TCP socket to a secure TLS socket. socket := tls.Socket.client tcp --server_name=host --root_certificates=[GLOBALSIGN_ROOT] From 3718b09480ff6ee3db6923fb09207645c826c342 Mon Sep 17 00:00:00 2001 From: Jacob Qvist Jensen Date: Tue, 14 Sep 2021 12:37:55 +0200 Subject: [PATCH 10/11] Update net/tls.toit Co-authored-by: Florian Loitsch --- net/tls.toit | 1 + 1 file changed, 1 insertion(+) diff --git a/net/tls.toit b/net/tls.toit index 1da6557..3960ef2 100644 --- a/net/tls.toit +++ b/net/tls.toit @@ -22,6 +22,7 @@ main: --server_name=host --root_certificates=[GLOBALSIGN_ROOT] + // Instantiate an HTTP connection object, on which we can do requests. connection := http.Connection socket host request := connection.new_request "GET" "/" response := request.send From c6d25aed16568c6255ee9ec02e6554b689edcb7b Mon Sep 17 00:00:00 2001 From: Jacob Qvist Jensen Date: Tue, 14 Sep 2021 12:38:21 +0200 Subject: [PATCH 11/11] Update net/tls.toit Co-authored-by: Florian Loitsch --- net/tls.toit | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/tls.toit b/net/tls.toit index 3960ef2..a0f9aa7 100644 --- a/net/tls.toit +++ b/net/tls.toit @@ -24,7 +24,9 @@ main: // Instantiate an HTTP connection object, on which we can do requests. connection := http.Connection socket host + // Create and send the request. request := connection.new_request "GET" "/" + // Before sending the request, we could tweak the headers of the request here. response := request.send bytes := 0