Skip to content

Language and net examples #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions basics/language/README.md
Original file line number Diff line number Diff line change
@@ -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/)
16 changes: 16 additions & 0 deletions basics/language/fibonacci.toit
Original file line number Diff line number Diff line change
@@ -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)"
15 changes: 15 additions & 0 deletions net/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# TLS 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/)
75 changes: 75 additions & 0 deletions net/tls.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
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
import http
import tls

main:
// Open the default network interface (typically WiFi).
network_interface := net.open

host := "www.google.com"
// 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]

// 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
while data := response.read:
bytes += data.size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our example should do more.
Now I would need to look up another example to figure out how to actually deal with the data.
If you know how to easily handle the data, it should be trivial to change it to something more useful.
If you don't then it shows that we are missing something here... ;)

Suggested change
bytes += data.size
// Discard the data from the server, and just count the number of bytes.
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/kr9rH5zoQdsfnFl97vufKj6bwS
iV6nqlKr+CMny6SxnGPb15l+8Ape62im9MZaRw1NEDPjTrETo8gYbEvs/AmQ351k
KSUjB6G00j0uYODP0gmHu81I8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