blob: fb6ce8804bcae533c6d60a37eeeedec63cb2e693 (
plain)
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
|
/*
* ReasonExtractor.java February 2014
*
* Copyright (C) 2014, Niall Gallagher <niallg@users.sf.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.simpleframework.http.socket.service;
import static org.simpleframework.http.socket.CloseCode.NO_STATUS_CODE;
import org.simpleframework.http.socket.CloseCode;
import org.simpleframework.http.socket.DataConverter;
import org.simpleframework.http.socket.Frame;
import org.simpleframework.http.socket.Reason;
/**
* The <code>ReasonExtractor</code> object is used to extract the close
* reason from a frame payload. If their is no close reason this will
* return a <code>Reason</code> with just the close code. Finally in
* the event of a botched frame being sent with no close code then the
* close code 1005 is used to indicate no reason.
*
* @author Niall Gallagher
*/
class ReasonExtractor {
/**
* This is the data converter object used to convert data.
*/
private final DataConverter converter;
/**
* Constructor for the <code>ReasonExtractor</code> object. This
* is used to create an extractor for close code and the close
* reason descriptions. All descriptions are decoded using the
* UTF-8 character encoding.
*/
public ReasonExtractor() {
this.converter = new DataConverter();
}
/**
* This is used to extract a reason from the provided frame. The
* close reason is taken from the first two bytes of the frame
* payload and the UTF-8 string that follows is the description.
*
* @param frame this is the frame to extract the reason from
*
* @return a reason containing the close code and reason
*/
public Reason extract(Frame frame) {
byte[] data = frame.getBinary();
if(data.length > 0) {
CloseCode code = extractCode(data);
String text = extractText(data);
return new Reason(code, text);
}
return new Reason(NO_STATUS_CODE);
}
/**
* This method is used to extract the UTF-8 description from the
* frame payload. If there are only two bytes within the payload
* then this will return null for the reason.
*
* @param data the frame payload to extract the description from
*
* @return returns the description within the payload
*/
private String extractText(byte[] data) {
int length = data.length - 2;
if(length > 0) {
return converter.convert(data, 2, length);
}
return null;
}
/**
* This method is used to extract the close code. The close code
* is an two byte integer in network byte order at the start
* of the close frame payload. This code is required by RFC 6455
* however if not code is available code 1005 is returned.
*
* @param data the frame payload to extract the description from
*
* @return returns the description within the payload
*/
private CloseCode extractCode(byte[] data) {
int length = data.length;
if(length > 0) {
int high = data[0];
int low = data[1];
return CloseCode.resolveCode(high, low);
}
return NO_STATUS_CODE;
}
}
|