public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownrole, boolean withcontent) { StringBuilder bui = new StringBuilder(100); if (handshakedata instanceof ClientHandshake) { bui.append("GET "); bui.append(((ClientHandshake) handshakedata).getResourceDescriptor()); bui.append(" HTTP/1.1"); } else if (handshakedata instanceof ServerHandshake) { bui.append("HTTP/1.1 101 " + ((ServerHandshake) handshakedata).getHttpStatusMessage()); } else { throw new RuntimeException("unknow role"); } bui.append("\r\n"); Iterator<String> it = handshakedata.iterateHttpFields(); while (it.hasNext()) { String fieldname = it.next(); String fieldvalue = handshakedata.getFieldValue(fieldname); bui.append(fieldname); bui.append(": "); bui.append(fieldvalue); bui.append("\r\n"); } bui.append("\r\n"); byte[] httpheader = Charsetfunctions.asciiBytes(bui.toString()); byte[] content = withcontent ? handshakedata.getContent() : null; ByteBuffer bytebuffer = ByteBuffer.allocate((content == null ? 0 : content.length) + httpheader.length); bytebuffer.put(httpheader); if (content != null) bytebuffer.put(content); bytebuffer.flip(); return Collections.singletonList(bytebuffer); }
protected boolean basicAccept(Handshakedata handshakedata) { return handshakedata.getFieldValue("Upgrade").equalsIgnoreCase("websocket") && handshakedata .getFieldValue("Connection") .toLowerCase(Locale.ENGLISH) .contains("upgrade"); }
public static int readVersion(Handshakedata handshakedata) { String vers = handshakedata.getFieldValue("Sec-WebSocket-Version"); if (vers.length() > 0) { int v; try { v = Integer.valueOf(vers.trim()); return v; } catch (NumberFormatException e) { return -1; } } return -1; }