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
| public class Handshake { private byte crlf13 = (byte) 13;
private byte crlf10 = (byte) 10;
private InputStream input;
Map http = new HashMap();
public Handshake(InputStream input) { this.input = input; }
public byte[] getResponse() throws IOException { StringBuffer header = new StringBuffer(); byte[] content = new byte[8]; byte[] crlf = new byte[1]; int crlfNum = 0;
while (input.read(crlf) != -1) { if (crlf[0] == crlf13 || crlf[0] == crlf10) { crlfNum++; } else { crlfNum = 0; } header.append(new String(crlf, 0, 1)); if (crlfNum == 4) { input.read(content); break; } }
String[] hhh = header.toString().split("\r\n"); http.put("Method", hhh[0].split(" ")[0]); http.put("Path", hhh[0].split(" ")[1]); http.put("Http-Protocol", hhh[0].split(" ")[2]);
http.put("Upgrade", header.substring(header.indexOf("Upgrade: ") + 9) .split("\r\n")[0]); http.put("Connection", header.substring( header.indexOf("Connection: ") + 12).split("\r\n")[0]); http.put("Host", header.substring(header.indexOf("Host: ") + 6).split( "\r\n")[0]); http.put("Origin", header.substring(header.indexOf("Origin: ") + 8 ) .split("\r\n")[0]); http.put("Sec-WebSocket-Key1", header.substring( header.indexOf("Sec-WebSocket-Key1: ") + 20).split("\r\n")[0]); http.put("Sec-WebSocket-Key2", header.substring( header.indexOf("Sec-WebSocket-Key2: ") + 20).split("\r\n")[0]); http.put("Content", new String(content));
String key1 = http.get("Sec-WebSocket-Key1"); String key2 = http.get("Sec-WebSocket-Key2"); long a = Long.parseLong(filterNonNumeric(key1)) / filterNonSpace(key1).length(); long b = Long.parseLong(filterNonNumeric(key2)) / filterNonSpace(key2).length(); String ekey1 = Long.toHexString(a).toUpperCase(); String ekey2 = Long.toHexString(b).toUpperCase(); String ekey3 = bytes2HexStr(content);
while (ekey1.length() < 8 ) ekey1 = "0" + ekey1; while (ekey2.length() < 8 ) ekey2 = "0" + ekey2; while (ekey3.length() < 8 ) ekey3 = "0" + ekey3;
byte[] bb = hexStr2Bytes(ekey1 + ekey2 + ekey3); byte[] challenge = null; try { challenge = MessageDigest.getInstance("MD5").digest(bb); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
StringBuffer sb = new StringBuffer(); sb.append("HTTP/1.1 101 WebSocket Protocol Handshake\r\n"); sb.append("Upgrade: WebSocket\r\n"); sb.append("Connection: Upgrade\r\n"); sb.append("Sec-WebSocket-Origin: " + http.get("Origin") + "\r\n"); sb.append("Sec-WebSocket-Location: ws://" + http.get("Host") + http.get("Path") + "\r\n\r\n");
return addByte(sb.toString().getBytes(), challenge); }
public byte[] getMsg() throws IOException { byte[] bbbb = null; byte[] crlf = new byte[1]; while (input.read(crlf) != -1) { if (crlf[0] == (byte) 0) { bbbb = new byte[] {}; } bbbb = addByte(bbbb, crlf); if (crlf[0] == (byte) 255) { break; } } return bbbb; } }
|