Skip to content

Commit 7ab96ce

Browse files
committed
socket.starttls options (alpn, early_data)
1 parent 6175064 commit 7ab96ce

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

docs/Runtime Environment/Socket.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,36 @@ Attempts to add the TLS crypto layer to the socket, making the transport layer a
112112
#### Parameters (Client)
113113
1. The socket instance.
114114
2. The host name of the intended remote. This may be the same as what was passed to `socket.connect`. This name must be on the certificate that the remote will send.
115+
3. An optional table of options.
115116
#### Parameters (Server)
116117
1. The socket instance.
117118
2. An array of tables describing available certificates. Each certificate needs a `chain` and `private_key` field, which should be a string containing valid PEM. The `private_key` must be RSA.
119+
3. An optional table of options.
120+
#### Options
121+
- `alpn` — An array of protocol names to negotiate via ALPN. On success, the selected protocol is returned as a second value.
122+
- `early_data` — Data to send immediately after the TLS handshake begins. Client only.
118123
#### Returns
119-
True on success. On failure, returns false and the socket is closed. If the socket is already using TLS, returns nil.
124+
`true` on success and, if ALPN is used, the selected protocol. On failure, returns `false` and the socket is closed. If the socket is already using TLS, returns `nil`.
120125
#### Multitasking
121126
If called inside of a coroutine, this function yields. Otherwise, it blocks.
122-
```pluto norun title="Client Example"
127+
```pluto norun title="Client Example (Early Data)"
123128
local socket = require "pluto:socket"
124129
125130
local s = socket.connect("pluto-lang.org", 443)
126-
assert(s:starttls("pluto-lang.org"), "Failed to establish secure connection.")
127-
s:send("GET / HTTP/1.1\r\nHost: pluto-lang.org\r\nConnection: close\r\n\r\n")
131+
assert(s:starttls("pluto-lang.org", {
132+
early_data = "GET / HTTP/1.1\r\nHost: pluto-lang.org\r\nConnection: close\r\n\r\n"
133+
}))
128134
while data := s:recv() do
129135
print(data)
130136
end
131137
```
138+
```pluto norun title="Client Example (ALPN)"
139+
local socket = require "pluto:socket"
140+
local s = socket.connect("pluto-lang.org", 443)
141+
print(select(2, s:starttls("pluto-lang.org", {
142+
alpn = { "h2", "http/1.1" }
143+
})))
144+
```
132145
```pluto norun title="Server Example"
133146
local { http, scheduler, socket } = require "*"
134147

0 commit comments

Comments
 (0)