/
server.clj
875 lines (771 loc) · 30.3 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
(ns aleph.http.server
(:require
[aleph.http.core :as http]
[aleph.netty :as netty]
[aleph.flow :as flow]
[clj-commons.byte-streams :as bs]
[clojure.tools.logging :as log]
[clojure.string :as str]
[manifold.deferred :as d]
[manifold.stream :as s])
(:import
[java.util
EnumSet
TimeZone
Date
Locale]
[java.text
DateFormat
SimpleDateFormat]
[io.aleph.dirigiste
Stats$Metric]
[aleph.http.core
NettyRequest]
[io.netty.buffer
ByteBuf
ByteBufHolder
Unpooled]
[io.netty.channel
Channel
ChannelHandlerContext
ChannelHandler
ChannelPipeline]
[io.netty.handler.stream ChunkedWriteHandler]
[io.netty.handler.timeout
IdleState
IdleStateEvent]
[io.netty.handler.codec
TooLongFrameException]
[io.netty.handler.codec.http
DefaultFullHttpResponse
FullHttpRequest
HttpContent HttpHeaders HttpUtil
HttpContentCompressor
HttpRequest HttpResponse
HttpResponseStatus DefaultHttpHeaders
HttpServerCodec HttpVersion HttpMethod
LastHttpContent HttpServerExpectContinueHandler
HttpHeaderNames]
[io.netty.handler.codec.http.websocketx
WebSocketServerHandshakerFactory
WebSocketServerHandshaker
PingWebSocketFrame
PongWebSocketFrame
TextWebSocketFrame
BinaryWebSocketFrame
CloseWebSocketFrame
WebSocketFrameAggregator]
[io.netty.handler.codec.http.websocketx.extensions.compression
WebSocketServerCompressionHandler]
[java.io
IOException]
[java.net
InetSocketAddress]
[io.netty.util.concurrent
FastThreadLocal]
[java.util.concurrent
TimeUnit
Executor
ExecutorService
RejectedExecutionException
ConcurrentLinkedQueue]
[java.util.concurrent.atomic
AtomicReference
AtomicInteger
AtomicBoolean]))
(set! *unchecked-math* true)
;;;
(defonce ^FastThreadLocal date-format (FastThreadLocal.))
(defonce ^FastThreadLocal date-value (FastThreadLocal.))
(defn rfc-1123-date-string []
(let [^DateFormat format
(or
(.get date-format)
(let [format (SimpleDateFormat. "EEE, dd MMM yyyy HH:mm:ss z" Locale/ENGLISH)]
(.setTimeZone format (TimeZone/getTimeZone "GMT"))
(.set date-format format)
format))]
(.format format (Date.))))
(defn ^CharSequence date-header-value [^ChannelHandlerContext ctx]
(if-let [^AtomicReference ref (.get date-value)]
(.get ref)
(let [ref (AtomicReference. (HttpHeaders/newEntity (rfc-1123-date-string)))]
(.set date-value ref)
(.scheduleAtFixedRate (.executor ctx)
#(.set ref (HttpHeaders/newEntity (rfc-1123-date-string)))
1000
1000
TimeUnit/MILLISECONDS)
(.get ref))))
(defn error-response [^Throwable e]
(log/error e "error in HTTP handler")
{:status 500
:headers {"content-type" "text/plain"}
:body (let [w (java.io.StringWriter.)]
(.printStackTrace e (java.io.PrintWriter. w))
(str w))})
(let [[server-name connection-name date-name]
(map #(HttpHeaders/newEntity %) ["Server" "Connection" "Date"])
[server-value keep-alive-value close-value]
(map #(HttpHeaders/newEntity %) ["Aleph/0.6.0" "Keep-Alive" "Close"])]
(defn send-response
[^ChannelHandlerContext ctx keep-alive? ssl? error-handler rsp]
(let [[^HttpResponse rsp body]
(try
[(http/ring-response->netty-response rsp)
(get rsp :body)]
(catch Throwable e
(let [rsp (error-handler e)]
[(http/ring-response->netty-response rsp)
(get rsp :body)])))]
(netty/safe-execute ctx
(let [headers (.headers rsp)]
(when-not (.contains headers ^CharSequence server-name)
(.set headers ^CharSequence server-name server-value))
(when-not (.contains headers ^CharSequence date-name)
(.set headers ^CharSequence date-name (date-header-value ctx)))
(.set headers ^CharSequence connection-name (if keep-alive? keep-alive-value close-value))
(http/send-message ctx keep-alive? ssl? rsp body))))))
;;;
(defn invalid-value-exception [req x]
(IllegalArgumentException.
(str "cannot treat "
(pr-str x)
" as HTTP response for request to '"
(:uri req)
"'")))
(defn handle-request
[^ChannelHandlerContext ctx
ssl?
handler
rejected-handler
error-handler
executor
^HttpRequest req
previous-response
body
keep-alive?]
(let [^NettyRequest req' (http/netty-request->ring-request req ssl? (.channel ctx) body)
head? (identical? HttpMethod/HEAD (.method req))
rsp (if executor
;; handle request on a separate thread
(try
(d/future-with executor
(handler req'))
(catch RejectedExecutionException e
(if rejected-handler
(try
(rejected-handler req')
(catch Throwable e
(error-handler e)))
{:status 503
:headers {"content-type" "text/plain"}
:body "503 Service Unavailable"})))
;; handle it inline (hope you know what you're doing)
(try
(handler req')
(catch Throwable e
(error-handler e))))]
(-> previous-response
(d/chain'
netty/wrap-future
(fn [_]
(netty/release req)
(-> rsp
(d/catch' error-handler)
(d/chain'
(fn [rsp]
(when (not (-> req' ^AtomicBoolean (.websocket?) .get))
(send-response ctx keep-alive? ssl? error-handler
(cond
(map? rsp)
(if head?
(assoc rsp :body :aleph/omitted)
rsp)
(nil? rsp)
{:status 204}
:else
(error-handler (invalid-value-exception req rsp)))))))))))))
(defn exception-handler [ctx ex]
(cond
;; do not need to log an entire stack trace when SSL handshake failed
(netty/ssl-handshake-error? ex)
(log/warn "SSL handshake failure:"
(.getMessage ^Throwable (.getCause ^Throwable ex)))
(not (instance? IOException ex))
(log/warn ex "error in HTTP server")))
(defn invalid-request? [^HttpRequest req]
(-> req .decoderResult .isFailure))
(defn- ^HttpResponseStatus cause->status [^Throwable cause]
(if (instance? TooLongFrameException cause)
(let [message (.getMessage cause)]
(cond
(.startsWith message "An HTTP line is larger than")
HttpResponseStatus/REQUEST_URI_TOO_LONG
(.startsWith message "HTTP header is larger than")
HttpResponseStatus/REQUEST_HEADER_FIELDS_TOO_LARGE
:else
HttpResponseStatus/BAD_REQUEST))
HttpResponseStatus/BAD_REQUEST))
(defn reject-invalid-request [ctx ^HttpRequest req]
(let [cause (-> req .decoderResult .cause)
status (cause->status cause)]
(d/chain
(netty/write-and-flush ctx
(DefaultFullHttpResponse.
HttpVersion/HTTP_1_1
status
(-> cause .getMessage netty/to-byte-buf)))
netty/wrap-future
(fn [_] (netty/close ctx)))))
(defn ring-handler
[ssl? handler rejected-handler error-handler executor buffer-capacity]
(let [buffer-capacity (long buffer-capacity)
request (atom nil)
buffer (atom [])
buffer-size (AtomicInteger. 0)
stream (atom nil)
previous-response (atom nil)
handle-request
(fn [^ChannelHandlerContext ctx req body]
(reset! previous-response
(handle-request
ctx
ssl?
handler
rejected-handler
error-handler
executor
req
@previous-response
(when body (bs/to-input-stream body))
(HttpHeaders/isKeepAlive req))))
process-request
(fn [ctx req]
(if (HttpHeaders/isTransferEncodingChunked req)
(let [s (netty/buffered-source (netty/channel ctx) #(alength ^bytes %) buffer-capacity)]
(reset! stream s)
(handle-request ctx req s))
(reset! request req)))
process-full-request
(fn [ctx ^FullHttpRequest req]
;; HttpObjectAggregator disables chunked encoding, no need to check for it.
(let [content (.content req)
body (when (pos? (.readableBytes content))
(netty/buf->array content))]
;; Don't release content as it will happen automatically once whole
;; request is released.
(handle-request ctx req body)))
process-last-content
(fn [ctx ^HttpContent msg]
(let [content (.content msg)]
(if-let [s @stream]
(do
(s/put! s (netty/buf->array content))
(netty/release content)
(s/close! s))
(if (and (zero? (.get buffer-size))
(zero? (.readableBytes content)))
;; there was never any body
(do
(netty/release content)
(handle-request ctx @request nil))
(let [bufs (conj @buffer content)
bytes (netty/bufs->array bufs)]
(doseq [b bufs]
(netty/release b))
(handle-request ctx @request bytes))))
(.set buffer-size 0)
(reset! stream nil)
(reset! buffer [])
(reset! request nil)))
process-content
(fn [ctx ^HttpContent msg]
(let [content (.content msg)]
(if-let [s @stream]
;; already have a stream going
(do
(netty/put! (netty/channel ctx) s (netty/buf->array content))
(netty/release content))
(let [len (.readableBytes ^ByteBuf content)]
(when-not (zero? len)
(swap! buffer conj content))
(let [size (.addAndGet buffer-size len)]
;; buffer size exceeded, flush it as a stream
(when (< buffer-capacity size)
(let [bufs @buffer
s (doto (netty/buffered-source (netty/channel ctx) #(alength ^bytes %) buffer-capacity)
(s/put! (netty/bufs->array bufs)))]
(doseq [b bufs]
(netty/release b))
(reset! buffer [])
(reset! stream s)
(handle-request ctx @request s))))))))]
(netty/channel-inbound-handler
:exception-caught
([_ ctx ex]
(exception-handler ctx ex))
:channel-inactive
([_ ctx]
(when-let [s @stream]
(s/close! s))
(doseq [b @buffer]
(netty/release b))
(.fireChannelInactive ctx))
:channel-read
([_ ctx msg]
(cond
;; Happens when io.netty.handler.codec.http.HttpObjectAggregator is part of the pipeline.
(instance? FullHttpRequest msg)
(if (invalid-request? msg)
(reject-invalid-request ctx msg)
(process-full-request ctx msg))
(instance? HttpRequest msg)
(if (invalid-request? msg)
(reject-invalid-request ctx msg)
(process-request ctx msg))
(instance? HttpContent msg)
(if (instance? LastHttpContent msg)
(process-last-content ctx msg)
(process-content ctx msg))
:else
(.fireChannelRead ctx msg))))))
(defn raw-ring-handler
[ssl? handler rejected-handler error-handler executor buffer-capacity]
(let [buffer-capacity (long buffer-capacity)
stream (atom nil)
previous-response (atom nil)
handle-request
(fn [^ChannelHandlerContext ctx req body]
(reset! previous-response
(handle-request
ctx
ssl?
handler
rejected-handler
error-handler
executor
req
@previous-response
body
(HttpUtil/isKeepAlive req))))]
(netty/channel-inbound-handler
:exception-caught
([_ ctx ex]
(exception-handler ctx ex))
:channel-inactive
([_ ctx]
(when-let [s @stream]
(s/close! s))
(.fireChannelInactive ctx))
:channel-read
([_ ctx msg]
(cond
;; Happens when io.netty.handler.codec.http.HttpObjectAggregator is part of the pipeline.
(instance? FullHttpRequest msg)
(if (invalid-request? msg)
(reject-invalid-request ctx msg)
(let [^FullHttpRequest req msg
content (.content req)
ch (netty/channel ctx)
s (netty/source ch)]
(when-not (zero? (.readableBytes content))
;; Retain the content of FullHttpRequest one extra time to
;; compensate for it being released together with the request.
(netty/put! ch s (netty/acquire content)))
(s/close! s)
(handle-request ctx req s)))
(instance? HttpRequest msg)
(if (invalid-request? msg)
(reject-invalid-request ctx msg)
(let [req msg]
(let [s (netty/buffered-source (netty/channel ctx) #(.readableBytes ^ByteBuf %) buffer-capacity)]
(reset! stream s)
(handle-request ctx req s))))
(instance? HttpContent msg)
(let [content (.content ^HttpContent msg)]
;; content might empty most probably in case of EmptyLastHttpContent
(when-not (zero? (.readableBytes content))
(netty/put! (.channel ctx) @stream content))
(when (instance? LastHttpContent msg)
(s/close! @stream)))
:else
(.fireChannelRead ctx msg))))))
(def ^HttpResponse default-accept-response
(DefaultFullHttpResponse. HttpVersion/HTTP_1_1
HttpResponseStatus/CONTINUE
Unpooled/EMPTY_BUFFER))
(HttpHeaders/setContentLength default-accept-response 0)
(def ^HttpResponse default-expectation-failed-response
(DefaultFullHttpResponse. HttpVersion/HTTP_1_1
HttpResponseStatus/EXPECTATION_FAILED
Unpooled/EMPTY_BUFFER))
(HttpHeaders/setContentLength default-expectation-failed-response 0)
(defn new-continue-handler [continue-handler continue-executor ssl?]
(netty/channel-inbound-handler
:channel-read
([_ ctx msg]
(if-not (and (instance? HttpRequest msg)
(HttpUtil/is100ContinueExpected ^HttpRequest msg))
(.fireChannelRead ctx msg)
(let [^HttpRequest req msg
ch (.channel ctx)
ring-req (http/netty-request->ring-request req ssl? ch nil)
resume (fn [accept?]
(if (true? accept?)
;; accepted
(let [rsp (.retainedDuplicate
^ByteBufHolder
default-accept-response)]
(netty/write-and-flush ctx rsp)
(.remove (.headers req) HttpHeaderNames/EXPECT)
(.fireChannelRead ctx req))
;; rejected, use the default reject response if
;; alternative is not provided
(do
(netty/release msg)
(if (false? accept?)
(let [rsp (.retainedDuplicate
^ByteBufHolder
default-expectation-failed-response)]
(netty/write-and-flush ctx rsp))
(let [keep-alive? (HttpUtil/isKeepAlive req)
rsp (http/ring-response->netty-response accept?)]
(http/send-message ctx keep-alive? ssl? rsp nil))))))]
(if (nil? continue-executor)
(resume (continue-handler ring-req))
(d/chain'
(d/future-with continue-executor (continue-handler ring-req))
resume)))))))
(defn pipeline-builder
[handler
pipeline-transform
{:keys
[executor
rejected-handler
error-handler
request-buffer-size
max-initial-line-length
max-header-size
max-chunk-size
raw-stream?
ssl?
compression?
compression-level
idle-timeout
continue-handler
continue-executor]
:or
{request-buffer-size 16384
max-initial-line-length 8192
max-header-size 8192
max-chunk-size 16384
compression? false
idle-timeout 0
error-handler error-response}}]
(fn [^ChannelPipeline pipeline]
(let [handler (if raw-stream?
(raw-ring-handler ssl? handler rejected-handler error-handler executor request-buffer-size)
(ring-handler ssl? handler rejected-handler error-handler executor request-buffer-size))
^ChannelHandler
continue-handler (if (nil? continue-handler)
(HttpServerExpectContinueHandler.)
(new-continue-handler continue-handler
continue-executor
ssl?))]
(doto pipeline
(http/attach-idle-handlers idle-timeout)
(.addLast "http-server"
(HttpServerCodec.
max-initial-line-length
max-header-size
max-chunk-size
false))
(.addLast "continue-handler" continue-handler)
(.addLast "request-handler" ^ChannelHandler handler)
(#(when (or compression? (some? compression-level))
(let [compressor (HttpContentCompressor. (int (or compression-level 6)))]
(.addAfter ^ChannelPipeline %1 "http-server" "deflater" compressor))
(.addAfter ^ChannelPipeline %1 "deflater" "streamer" (ChunkedWriteHandler.))))
pipeline-transform))))
;;;
(defn start-server
[handler
{:keys [port
socket-address
executor
bootstrap-transform
pipeline-transform
ssl-context
manual-ssl?
shutdown-executor?
epoll?
compression?
continue-handler
continue-executor
shutdown-timeout]
:or {bootstrap-transform identity
pipeline-transform identity
shutdown-executor? true
epoll? false
compression? false
shutdown-timeout netty/default-shutdown-timeout}
:as options}]
(let [executor (cond
(instance? Executor executor)
executor
(nil? executor)
(flow/utilization-executor 0.9 512
{:metrics (EnumSet/of Stats$Metric/UTILIZATION)
;;:onto? false
})
(= :none executor)
nil
:else
(throw
(IllegalArgumentException.
(str "invalid executor specification: " (pr-str executor)))))
continue-executor' (cond
(nil? continue-executor)
executor
(identical? :none continue-executor)
nil
(instance? Executor continue-executor)
continue-executor
:else
(throw
(IllegalArgumentException.
(str "invalid continue-executor specification: "
(pr-str continue-executor)))))]
(netty/start-server
{:pipeline-builder (pipeline-builder
handler
pipeline-transform
(assoc options
:executor executor
:ssl? (or manual-ssl? (boolean ssl-context))
:continue-executor continue-executor'))
:ssl-context ssl-context
:bootstrap-transform bootstrap-transform
:socket-address (if socket-address
socket-address
(InetSocketAddress. port))
:on-close (when (and shutdown-executor? (or (instance? ExecutorService executor)
(instance? ExecutorService continue-executor)))
#(do
(when (instance? ExecutorService executor)
(.shutdown ^ExecutorService executor))
(when (instance? ExecutorService continue-executor)
(.shutdown ^ExecutorService continue-executor))))
:transport (if epoll? :epoll :nio)
:shutdown-timeout shutdown-timeout})))
;;;
(defn websocket-server-handler
([raw-stream? ch handshaker]
(websocket-server-handler raw-stream? ch handshaker nil))
([raw-stream?
^Channel ch
^WebSocketServerHandshaker handshaker
heartbeats]
(let [d (d/deferred)
^ConcurrentLinkedQueue pending-pings (ConcurrentLinkedQueue.)
closing? (AtomicBoolean. false)
coerce-fn (http/websocket-message-coerce-fn
ch
pending-pings
(fn [^CloseWebSocketFrame frame]
(if-not (.compareAndSet closing? false true)
false
(do
(.close handshaker ch frame)
true))))
description (fn [] {:websocket-selected-subprotocol (.selectedSubprotocol handshaker)})
out (netty/sink ch false coerce-fn description)
in (netty/buffered-source ch (constantly 1) 16)]
(s/on-closed out #(http/resolve-pings! pending-pings false))
(s/on-drained
in
;; there's a chance that the connection was closed by the client,
;; in that case *out* would be closed earlier and the underlying
;; netty channel is already terminated
#(when (and (.isOpen ch)
(.compareAndSet closing? false true))
(.close handshaker ch (CloseWebSocketFrame.))))
(let [s (doto
(s/splice out in)
(reset-meta! {:aleph/channel ch}))]
[s
(netty/channel-inbound-handler
:exception-caught
([_ ctx ex]
(when-not (instance? IOException ex)
(log/warn ex "error in websocket handler"))
(s/close! out)
(netty/close ctx))
:channel-inactive
([_ ctx]
(s/close! out)
(s/close! in)
(.fireChannelInactive ctx))
:user-event-triggered
([_ ctx evt]
(if (and (instance? IdleStateEvent evt)
(= IdleState/ALL_IDLE (.state ^IdleStateEvent evt)))
(http/handle-heartbeat ctx s heartbeats)
(.fireUserEventTriggered ctx evt)))
:channel-read
([_ ctx msg]
(let [ch (.channel ctx)]
(cond
(instance? TextWebSocketFrame msg)
(if raw-stream?
(let [body (.content ^TextWebSocketFrame msg)]
;; pass ByteBuf body directly to next level (it's
;; their reponsibility to release)
(netty/put! ch in body))
(let [text (.text ^TextWebSocketFrame msg)]
;; working with text now, so we do not need
;; ByteBuf inside TextWebSocketFrame
;; note, that all *WebSocketFrame classes are
;; subclasses of DefaultByteBufHolder, meaning
;; there's no difference between releasing
;; frame & frame's content
(netty/release msg)
(netty/put! ch in text)))
(instance? BinaryWebSocketFrame msg)
(let [body (.content ^BinaryWebSocketFrame msg)]
(netty/put! ch in
(if raw-stream?
body
;; copied data into byte array, deallocating ByteBuf
(netty/release-buf->array body))))
(instance? PingWebSocketFrame msg)
(let [body (.content ^PingWebSocketFrame msg)]
;; reusing the same buffer
;; will be deallocated by Netty
(netty/write-and-flush ch (PongWebSocketFrame. body)))
(instance? PongWebSocketFrame msg)
(do
(netty/release msg)
(http/resolve-pings! pending-pings true))
(instance? CloseWebSocketFrame msg)
(if-not (.compareAndSet closing? false true)
;; closing already, nothing else could be done
(netty/release msg)
;; reusing the same buffer
;; will be deallocated by Netty
(.close handshaker ch ^CloseWebSocketFrame msg))
:else
;; no need to release buffer when passing to a next handler
(.fireChannelRead ctx msg)))))]))))
;; note, as we set `keep-alive?` to `false`, `send-message` will close the connection
;; after writes are done, which is exactly what we expect to happen
(defn send-websocket-request-expected! [ch ssl?]
(http/send-message
ch
false
ssl?
(http/ring-response->netty-response
{:status 400
:headers {"content-type" "text/plain"}})
"expected websocket request"))
(defn websocket-upgrade-request?
"Returns `true` if given request is an attempt to upgrade to websockets"
[^NettyRequest req]
(let [headers (:headers req)
conn (get headers :connection)
upgrade (get headers :upgrade)]
(and (contains? (when (some? conn)
(set (map str/trim
(-> (str/lower-case conn) (str/split #",")))))
"upgrade")
(= "websocket" (when (some? upgrade) (str/lower-case upgrade))))))
(defn initialize-websocket-handler
[^NettyRequest req
{:keys [raw-stream?
headers
max-frame-payload
max-frame-size
allow-extensions?
compression?
pipeline-transform
heartbeats]
:or {raw-stream? false
max-frame-payload 65536
max-frame-size 1048576
allow-extensions? false
compression? false}
:as options}]
(when (and (true? (:compression? options))
(false? (:allow-extensions? options)))
(throw (IllegalArgumentException.
"Per-message deflate requires extensions to be allowed")))
(-> req ^AtomicBoolean (.websocket?) (.set true))
(let [^Channel ch (.ch req)
ssl? (identical? :https (:scheme req))
url (str
(if ssl? "wss://" "ws://")
(get-in req [:headers "host"])
(:uri req))
req (http/ring-request->full-netty-request req)
factory (WebSocketServerHandshakerFactory.
url
nil
(or allow-extensions? compression?)
max-frame-payload)]
(try
(if-let [handshaker (.newHandshaker factory req)]
(try
(let [[s ^ChannelHandler handler] (websocket-server-handler raw-stream?
ch
handshaker
heartbeats)
p (.newPromise ch)
h (doto (DefaultHttpHeaders.) (http/map->headers! headers))
^ChannelPipeline pipeline (.pipeline ch)]
;; actually, we're not going to except anything but websocket, so...
(doto pipeline
(.remove "request-handler")
(.remove "continue-handler")
(netty/remove-if-present HttpContentCompressor)
(netty/remove-if-present ChunkedWriteHandler)
(.addLast "websocket-frame-aggregator" (WebSocketFrameAggregator. max-frame-size))
(http/attach-heartbeats-handler heartbeats)
(.addLast "websocket-handler" handler))
(when compression?
;; Hack:
;; WebSocketServerCompressionHandler is stateful and requires
;; HTTP request to be send through the pipeline
;; See more:
;; * https://github.com/clj-commons/aleph/issues/494
;; * https://github.com/netty/netty/pull/8973
(let [compression-handler (WebSocketServerCompressionHandler.)
ctx (.context pipeline "websocket-frame-aggregator")]
(.addAfter pipeline
"websocket-frame-aggregator"
"websocket-deflater"
compression-handler)
(.fireChannelRead ctx req)))
(-> (try
(netty/wrap-future (.handshake handshaker ch ^HttpRequest req h p))
(catch Throwable e
(d/error-deferred e)))
(d/chain'
(fn [_]
(when (some? pipeline-transform)
(pipeline-transform (.pipeline ch)))
s))
(d/catch'
(fn [e]
(send-websocket-request-expected! ch ssl?)
(d/error-deferred e)))))
(catch Throwable e
(d/error-deferred e)))
(do
(WebSocketServerHandshakerFactory/sendUnsupportedVersionResponse ch)
(d/error-deferred (IllegalStateException. "unsupported version"))))
(finally
;; I find this approach to handle request release somewhat
;; fragile... We have to release the object both in case of
;; handshake initialization and "unsupported version" response
(netty/release req)))))