summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/BUILD.gn11
-rw-r--r--net/DEPS9
-rw-r--r--net/data/filter_unittests/google.brbin0 -> 1041 bytes
-rw-r--r--net/filter/brotli_filter.cc113
-rw-r--r--net/filter/brotli_filter.h17
-rw-r--r--net/filter/brotli_filter_disabled.cc13
-rw-r--r--net/filter/brotli_filter_unittest.cc253
-rw-r--r--net/filter/filter.cc21
-rw-r--r--net/filter/filter.h3
-rw-r--r--net/http/http_network_session.cc1
-rw-r--r--net/http/http_network_session.h3
-rw-r--r--net/net.gyp4
-rw-r--r--net/net.gypi1
-rw-r--r--net/url_request/url_request_http_job.cc43
-rw-r--r--net/url_request/url_request_http_job_unittest.cc78
-rw-r--r--net/url_request/url_request_job_unittest.cc45
16 files changed, 593 insertions, 22 deletions
diff --git a/net/BUILD.gn b/net/BUILD.gn
index 4ca6128..4c453f3 100644
--- a/net/BUILD.gn
+++ b/net/BUILD.gn
@@ -451,11 +451,16 @@ component("net") {
"base/net_string_util_icu.cc",
"base/net_util_icu.cc",
]
+
+ # Brotli support.
+ deps += [ "//third_party/brotli" ]
+ sources += [ "filter/brotli_filter.cc" ]
}
}
if (is_android) {
- # Same as net, but with ICU, file, ftp, and websocket support stripped.
+ # Same as net, but with brotli encoding, ICU, file, ftp, and websocket
+ # support stripped.
component("net_small") {
sources = net_shared_sources
@@ -484,6 +489,9 @@ if (is_android) {
"base/net_string_util_icu_alternatives_android.cc",
"base/net_string_util_icu_alternatives_android.h",
]
+
+ # Disable Brotli support.
+ sources += [ "filter/brotli_filter_disabled.cc" ]
}
}
@@ -1620,6 +1628,7 @@ test("net_unittests") {
"disk_cache/blockfile/block_files_unittest.cc",
# Need to read input data files.
+ "filter/brotli_filter_unittest.cc",
"filter/gzip_filter_unittest.cc",
"socket/ssl_server_socket_unittest.cc",
"spdy/fuzzing/hpack_fuzz_util_test.cc",
diff --git a/net/DEPS b/net/DEPS
index 5f49c80..c7d7951 100644
--- a/net/DEPS
+++ b/net/DEPS
@@ -11,9 +11,10 @@ include_rules = [
"+sdch/open-vcdiff",
"+v8",
- # Most of net should not depend on icu, to keep size down when built as a
- # library.
+ # Most of net should not depend on icu, and brotli to keep size down when
+ # built as a library.
"-base/i18n",
+ "-third_party/brotli",
"-third_party/icu",
]
@@ -56,6 +57,10 @@ specific_include_rules = {
"run_all_unittests\.cc": [
"+third_party/mojo/src/mojo/edk",
],
+
+ "brotli_filter\.cc": [
+ "+third_party/brotli",
+ ],
}
skip_child_includes = [
diff --git a/net/data/filter_unittests/google.br b/net/data/filter_unittests/google.br
new file mode 100644
index 0000000..223a8c3
--- /dev/null
+++ b/net/data/filter_unittests/google.br
Binary files differ
diff --git a/net/filter/brotli_filter.cc b/net/filter/brotli_filter.cc
new file mode 100644
index 0000000..66e778a
--- /dev/null
+++ b/net/filter/brotli_filter.cc
@@ -0,0 +1,113 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/filter/brotli_filter.h"
+
+#include "base/macros.h"
+#include "base/numerics/safe_conversions.h"
+#include "base/numerics/safe_math.h"
+#include "third_party/brotli/dec/decode.h"
+
+namespace net {
+
+// BrotliFilter applies Brotli content decoding to a data stream.
+// Brotli format specification: http://www.ietf.org/id/draft-alakuijala-brotli
+//
+// BrotliFilter is a subclass of Filter. See the latter's header file filter.h
+// for sample usage.
+class BrotliFilter : public Filter {
+ public:
+ BrotliFilter(FilterType type)
+ : Filter(type), decoding_status_(DECODING_IN_PROGRESS) {
+ BrotliStateInit(&brotli_state_);
+ }
+
+ ~BrotliFilter() override { BrotliStateCleanup(&brotli_state_); }
+
+ // Decodes the pre-filter data and writes the output into the |dest_buffer|
+ // passed in.
+ // The function returns FilterStatus. See filter.h for its description.
+ //
+ // Upon entry, |*dest_len| is the total size (in number of chars) of the
+ // destination buffer. Upon exit, |*dest_len| is the actual number of chars
+ // written into the destination buffer.
+ //
+ // This function will fail if there is no pre-filter data in the
+ // |stream_buffer_|. On the other hand, |*dest_len| can be 0 upon successful
+ // return. For example, decompressor may process some pre-filter data
+ // but not produce output yet.
+ FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override {
+ if (!dest_buffer || !dest_len)
+ return Filter::FILTER_ERROR;
+
+ if (decoding_status_ == DECODING_DONE) {
+ *dest_len = 0;
+ return Filter::FILTER_DONE;
+ }
+
+ if (decoding_status_ != DECODING_IN_PROGRESS)
+ return Filter::FILTER_ERROR;
+
+ size_t output_buffer_size = base::checked_cast<size_t>(*dest_len);
+ size_t input_buffer_size = base::checked_cast<size_t>(stream_data_len_);
+
+ size_t available_in = input_buffer_size;
+ const uint8_t* next_in = bit_cast<uint8_t*>(next_stream_data_);
+ size_t available_out = output_buffer_size;
+ uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer);
+ size_t total_out = 0;
+ BrotliResult result =
+ BrotliDecompressStream(&available_in, &next_in, &available_out,
+ &next_out, &total_out, &brotli_state_);
+
+ CHECK(available_in <= input_buffer_size);
+ CHECK(available_out <= output_buffer_size);
+
+ base::CheckedNumeric<size_t> safe_bytes_written(output_buffer_size);
+ safe_bytes_written -= available_out;
+ int bytes_written =
+ base::checked_cast<int>(safe_bytes_written.ValueOrDie());
+
+ switch (result) {
+ case BROTLI_RESULT_NEEDS_MORE_OUTPUT:
+ // Fall through.
+ case BROTLI_RESULT_SUCCESS:
+ *dest_len = bytes_written;
+ stream_data_len_ = base::checked_cast<int>(available_in);
+ next_stream_data_ = bit_cast<char*>(next_in);
+ if (result == BROTLI_RESULT_SUCCESS) {
+ decoding_status_ = DECODING_DONE;
+ return Filter::FILTER_DONE;
+ }
+ return Filter::FILTER_OK;
+
+ case BROTLI_RESULT_NEEDS_MORE_INPUT:
+ *dest_len = bytes_written;
+ stream_data_len_ = 0;
+ next_stream_data_ = nullptr;
+ return Filter::FILTER_NEED_MORE_DATA;
+
+ default:
+ decoding_status_ = DECODING_ERROR;
+ return Filter::FILTER_ERROR;
+ }
+ }
+
+ private:
+ enum DecodingStatus { DECODING_IN_PROGRESS, DECODING_DONE, DECODING_ERROR };
+
+ // Tracks the status of decoding.
+ // This variable is updated only by ReadFilteredData.
+ DecodingStatus decoding_status_;
+
+ BrotliState brotli_state_;
+
+ DISALLOW_COPY_AND_ASSIGN(BrotliFilter);
+};
+
+Filter* CreateBrotliFilter(Filter::FilterType type_id) {
+ return new BrotliFilter(type_id);
+}
+
+} // namespace net
diff --git a/net/filter/brotli_filter.h b/net/filter/brotli_filter.h
new file mode 100644
index 0000000..5570ae4
--- /dev/null
+++ b/net/filter/brotli_filter.h
@@ -0,0 +1,17 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_FILTER_BROTLI_FILTER_H_
+#define NET_FILTER_BROTLI_FILTER_H_
+
+#include "net/filter/filter.h"
+
+namespace net {
+
+// Creates instance of filter or returns nullptr if brotli is not supported.
+Filter* CreateBrotliFilter(Filter::FilterType type_id);
+
+} // namespace net
+
+#endif // NET_FILTER_BROTLI_FILTER_H__
diff --git a/net/filter/brotli_filter_disabled.cc b/net/filter/brotli_filter_disabled.cc
new file mode 100644
index 0000000..1081ff4
--- /dev/null
+++ b/net/filter/brotli_filter_disabled.cc
@@ -0,0 +1,13 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/filter/brotli_filter.h"
+
+namespace net {
+
+Filter* CreateBrotliFilter(Filter::FilterType type_id) {
+ return nullptr;
+}
+
+} // namespace net
diff --git a/net/filter/brotli_filter_unittest.cc b/net/filter/brotli_filter_unittest.cc
new file mode 100644
index 0000000..9c9ce65
--- /dev/null
+++ b/net/filter/brotli_filter_unittest.cc
@@ -0,0 +1,253 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/files/file_util.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/path_service.h"
+#include "net/base/io_buffer.h"
+#include "net/filter/brotli_filter.h"
+#include "net/filter/mock_filter_context.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+namespace {
+const int kDefaultBufferSize = 4096;
+const int kSmallBufferSize = 128;
+} // namespace
+
+namespace net {
+
+// These tests use the path service, which uses autoreleased objects on the
+// Mac, so this needs to be a PlatformTest.
+class BrotliUnitTest : public PlatformTest {
+ protected:
+ void SetUp() override {
+ PlatformTest::SetUp();
+
+ // Get the path of data directory.
+ base::FilePath data_dir;
+ PathService::Get(base::DIR_SOURCE_ROOT, &data_dir);
+ data_dir = data_dir.AppendASCII("net");
+ data_dir = data_dir.AppendASCII("data");
+ data_dir = data_dir.AppendASCII("filter_unittests");
+
+ // Read data from the original file into buffer.
+ base::FilePath file_path;
+ file_path = data_dir.AppendASCII("google.txt");
+ ASSERT_TRUE(base::ReadFileToString(file_path, &source_buffer_));
+
+ // Read data from the encoded file into buffer.
+ base::FilePath encoded_file_path;
+ encoded_file_path = data_dir.AppendASCII("google.br");
+ ASSERT_TRUE(base::ReadFileToString(encoded_file_path, &encoded_buffer_));
+ ASSERT_GE(kDefaultBufferSize, static_cast<int>(encoded_buffer_.size()));
+ }
+
+ // Use filter to decode compressed data, and compare the decoded result with
+ // the orginal data.
+ // Parameters: |source| and |source_len| are original data and its size.
+ // |encoded_source| and |encoded_source_len| are compressed data and its size.
+ // |output_buffer_size| specifies the size of buffer to read out data from
+ // filter.
+ void DecodeAndCompareWithFilter(Filter* filter,
+ const char* source,
+ int source_len,
+ const char* encoded_source,
+ int encoded_source_len,
+ int output_buffer_size) {
+ // Make sure we have enough space to hold the decoding output.
+ ASSERT_GE(kDefaultBufferSize, source_len);
+ ASSERT_GE(kDefaultBufferSize, output_buffer_size);
+
+ char decode_buffer[kDefaultBufferSize];
+ char* decode_next = decode_buffer;
+ int decode_avail_size = kDefaultBufferSize;
+
+ const char* encode_next = encoded_source;
+ int encode_avail_size = encoded_source_len;
+
+ int code = Filter::FILTER_OK;
+ while (code != Filter::FILTER_DONE) {
+ int encode_data_len =
+ std::min(encode_avail_size, filter->stream_buffer_size());
+ memcpy(filter->stream_buffer()->data(), encode_next, encode_data_len);
+ filter->FlushStreamBuffer(encode_data_len);
+ encode_next += encode_data_len;
+ encode_avail_size -= encode_data_len;
+
+ while (true) {
+ int decode_data_len = std::min(decode_avail_size, output_buffer_size);
+
+ code = filter->ReadData(decode_next, &decode_data_len);
+ decode_next += decode_data_len;
+ decode_avail_size -= decode_data_len;
+
+ ASSERT_NE(Filter::FILTER_ERROR, code);
+
+ if (code == Filter::FILTER_NEED_MORE_DATA ||
+ code == Filter::FILTER_DONE) {
+ break;
+ }
+ }
+ }
+
+ // Compare the decoding result with source data
+ int decode_total_data_len = kDefaultBufferSize - decode_avail_size;
+ EXPECT_EQ(source_len, decode_total_data_len);
+ EXPECT_EQ(memcmp(source, decode_buffer, source_len), 0);
+ }
+
+ // Unsafe function to use filter to decode compressed data.
+ // Parameters: |source| and |source_len| are compressed data and its size.
+ // |dest| is the buffer for decoding results. Upon entry, |*dest_len| is the
+ // size of the output buffer. Upon exit, |*dest_len| is the number of chars
+ // written into the buffer.
+ int DecodeAllWithFilter(Filter* filter,
+ const char* source,
+ int source_len,
+ char* dest,
+ int* dest_len) {
+ memcpy(filter->stream_buffer()->data(), source, source_len);
+ filter->FlushStreamBuffer(source_len);
+ return filter->ReadData(dest, dest_len);
+ }
+
+ void InitFilter() {
+ std::vector<Filter::FilterType> filter_types;
+ filter_types.push_back(Filter::FILTER_TYPE_BROTLI);
+ filter_.reset(Filter::Factory(filter_types, filter_context_));
+ ASSERT_TRUE(filter_.get());
+ ASSERT_LE(kDefaultBufferSize, filter_->stream_buffer_size());
+ }
+
+ void InitFilterWithBufferSize(int buffer_size) {
+ std::vector<Filter::FilterType> filter_types;
+ filter_types.push_back(Filter::FILTER_TYPE_BROTLI);
+ filter_.reset(
+ Filter::FactoryForTests(filter_types, filter_context_, buffer_size));
+ ASSERT_TRUE(filter_.get());
+ }
+
+ const char* source_buffer() const { return source_buffer_.data(); }
+ int source_len() const { return static_cast<int>(source_buffer_.size()); }
+
+ const char* encoded_buffer() const { return encoded_buffer_.data(); }
+ int encoded_len() const { return static_cast<int>(encoded_buffer_.size()); }
+
+ scoped_ptr<Filter> filter_;
+
+ private:
+ MockFilterContext filter_context_;
+ std::string source_buffer_;
+ std::string encoded_buffer_;
+};
+
+// Basic scenario: decoding brotli data with big enough buffer.
+TEST_F(BrotliUnitTest, DecodeBrotli) {
+ InitFilter();
+ memcpy(filter_->stream_buffer()->data(), encoded_buffer(), encoded_len());
+ filter_->FlushStreamBuffer(encoded_len());
+
+ ASSERT_GE(kDefaultBufferSize, source_len());
+ char decode_buffer[kDefaultBufferSize];
+ int decode_size = kDefaultBufferSize;
+ filter_->ReadData(decode_buffer, &decode_size);
+
+ // Compare the decoding result with source data
+ EXPECT_EQ(source_len(), decode_size);
+ EXPECT_EQ(memcmp(source_buffer(), decode_buffer, source_len()), 0);
+}
+
+// Tests we can call filter repeatedly to get all the data decoded.
+// To do that, we create a filter with a small buffer that can not hold all
+// the input data.
+TEST_F(BrotliUnitTest, DecodeWithSmallBuffer) {
+ InitFilterWithBufferSize(kSmallBufferSize);
+ EXPECT_EQ(kSmallBufferSize, filter_->stream_buffer_size());
+ DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
+ encoded_buffer(), encoded_len(),
+ kDefaultBufferSize);
+}
+
+// Tests we can still decode with just 1 byte buffer in the filter.
+// The purpose of this test: sometimes the filter will consume input without
+// generating output. Verify filter can handle it correctly.
+TEST_F(BrotliUnitTest, DecodeWithOneByteBuffer) {
+ InitFilterWithBufferSize(1);
+ EXPECT_EQ(1, filter_->stream_buffer_size());
+ DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
+ encoded_buffer(), encoded_len(),
+ kDefaultBufferSize);
+}
+
+// Tests we can decode when caller has small buffer to read out from filter.
+TEST_F(BrotliUnitTest, DecodeWithSmallOutputBuffer) {
+ InitFilter();
+ DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
+ encoded_buffer(), encoded_len(), kSmallBufferSize);
+}
+
+// Tests we can decode when caller has small buffer and input is also broken
+// into small parts. This may uncover some corner cases that doesn't happen with
+// one-byte buffers.
+TEST_F(BrotliUnitTest, DecodeWithSmallInputAndOutputBuffer) {
+ InitFilterWithBufferSize(kSmallBufferSize);
+ DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
+ encoded_buffer(), encoded_len(), kSmallBufferSize);
+}
+
+// Tests we can still decode with just 1 byte buffer in the filter and just 1
+// byte buffer in the caller.
+TEST_F(BrotliUnitTest, DecodeWithOneByteInputAndOutputBuffer) {
+ InitFilterWithBufferSize(1);
+ EXPECT_EQ(1, filter_->stream_buffer_size());
+ DecodeAndCompareWithFilter(filter_.get(), source_buffer(), source_len(),
+ encoded_buffer(), encoded_len(), 1);
+}
+
+// Decoding deflate stream with corrupted data.
+TEST_F(BrotliUnitTest, DecodeCorruptedData) {
+ char corrupt_data[kDefaultBufferSize];
+ int corrupt_data_len = encoded_len();
+ memcpy(corrupt_data, encoded_buffer(), encoded_len());
+
+ int pos = corrupt_data_len / 2;
+ corrupt_data[pos] = !corrupt_data[pos];
+
+ // Decode the corrupted data with filter
+ InitFilter();
+ char corrupt_decode_buffer[kDefaultBufferSize];
+ int corrupt_decode_size = kDefaultBufferSize;
+
+ int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
+ corrupt_decode_buffer, &corrupt_decode_size);
+
+ // Expect failures
+ EXPECT_EQ(Filter::FILTER_ERROR, code);
+}
+
+// Decoding deflate stream with missing data.
+TEST_F(BrotliUnitTest, DecodeMissingData) {
+ char corrupt_data[kDefaultBufferSize];
+ int corrupt_data_len = encoded_len();
+ memcpy(corrupt_data, encoded_buffer(), encoded_len());
+
+ int pos = corrupt_data_len / 2;
+ int len = corrupt_data_len - pos - 1;
+ memmove(&corrupt_data[pos], &corrupt_data[pos + 1], len);
+ --corrupt_data_len;
+
+ // Decode the corrupted data with filter
+ InitFilter();
+ char corrupt_decode_buffer[kDefaultBufferSize];
+ int corrupt_decode_size = kDefaultBufferSize;
+
+ int code = DecodeAllWithFilter(filter_.get(), corrupt_data, corrupt_data_len,
+ corrupt_decode_buffer, &corrupt_decode_size);
+
+ // Expect failures
+ EXPECT_EQ(Filter::FILTER_ERROR, code);
+}
+
+} // namespace net
diff --git a/net/filter/filter.cc b/net/filter/filter.cc
index f9c3712..466aa10 100644
--- a/net/filter/filter.cc
+++ b/net/filter/filter.cc
@@ -28,6 +28,7 @@
#include "base/values.h"
#include "net/base/io_buffer.h"
#include "net/base/sdch_net_log_params.h"
+#include "net/filter/brotli_filter.h"
#include "net/filter/gzip_filter.h"
#include "net/filter/sdch_filter.h"
#include "net/url_request/url_request_context.h"
@@ -38,6 +39,7 @@ namespace net {
namespace {
// Filter types (using canonical lower case only):
+const char kBrotli[] = "br";
const char kDeflate[] = "deflate";
const char kGZip[] = "gzip";
const char kXGZip[] = "x-gzip";
@@ -62,6 +64,8 @@ void LogSdchProblem(const FilterContext& filter_context,
std::string FilterTypeAsString(Filter::FilterType type_id) {
switch (type_id) {
+ case Filter::FILTER_TYPE_BROTLI:
+ return "FILTER_TYPE_BROTLI";
case Filter::FILTER_TYPE_DEFLATE:
return "FILTER_TYPE_DEFLATE";
case Filter::FILTER_TYPE_GZIP:
@@ -184,7 +188,9 @@ bool Filter::FlushStreamBuffer(int stream_data_len) {
Filter::FilterType Filter::ConvertEncodingToType(
const std::string& filter_type) {
FilterType type_id;
- if (base::LowerCaseEqualsASCII(filter_type, kDeflate)) {
+ if (base::LowerCaseEqualsASCII(filter_type, kBrotli)) {
+ type_id = FILTER_TYPE_BROTLI;
+ } else if (base::LowerCaseEqualsASCII(filter_type, kDeflate)) {
type_id = FILTER_TYPE_DEFLATE;
} else if (base::LowerCaseEqualsASCII(filter_type, kGZip) ||
base::LowerCaseEqualsASCII(filter_type, kXGZip)) {
@@ -350,6 +356,16 @@ Filter::FilterStatus Filter::CopyOut(char* dest_buffer, int* dest_len) {
}
// static
+Filter* Filter::InitBrotliFilter(FilterType type_id, int buffer_size) {
+ scoped_ptr<Filter> brotli_filter(CreateBrotliFilter(type_id));
+ if (!brotli_filter.get())
+ return nullptr;
+
+ brotli_filter->InitBuffer(buffer_size);
+ return brotli_filter.release();
+}
+
+// static
Filter* Filter::InitGZipFilter(FilterType type_id, int buffer_size) {
scoped_ptr<GZipFilter> gz_filter(new GZipFilter(type_id));
gz_filter->InitBuffer(buffer_size);
@@ -372,6 +388,9 @@ Filter* Filter::PrependNewFilter(FilterType type_id,
Filter* filter_list) {
scoped_ptr<Filter> first_filter; // Soon to be start of chain.
switch (type_id) {
+ case FILTER_TYPE_BROTLI:
+ first_filter.reset(InitBrotliFilter(type_id, buffer_size));
+ break;
case FILTER_TYPE_GZIP_HELPING_SDCH:
case FILTER_TYPE_DEFLATE:
case FILTER_TYPE_GZIP:
diff --git a/net/filter/filter.h b/net/filter/filter.h
index 6e111e1..e9df3db 100644
--- a/net/filter/filter.h
+++ b/net/filter/filter.h
@@ -153,6 +153,7 @@ class NET_EXPORT_PRIVATE Filter {
// Specifies type of filters that can be created.
enum FilterType {
+ FILTER_TYPE_BROTLI,
FILTER_TYPE_DEFLATE,
FILTER_TYPE_GZIP,
FILTER_TYPE_GZIP_HELPING_SDCH, // Gzip possible, but pass through allowed.
@@ -233,6 +234,7 @@ class NET_EXPORT_PRIVATE Filter {
std::string OrderedFilterList() const;
protected:
+ friend class BrotliUnitTest;
friend class GZipUnitTest;
friend class SdchFilterChainingTest;
FRIEND_TEST_ALL_PREFIXES(FilterTest, ThreeFilterChain);
@@ -285,6 +287,7 @@ class NET_EXPORT_PRIVATE Filter {
// Helper methods for PrependNewFilter. If initialization is successful,
// they return a fully initialized Filter. Otherwise, return NULL.
+ static Filter* InitBrotliFilter(FilterType type_id, int buffer_size);
static Filter* InitGZipFilter(FilterType type_id, int buffer_size);
static Filter* InitSdchFilter(FilterType type_id,
const FilterContext& filter_context,
diff --git a/net/http/http_network_session.cc b/net/http/http_network_session.cc
index 8188f8e..94f4b0c 100644
--- a/net/http/http_network_session.cc
+++ b/net/http/http_network_session.cc
@@ -99,6 +99,7 @@ HttpNetworkSession::Params::Params()
use_alternative_services(false),
alternative_service_probability_threshold(1),
enable_npn(true),
+ enable_brotli(false),
enable_quic(false),
enable_quic_for_proxies(false),
enable_quic_port_selection(true),
diff --git a/net/http/http_network_session.h b/net/http/http_network_session.h
index 0671ea4..1c3d6a9 100644
--- a/net/http/http_network_session.h
+++ b/net/http/http_network_session.h
@@ -115,6 +115,9 @@ class NET_EXPORT HttpNetworkSession
// Enables NPN support. Note that ALPN is always enabled.
bool enable_npn;
+ // Enables Brotli Content-Encoding support.
+ bool enable_brotli;
+
// Enables QUIC support.
bool enable_quic;
// Enables QUIC for proxies.
diff --git a/net/net.gyp b/net/net.gyp
index f183bef..c11f60f 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -107,6 +107,7 @@
'target_name': 'net',
'dependencies': [
'../base/base.gyp:base_i18n',
+ '../third_party/brotli/brotli.gyp:brotli',
'../third_party/icu/icu.gyp:icui18n',
'../third_party/icu/icu.gyp:icuuc',
'../third_party/protobuf/protobuf.gyp:protobuf_lite',
@@ -117,6 +118,7 @@
'base/filename_util_icu.cc',
'base/net_string_util_icu.cc',
'base/net_util_icu.cc',
+ 'filter/brotli_filter.cc',
],
'includes': [ 'net_common.gypi' ],
},
@@ -384,6 +386,7 @@
'disk_cache/backend_unittest.cc',
'disk_cache/blockfile/block_files_unittest.cc',
# Need to read input data files.
+ 'filter/brotli_filter_unittest.cc',
'filter/gzip_filter_unittest.cc',
# Need TestServer.
"cert_net/cert_net_fetcher_impl_unittest.cc",
@@ -1357,6 +1360,7 @@
'DISABLE_FTP_SUPPORT=1',
],
'sources': [
+ 'filter/brotli_filter_disabled.cc',
'base/net_string_util_icu_alternatives_android.cc',
'base/net_string_util_icu_alternatives_android.h',
],
diff --git a/net/net.gypi b/net/net.gypi
index 3f17b45..999d87d 100644
--- a/net/net.gypi
+++ b/net/net.gypi
@@ -1418,6 +1418,7 @@
'dns/single_request_host_resolver_unittest.cc',
'extras/sqlite/sqlite_channel_id_store_unittest.cc',
'extras/sqlite/sqlite_persistent_cookie_store_unittest.cc',
+ 'filter/brotli_filter_unittest.cc',
'filter/filter_unittest.cc',
'filter/gzip_filter_unittest.cc',
'filter/mock_filter_context.cc',
diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc
index 6565a8d..47a0666 100644
--- a/net/url_request/url_request_http_job.cc
+++ b/net/url_request/url_request_http_job.cc
@@ -603,30 +603,37 @@ void URLRequestHttpJob::AddExtraHeaders() {
}
}
+ // Advertise "br" encoding only if transferred data is opaque to proxy.
+ bool advertise_brotli = false;
+ const HttpNetworkSession::Params* network_session_params =
+ request()->context()->GetNetworkSessionParams();
+ if (network_session_params && network_session_params->enable_brotli)
+ advertise_brotli = request()->url().SchemeIsCryptographic();
+
// Supply Accept-Encoding headers first so that it is more likely that they
// will be in the first transmitted packet. This can sometimes make it
// easier to filter and analyze the streams to assure that a proxy has not
// damaged these headers. Some proxies deliberately corrupt Accept-Encoding
// headers.
- if (!advertise_sdch) {
- // Tell the server what compression formats we support (other than SDCH).
- request_info_.extra_headers.SetHeader(
- HttpRequestHeaders::kAcceptEncoding, "gzip, deflate");
- } else {
- // Include SDCH in acceptable list.
+ std::string advertised_encodings = "gzip, deflate";
+ if (advertise_sdch)
+ advertised_encodings += ", sdch";
+ if (advertise_brotli)
+ advertised_encodings += ", br";
+ // Tell the server what compression formats are supported.
+ request_info_.extra_headers.SetHeader(HttpRequestHeaders::kAcceptEncoding,
+ advertised_encodings);
+
+ if (dictionaries_advertised_) {
request_info_.extra_headers.SetHeader(
- HttpRequestHeaders::kAcceptEncoding, "gzip, deflate, sdch");
- if (dictionaries_advertised_) {
- request_info_.extra_headers.SetHeader(
- kAvailDictionaryHeader,
- dictionaries_advertised_->GetDictionaryClientHashList());
- // Since we're tagging this transaction as advertising a dictionary,
- // we'll definitely employ an SDCH filter (or tentative sdch filter)
- // when we get a response. When done, we'll record histograms via
- // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet
- // arrival times.
- packet_timing_enabled_ = true;
- }
+ kAvailDictionaryHeader,
+ dictionaries_advertised_->GetDictionaryClientHashList());
+ // Since we're tagging this transaction as advertising a dictionary,
+ // we'll definitely employ an SDCH filter (or tentative sdch filter)
+ // when we get a response. When done, we'll record histograms via
+ // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet
+ // arrival times.
+ packet_timing_enabled_ = true;
}
}
diff --git a/net/url_request/url_request_http_job_unittest.cc b/net/url_request/url_request_http_job_unittest.cc
index 1714af9..351597b 100644
--- a/net/url_request/url_request_http_job_unittest.cc
+++ b/net/url_request/url_request_http_job_unittest.cc
@@ -606,6 +606,84 @@ TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) {
EXPECT_FALSE(TransactionAcceptsSdchEncoding());
}
+class URLRequestHttpJobWithBrotliSupportTest : public ::testing::Test {
+ protected:
+ URLRequestHttpJobWithBrotliSupportTest()
+ : context_(new TestURLRequestContext(true)) {
+ scoped_ptr<HttpNetworkSession::Params> params(
+ new HttpNetworkSession::Params);
+ params->enable_brotli = true;
+ context_->set_http_network_session_params(std::move(params));
+ context_->set_client_socket_factory(&socket_factory_);
+ context_->Init();
+ }
+
+ MockClientSocketFactory socket_factory_;
+ scoped_ptr<TestURLRequestContext> context_;
+};
+
+TEST_F(URLRequestHttpJobWithBrotliSupportTest, NoBrotliAdvertisementOverHttp) {
+ MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
+ MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
+ "Content-Length: 12\r\n\r\n"),
+ MockRead("Test Content")};
+ StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
+ arraysize(writes));
+ socket_factory_.AddSocketDataProvider(&socket_data);
+
+ TestDelegate delegate;
+ scoped_ptr<URLRequest> request =
+ context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
+ &delegate)
+ .Pass();
+ request->Start();
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_TRUE(request->status().is_success());
+ EXPECT_EQ(12, request->received_response_content_length());
+ EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
+ request->GetTotalSentBytes());
+ EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
+ request->GetTotalReceivedBytes());
+}
+
+TEST_F(URLRequestHttpJobWithBrotliSupportTest, BrotliAdvertisement) {
+ net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK);
+ ssl_socket_data_provider.SetNextProto(kProtoHTTP11);
+ ssl_socket_data_provider.cert =
+ ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der");
+ socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider);
+
+ MockWrite writes[] = {
+ MockWrite("GET / HTTP/1.1\r\n"
+ "Host: www.example.com\r\n"
+ "Connection: keep-alive\r\n"
+ "User-Agent:\r\n"
+ "Accept-Encoding: gzip, deflate, br\r\n"
+ "Accept-Language: en-us,fr\r\n\r\n")};
+ MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
+ "Content-Length: 12\r\n\r\n"),
+ MockRead("Test Content")};
+ StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
+ arraysize(writes));
+ socket_factory_.AddSocketDataProvider(&socket_data);
+
+ TestDelegate delegate;
+ scoped_ptr<URLRequest> request =
+ context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY,
+ &delegate)
+ .Pass();
+ request->Start();
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_TRUE(request->status().is_success());
+ EXPECT_EQ(12, request->received_response_content_length());
+ EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
+ request->GetTotalSentBytes());
+ EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
+ request->GetTotalReceivedBytes());
+}
+
// This base class just serves to set up some things before the TestURLRequest
// constructor is called.
class URLRequestHttpJobWebSocketTestBase : public ::testing::Test {
diff --git a/net/url_request/url_request_job_unittest.cc b/net/url_request/url_request_job_unittest.cc
index 62b09f1..f3796bd 100644
--- a/net/url_request/url_request_job_unittest.cc
+++ b/net/url_request/url_request_job_unittest.cc
@@ -16,6 +16,12 @@ namespace net {
namespace {
+// Data encoded in kBrotliHelloData.
+const char kBrotliDecodedHelloData[] = "hello, world!\n";
+// kBrotliDecodedHelloData encoded with brotli.
+const char kBrotliHelloData[] =
+ "\033\015\0\0\244\024\102\152\020\111\152\072\235\126\034";
+
// This is a header that signals the end of the data.
const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0";
const char kGzipDataWithName[] =
@@ -47,6 +53,13 @@ void BigGZipServer(const HttpRequestInfo* request,
response_data->insert(10, 64 * 1024, 'a');
}
+void BrotliHelloServer(const HttpRequestInfo* request,
+ std::string* response_status,
+ std::string* response_headers,
+ std::string* response_data) {
+ response_data->assign(kBrotliHelloData, sizeof(kBrotliHelloData) - 1);
+}
+
const MockTransaction kGZip_Transaction = {
"http://www.google.com/gzyp",
"GET",
@@ -113,6 +126,15 @@ const MockTransaction kEmptyBodyGzip_Transaction = {
OK,
};
+const MockTransaction kBrotli_Slow_Transaction = {
+ "http://www.google.com/brotli", "GET", base::Time(), "", LOAD_NORMAL,
+ "HTTP/1.1 200 OK",
+ "Cache-Control: max-age=10000\n"
+ "Content-Encoding: br\n",
+ base::Time(), "", TEST_MODE_SLOW_READ, &BrotliHelloServer, nullptr, 0, 0,
+ OK,
+};
+
} // namespace
TEST(URLRequestJob, TransactionNotifiedWhenDone) {
@@ -274,4 +296,27 @@ TEST(URLRequestJob, SlowFilterRead) {
RemoveMockTransaction(&kGzip_Slow_Transaction);
}
+TEST(URLRequestJob, SlowBrotliRead) {
+ MockNetworkLayer network_layer;
+ TestURLRequestContext context;
+ context.set_http_transaction_factory(&network_layer);
+
+ TestDelegate d;
+ scoped_ptr<URLRequest> req(context.CreateRequest(
+ GURL(kBrotli_Slow_Transaction.url), DEFAULT_PRIORITY, &d));
+ AddMockTransaction(&kBrotli_Slow_Transaction);
+
+ req->set_method("GET");
+ req->Start();
+
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_FALSE(d.request_failed());
+ EXPECT_EQ(200, req->GetResponseCode());
+ EXPECT_EQ(kBrotliDecodedHelloData, d.data_received());
+ EXPECT_TRUE(network_layer.done_reading_called());
+
+ RemoveMockTransaction(&kBrotli_Slow_Transaction);
+}
+
} // namespace net