|
4 | 4 |
|
5 | 5 | package play.it.http.websocket |
6 | 6 |
|
| 7 | +import java.io.ByteArrayOutputStream |
7 | 8 | import java.net.URI |
8 | 9 | import java.util.concurrent.atomic.AtomicReference |
| 10 | +import java.util.zip.Inflater |
9 | 11 |
|
10 | 12 | import scala.collection.immutable |
11 | 13 | import scala.concurrent.duration._ |
@@ -40,11 +42,165 @@ import play.api.test._ |
40 | 42 | import play.api.Application |
41 | 43 | import play.api.Configuration |
42 | 44 | import play.it._ |
| 45 | +import play.it.http.websocket.WebSocketClient.CompressionMode |
43 | 46 | import play.it.http.websocket.WebSocketClient.ContinuationMessage |
44 | 47 | import play.it.http.websocket.WebSocketClient.ExtendedMessage |
| 48 | +import play.it.http.websocket.WebSocketClient.RawWebSocketFrame |
45 | 49 | import play.it.http.websocket.WebSocketClient.SimpleMessage |
46 | 50 |
|
47 | | -class NettyWebSocketSpec extends WebSocketSpec with NettyIntegrationSpecification |
| 51 | +class NettyWebSocketSpec extends WebSocketSpec with NettyIntegrationSpecification { |
| 52 | + "Plays WebSockets using netty backend with compression" should { |
| 53 | + "negotiate permessage-deflate" in { |
| 54 | + withServer(app => |
| 55 | + WebSocket.accept[String, String] { req => Flow.fromSinkAndSource(Sink.ignore, Source.maybe[String]) } |
| 56 | + ) { (app, port) => |
| 57 | + import app.materializer |
| 58 | + val (_, headers) = runWebSocket( |
| 59 | + port, |
| 60 | + { flow => |
| 61 | + Source.empty[ExtendedMessage].via(flow).runWith(Sink.ignore) |
| 62 | + Future.successful(()) |
| 63 | + }, |
| 64 | + subprotocol = None, |
| 65 | + handleConnect = c => c, |
| 66 | + compressionMode = CompressionMode.Enabled() |
| 67 | + ) |
| 68 | + |
| 69 | + headers.collectFirst { |
| 70 | + case (name, value) if name.equalsIgnoreCase("Sec-WebSocket-Extensions") => value |
| 71 | + } must beSome(contain("permessage-deflate")) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + "decompress inbound permessage-deflate frames before passing them to the application" in { |
| 76 | + val consumed = Promise[List[String]]() |
| 77 | + withServer(app => |
| 78 | + WebSocket.accept[String, String] { req => |
| 79 | + Flow.fromSinkAndSource(onFramesConsumed[String](consumed.success(_)), Source.maybe[String]) |
| 80 | + } |
| 81 | + ) { (app, port) => |
| 82 | + import app.materializer |
| 83 | + val result = runWebSocket( |
| 84 | + port, |
| 85 | + { flow => |
| 86 | + sendFrames( |
| 87 | + TextMessage("compressed client message"), |
| 88 | + CloseMessage(1000) |
| 89 | + ).via(flow).runWith(Sink.ignore) |
| 90 | + consumed.future |
| 91 | + }, |
| 92 | + compressionMode = CompressionMode.Enabled() |
| 93 | + ) |
| 94 | + |
| 95 | + result must_== Seq("compressed client message") |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + "compress outbound permessage-deflate frames sent by the application" in { |
| 100 | + withServer(app => |
| 101 | + WebSocket.accept[String, String] { req => |
| 102 | + Flow.fromSinkAndSource(Sink.ignore, Source.single("compressed server message")) |
| 103 | + } |
| 104 | + ) { (app, port) => |
| 105 | + import app.materializer |
| 106 | + val frames = runWebSocket( |
| 107 | + port, |
| 108 | + { flow => Source.maybe[ExtendedMessage].via(flow).runWith(consumeFrames) }, |
| 109 | + compressionMode = CompressionMode.RequestOnly() |
| 110 | + ) |
| 111 | + |
| 112 | + frames.collectFirst { |
| 113 | + case RawWebSocketFrame("text", data, rsv, true) => (data, rsv) |
| 114 | + } must beSome[(ByteString, Int)].which { |
| 115 | + case (data, rsv) => |
| 116 | + data.nonEmpty && |
| 117 | + (rsv & 4) == 4 && |
| 118 | + data != ByteString("compressed server message") && |
| 119 | + inflatePerMessageDeflate(data).utf8String == "compressed server message" |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + "not negotiate compression when websocket compression is disabled" in { |
| 125 | + withServer( |
| 126 | + app => WebSocket.accept[String, String] { req => Flow.fromSinkAndSource(Sink.ignore, Source.maybe[String]) }, |
| 127 | + Map("play.server.websocket.compression.enabled" -> false) |
| 128 | + ) { (app, port) => |
| 129 | + import app.materializer |
| 130 | + val (_, headers) = runWebSocket( |
| 131 | + port, |
| 132 | + { flow => |
| 133 | + Source.empty[ExtendedMessage].via(flow).runWith(Sink.ignore) |
| 134 | + Future.successful(()) |
| 135 | + }, |
| 136 | + subprotocol = None, |
| 137 | + handleConnect = c => c, |
| 138 | + compressionMode = CompressionMode.RequestOnly() |
| 139 | + ) |
| 140 | + |
| 141 | + headers.collectFirst { |
| 142 | + case (name, value) if name.equalsIgnoreCase("Sec-WebSocket-Extensions") => value |
| 143 | + } must beNone |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + "use configured Netty permessage-deflate negotiation settings" in { |
| 148 | + withServer( |
| 149 | + app => WebSocket.accept[String, String] { req => Flow.fromSinkAndSource(Sink.ignore, Source.maybe[String]) }, |
| 150 | + Map( |
| 151 | + "play.server.netty.websocket.compression.perMessageDeflate.allowServerWindowSize" -> true, |
| 152 | + "play.server.netty.websocket.compression.perMessageDeflate.serverWindowSize" -> 12, |
| 153 | + "play.server.netty.websocket.compression.perMessageDeflate.memLevel" -> 7, |
| 154 | + "play.server.netty.websocket.compression.perMessageDeflate.preferredClientWindowSize" -> 11, |
| 155 | + "play.server.netty.websocket.compression.perMessageDeflate.allowServerNoContext" -> true, |
| 156 | + "play.server.netty.websocket.compression.perMessageDeflate.preferredClientNoContext" -> true, |
| 157 | + "play.server.netty.websocket.compression.perMessageDeflate.compressionLevel" -> 5 |
| 158 | + ) |
| 159 | + ) { (app, port) => |
| 160 | + import app.materializer |
| 161 | + val (_, headers) = runWebSocket( |
| 162 | + port, |
| 163 | + { flow => |
| 164 | + Source.empty[ExtendedMessage].via(flow).runWith(Sink.ignore) |
| 165 | + Future.successful(()) |
| 166 | + }, |
| 167 | + subprotocol = None, |
| 168 | + handleConnect = c => c, |
| 169 | + compressionMode = CompressionMode.RequestOnly( |
| 170 | + "permessage-deflate; client_max_window_bits; server_max_window_bits=15; " + |
| 171 | + "client_no_context_takeover; server_no_context_takeover" |
| 172 | + ) |
| 173 | + ) |
| 174 | + |
| 175 | + val extension = headers.collectFirst { |
| 176 | + case (name, value) if name.equalsIgnoreCase("Sec-WebSocket-Extensions") => value |
| 177 | + } |
| 178 | + |
| 179 | + extension must beSome[String].which { value => |
| 180 | + value.contains("permessage-deflate") && |
| 181 | + value.contains("server_max_window_bits=12") && |
| 182 | + value.contains("client_max_window_bits=11") && |
| 183 | + value.contains("server_no_context_takeover") && |
| 184 | + value.contains("client_no_context_takeover") |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + "fail clearly when Netty websocket compression maxAllocation exceeds Netty's integer limit" in { |
| 190 | + withServer( |
| 191 | + app => WebSocket.accept[String, String] { req => Flow.fromSinkAndSource(Sink.ignore, Source.maybe[String]) }, |
| 192 | + Map("play.server.netty.websocket.compression.maxAllocation" -> "3g") |
| 193 | + ) { (_, _) => |
| 194 | + () |
| 195 | + } must throwA[RuntimeException].like { |
| 196 | + case exception => |
| 197 | + (exception.getCause must beAnInstanceOf[play.core.server.ServerStartException]) |
| 198 | + .and(exception.getMessage must contain("play.server.netty.websocket.compression.maxAllocation")) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + } |
| 203 | +} |
48 | 204 | class PekkoHttpWebSocketSpec extends WebSocketSpec with PekkoHttpIntegrationSpecification { |
49 | 205 | "Plays WebSockets using pekko-http backend with HTTP2 enabled" should { |
50 | 206 | "time out after play.server.http.idleTimeout" in delayedSend( |
@@ -391,7 +547,8 @@ trait WebSocketSpec |
391 | 547 | sendFrames(TextMessage("foo"), CloseMessage(1000)).via(flow).runWith(Sink.ignore) |
392 | 548 | }, |
393 | 549 | Some("my_crazy_subprotocol"), |
394 | | - c => c |
| 550 | + c => c, |
| 551 | + CompressionMode.Disabled |
395 | 552 | ) |
396 | 553 | (headers |
397 | 554 | .map { case (key, value) => (key.toLowerCase, value) } |
@@ -411,7 +568,8 @@ trait WebSocketSpec |
411 | 568 | sendFrames(TextMessage("foo"), CloseMessage(1000)).via(flow).runWith(Sink.ignore) |
412 | 569 | }, |
413 | 570 | Some("first-protocol, second-protocol, third-protocol"), |
414 | | - c => c |
| 571 | + c => c, |
| 572 | + CompressionMode.Disabled |
415 | 573 | ) |
416 | 574 | (headers |
417 | 575 | .map { case (key, value) => (key.toLowerCase, value) } |
@@ -436,7 +594,8 @@ trait WebSocketSpec |
436 | 594 | sendFrames(TextMessage("foo"), CloseMessage(1000)).via(flow).runWith(Sink.ignore) |
437 | 595 | }, |
438 | 596 | Some("graphql-ws, graphql-transport-ws"), |
439 | | - c => c |
| 597 | + c => c, |
| 598 | + CompressionMode.Disabled |
440 | 599 | ) |
441 | 600 | (headers |
442 | 601 | .map { case (key, value) => (key.toLowerCase, value) } |
@@ -666,7 +825,8 @@ trait WebSocketSpec |
666 | 825 | sendFrames(TextMessage("foo"), CloseMessage(1000)).via(flow).runWith(Sink.ignore) |
667 | 826 | }, |
668 | 827 | Some("graphql-ws, graphql-transport-ws"), |
669 | | - c => c |
| 828 | + c => c, |
| 829 | + CompressionMode.Disabled |
670 | 830 | ) |
671 | 831 | (headers |
672 | 832 | .map { case (key, value) => (key.toLowerCase, value) } |
@@ -784,32 +944,54 @@ trait WebSocketSpecMethods extends PlaySpecification with WsTestClient with Serv |
784 | 944 | def runWebSocket[A]( |
785 | 945 | port: Int, |
786 | 946 | handler: Flow[ExtendedMessage, ExtendedMessage, ?] => Future[A], |
787 | | - handleConnect: Future[?] => Future[?] = c => c |
| 947 | + handleConnect: Future[?] => Future[?] = c => c, |
| 948 | + compressionMode: CompressionMode = CompressionMode.Disabled |
788 | 949 | ): A = |
789 | | - runWebSocket(port, handler, subprotocol = None, handleConnect) match { case (result, _) => result } |
| 950 | + runWebSocket(port, handler, subprotocol = None, handleConnect, compressionMode) match { case (result, _) => result } |
790 | 951 |
|
791 | 952 | def runWebSocket[A]( |
792 | 953 | port: Int, |
793 | 954 | handler: Flow[ExtendedMessage, ExtendedMessage, ?] => Future[A], |
794 | 955 | subprotocol: Option[String], |
795 | | - handleConnect: Future[?] => Future[?] |
| 956 | + handleConnect: Future[?] => Future[?], |
| 957 | + compressionMode: CompressionMode |
796 | 958 | ): (A, immutable.Seq[(String, String)]) = { |
797 | 959 | WebSocketClient { client => |
798 | 960 | val innerResult = Promise[A]() |
799 | 961 | val responseHeaders = Promise[immutable.Seq[(String, String)]]() |
800 | 962 | await( |
801 | 963 | handleConnect( |
802 | | - client.connect(URI.create("ws://localhost:" + port + "/stream"), subprotocol = subprotocol) { |
803 | | - (headers, flow) => |
804 | | - innerResult.completeWith(handler(flow)) |
805 | | - responseHeaders.success(headers) |
| 964 | + client.connect( |
| 965 | + URI.create("ws://localhost:" + port + "/stream"), |
| 966 | + subprotocol = subprotocol, |
| 967 | + compressionMode = compressionMode |
| 968 | + ) { (headers, flow) => |
| 969 | + innerResult.completeWith(handler(flow)) |
| 970 | + responseHeaders.success(headers) |
806 | 971 | } |
807 | 972 | ) |
808 | 973 | ) |
809 | 974 | (await(innerResult.future), await(responseHeaders.future)) |
810 | 975 | } |
811 | 976 | } |
812 | 977 |
|
| 978 | + def inflatePerMessageDeflate(data: ByteString): ByteString = { |
| 979 | + val inflater = new Inflater(true) |
| 980 | + try { |
| 981 | + inflater.setInput((data ++ ByteString(0x00, 0x00, 0xff.toByte, 0xff.toByte)).toArray) |
| 982 | + val output = new ByteArrayOutputStream() |
| 983 | + val buffer = new Array[Byte](256) |
| 984 | + var count = inflater.inflate(buffer) |
| 985 | + while (count > 0) { |
| 986 | + output.write(buffer, 0, count) |
| 987 | + count = inflater.inflate(buffer) |
| 988 | + } |
| 989 | + ByteString.fromArray(output.toByteArray) |
| 990 | + } finally { |
| 991 | + inflater.end() |
| 992 | + } |
| 993 | + } |
| 994 | + |
813 | 995 | def pongFrame(matcher: Matcher[String]): Matcher[ExtendedMessage] = beLike { |
814 | 996 | case SimpleMessage(PongMessage(data), _) => data.utf8String must matcher |
815 | 997 | } |
|