forked from http-kit/http-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.clj
243 lines (210 loc) · 10.1 KB
/
server.clj
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
(ns org.httpkit.server
(:require [org.httpkit.encode :refer [base64-encode]])
(:import [org.httpkit.server AsyncChannel HttpServer RingHandler ProxyProtocolOption]
[org.httpkit.logger ContextLogger EventLogger EventNames]
java.security.MessageDigest))
;;;; Ring server
(defn run-server
"Starts HTTP server and returns
(fn [& {:keys [timeout] ; Timeout (msecs) to wait on existing reqs to complete
:or {timeout 100}}])
Server is mostly Ring compatible, see http://http-kit.org/migration.html
for differences.
Options:
:ip ; Which ip (if has many ips) to bind
:port ; Which port listen incomming request
:thread ; Http worker thread count
:queue-size ; Max job queued before reject to project self
:max-body ; Max http body: 8m
:max-ws ; Max websocket message size
:max-line ; Max http inital line length
:proxy-protocol ; Proxy protocol e/o #{:disable :enable :optional}
:worker-name-prefix ; Worker thread name prefix
:worker-pool ; ExecutorService to use for request-handling (:thread,
:worker-name-prefix, :queue-size are ignored if set)
:error-logger ; Arity-2 fn (args: string text, exception) to log errors
:warn-logger ; Arity-2 fn (args: string text, exception) to log warnings
:event-logger ; Arity-1 fn (arg: string event name)
:event-names ; map of HTTP-Kit event names to respective loggable event names"
[handler
& [{:keys [ip port thread queue-size max-body max-ws max-line
proxy-protocol worker-name-prefix worker-pool
error-logger warn-logger event-logger event-names]
:or {ip "0.0.0.0"
port 8090
thread 4
queue-size 20480
max-body 8388608
max-ws 4194304
max-line 8192
proxy-protocol :disable
worker-name-prefix "worker-"}}]]
(let [err-logger (if error-logger
(reify ContextLogger
(log [this message error] (error-logger message error)))
ContextLogger/ERROR_PRINTER)
evt-logger (if event-logger
(reify EventLogger
(log [this event] (event-logger event)))
EventLogger/NOP)
evt-names (cond
(nil? event-names) EventNames/DEFAULT
(map? event-names) (EventNames. event-names)
(instance? EventNames
event-names) event-names
:otherwise (throw (IllegalArgumentException.
(format "Invalid event-names: (%s) %s"
(class event-names) (pr-str event-names)))))
h (if worker-pool
(RingHandler. handler worker-pool err-logger evt-logger evt-names)
(RingHandler. thread handler worker-name-prefix queue-size err-logger evt-logger evt-names))
proxy-enum (case proxy-protocol
:enable ProxyProtocolOption/ENABLED
:disable ProxyProtocolOption/DISABLED
:optional ProxyProtocolOption/OPTIONAL)
s (HttpServer. ip port h max-body max-line max-ws proxy-enum
err-logger
(if warn-logger
(reify ContextLogger
(log [this message error] (warn-logger message error)))
HttpServer/DEFAULT_WARN_LOGGER)
evt-logger
evt-names)]
(.start s)
(with-meta
(fn stop-server [& {:keys [timeout] :or {timeout 100}}]
;; graceful shutdown:
;; 1. server stop accept new request
;; 2. wait for existing requests to finish
;; 3. close the server
(.stop s timeout))
{:local-port (.getPort s)
:server s})))
;;;; Asynchronous extension
(defprotocol Channel
"Unified asynchronous channel interface for HTTP (streaming or long-polling)
and WebSocket."
(open? [ch] "Returns true iff channel is open.")
(close [ch]
"Closes the channel. Idempotent: returns true if the channel was actually
closed, or false if it was already closed.")
(websocket? [ch] "Returns true iff channel is a WebSocket.")
(send! [ch data] [ch data close-after-send?]
"Sends data to client and returns true if the data was successfully sent,
or false if the channel is closed. Data is sent directly to the client,
NO RING MIDDLEWARE IS APPLIED.
When unspecified, `close-after-send?` defaults to true for HTTP channels
and false for WebSocket.
Data form: {:headers _ :status _ :body _} or just body. Note that :headers
and :status will be stripped for WebSocket and for HTTP streaming responses
after the first.
For WebSocket, a text frame is sent to client if data is String,
a binary frame when data is byte[] or InputStream. For for HTTP streaming
responses, data can be one of the type defined by Ring spec")
(on-receive [ch callback]
"Sets handler (fn [message]) for notification of client WebSocket
messages. Message ordering is guaranteed by server.
The message argument could be a string or a byte[].")
(on-ping [ch callback]
"Sets handler (fn [data]) for notification of client WebSocket pings. The
data param represents application data and will by a byte[].")
(on-close [ch callback]
"Sets handler (fn [status]) for notification of channel being closed by the
server or client. Handler will be invoked at most once. Useful for clean-up.
Callback status argument:
:server-close : Channel closed by sever
:client-close : HTTP channel closed by client
:normal : WebSocket closed by client (CLOSE_NORMAL)
:going-away : WebSocket closed by client (CLOSE_GOING_AWAY)
:protocol-error : WebSocket closed by client (CLOSE_PROTOCOL_ERROR)
:unsupported : WebSocket closed by client (CLOSE_UNSUPPORTED)
:unknown : WebSocket closed by client (unknown reason)"))
(extend-type AsyncChannel
Channel
(open? [ch] (not (.isClosed ch)))
(close [ch] (.serverClose ch 1000))
(websocket? [ch] (.isWebSocket ch))
(send!
([ch data] (.send ch data (not (websocket? ch))))
([ch data close-after-send?] (.send ch data (boolean close-after-send?))))
(on-receive [ch callback] (.setReceiveHandler ch callback))
(on-ping [ch callback] (.setPingHandler ch callback))
(on-close [ch callback] (.setCloseHandler ch callback)))
;;;; WebSocket
(defn sec-websocket-accept [sec-websocket-key]
(let [md (MessageDigest/getInstance "SHA1")
websocket-13-guid "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"]
(base64-encode
(.digest md (.getBytes (str sec-websocket-key websocket-13-guid))))))
(def accept "DEPRECATED for `sec-websocket-accept" sec-websocket-accept)
(defn websocket-handshake-check
"Returns `sec-ws-accept` string iff given Ring request is a valid
WebSocket handshake."
[^AsyncChannel ch ring-req]
(when-let [sec-ws-key (get-in ring-req [:headers "sec-websocket-key"])]
(try
(sec-websocket-accept sec-ws-key)
(catch Exception _ nil))))
(defn send-checked-websocket-handshake!
"Given an AsyncChannel and `sec-ws-accept` string, unconditionally
sends handshake to upgrade given AsyncChannel to a WebSocket.
See also `websocket-handshake-check`."
[^AsyncChannel ch ^String sec-ws-accept]
(.sendHandshake ch
{"Upgrade" "websocket"
"Connection" "Upgrade"
"Sec-WebSocket-Accept" sec-ws-accept}))
(defn send-websocket-handshake!
"Returns true iff successfully upgraded a valid WebSocket request."
[^AsyncChannel ch ring-req]
(when-let [sec-ws-accept (websocket-handshake-check ch ring-req)]
(send-checked-websocket-handshake! ch sec-ws-accept)))
;; (defn websocket-req? [ring-req] (:websocket? ring-req))
;; (defn async-channel [ring-req] (:async-channel ring-req))
;; (defn async-response [ring-req] {:body (:async-channel ring-req)})
(defmacro with-channel
"Evaluates body with `ch-name` bound to the request's underlying
asynchronous HTTP or WebSocket channel, and returns {:body AsyncChannel}
as an implementation detail.
Note: for WebSocket requests, WebSocket handshake will be sent *after*
body is evaluated. I.e. body cannot call `send!`.
;; Asynchronous HTTP response (with optional streaming)
(defn my-async-handler [request]
(with-channel request ch ; Request's channel
;; Make ch available to whoever can deliver the response to it; ex.:
(swap! clients conj ch))) ; given (def clients (atom #{}))
;; Some place later:
(doseq [ch @clients]
(swap! clients disj ch)
(send! ch {:status 200
:headers {\"Content-Type\" \"text/html\"}
:body your-async-response}
;; false ; Uncomment to use chunk encoding for HTTP streaming
)))
;; WebSocket response
(defn my-chatroom-handler [request]
(if-not (:websocket? request)
{:status 200 :body \"Welcome to the chatroom! JS client connecting...\"}
(with-channel request ch
(println \"New WebSocket channel:\" ch)
(on-receive ch (fn [msg] (println \"on-receive:\" msg)))
(on-close ch (fn [status] (println \"on-close:\" status))))))
Channel API (see relevant docstrings for more info):
(open? [ch])
(close [ch])
(websocket? [ch])
(send! [ch data] [ch data close-after-send?])
(on-receieve [ch callback])
(on-close [ch callback])
See org.httpkit.timer ns for optional timeout facilities."
[ring-req ch-name & body]
`(let [ring-req# ~ring-req
~ch-name (:async-channel ring-req#)]
(if (:websocket? ring-req#)
(if-let [sec-ws-accept# (websocket-handshake-check ~ch-name ring-req#)]
(do
~@body ; Eval body before handshake to allow hooks to be established, Ref. #318
(send-checked-websocket-handshake! ~ch-name sec-ws-accept#)
{:body ~ch-name})
{:status 400 :body "Bad Sec-WebSocket-Key header"})
(do ~@body {:body ~ch-name}))))