-
Notifications
You must be signed in to change notification settings - Fork 9
[#120179893] Make sure compression and gzip header go together #27
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,14 +14,19 @@ | |
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.client.HttpClient; | ||
| import org.apache.http.entity.AbstractHttpEntity; | ||
| import org.apache.http.entity.ByteArrayEntity; | ||
| import org.apache.http.entity.StringEntity; | ||
| import org.apache.http.impl.client.AbstractHttpClient; | ||
| import org.apache.http.protocol.HTTP; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.URI; | ||
| import java.text.ParseException; | ||
| import java.util.ArrayList; | ||
| import java.util.GregorianCalendar; | ||
| import java.util.Locale; | ||
| import java.util.zip.GZIPOutputStream; | ||
|
|
||
| public class Dispatcher { | ||
|
|
||
|
|
@@ -232,8 +237,7 @@ private static Header[] removeGzipEncodingHeader(Header[] headers) { | |
| ArrayList<Header> filteredHeaders = new ArrayList<Header>(); | ||
|
|
||
| for (Header header : headers) { | ||
| if (header.getKey().equalsIgnoreCase("content-encoding") | ||
| && header.getValue().equalsIgnoreCase("gzip")) { | ||
| if (header.getKey().equalsIgnoreCase("content-encoding")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this shouldGzip() method isn't being used anymore and can be removed
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very good spot. Thanks. Actually shouldGzip should be used. If we don't receive the header from elemez (or any app using reyna lib) we should not compress at all. Fixing it. |
||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -244,9 +248,44 @@ private static Header[] removeGzipEncodingHeader(Header[] headers) { | |
| return filteredHeaders.toArray(returnedHeaders); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we leave the old logic as it is and check if the steam is compressed or not Or just replace the function as you did but remove this check as this means we have to go through every single bytes if it is not compressed. |
||
|
|
||
| private static AbstractHttpEntity getCompressedEntity(String content, Context context) throws Exception { | ||
| byte[] data = content.getBytes(); | ||
| return AndroidHttpClient.getCompressedEntity(data, context.getContentResolver()); | ||
| private static AbstractHttpEntity getEntity(Message message, Context context) throws Exception { | ||
| String content = message.getBody(); | ||
|
|
||
| byte[] data = content.getBytes("UTF-8"); | ||
| if (data.length <= (AndroidHttpClient.getMinGzipSize(context.getContentResolver()) * 2)){ | ||
|
||
| return new StringEntity(content, HTTP.UTF_8); | ||
| } | ||
| else { | ||
| return getCompressedEntity(data, context); | ||
| } | ||
| } | ||
|
|
||
| private static AbstractHttpEntity getCompressedEntity(byte[] data, Context context) throws Exception { | ||
| ByteArrayOutputStream arr = new ByteArrayOutputStream(); | ||
| OutputStream zipper = new GZIPOutputStream(arr); | ||
| zipper.write(data); | ||
| zipper.close(); | ||
|
|
||
| byte[] compressedData = arr.toByteArray(); | ||
| int end = compressedData.length > 500 ? 500 : compressedData.length; | ||
|
|
||
| Boolean equal = true; | ||
| for (int i = 0; i < end; i++){ | ||
| if (compressedData[i] != data[i]){ | ||
| equal = false; | ||
| break; | ||
| } | ||
| } | ||
| AbstractHttpEntity entity; | ||
| if (!equal){ | ||
| entity = new ByteArrayEntity(compressedData); | ||
| entity.setContentEncoding("gzip"); | ||
| } | ||
| else { | ||
| entity = new ByteArrayEntity(data); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this is maybe missing a unit test case |
||
| entity.setContentEncoding(""); | ||
| } | ||
| return entity; | ||
| } | ||
|
|
||
| private void setHeaders(HttpPost httpPost, Header[] headers) { | ||
|
|
@@ -263,16 +302,7 @@ private String getSubmittedTimestamp() { | |
| return String.valueOf(this.clock.getCurrentTimeMillis()); | ||
| } | ||
|
|
||
| private static AbstractHttpEntity getEntity(Message message, Context context) throws Exception { | ||
| String content = message.getBody(); | ||
| AbstractHttpEntity entity = new StringEntity(content, HTTP.UTF_8); | ||
|
|
||
| if (Dispatcher.shouldGzip(message.getHeaders())) { | ||
| entity = getCompressedEntity(content, context); | ||
| } | ||
|
|
||
| return entity; | ||
| } | ||
|
|
||
| public static boolean isBatteryCharging(Context context) { | ||
| Intent batteryStatus = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you explained to me already but I'm still not quite understanding why this has changed, this test should not be gzipping anything as far as i can tell, meaning it shouldn't be limited by gzip min size and so shouldn't be entering the getCompressedEntity() method, and staying as text/plain. Or am i missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving back to the old version. This was failing due to the missing check for the shouldGzip function. With the check in place the previous test case works. I didn't observe that this test sets up for no compression headers (getMessageWithHeaders instead of getMessageWithGzipHeaders). Nice spot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😎