0

I’m interesting in processing chunked requests with Scalatra. Does Scalatra support an access to the single chunk of chunked http request? Or I only have to wait the end of a chunked request and process the whole request after?

StEVee
  • 48
  • 4

1 Answers1

1

The Scalalatra is just a wrapper around Java Servlets. It allows you to access richRequest.inputStream directly. Everything else is same as for Java.

You might need to parse chunked encoding from the input stream.

See also: Chunked http decoding in java?

You can find a wrapper for InputStream here: http://www.java2s.com/Code/Java/File-Input-Output/AnInputStreamthatimplementsHTTP11chunking.htm

Community
  • 1
  • 1
dk14
  • 21,273
  • 4
  • 45
  • 81
  • Is this input stream available immediately after receiving the first chunk? Should next chunks also be in this input stream? – StEVee Mar 28 '17 at 13:53
  • 1
    @StEVee Yes, Yes. BUT , you have to parse chunked data encoding on your own ([the data inside input stream is raw](https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1)) or using the wrapper link I provided (actually you could check the code of wrapper - it's pretty self-explanatory) – dk14 Mar 28 '17 at 17:47
  • More precisely - InputStream is lazy in it's nature, (it's a little simplified explanation, but) it just reads data from socket's buffer when it's available even if it's not chunked. Basically the only difference chunked-encoding brings is that you don't have to specify Content-Length, so client could dynamically choose size of the data during streaming, so it could dynamically choose when the stream ends (when to close a connection). There is no more magic in it. – dk14 Mar 28 '17 at 18:39
  • One more question about magic. There is the method `inputStream.available` that _returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking_. Could it return 0 for a non-chunked request with body? Some rare case when headers are already read from the socket but the body is not yet – StEVee Mar 28 '17 at 21:11
  • @StEVee If you want to get some iterator of chunks themselves - I wouldn't use available - you can always read chunk's size from the first line of every chunk, it's basically your real "available" (available returned by InputStream could be less or even 0 when you read the whole local buffer: local buffer size not equals chunk size) – dk14 Mar 29 '17 at 02:15
  • @StEVee if you use the wrapper provided - it always reads no more than chunk-size bytes.You can indicate end of chunk by `chunkedInputStream.chunkCount == 0` – dk14 Mar 29 '17 at 02:20
  • 1
    @StEVee Answering the question about "available". It actually depends on implementation, but overall when you read data from socket to some local buffer 0 indicates end of connection. However, available indicates how much data is left in the local buffer, and in some implementations it's theoretically possible that local buffer size is exactly equal to header's size, so available would be zero after reading the header. – dk14 Mar 29 '17 at 02:45