Index: content/browser/webui/web_ui_data_source_impl.cc |
diff --git a/content/browser/webui/web_ui_data_source_impl.cc b/content/browser/webui/web_ui_data_source_impl.cc |
index 0a4c341649709c106323a5199d3900d3afbed755..8053526f59038263a7493475320752a185e033f0 100644 |
--- a/content/browser/webui/web_ui_data_source_impl.cc |
+++ b/content/browser/webui/web_ui_data_source_impl.cc |
@@ -13,13 +13,45 @@ |
#include "base/memory/ref_counted_memory.h" |
#include "base/strings/string_util.h" |
#include "base/strings/utf_string_conversions.h" |
+#include "base/sys_byteorder.h" |
#include "content/grit/content_resources.h" |
#include "content/public/browser/content_browser_client.h" |
#include "content/public/common/content_client.h" |
+#include "third_party/zlib/zlib.h" |
#include "ui/base/template_expressions.h" |
#include "ui/base/webui/jstemplate_builder.h" |
#include "ui/base/webui/web_ui_util.h" |
+namespace { |
+// Decodes given gzip input via zlib. |
+scoped_refptr<base::RefCountedString> DecodeGzipData( |
+ const unsigned char* input_buffer, |
+ size_t input_size) { |
+ z_stream infstream; |
+ infstream.zalloc = Z_NULL; |
+ infstream.zfree = Z_NULL; |
+ infstream.opaque = Z_NULL; |
+ |
+ infstream.avail_in = input_size; |
+ infstream.next_in = const_cast<Bytef*>(input_buffer); |
+ |
+ // Size of output comes from footer of gzip file format, found as the last 4 |
+ // bytes in the compressed file, which are stored little endian. |
+ infstream.avail_out = base::ByteSwapToLE32( |
+ *reinterpret_cast<const uint32_t*>(&input_buffer[input_size - 4])); |
+ |
+ std::string output; |
+ output.resize(infstream.avail_out); |
+ infstream.next_out = reinterpret_cast<Bytef*>(&output[0]); // output buffer |
+ |
+ CHECK(inflateInit2(&infstream, 16) == Z_OK); |
+ CHECK(inflate(&infstream, Z_FINISH) == Z_STREAM_END); |
+ CHECK(inflateEnd(&infstream) == Z_OK); |
+ |
+ return base::RefCountedString::TakeString(&output); |
+} |
+} // end of anynomous namespace |
+ |
namespace content { |
// static |
@@ -239,6 +271,13 @@ void WebUIDataSourceImpl::StartDataRequest( |
scoped_refptr<base::RefCountedMemory> response( |
GetContentClient()->GetDataResourceBytes(resource_id)); |
+ const unsigned char* response_array = response->front(); |
+ if (response_array[0] == 0x1f && response_array[1] == 0x8b) { |
flackr
2016/05/13 23:02:16
I'm concerned with detecting gzipped resources by
|
+ // We are assuming that since we have hit the "magic header" (1f:8b) of a |
+ // gzipped file, we can now decode it. |
+ response = DecodeGzipData(response_array, response->size()); |
+ } |
+ |
// TODO(dschuyler): improve filtering of which resource to run template |
// expansion upon. |
if (GetMimeType(path) == "text/html") { |