Skip to content

Commit 0b3a6f5

Browse files
Merge pull request #435 from mattrjacobs/http-chunked
rx-apache-http recognizes "Transfer-Encoding: chunked" as an HTTP stream
2 parents fcf13c2 + 18eed1a commit 0b3a6f5

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Diff for: rxjava-contrib/rxjava-apache-http/src/main/java/rx/apache/http/consumers/ResponseConsumerDelegate.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.IOException;
1919

20+
import org.apache.http.Header;
2021
import org.apache.http.HttpEntity;
2122
import org.apache.http.HttpException;
2223
import org.apache.http.HttpResponse;
@@ -52,9 +53,7 @@ public ResponseConsumerDelegate(final Observer<? super ObservableHttpResponse> o
5253
@Override
5354
protected void onResponseReceived(HttpResponse response) throws HttpException, IOException {
5455
// when we receive the response with headers we evaluate what type of consumer we want
55-
if (response.getFirstHeader("Content-Type").getValue().contains("text/event-stream")) {
56-
// use 'contains' instead of equals since Content-Type can contain additional information
57-
// such as charset ... see here: http://www.w3.org/International/O-HTTP-charset
56+
if (responseIsStreamLike(response)) {
5857
consumer = new ResponseConsumerEventStream(observer, subscription);
5958
} else {
6059
consumer = new ResponseConsumerBasic(observer, subscription);
@@ -63,6 +62,20 @@ protected void onResponseReceived(HttpResponse response) throws HttpException, I
6362
consumer._onResponseReceived(response);
6463
}
6564

65+
private boolean responseIsStreamLike(HttpResponse response) {
66+
final Header contentType = response.getFirstHeader("Content-Type");
67+
// use 'contains' instead of equals since Content-Type can contain additional information
68+
// such as charset ... see here: http://www.w3.org/International/O-HTTP-charset
69+
if (contentType != null && contentType.getValue().contains("text/event-stream")) {
70+
return true;
71+
}
72+
final Header transferEncoding = response.getFirstHeader("Transfer-Encoding");
73+
if (transferEncoding != null && transferEncoding.getValue().equals("chunked")) {
74+
return true;
75+
}
76+
return false;
77+
}
78+
6679
@Override
6780
protected void onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException {
6881
consumer._onContentReceived(decoder, ioctrl);

0 commit comments

Comments
 (0)