Commit8158fa09Recorded4 Mar 2026Repositorysigil-tls

Add Scheme module definition for sigil-tls package

Message

The sigil-tls package was native-only with no .sgl source file, so (sigil tls) couldn't be loaded by the build system. This blocked all downstream packages (sigil-websocket, sigil-http, sigil-org, etc.).

Add src/sigil/tls.sgl with define-native declarations and specs for all TLS functions, following the same pattern used by sigil-crypto. Add compile-sigil-modules step to the build task.

Changed
 package.sgl       |   6 +++++-
 src/sigil/tls.sgl | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 1 deletion(-)
Diff
package.sglmodified
@@ -53,4 +53,8 @@
53
54
;; Create static library
55
(create-static-library
56
name: "sigil-tls")))))
+56
name: "sigil-tls")
+57
+58
;; Compile Scheme module
+59
(compile-sigil-modules
+60
sources: "src/**/*.sgl")))))
src/sigil/tls.sgladded
@@ -0,0 +1,112 @@
+1
;;; (sigil tls) - TLS/SSL Connections
+2
;;;
+3
;;; Secure TCP connections using mbedTLS. Supports TLS 1.2 client
+4
;;; connections with system CA certificate verification.
+5
;;;
+6
;;; ```scheme
+7
;;; (import (sigil tls))
+8
;;;
+9
;;; (let ((conn (tls-connect "example.com" 443)))
+10
;;; (tls-write conn "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
+11
;;; (display (tls-read conn))
+12
;;; (tls-close conn))
+13
;;; ```
+14
+15
(define-library (sigil tls)
+16
(export
+17
tls-connection?
+18
tls-connect
+19
tls-read
+20
tls-read-bytevector
+21
tls-write
+22
tls-close
+23
tls-closed?
+24
tls-set-non-blocking!
+25
tls-upgrade)
+26
+27
(begin
+28
+29
;;; Check if value is a TLS connection object.
+30
(define-native (tls-connection? value)
+31
(: any? -> boolean?))
+32
+33
;;; Establish a TLS connection to the specified host and port.
+34
;;;
+35
;;; Returns a TLS connection object on success, #f on failure.
+36
;;; Certificates are verified against system CA certificates by default.
+37
;;; Set SIGIL_TLS_INSECURE=1 to skip verification (testing only).
+38
;;;
+39
;;; ```scheme
+40
;;; (tls-connect "example.com" 443) ; => tls-connection | #f
+41
;;; ```
+42
(define-native (tls-connect hostname port)
+43
(: string? integer? -> any?))
+44
+45
;;; Read data from a TLS connection.
+46
;;;
+47
;;; Returns a string with data, #f on error, or eof-object if the
+48
;;; connection was closed by the peer. An optional max-bytes argument
+49
;;; controls the buffer size (default 4096).
+50
;;;
+51
;;; ```scheme
+52
;;; (tls-read conn) ; => string | #f | eof-object
+53
;;; (tls-read conn 8192) ; read up to 8192 bytes
+54
;;; ```
+55
(define-native (tls-read connection . max-bytes)
+56
(: any? -> any?))
+57
+58
;;; Read raw bytes from a TLS connection into a bytevector.
+59
;;;
+60
;;; Like `tls-read` but returns a bytevector instead of a string,
+61
;;; preserving raw bytes without encoding interpretation.
+62
;;;
+63
;;; ```scheme
+64
;;; (tls-read-bytevector conn) ; => bytevector | #f | eof-object
+65
;;; (tls-read-bytevector conn 8192) ; read up to 8192 bytes
+66
;;; ```
+67
(define-native (tls-read-bytevector connection . max-bytes)
+68
(: any? -> any?))
+69
+70
;;; Write data to a TLS connection.
+71
;;;
+72
;;; Accepts a string or bytevector. Returns the number of bytes
+73
;;; written, or #f on error.
+74
;;;
+75
;;; ```scheme
+76
;;; (tls-write conn "GET / HTTP/1.1\r\n\r\n") ; => integer | #f
+77
;;; ```
+78
(define-native (tls-write connection data)
+79
(: any? (any-of string? bytevector?) -> (any-of integer? boolean?)))
+80
+81
;;; Close a TLS connection.
+82
;;;
+83
;;; Sends a close notification and frees resources. Returns #t.
+84
(define-native (tls-close connection)
+85
(: any? -> boolean?))
+86
+87
;;; Check if a TLS connection is closed.
+88
(define-native (tls-closed? connection)
+89
(: any? -> boolean?))
+90
+91
;;; Set the underlying socket to non-blocking mode.
+92
;;;
+93
;;; Enable defaults to #t if not provided. Returns #t on success.
+94
;;;
+95
;;; ```scheme
+96
;;; (tls-set-non-blocking! conn) ; enable non-blocking
+97
;;; (tls-set-non-blocking! conn #f) ; disable non-blocking
+98
;;; ```
+99
(define-native (tls-set-non-blocking! connection . enable)
+100
(: any? -> boolean?))
+101
+102
;;; Upgrade an existing TCP socket to a TLS connection.
+103
;;;
+104
;;; Performs a TLS handshake on an existing socket connection (STARTTLS).
+105
;;; Takes ownership of the socket's file descriptor; the original socket
+106
;;; should not be used after this call.
+107
;;;
+108
;;; ```scheme
+109
;;; (tls-upgrade sock "mail.example.com") ; => tls-connection | #f
+110
;;; ```
+111
(define-native (tls-upgrade socket hostname)
+112
(: any? string? -> any?))))