예제 #1
0
  @Test
  public void testCase() throws Exception {
    HttpFields fields = new HttpFields();
    Set s;
    //         0123456789012345678901234567890
    byte[] b = "Message-IDmessage-idvalueVALUE".getBytes();
    ByteArrayBuffer buf = new ByteArrayBuffer(512);
    buf.put(b);

    View headUC = new View.CaseInsensitive(buf);
    View headLC = new View.CaseInsensitive(buf);
    View valUC = new View(buf);
    View valLC = new View(buf);
    headUC.update(0, 10);
    headLC.update(10, 20);
    valUC.update(20, 25);
    valLC.update(25, 30);

    fields.add("header", "value");
    fields.add(headUC, valLC);
    fields.add("other", "data");
    s = enum2set(fields.getFieldNames());
    assertEquals(3, s.size());
    assertTrue(s.contains("message-id"));
    assertEquals("value", fields.getStringField("message-id").toLowerCase());
    assertEquals("value", fields.getStringField("Message-ID").toLowerCase());

    fields.clear();

    fields.add("header", "value");
    fields.add(headLC, valLC);
    fields.add("other", "data");
    s = enum2set(fields.getFieldNames());
    assertEquals(3, s.size());
    assertTrue(s.contains("message-id"));
    assertEquals("value", fields.getStringField("Message-ID").toLowerCase());
    assertEquals("value", fields.getStringField("message-id").toLowerCase());

    fields.clear();

    fields.add("header", "value");
    fields.add(headUC, valUC);
    fields.add("other", "data");
    s = enum2set(fields.getFieldNames());
    assertEquals(3, s.size());
    assertTrue(s.contains("message-id"));
    assertEquals("value", fields.getStringField("message-id").toLowerCase());
    assertEquals("value", fields.getStringField("Message-ID").toLowerCase());

    fields.clear();

    fields.add("header", "value");
    fields.add(headLC, valUC);
    fields.add("other", "data");
    s = enum2set(fields.getFieldNames());
    assertEquals(3, s.size());
    assertTrue(s.contains("message-id"));
    assertEquals("value", fields.getStringField("Message-ID").toLowerCase());
    assertEquals("value", fields.getStringField("message-id").toLowerCase());
  }
 /* ------------------------------------------------------------ */
 private void doTheHixieHixieShake() {
   byte[] result =
       WebSocketConnectionD00.doTheHixieHixieShake(
           WebSocketConnectionD00.hixieCrypt(_key1),
           WebSocketConnectionD00.hixieCrypt(_key2),
           _hixieBytes.asArray());
   _hixieBytes.clear();
   _hixieBytes.put(result);
 }
예제 #3
0
  public void sendMessage(String... msgs) throws IOException {
    int len = 0;
    for (String msg : msgs) {
      LOG.debug("sending message: " + msg);
      len += (msg.length() + 2);
    }

    ByteArrayBuffer buf = new ByteArrayBuffer(len);

    for (String msg : msgs) {
      buf.put((byte) 0x00);
      buf.put(msg.getBytes("UTF-8"));
      buf.put((byte) 0xFF);
    }

    out.write(buf.array());
    out.flush();
  }
  /* ------------------------------------------------------------ */
  public Connection handle() throws IOException {
    try {
      // handle stupid hixie random bytes
      if (_hixieBytes != null) {

        // take any available bytes from the parser buffer, which may have already been read
        Buffer buffer = _parser.getBuffer();
        if (buffer != null && buffer.length() > 0) {
          int l = buffer.length();
          if (l > (8 - _hixieBytes.length())) l = 8 - _hixieBytes.length();
          _hixieBytes.put(buffer.peek(buffer.getIndex(), l));
          buffer.skip(l);
        }

        // while we are not blocked
        while (_endp.isOpen()) {
          // do we now have enough
          if (_hixieBytes.length() == 8) {
            // we have the silly random bytes
            // so let's work out the stupid 16 byte reply.
            doTheHixieHixieShake();
            _endp.flush(_hixieBytes);
            _hixieBytes = null;
            _endp.flush();
            break;
          }

          // no, then let's fill
          int filled = _endp.fill(_hixieBytes);
          if (filled < 0) {
            _endp.flush();
            _endp.close();
            break;
          }
        }

        if (_websocket instanceof OnFrame) ((OnFrame) _websocket).onHandshake(this);
        _websocket.onOpen(this);
        return this;
      }

      // handle the framing protocol
      boolean progress = true;

      while (progress) {
        int flushed = _generator.flush();
        int filled = _parser.parseNext();

        progress = flushed > 0 || filled > 0;

        _endp.flush();

        if (_endp instanceof AsyncEndPoint && ((AsyncEndPoint) _endp).hasProgressed())
          progress = true;
      }
    } catch (IOException e) {
      LOG.debug(e);
      try {
        if (_endp.isOpen()) _endp.close();
      } catch (IOException e2) {
        LOG.ignore(e2);
      }
      throw e;
    } finally {
      if (_endp.isOpen()) {
        if (_endp.isInputShutdown() && _generator.isBufferEmpty()) _endp.close();
        else checkWriteable();

        checkWriteable();
      }
    }
    return this;
  }