Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rx-apache-http recognizes "Transfer-Encoding: chunked" as an HTTP stream #435

Merged
merged 1 commit into from
Oct 16, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.IOException;

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

private boolean responseIsStreamLike(HttpResponse response) {
final Header contentType = response.getFirstHeader("Content-Type");
// use 'contains' instead of equals since Content-Type can contain additional information
// such as charset ... see here: http://www.w3.org/International/O-HTTP-charset
if (contentType != null && contentType.getValue().contains("text/event-stream")) {
return true;
}
final Header transferEncoding = response.getFirstHeader("Transfer-Encoding");
if (transferEncoding != null && transferEncoding.getValue().equals("chunked")) {
return true;
}
return false;
}

@Override
protected void onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException {
consumer._onContentReceived(decoder, ioctrl);
Expand Down