Chunked transfer encoding in Play! framework

1,069 views
Skip to first unread message

unkx80

unread,
Aug 13, 2011, 12:46:40 PM8/13/11
to play-framework
Hi,

Many thanks for creating and maintaining the Play! framework. It
simplifies the development of web applications quite a bit.

However, it seems that trying to stream data is unusually difficult,
particularly binary data. In some cases, I am interested to stream
large amounts of data. In other cases, I am interested in streaming
live data. Either way, it is not practical to write the data to a file
and then render it. Therefore, I am looking into chunked transfer
encoding. However, I ran into some problems trying to stream data, and
documentation for this area appears to be almost non-existent.

public class Application extends Controller {
public static void test1() {
response.setContentTypeIfNotSet("text/plain");
response.writeChunk("a");
response.writeChunk("b");
response.writeChunk("c");
}
public static void test2() {
response.setContentTypeIfNotSet("text/plain");
byte[] bytes = "abc".getBytes();
response.writeChunk(bytes);
}
}

For both test1 and test2, I was expecting the following output:

abc

However, I got this instead for test1:

a
b
c

And for test2, I got this:

[B@129552

For both cases, it appears that there is no support for sending out
binary data in chunks. In particular, the first case seems to
introduce extra CRLF bytes, which breaks certain kinds of string data
as well.

I would like advice on whether I am streaming the data correctly, or
whether there will be better support for the chunked transfer encoding
in future versions of Play! framework. In addition, I would like to
suggest that code examples for streaming be included in the Play!
documentation. For your information, I am running Play! framework
1.2.2 as a standalone web server.

Thanks in advance.

Regards,
Yee Fan TAN

unkx80

unread,
Aug 17, 2011, 4:22:56 PM8/17/11
to play-framework
Ticket 920 (http://play.lighthouseapp.com/projects/57987/tickets/920-
response-streaming-chunked-encoding-bug) pointed me to the location to
fix these two issues. Hence, I have created a ticket (http://
play.lighthouseapp.com/projects/57987/tickets/1043-chunked-transfer-
encoding-issues) and a pull request (https://github.com/playframework/
play/pull/312). My pull request is intended to supersede ticket 920.

I am quite new to this process, so I hope I have followed the
contributor guide correctly. Please consider my pull request. Many
thanks!

Regards,
Yee Fan

Nicolas Leroux

unread,
Aug 17, 2011, 4:25:22 PM8/17/11
to play-fr...@googlegroups.com
Thank you very much. We will review the pull request as soon as possible.

Nicolas

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

unkx80

unread,
Aug 18, 2011, 2:52:38 PM8/18/11
to play-framework
Hi Nicholas,

On a related note, I am wondering whether there is a way to know that
the client has closed the HTTP connection in the middle of sending a
chunked response stream, so that the server can stop sending as well.
Controller code would be similar to the following:

public class Application extends Controller {
public static void index() {
response.setContentTypeIfNotSet("text/plain");
while (true) {
response.writeChunk("somedata");
boolean closed = // ???
if (closed)
break;
}
}
}

This kind of use case is for streaming of live data, where the server
keeps sending the data indefinitely until the client closes the HTTP
connection. An example is streaming of live video from a camera.

Thanks.

Regards,
Yee Fan

Nicolas

unread,
Aug 18, 2011, 4:16:36 PM8/18/11
to play-fr...@googlegroups.com
Hi,

Hmm, I think that writeChunck will then throw some sort of exception because it would not be able to write to the socket, no?

Nicolas

unkx80

unread,
Aug 19, 2011, 1:52:16 AM8/19/11
to play-framework
No, at least not in Play 1.2.2. The index counter in the code below
keeps printing even though the browser is closed.

public static void index() {
response.setContentTypeIfNotSet("text/plain");
int index = 0;
while (true) {
response.writeChunk("data");
System.out.println(index);
try {
Thread.sleep(1000L);
}
catch (InterruptedException e) {
}
index++;
}
}

Let me see whether I can figure this out.

Regards,
Yee Fan

unkx80

unread,
Aug 19, 2011, 3:19:34 AM8/19/11
to play-framework
Okay I figured it out. I pushed a commit into the branch with the pull
request.
https://github.com/playframework/play/pull/312

It turns out that in PlayHandler.java, the writeChunk() method in
LazyChunkedInput class did not check for the closed state, so I added
an exception for that case.

Regards,
Yee Fan

unkx80

unread,
Mar 29, 2012, 5:37:00 PM3/29/12
to play-fr...@googlegroups.com
Dear Play! developers,

As of version 1.2.4, I still see no fix to the chunked transfer encoding issues.
Any chance I see these issues fixed in 1.2.5?

Thanks,
Yee Fan
Reply all
Reply to author
Forward
0 new messages