Skip to content
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

Kebabify. #141

Merged
merged 1 commit into from
Jun 21, 2024
Merged
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
6 changes: 3 additions & 3 deletions examples/client-get-tls-custom.toit
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import net.x509
main:
network := net.open
client := http.Client.tls network
--root_certificates=[SERVER_CERT]
--root-certificates=[SERVER-CERT]

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

client.close

SERVER_CERT ::= x509.Certificate.parse """
SERVER-CERT ::= x509.Certificate.parse """
-----BEGIN CERTIFICATE-----
MIIDkzCCAnugAwIBAgIUb3nSgGzXBdgsDhg8shods8EHszAwDQYJKoZIhvcNAQEL
BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
Expand Down
2 changes: 1 addition & 1 deletion examples/client-post.toit
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ main:
"foo": 42,
"bar": 499,
}
data := json.decode_stream response.body
data := json.decode-stream response.body
client.close
print data
10 changes: 5 additions & 5 deletions examples/local-ws-client.toit
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ main:
network := net.open
client := http.Client network

websocket := client.web_socket --host="localhost" --port=8000
websocket := client.web-socket --host="localhost" --port=8000

task --background::
print "Message received: '$websocket.receive'"
while reader := websocket.start_receiving:
while reader := websocket.start-receiving:
size := 0
data := #[]
while chunk := reader.read:
data += chunk
if reader.is_text:
print "Message received: '$data.to_string'"
if reader.is-text:
print "Message received: '$data.to-string'"
else:
print "Message received: size $data.size."

websocket.send "Hello, World!"
websocket.send "Hello, World!"
websocket.send "Hello, World!"
writer := websocket.start_sending
writer := websocket.start-sending
writer.write "Hello, World!"
writer.write "Now is the time for all good men"
writer.write "to come to the aid of the party."
Expand Down
12 changes: 6 additions & 6 deletions examples/server-tls.toit
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ITEMS := ["FOO", "BAR", "BAZ"]
main:
network := net.open
server := http.Server.tls
--certificate=TLS_SERVER_CERT
--certificate=TLS-SERVER-CERT
server.listen network 8080:: | request/http.RequestIncoming writer/http.ResponseWriter |
if request.path == "/empty":
else if request.path == "/json":
Expand All @@ -27,17 +27,17 @@ main:
writer.out.write "hello\n"
else if request.path == "/500":
writer.headers.set "Content-Type" "text/plain"
writer.write_headers 500
writer.write-headers 500
writer.out.write "hello\n"
else if request.path == "/599":
writer.headers.set "Content-Type" "text/plain"
writer.write_headers 599 --message="Dazed and confused"
writer.write-headers 599 --message="Dazed and confused"
writer.close

// Self-signed certificate with "localhost" Common-Name.
TLS_SERVER_CERT ::= tls.Certificate SERVER_CERT SERVER_KEY
TLS-SERVER-CERT ::= tls.Certificate SERVER-CERT SERVER-KEY

