• R/O
  • SSH
  • HTTPS

webserver: Commit


Commit MetaInfo

Revision74 (tree)
Time2012-12-13 04:48:58
Authorhoritaku

Log Message

WebSocket周りのソース整理

Change Summary

Incremental Difference

--- branch/Basic 0.2x/src/servercore/html5/WebSocketFrame.java (revision 0)
+++ branch/Basic 0.2x/src/servercore/html5/WebSocketFrame.java (revision 74)
@@ -0,0 +1,62 @@
1+package servercore.html5;
2+
3+import java.io.IOException;
4+import java.io.InputStream;
5+import java.io.OutputStream;
6+
7+public class WebSocketFrame {
8+ protected static OutputStream ops = null;
9+ protected static InputStream ips = null;
10+
11+ public String read() throws IOException{
12+ byte[] data = new byte[8098];
13+ int len = ips.read(data);
14+/*
15+ for(int i = 0;i < len;i++) {
16+ System.out.printf("%2d:%#x\n", i, data[i]);
17+ }
18+*/
19+ String str = new String(data, 0, len);
20+ if(len < 8) {
21+ return "";
22+ }
23+ boolean isFinalPacket = (data[0] & 0x80) == 0x80;
24+ int opCode = data[0] & 0x15;
25+ String opCodeType = "";
26+ switch(opCode) {
27+ case 0x01:
28+ opCodeType = "text";
29+ break;
30+ default:
31+ break;
32+ }
33+ boolean masked = (data[1] & 0x80) == 0x80;
34+ byte payloadLength = (byte)(data[1] & 0x7f);
35+ if(payloadLength == 0x7e) {
36+ // next Uint16
37+ } else if(payloadLength == 0x7f) {
38+ // nexy Uint64
39+ }
40+ byte[] maskingKey = new byte[4];
41+ maskingKey[0] = data[2];
42+ maskingKey[1] = data[3];
43+ maskingKey[2] = data[4];
44+ maskingKey[3] = data[5];
45+ byte[] appData = new byte[payloadLength];
46+ for(int i = 0;i < payloadLength;i++) {
47+ appData[i] = (byte)(data[6 + i] ^ maskingKey[i % 4]);
48+
49+ }
50+ return new String(appData);
51+ }
52+ public static void send(String str) throws IOException {
53+ if(ops == null) {
54+ return;
55+ }
56+ ops.write(0x81);
57+ ops.write((byte)str.length());
58+ ops.write(str.getBytes());
59+ ops.flush();
60+
61+ }
62+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- branch/Basic 0.2x/src/servercore/job/GeneratePage.java (revision 73)
+++ branch/Basic 0.2x/src/servercore/job/GeneratePage.java (revision 74)
@@ -78,7 +78,6 @@
7878 private static void executeWebSokcet(HttpRequest req, String PackageName, OutputStream ops, InputStream ips) throws Exception {
7979
8080 StringBuffer sb = new StringBuffer();
81- System.out.println(req.getHeader());
8281 String webSocketVersion = req.getHeader().get("Sec-WebSocket-Version");
8382 String webSocketProtocol = req.getHeader().get("Sec-websocket-protocol");
8483 String[] protocols = webSocketProtocol != null ? webSocketProtocol.split(",") : new String[0];
@@ -89,8 +88,6 @@
8988
9089 String path = req.getRowPath();
9190 String key1 = req.getHeader().get("Sec-WebSocket-Key").trim();
92- //byte[] key2 = new BASE64Decoder().decodeBuffer(key1);
93- //key1 = new String(key2, 0, key2.length);
9491
9592 MessageDigest md = MessageDigest.getInstance("SHA1");
9693 md.update((key1 + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes());
@@ -114,71 +111,4 @@
114111 // 実行する
115112 method.invoke(cls.newInstance(), req, ops, ips);
116113 }
117- public static byte[] getChalengeKey(byte[] header) {
118- byte[] ret = null;
119- try {
120- for(int i = 0;i < header.length;i++) {
121- if(header[i] == '\r' && header[i + 1] == '\n') {
122- if(header[i + 2] == '\r' && header[i + 3] == '\n') {
123- int lastAt = header.length - (i + 4);
124- ret = new byte[lastAt];
125- System.arraycopy(header, i + 4, ret, 0, lastAt);
126- break;
127- }
128- }
129- }
130- } catch(Exception e) {
131- e.printStackTrace();
132- }
133- return ret;
134- }
135- public static long chooseNum(String str) {
136- String code = "";
137- for(int i = 0;i < str.length();i++) {
138- try {
139- int num = Integer.parseInt("" + str.charAt(i));
140- code += num;
141- } catch(Exception e) {}
142- }
143- long ret = 0;
144- try {
145- ret = Long.parseLong(code);
146- } catch(Exception e) {}
147- return ret;
148- }
149- public static int chooseSpace(String str) {
150- int ret = 0;
151- for(int i =0;i < str.length();i++) {
152- if(str.charAt(i) == ' ') {
153- ret++;
154- }
155- }
156- return ret;
157- }
158- public static byte[] bigendiean(long key1_w, long key2_w) {
159- long lon1 = key1_w;
160- long lon2 = key2_w;
161- byte[] bytes = new byte[8];
162- int[] ints = new int[8];
163- ints[3] = (int)(lon1 % 256);
164- lon1 /= 256;
165- ints[2] = (int)(lon1 % 256);
166- lon1 /= 256;
167- ints[1] = (int)(lon1 % 256);
168- lon1 /= 256;
169- ints[0] = (int)lon1;
170-
171- ints[7] = (int)(lon2 % 256);
172- lon2 /= 256;
173- ints[6] = (int)(lon2 % 256);
174- lon2 /= 256;
175- ints[5] = (int)(lon2 % 256);
176- lon2 /= 256;
177- ints[4] = (int)lon2;
178-
179- for(int i = 0;i < 8;i++) {
180- bytes[i] = (byte)ints[i];
181- }
182- return bytes;
183- }
184114 }
--- branch/Basic 0.2x/sokect_action/socket_action/WebTest.java (revision 73)
+++ branch/Basic 0.2x/sokect_action/socket_action/WebTest.java (revision 74)
@@ -5,20 +5,19 @@
55 import java.io.OutputStream;
66
77 import servercore.http.HttpRequest;
8+import servercore.html5.WebSocketFrame;
89
9-public class WebTest {
10- private static OutputStream ops = null;
11- private static InputStream ips = null;
10+public class WebTest extends WebSocketFrame{
1211 public boolean cont = true;
1312 public void execute(HttpRequest req, OutputStream ops, InputStream ips) {
14- this.ops = ops;
15- this.ips = ips;
13+ super.ops = ops;
14+ super.ips = ips;
1615 System.out.println("\r\nWebTest start");
1716 try {
1817 System.out.println("READ:" + read());
1918 while(cont) {
2019 try {
21- Thread.sleep(500);
20+ Thread.sleep(1000);
2221 send(String.valueOf(System.currentTimeMillis()));
2322 } catch (InterruptedException e) {
2423 e.printStackTrace();
@@ -30,55 +29,4 @@
3029 System.out.println("\r\nWebTest end");
3130 }
3231
33- public String read() throws IOException{
34- byte[] data = new byte[8098];
35- int len = ips.read(data);
36-/*
37- for(int i = 0;i < len;i++) {
38- System.out.printf("%2d:%#x\n", i, data[i]);
39- }
40-*/
41- String str = new String(data, 0, len);
42- if(len < 8) {
43- return "";
44- }
45- boolean isFinalPacket = (data[0] & 0x80) == 0x80;
46- int opCode = data[0] & 0x15;
47- String opCodeType = "";
48- switch(opCode) {
49- case 0x01:
50- opCodeType = "text";
51- break;
52- default:
53- break;
54- }
55- boolean masked = (data[1] & 0x80) == 0x80;
56- byte payloadLength = (byte)(data[1] & 0x7f);
57- if(payloadLength == 0x7e) {
58- // next Uint16
59- } else if(payloadLength == 0x7f) {
60- // nexy Uint64
61- }
62- byte[] maskingKey = new byte[4];
63- maskingKey[0] = data[2];
64- maskingKey[1] = data[3];
65- maskingKey[2] = data[4];
66- maskingKey[3] = data[5];
67- byte[] appData = new byte[payloadLength];
68- for(int i = 0;i < payloadLength;i++) {
69- appData[i] = (byte)(data[6 + i] ^ maskingKey[i % 4]);
70-
71- }
72- return new String(appData);
73- }
74- public static void send(String str) throws IOException {
75- if(ops == null) {
76- return;
77- }
78- ops.write(0x81);
79- ops.write((byte)str.length());
80- ops.write(str.getBytes());
81- ops.flush();
82-
83- }
8432 }
--- branch/Basic 0.2x/sokect_action/socket_action/WebTest2.java (revision 73)
+++ branch/Basic 0.2x/sokect_action/socket_action/WebTest2.java (revision 74)
@@ -4,17 +4,16 @@
44 import java.io.InputStream;
55 import java.io.OutputStream;
66
7+import servercore.html5.WebSocketFrame;
78 import servercore.http.HttpRequest;
89
9-public class WebTest2 {
10- private OutputStream ops = null;
11- private InputStream ips = null;
10+public class WebTest2 extends WebSocketFrame{
1211 public boolean cont = true;
1312 public static String A = "";
1413 private String B = null;
1514 public void execute(HttpRequest req, OutputStream ops, InputStream ips) {
16- this.ops = ops;
17- this.ips = ips;
15+ super.ops = ops;
16+ super.ips = ips;
1817 System.out.println("\r\nWebTest start");
1918 B = WebTest2.A;
2019 SyncsThread th = new SyncsThread();
@@ -42,27 +41,6 @@
4241 }catch(Exception e) {
4342 e.fillInStackTrace();
4443 }
45-
4644 }
4745 }
48- public String read() throws IOException{
49- byte[] data = new byte[8098];
50- int len = ips.read(data);
51- String str = "";
52- if(data[0] == 0 && data[len - 1] == -1) {
53- str = new String(data, 1, len - 2);
54- }
55- return str;
56- }
57-
58- public void send(String str) throws IOException {
59- if(ops == null) {
60- return;
61- }
62- ops.write(0x00);
63- ops.write(str.getBytes());
64- ops.write(0xff);
65- ops.flush();
66-
67- }
6846 }
Show on old repository browser