Example #1
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();
    }
  /* (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());
    }
  }
    /*
     *
     * @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);
      }
    }
Example #4
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());
   }
 }
Example #5
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 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;
  }