• R/O
  • SSH
  • HTTPS

webserver: Commit


Commit MetaInfo

Revision73 (tree)
Time2012-12-13 04:14:28
Authorhoritaku

Log Message

WebSocket-Version 13に対応中

Change Summary

Incremental Difference

--- branch/Basic 0.2x/sokect_action/socket_action/WebTest.java (revision 72)
+++ branch/Basic 0.2x/sokect_action/socket_action/WebTest.java (revision 73)
@@ -8,14 +8,17 @@
88
99 public class WebTest {
1010 private static OutputStream ops = null;
11+ private static InputStream ips = null;
1112 public boolean cont = true;
1213 public void execute(HttpRequest req, OutputStream ops, InputStream ips) {
1314 this.ops = ops;
15+ this.ips = ips;
1416 System.out.println("\r\nWebTest start");
1517 try {
18+ System.out.println("READ:" + read());
1619 while(cont) {
1720 try {
18- Thread.sleep(2000);
21+ Thread.sleep(500);
1922 send(String.valueOf(System.currentTimeMillis()));
2023 } catch (InterruptedException e) {
2124 e.printStackTrace();
@@ -27,13 +30,54 @@
2730 System.out.println("\r\nWebTest end");
2831 }
2932
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+ }
3074 public static void send(String str) throws IOException {
3175 if(ops == null) {
3276 return;
3377 }
34- ops.write(0x00);
78+ ops.write(0x81);
79+ ops.write((byte)str.length());
3580 ops.write(str.getBytes());
36- ops.write(0xff);
3781 ops.flush();
3882
3983 }
--- branch/Basic 0.2x/src/servercore/job/GeneratePage.java (revision 72)
+++ branch/Basic 0.2x/src/servercore/job/GeneratePage.java (revision 73)
@@ -7,6 +7,8 @@
77 import java.lang.reflect.Method;
88 import java.security.MessageDigest;
99 import java.security.NoSuchAlgorithmException;
10+import sun.misc.BASE64Decoder;
11+import sun.misc.BASE64Encoder;
1012
1113 import lib.io.ByteUtil;
1214 import servercore.http.HttpRequest;
@@ -74,42 +76,36 @@
7476 return (HttpResponse)method.invoke(cls.newInstance(),req);
7577 }
7678 private static void executeWebSokcet(HttpRequest req, String PackageName, OutputStream ops, InputStream ips) throws Exception {
77- String host = req.getHeader().get("Host");
79+
7880 StringBuffer sb = new StringBuffer();
81+ System.out.println(req.getHeader());
82+ String webSocketVersion = req.getHeader().get("Sec-WebSocket-Version");
83+ String webSocketProtocol = req.getHeader().get("Sec-websocket-protocol");
84+ String[] protocols = webSocketProtocol != null ? webSocketProtocol.split(",") : new String[0];
85+ int wsVersion = 0;
86+ if(!"".equals(webSocketVersion)) {
87+ wsVersion = Integer.parseInt(webSocketVersion);
88+ }
7989
8090 String path = req.getRowPath();
81- String key1 = req.getHeader().get("Sec-WebSocket-Key1");
82- String key2 = req.getHeader().get("Sec-WebSocket-Key2");
83- String origin = req.getHeader().get("Origin");
84- String res_ws = "ws://" + host + path;
85- byte[] webSocketsByte = new byte[128];
86- int readBytes = ips.read(webSocketsByte);
87- byte[] body_code = new byte[readBytes];
88- System.arraycopy(webSocketsByte, 0, body_code, 0,readBytes);
89- System.out.println(body_code);
90- long key1_num = chooseNum(key1);
91- long key2_num = chooseNum(key2);
92- int key1_s = chooseSpace(key1);
93- int key2_s = chooseSpace(key2);
94- long key1_w = key1_num / key1_s;
95- long key2_w = key2_num / key2_s;
96- byte[] key_head = bigendiean(key1_w, key2_w);
97- byte[] byte_keys = new byte[16];
98- System.arraycopy(key_head, 0, byte_keys, 0, 8);
99- System.arraycopy(body_code, 0, byte_keys, 8, 8);
91+ String key1 = req.getHeader().get("Sec-WebSocket-Key").trim();
92+ //byte[] key2 = new BASE64Decoder().decodeBuffer(key1);
93+ //key1 = new String(key2, 0, key2.length);
10094
101- MessageDigest md = MessageDigest.getInstance("MD5");
102- md.update(byte_keys);
95+ MessageDigest md = MessageDigest.getInstance("SHA1");
96+ md.update((key1 + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes());
10397 byte[] hash = md.digest();
98+ String returnKey = new BASE64Encoder().encode(hash);
10499
105- sb.append("HTTP/1.1 101 WebSocket Protocol Handshake\r\n");
106- sb.append("Upgrade: WebSocket\r\n");
100+ sb.append("HTTP/1.1 101 Switching Protocol\r\n");
101+ sb.append("Upgrade: websocket\r\n");
107102 sb.append("Connection: Upgrade\r\n");
108- sb.append("Sec-WebSocket-Location: " + res_ws + "\r\n");
109- sb.append("Sec-WebSocket-Origin: http://" + host + "\r\n");
103+ sb.append("Sec-WebSocket-Accept: " + returnKey + "\r\n");
104+ if(protocols.length > 0) {
105+ sb.append("Sec-WebSocket-Protocol: " + protocols[0] + "\r\n");
106+ }
110107 sb.append("\r\n");
111108 ops.write(sb.toString().getBytes());
112- ops.write(hash);
113109
114110 Class<Action> cls = null;
115111
Show on old repository browser