1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.rtsp;
17
18 import io.netty5.channel.ChannelHandlerContext;
19 import io.netty5.handler.codec.http.DefaultFullHttpRequest;
20 import io.netty5.handler.codec.http.DefaultFullHttpResponse;
21 import io.netty5.handler.codec.http.DefaultHttpRequest;
22 import io.netty5.handler.codec.http.DefaultHttpResponse;
23 import io.netty5.handler.codec.http.HttpMessage;
24 import io.netty5.handler.codec.http.HttpObjectDecoder;
25 import io.netty5.handler.codec.http.HttpResponseStatus;
26
27 import java.util.regex.Pattern;
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 public class RtspDecoder extends HttpObjectDecoder {
59
60
61
62 private static final HttpResponseStatus UNKNOWN_STATUS =
63 new HttpResponseStatus(999, "Unknown");
64
65
66
67
68 private boolean isDecodingRequest;
69
70
71
72
73 private static final Pattern versionPattern = Pattern.compile("RTSP/\\d\\.\\d");
74
75
76
77
78 public static final int DEFAULT_MAX_CONTENT_LENGTH = 8192;
79
80
81
82
83
84
85 public RtspDecoder() {
86 this(DEFAULT_MAX_INITIAL_LINE_LENGTH,
87 DEFAULT_MAX_HEADER_SIZE,
88 DEFAULT_MAX_CONTENT_LENGTH);
89 }
90
91
92
93
94
95
96
97 public RtspDecoder(final int maxInitialLineLength,
98 final int maxHeaderSize,
99 final int maxContentLength) {
100 super(maxInitialLineLength, maxHeaderSize, false);
101 }
102
103
104
105
106
107
108
109
110 public RtspDecoder(final int maxInitialLineLength,
111 final int maxHeaderSize,
112 final int maxContentLength,
113 final boolean validateHeaders) {
114 super(maxInitialLineLength,
115 maxHeaderSize,
116 false,
117 validateHeaders);
118 }
119
120 @Override
121 protected HttpMessage createMessage(final String[] initialLine)
122 throws Exception {
123
124
125 if (versionPattern.matcher(initialLine[0]).matches()) {
126 isDecodingRequest = false;
127 return new DefaultHttpResponse(RtspVersions.valueOf(initialLine[0]),
128 new HttpResponseStatus(Integer.parseInt(initialLine[1]),
129 initialLine[2]),
130 validateHeaders);
131 } else {
132 isDecodingRequest = true;
133 return new DefaultHttpRequest(RtspVersions.valueOf(initialLine[2]),
134 RtspMethods.valueOf(initialLine[0]),
135 initialLine[1],
136 validateHeaders);
137 }
138 }
139
140 @Override
141 protected boolean isContentAlwaysEmpty(final HttpMessage msg) {
142
143
144 return super.isContentAlwaysEmpty(msg) || !msg.headers().contains(RtspHeaderNames.CONTENT_LENGTH);
145 }
146
147 @Override
148 protected HttpMessage createInvalidMessage(ChannelHandlerContext ctx) {
149 if (isDecodingRequest) {
150 return new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.OPTIONS, "/bad-request",
151 ctx.bufferAllocator().allocate(0), validateHeaders);
152 } else {
153 return new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, UNKNOWN_STATUS,
154 ctx.bufferAllocator().allocate(0), validateHeaders);
155 }
156 }
157
158 @Override
159 protected boolean isDecodingRequest() {
160 return isDecodingRequest;
161 }
162 }