1
- import io.netty.channel.ChannelFutureListener
2
- import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame
3
- import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame
4
- import io.netty.handler.codec.http.websocketx.TextWebSocketFrame
5
- import io.netty.handler.codec.http.websocketx.WebSocketFrame
6
- import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler
7
-
8
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_URLENCODED
9
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR
10
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
11
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.FORWARDED
12
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.NOT_FOUND
13
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ENCODED_BOTH
14
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ENCODED_QUERY
15
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
16
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.REDIRECT
17
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SUCCESS
18
- import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.USER_BLOCK
19
- import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
20
- import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH
21
- import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE
22
- import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE
23
- import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR
24
- import static io.netty.handler.codec.http.HttpUtil.is100ContinueExpected
25
- import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1
26
-
27
1
import datadog.appsec.api.blocking.Blocking
28
2
import datadog.trace.agent.test.base.HttpServer
29
3
import datadog.trace.agent.test.base.HttpServerTest
@@ -34,6 +8,7 @@ import datadog.trace.instrumentation.netty41.server.NettyHttpServerDecorator
34
8
import io.netty.bootstrap.ServerBootstrap
35
9
import io.netty.buffer.ByteBuf
36
10
import io.netty.buffer.Unpooled
11
+ import io.netty.channel.ChannelFutureListener
37
12
import io.netty.channel.ChannelHandlerContext
38
13
import io.netty.channel.ChannelInitializer
39
14
import io.netty.channel.ChannelPipeline
@@ -51,10 +26,33 @@ import io.netty.handler.codec.http.HttpResponseStatus
51
26
import io.netty.handler.codec.http.HttpServerCodec
52
27
import io.netty.handler.codec.http.multipart.Attribute
53
28
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder
29
+ import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame
30
+ import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame
31
+ import io.netty.handler.codec.http.websocketx.TextWebSocketFrame
32
+ import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler
54
33
import io.netty.handler.logging.LogLevel
55
34
import io.netty.handler.logging.LoggingHandler
56
35
import io.netty.util.CharsetUtil
57
36
37
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_URLENCODED
38
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR
39
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
40
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.FORWARDED
41
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.NOT_FOUND
42
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ENCODED_BOTH
43
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ENCODED_QUERY
44
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
45
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.REDIRECT
46
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SUCCESS
47
+ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.USER_BLOCK
48
+ import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
49
+ import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH
50
+ import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE
51
+ import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE
52
+ import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR
53
+ import static io.netty.handler.codec.http.HttpUtil.is100ContinueExpected
54
+ import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1
55
+
58
56
abstract class Netty41ServerTest extends HttpServerTest<EventLoopGroup > {
59
57
60
58
static final LoggingHandler LOGGING_HANDLER = new LoggingHandler (SERVER_LOGGER . name, LogLevel . DEBUG )
@@ -159,6 +157,7 @@ abstract class Netty41ServerTest extends HttpServerTest<EventLoopGroup> {
159
157
}
160
158
}
161
159
if (msg instanceof TextWebSocketFrame || msg instanceof BinaryWebSocketFrame ) {
160
+ // generate a child span. The websocket test expects this way
162
161
runUnderTrace(" onRead" , {})
163
162
}
164
163
},
@@ -172,15 +171,13 @@ abstract class Netty41ServerTest extends HttpServerTest<EventLoopGroup> {
172
171
channelReadComplete : {
173
172
it. flush()
174
173
},
175
- handlerAdded : {
176
- ChannelHandlerContext ctx -> {
177
- WsEndpoint . onOpen(ctx)
178
- }
179
- },
180
174
userEventTriggered : { ChannelHandlerContext ctx , Object evt ->
181
- if (evt == WebSocketServerProtocolHandler.HandshakeComplete ) {
175
+ if (evt instanceof WebSocketServerProtocolHandler.HandshakeComplete ) {
182
176
WsEndpoint . onOpen(ctx)
183
177
}
178
+ },
179
+ channelInactive : { ChannelHandlerContext ctx ->
180
+ WsEndpoint . onClose()
184
181
}
185
182
] as SimpleChannelInboundHandler )
186
183
}
@@ -210,12 +207,16 @@ abstract class Netty41ServerTest extends HttpServerTest<EventLoopGroup> {
210
207
211
208
@Override
212
209
void serverSendText (String [] messages ) {
213
- WsEndpoint . onMessage(messages[0 ], false )
210
+ for (int i = 0 ; i< messages. size(); i++ ) {
211
+ WsEndpoint . activeSession. writeAndFlush(new TextWebSocketFrame (i == messages. size() - 1 , 0 , messages[i]))
212
+ }
214
213
}
215
214
216
215
@Override
217
216
void serverSendBinary (byte [][] binaries ) {
218
- WsEndpoint . onMessage(binaries[0 ], false )
217
+ for (int i = 0 ; i< binaries. length; i++ ) {
218
+ WsEndpoint . activeSession. writeAndFlush(new BinaryWebSocketFrame (i == binaries. length - 1 , 0 , Unpooled . wrappedBuffer(binaries[i])))
219
+ }
219
220
}
220
221
221
222
@Override
@@ -227,6 +228,11 @@ abstract class Netty41ServerTest extends HttpServerTest<EventLoopGroup> {
227
228
void setMaxPayloadSize (int size ) {
228
229
// not applicable
229
230
}
231
+
232
+ @Override
233
+ boolean canSplitLargeWebsocketPayloads () {
234
+ false
235
+ }
230
236
}
231
237
232
238
@Override
@@ -292,17 +298,4 @@ class WsEndpoint {
292
298
static void onClose () {
293
299
activeSession = null
294
300
}
295
-
296
- static void onMessage (String s , boolean last ) {
297
- synchronized (WsEndpoint ) {
298
- activeSession. writeAndFlush(new TextWebSocketFrame (s))
299
- }
300
- }
301
-
302
- static void onMessage (byte [] b , boolean last ) {
303
- synchronized (WsEndpoint ) {
304
- ByteBuf responseBuf = Unpooled . wrappedBuffer(b)
305
- activeSession. writeAndFlush(new BinaryWebSocketFrame (responseBuf))
306
- }
307
- }
308
301
}
0 commit comments