Skip to content

Commit

Permalink
Merge pull request ReactiveX#435 from mattrjacobs/http-chunked
Browse files Browse the repository at this point in the history
rx-apache-http recognizes "Transfer-Encoding: chunked" as an HTTP stream
  • Loading branch information
benjchristensen committed Oct 16, 2013
2 parents 229914e + 2bcadba commit b33657d
Showing 1 changed file with 16 additions and 3 deletions.
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

0 comments on commit b33657d

Please sign in to comment.