Beispiel #1
0
 private ByteBuffer wrap(Buffer b) {
   byte[] array = b.array();
   if (array != null) {
     return ByteBuffer.wrap(array, b.getIndex(), b.length());
   } else {
     return ByteBuffer.wrap(b.asArray(), 0, b.length());
   }
 }
Beispiel #2
0
  public void addContent(Buffer content, boolean last) throws IOException {
    LOG.debug("addContent {} {}", content.length(), last);
    if (_noContent) {
      content.clear();
      return;
    }

    if (content.isImmutable()) throw new IllegalArgumentException("immutable");

    if (_last || _state == STATE_END) {
      LOG.debug("Ignoring extra content {}", content);
      content.clear();
      return;
    }
    _last = last;

    if (!_endp.isOpen()) {
      _state = STATE_END;
      return;
    }

    // Handle any unfinished business?
    if (_content != null && _content.length() > 0) {
      flushBuffer();
      if (_content != null && _content.length() > 0) throw new IllegalStateException("FULL");
    }

    _content = content;

    _contentWritten += content.length();

    // Handle the _content
    if (_head) {
      content.clear();
      _content = null;
    } else if (!last || _buffer != null) {
      // Yes - so we better check we have a buffer
      initBuffer();
      // Copy _content to buffer;
      int len = 0;
      len = _buffer.put(_content);

      // make sure there is space for a trailing null   (???)
      if (len > 0 && _buffer.space() == 0) {
        len--;
        _buffer.setPutIndex(_buffer.putIndex() - 1);
      }

      LOG.debug("copied {} to buffer", len);

      _content.skip(len);

      if (_content.length() == 0) _content = null;
    }
  }
  /* (non-Javadoc)
   * @see org.eclipse.jetty.websocket.AbstractExtension#onFrame(byte, byte, org.eclipse.jetty.io.Buffer)
   */
  @Override
  public void onFrame(byte flags, byte opcode, Buffer buffer) {
    if (getConnection().isControl(opcode) || !isFlag(flags, 1)) {
      super.onFrame(flags, opcode, buffer);
      return;
    }

    if (buffer.array() == null) buffer = buffer.asMutableBuffer();

    int length = 0xff & buffer.get();
    if (length >= 0x7e) {
      int b = (length == 0x7f) ? 8 : 2;
      length = 0;
      while (b-- > 0) length = 0x100 * length + (0xff & buffer.get());
    }

    // TODO check a max framesize

    _inflater.setInput(buffer.array(), buffer.getIndex(), buffer.length());
    ByteArrayBuffer buf = new ByteArrayBuffer(length);
    try {
      while (_inflater.getRemaining() > 0) {
        int inflated = _inflater.inflate(buf.array(), buf.putIndex(), buf.space());
        if (inflated == 0) throw new DataFormatException("insufficient data");
        buf.setPutIndex(buf.putIndex() + inflated);
      }

      super.onFrame(clearFlag(flags, 1), opcode, buf);
    } catch (DataFormatException e) {
      LOG.warn(e);
      getConnection().close(WebSocketConnectionRFC6455.CLOSE_BAD_PAYLOAD, e.toString());
    }
  }
Beispiel #4
0
    /* ------------------------------------------------------------ */
    public InputStream getInputStream() throws IOException {
      Buffer indirect = getIndirectBuffer();
      if (indirect != null && indirect.array() != null)
        return new ByteArrayInputStream(indirect.array(), indirect.getIndex(), indirect.length());

      return _resource.getInputStream();
    }
    /*
     *
     * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#startRequest(org.eclipse.io.Buffer,
     *      org.eclipse.io.Buffer, org.eclipse.io.Buffer)
     */
    @Override
    public void startRequest(Buffer method, Buffer uri, Buffer version) throws IOException {
      uri = uri.asImmutableBuffer();

      _host = false;
      _expect = false;
      _expect100Continue = false;
      _expect102Processing = false;
      _delayedHandling = false;
      _charset = null;

      if (_request.getTimeStamp() == 0) _request.setTimeStamp(System.currentTimeMillis());
      _request.setMethod(method.toString());

      try {
        _head = false;
        switch (HttpMethods.CACHE.getOrdinal(method)) {
          case HttpMethods.CONNECT_ORDINAL:
            _uri.parseConnect(uri.array(), uri.getIndex(), uri.length());
            break;

          case HttpMethods.HEAD_ORDINAL:
            _head = true;
            // fall through

          default:
            _uri.parse(uri.array(), uri.getIndex(), uri.length());
        }

        _request.setUri(_uri);

        if (version == null) {
          _request.setProtocol(HttpVersions.HTTP_0_9);
          _version = HttpVersions.HTTP_0_9_ORDINAL;
        } else {
          version = HttpVersions.CACHE.get(version);
          if (version == null) throw new HttpException(HttpStatus.BAD_REQUEST_400, null);
          _version = HttpVersions.CACHE.getOrdinal(version);
          if (_version <= 0) _version = HttpVersions.HTTP_1_0_ORDINAL;
          _request.setProtocol(version.toString());
        }
      } catch (Exception e) {
        LOG.debug(e);
        if (e instanceof HttpException) throw (HttpException) e;
        throw new HttpException(HttpStatus.BAD_REQUEST_400, null, e);
      }
    }
 public synchronized int flush() throws IOException {
   int flushed = flushBuffer();
   if (_buffer != null && _buffer.length() == 0) {
     _buffers.returnBuffer(_buffer);
     _buffer = null;
   }
   return flushed;
 }
    public void onFrame(byte flags, byte opcode, Buffer buffer) {
      try {
        byte[] array = buffer.array();

        if (opcode == 0) {
          if (_websocket instanceof WebSocket.OnTextMessage)
            ((WebSocket.OnTextMessage) _websocket).onMessage(buffer.toString(StringUtil.__UTF8));
        } else {
          if (_websocket instanceof WebSocket.OnBinaryMessage)
            ((WebSocket.OnBinaryMessage) _websocket)
                .onMessage(array, buffer.getIndex(), buffer.length());
        }
      } catch (Throwable th) {
        LOG.warn(th);
      }
    }
    public void startRequest(Buffer tok0, Buffer tok1, Buffer tok2) {
      try {
        request = true;
        h = -1;
        hdr = new String[9];
        val = new String[9];
        f0 = tok0.toString();
        f1 = new String(tok1.array(), tok1.getIndex(), tok1.length(), StringUtil.__UTF8);
        if (tok2 != null) f2 = tok2.toString();
        else f2 = null;

        fields = new HttpFields();
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
      }

      messageCompleted = false;
      headerCompleted = false;
    }
  /* ------------------------------------------------------------ */
  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;
  }
 public synchronized boolean isBufferEmpty() {
   return _buffer == null || _buffer.length() == 0;
 }