Esempio n. 1
0
    /* ------------------------------------------------------------ */
    public void putTo(Buffer buffer) throws IOException {
      int o = (_name instanceof CachedBuffer) ? ((CachedBuffer) _name).getOrdinal() : -1;
      if (o >= 0) buffer.put(_name);
      else {
        int s = _name.getIndex();
        int e = _name.putIndex();
        while (s < e) {
          byte b = _name.peek(s++);
          switch (b) {
            case '\r':
            case '\n':
            case ':':
              continue;
            default:
              buffer.put(b);
          }
        }
      }

      buffer.put((byte) ':');
      buffer.put((byte) ' ');

      o = (_value instanceof CachedBuffer) ? ((CachedBuffer) _value).getOrdinal() : -1;
      if (o >= 0) buffer.put(_value);
      else {
        int s = _value.getIndex();
        int e = _value.putIndex();
        while (s < e) {
          byte b = _value.peek(s++);
          switch (b) {
            case '\r':
            case '\n':
              continue;
            default:
              buffer.put(b);
          }
        }
      }

      BufferUtil.putCRLF(buffer);
    }
  /* ------------------------------------------------------------ */
  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;
  }