Spinoco / fs2-http

Http Server and client using fs2
MIT License
135 stars 26 forks source link

Websocket trying to read 2GB of a 728 bit string #33

Closed s5bug closed 5 years ago

s5bug commented 5 years ago
Failed to decode value: cannot acquire 16526844176 bits from a vector that contains 728 bits, content: ByteVector(95 bytes, 0x7b2274223a6e756c6c2c2273223a6e756c6c2c226f70223a31302c2264223a7b226865617274626561745f696e74657276616c223a34313235302c225f7472616365223a5b22676174657761792d7072642d6d61696e2d6b673677225d7d7d)
    at spinoco.fs2.http.websocket.WebSocket$impl$.decode$1(WebSocket.scala:323)

I'm using websockets here and here.

A test on your end can be done like so:

object Main extends IOApp {

  override def run(args: List[String]): IO[ExitCode] = {
    implicit val ag = AsynchronousChannelGroup.withThreadPool(Executors.newCachedThreadPool())
    Stream
      .eval(Queue.unbounded[IO, Event])
      .flatMap(queue => {
        Client[IO]("TOKEN_HERE") { in =>
          in.flatMap(e => {
            println(s"Received: $e")
            Stream.empty
          })
        }.login(queue, Sharding.Single())
      })
      .compile
      .drain
      .as(ExitCode.Success)
  }

}

Replacing TOKEN_HERE with a Bot token you get from the Discord Developer Console.

pchlupacek commented 5 years ago

@AdamChlupacek could you please take a loot on this one? This seem to be specific to discord usage.

AdamChlupacek commented 5 years ago

@sorenbug Checking out your code and doing bit of tracking, it would seem that the error is coming from how the decode of the websocket frames into string is handled. By default it would seem that you are getting the default implicit val implicitStringCodec: Codec[String] = utf8_32 decoder which is utf8 prefixed with size. In case of websocket therre is no size prefix so you need just plain utf8.

If you change the line client.websocket(req, pipe) into client.websocket(req, pipe)(scodec.codecs.utf8, scodec.codecs.utf8), it will be just doing plain utf8 decode and it will work.

s5bug commented 5 years ago

It works! Thank you!