-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspork-https.janet
More file actions
102 lines (87 loc) · 3.12 KB
/
spork-https.janet
File metadata and controls
102 lines (87 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
###
### spork-https.janet
###
### HTTPS support for spork using jsec
### Provides convenience wrappers around spork/http with TLS via jsec
###
(import spork/http)
(import jsec/tls :as tls)
(import jsec/crypto)
(import jsec/cert)
(defn- make-https-factory
"Create a stream factory for HTTPS with given TLS options"
[tls-opts]
(fn https-connect [host port &opt stream-opts]
# Merge TLS options from both sources
(let [opts (merge (or tls-opts {}) (or stream-opts {}))]
(tls/connect host port opts))))
(defn request
``Make an HTTPS request using spork/http with TLS stream factory.
Options:
* `:body` - Request body content
* `:headers` - Request headers table
* `:tls-opts` - TLS options passed to jsec/tls/connect
- `:verify` - Verify server certificate (default true)
- `:ca` - Path to CA certificate file
- `:cert` - Client certificate (for mTLS)
- `:key` - Client private key (for mTLS)
- `:security` - Security options (TLS versions, ciphers, etc.)
* `:stream-opts` - Additional stream options (merged with tls-opts)``
[method url &keys opts]
(let [tls-opts (get opts :tls-opts {})
# Build http-opts without :tls-opts
http-opts (merge opts {:stream-factory (make-https-factory tls-opts)})]
# Remove :tls-opts from http-opts
(put http-opts :tls-opts nil)
(http/request method url ;(kvs http-opts))))
(defn get
"Make an HTTPS GET request"
[url &keys opts]
(request "GET" url ;(kvs opts)))
(defn post
"Make an HTTPS POST request"
[url &keys opts]
(request "POST" url ;(kvs opts)))
(defn put
"Make an HTTPS PUT request"
[url &keys opts]
(request "PUT" url ;(kvs opts)))
(defn patch
"Make an HTTPS PATCH request"
[url &keys opts]
(request "PATCH" url ;(kvs opts)))
(defn delete
"Make an HTTPS DELETE request"
[url &keys opts]
(request "DELETE" url ;(kvs opts)))
(defn head
"Make an HTTPS HEAD request"
[url &keys opts]
(request "HEAD" url ;(kvs opts)))
(defn server
``Makes a simple HTTPS server using TLS.
Creates an HTTPS server that wraps connections with TLS.
Returns a new server stream.
Arguments:
* `handler` - Request handler function (same as spork/http)
* `cert` - Server certificate (PEM string or file path)
* `key` - Server private key (PEM string or file path)
* `host` - Host to bind to (default "0.0.0.0")
* `port` - Port to bind to (default "8443")
* `tls-opts` - Additional TLS options
- `:ca` - CA file for client certificate verification
- `:verify` - Whether to verify client certificates (default false)
- `:security` - Security options (TLS versions, ciphers, etc.)``
[handler cert key &opt host port tls-opts]
(default host "0.0.0.0")
(default port "8443")
(default tls-opts {})
# Convert port to string if needed
(def port-str (if (number? port) (string port) port))
# Create TLS context options with certificate and key
(let [ctx-opts (merge tls-opts {:cert cert :key key})]
# Use jsec/tls/server which handles TLS wrapping
(tls/server host port-str
(fn [tls-stream]
(http/server-handler tls-stream handler))
ctx-opts)))