예제 #1
0
파일: Draft.java 프로젝트: nekitus/helper-2
  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);
  }
예제 #2
0
파일: Draft.java 프로젝트: nekitus/helper-2
 protected boolean basicAccept(Handshakedata handshakedata) {
   return handshakedata.getFieldValue("Upgrade").equalsIgnoreCase("websocket")
       && handshakedata
           .getFieldValue("Connection")
           .toLowerCase(Locale.ENGLISH)
           .contains("upgrade");
 }