SERVER_CERT ::= x509.Certificate.parse """
SERVER-CERT ::= x509.Certificate.parse """
-----BEGIN CERTIFICATE-----
MIIDkzCCAnugAwIBAgIUb3nSgGzXBdgsDhg8shods8EHszAwDQYJKoZIhvcNAQEL
BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
Expand All @@ -62,7 +62,7 @@ eLYDrha/bg==
-----END CERTIFICATE-----
"""

SERVER_KEY ::= """
SERVER-KEY ::= """
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0uBjd9ybX8chE
+cAfuuxZ/Ifhtay1M/UtcgzVnfW+/dxg3KDO3aY2OiHYkB5LrhS3jTcyzhOzS07R
Expand Down
14 changes: 7 additions & 7 deletions examples/server.toit
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ ITEMS := ["FOO", "BAR", "BAZ"]
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/"
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 |
server.listen tcp-socket:: | request/http.RequestIncoming writer/http.ResponseWriter |
resource := request.query.resource
if resource == "/empty":
else if resource == "/":
Expand All @@ -36,16 +36,16 @@ main:
json.encode ITEMS
else if resource == "/headers":
writer.headers.set "Http-Test-Header" "going strong"
writer.write_headers 200
writer.write-headers 200
else if resource == "/500":
writer.headers.set "Content-Type" "text/plain"
writer.write_headers 500
writer.write-headers 500
writer.out.write "Failure\n"
else if resource == "/599":
writer.headers.set "Content-Type" "text/plain"
writer.write_headers 599 --message="Dazed and confused"
writer.write-headers 599 --message="Dazed and confused"
else:
writer.headers.set "Content-Type" "text/plain"
writer.write_headers 404
writer.write-headers 404
writer.out.write "Not found\n"
writer.close
48 changes: 24 additions & 24 deletions examples/ws-talk-to-self.toit
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,58 @@ The server simply echos back any incoming message.

main:
network := net.open
port := start_server network
run_client network port
port := start-server network
run-client network port

run_client network port/int -> none:
run-client network port/int -> none:
client := http.Client network

web_socket := client.web_socket --host="localhost" --port=port
web-socket := client.web-socket --host="localhost" --port=port

task --background:: client_reading web_socket
task --background:: client-reading web-socket

client_sending web_socket
client-sending web-socket
sleep --ms=200

client_sending web_socket -> none:
web_socket.send "Hello, World!"
web_socket.send "Hello, World!"
web_socket.send "Hello, World!"
writer := web_socket.start_sending
client-sending web-socket -> none:
web-socket.send "Hello, World!"
web-socket.send "Hello, World!"
web-socket.send "Hello, World!"
writer := web-socket.start-sending
writer.write "Hello, World!"
writer.write "Now is the time for all good men"
writer.write "to come to the aid of the party."
writer.close
web_socket.send #[3, 4, 5]
web-socket.send #[3, 4, 5]

client_reading web_socket -> none:
client-reading web-socket -> none:
// Each message can come with its own reader, which can be
// useful if messages are large.
while reader := web_socket.start_receiving:
while reader := web-socket.start-receiving:
size := 0
text := reader.is_text ? "" : null
text := reader.is-text ? "" : null
while ba := reader.read:
if text: text += ba.to_string
if text: text += ba.to-string
size += ba.size
if text:
print "Message received: '$text'"
else:
print "Message received: size $size."

start_server network -> int:
server_socket := network.tcp_listen 0
port := server_socket.local_address.port
start-server network -> int:
server-socket := network.tcp-listen 0
port := server-socket.local-address.port
server := http.Server
task --background::
server.listen server_socket:: | request/http.RequestIncoming response_writer/http.ResponseWriter |
server.listen server-socket:: | request/http.RequestIncoming response-writer/http.ResponseWriter |
if request.path == "/":
web_socket := server.web_socket request response_writer
web-socket := server.web-socket request response-writer
// The server end of the web socket just echoes back what it gets.
// Here we don't use a new reader for each message, but just get
// the message with a single call to `receive`.
while data := web_socket.receive:
while data := web-socket.receive:
print "Got $data"
web_socket.send data
web-socket.send data
else:
response_writer.write_headers http.STATUS_NOT_FOUND --message="Not Found"
response-writer.write-headers http.STATUS-NOT-FOUND --message="Not Found"
return port
36 changes: 18 additions & 18 deletions src/chunked.toit
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ This is an adapter that converts a chunked stream (RFC 2616) to a stream of
class ChunkedReader_ extends io.Reader:
connection_/Connection? := null
reader_/io.Reader? := ?
left_in_chunk_ := 0 // How much more raw data we are waiting for before the next size line.
left-in-chunk_ := 0 // How much more raw data we are waiting for before the next size line.

constructor .connection_ .reader_:

/**
Returns the underlying reader, which may have buffered up data.

The ChunkedReader is unusable after a called to $detach_reader.
The ChunkedReader is unusable after a called to $detach-reader.

Deprecated.
*/
// TODO(florian): remove already now?
detach_reader -> io.Reader:
detach-reader -> io.Reader:
r := reader_
reader_ = null
return r
Expand All @@ -47,29 +47,29 @@ class ChunkedReader_ extends io.Reader:
while true:
if not connection_:
return null
if left_in_chunk_ > 0:
result := reader_.read --max_size=left_in_chunk_
if not result: throw io.Reader.UNEXPECTED_END_OF_READER
left_in_chunk_ -= result.size
if left_in_chunk_ == 0:
if left-in-chunk_ > 0:
result := reader_.read --max-size=left-in-chunk_
if not result: throw io.Reader.UNEXPECTED-END-OF-READER
left-in-chunk_ -= result.size
if left-in-chunk_ == 0:
expect_ '\r'
expect_ '\n'
return result

raw_length := reader_.read_bytes_up_to '\r'
raw-length := reader_.read-bytes-up-to '\r'
expect_ '\n'

left_in_chunk_ = int.parse raw_length --radix=16
left-in-chunk_ = int.parse raw-length --radix=16

// End is indicated by a zero hex length.
if left_in_chunk_ == 0:
if left-in-chunk_ == 0:
expect_ '\r'
expect_ '\n'
connection_.reading_done_ this
connection_.reading-done_ this
connection_ = null

expect_ byte/int:
b := reader_.peek_byte 0
b := reader_.peek-byte 0
if b != byte: throw "PROTOCOL_ERROR"
reader_.skip 1

Expand All @@ -90,13 +90,13 @@ class ChunkedWriter_ extends io.CloseableWriter:
constructor .connection_ .writer_:

// We don't know the amount of data ahead of time, so it may already be done.
is_done_ -> bool:
is-done_ -> bool:
return true

try_write_ data/io.Data from/int to/int -> int:
try-write_ data/io.Data from/int to/int -> int:
size := to - from
if size == 0: return 0
write_header_ size
write-header_ size
writer_.write data from to // Always writes all data.
// Once we've sent the data, the other side might conclude that
// they have gotten everything they need, so we don't want to throw
Expand All @@ -113,10 +113,10 @@ class ChunkedWriter_ extends io.CloseableWriter:
writer_.write "0"
writer_.write CRLF_
writer_.write CRLF_
connection_.writing_done_ this
connection_.writing-done_ this
connection_ = null

write_header_ length/int:
write-header_ length/int:
writer_.write
length.stringify 16
writer_.write CRLF_
Loading