This source file includes following definitions.
- Factory
- GZipFactory
- FactoryForTests
- ReadData
- FlushStreamBuffer
- ConvertEncodingToType
- FixupEncodingTypes
- last_status_
- CopyOut
- InitGZipFilter
- InitSdchFilter
- PrependNewFilter
- InitBuffer
- PushDataIntoNextFilter
#include "net/filter/filter.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#include "net/base/io_buffer.h"
#include "net/base/mime_util.h"
#include "net/base/net_util.h"
#include "net/filter/gzip_filter.h"
#include "net/filter/sdch_filter.h"
namespace {
const char kDeflate[] = "deflate";
const char kGZip[] = "gzip";
const char kXGZip[] = "x-gzip";
const char kSdch[] = "sdch";
const char kApplicationXGzip[] = "application/x-gzip";
const char kApplicationGzip[] = "application/gzip";
const char kApplicationXGunzip[] = "application/x-gunzip";
const char kTextHtml[] = "text/html";
const int kFilterBufSize = 32 * 1024;
}
namespace net {
FilterContext::~FilterContext() {
}
Filter::~Filter() {}
Filter* Filter::Factory(const std::vector<FilterType>& filter_types,
const FilterContext& filter_context) {
if (filter_types.empty())
return NULL;
Filter* filter_list = NULL;
for (size_t i = 0; i < filter_types.size(); i++) {
filter_list = PrependNewFilter(filter_types[i], filter_context,
kFilterBufSize, filter_list);
if (!filter_list)
return NULL;
}
return filter_list;
}
Filter* Filter::GZipFactory() {
return InitGZipFilter(FILTER_TYPE_GZIP, kFilterBufSize);
}
Filter* Filter::FactoryForTests(const std::vector<FilterType>& filter_types,
const FilterContext& filter_context,
int buffer_size) {
if (filter_types.empty())
return NULL;
Filter* filter_list = NULL;
for (size_t i = 0; i < filter_types.size(); i++) {
filter_list = PrependNewFilter(filter_types[i], filter_context,
buffer_size, filter_list);
if (!filter_list)
return NULL;
}
return filter_list;
}
Filter::FilterStatus Filter::ReadData(char* dest_buffer, int* dest_len) {
const int dest_buffer_capacity = *dest_len;
if (last_status_ == FILTER_ERROR)
return last_status_;
if (!next_filter_.get())
return last_status_ = ReadFilteredData(dest_buffer, dest_len);
if (last_status_ == FILTER_NEED_MORE_DATA && !stream_data_len())
return next_filter_->ReadData(dest_buffer, dest_len);
do {
if (next_filter_->last_status() == FILTER_NEED_MORE_DATA) {
PushDataIntoNextFilter();
if (FILTER_ERROR == last_status_)
return FILTER_ERROR;
}
*dest_len = dest_buffer_capacity;
next_filter_->ReadData(dest_buffer, dest_len);
if (FILTER_NEED_MORE_DATA == last_status_)
return next_filter_->last_status();
} while (FILTER_OK == last_status_ &&
FILTER_NEED_MORE_DATA == next_filter_->last_status() &&
0 == *dest_len);
if (next_filter_->last_status() == FILTER_ERROR)
return FILTER_ERROR;
return FILTER_OK;
}
bool Filter::FlushStreamBuffer(int stream_data_len) {
DCHECK_LE(stream_data_len, stream_buffer_size_);
if (stream_data_len <= 0 || stream_data_len > stream_buffer_size_)
return false;
DCHECK(stream_buffer());
if (!stream_buffer() || stream_data_len_)
return false;
next_stream_data_ = stream_buffer()->data();
stream_data_len_ = stream_data_len;
return true;
}
Filter::FilterType Filter::ConvertEncodingToType(
const std::string& filter_type) {
FilterType type_id;
if (LowerCaseEqualsASCII(filter_type, kDeflate)) {
type_id = FILTER_TYPE_DEFLATE;
} else if (LowerCaseEqualsASCII(filter_type, kGZip) ||
LowerCaseEqualsASCII(filter_type, kXGZip)) {
type_id = FILTER_TYPE_GZIP;
} else if (LowerCaseEqualsASCII(filter_type, kSdch)) {
type_id = FILTER_TYPE_SDCH;
} else {
type_id = FILTER_TYPE_UNSUPPORTED;
}
return type_id;
}
void Filter::FixupEncodingTypes(
const FilterContext& filter_context,
std::vector<FilterType>* encoding_types) {
std::string mime_type;
bool success = filter_context.GetMimeType(&mime_type);
DCHECK(success || mime_type.empty());
if ((1 == encoding_types->size()) &&
(FILTER_TYPE_GZIP == encoding_types->front())) {
if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) ||
LowerCaseEqualsASCII(mime_type, kApplicationGzip) ||
LowerCaseEqualsASCII(mime_type, kApplicationXGunzip))
encoding_types->clear();
GURL url;
std::string disposition;
success = filter_context.GetURL(&url);
DCHECK(success);
filter_context.GetContentDisposition(&disposition);
base::FilePath filepath = GenerateFileName(url, disposition, "UTF-8", "",
"", "");
base::FilePath::StringType extension = filepath.Extension();
if (filter_context.IsDownload()) {
if (EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) ||
LowerCaseEqualsASCII(extension, ".tgz") ||
LowerCaseEqualsASCII(extension, ".svgz"))
encoding_types->clear();
} else {
if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) ||
LowerCaseEqualsASCII(extension, ".tgz")) &&
!IsSupportedMimeType(mime_type))
encoding_types->clear();
}
}
if (!filter_context.IsSdchResponse()) {
if (1 < encoding_types->size()) {
SdchManager::SdchErrorRecovery(
SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST);
}
if ((1 == encoding_types->size()) &&
(FILTER_TYPE_SDCH == encoding_types->front())) {
SdchManager::SdchErrorRecovery(
SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST);
}
return;
}
if (!encoding_types->empty() &&
(FILTER_TYPE_SDCH == encoding_types->front())) {
if (1 == encoding_types->size()) {
encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH);
SdchManager::SdchErrorRecovery(
SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED);
}
return;
}
if (StartsWithASCII(mime_type, kTextHtml, false)) {
if (encoding_types->empty()) {
SdchManager::SdchErrorRecovery(
SdchManager::ADDED_CONTENT_ENCODING);
} else if (1 == encoding_types->size()) {
SdchManager::SdchErrorRecovery(
SdchManager::FIXED_CONTENT_ENCODING);
} else {
SdchManager::SdchErrorRecovery(
SdchManager::FIXED_CONTENT_ENCODINGS);
}
} else {
if (encoding_types->empty()) {
SdchManager::SdchErrorRecovery(
SdchManager::BINARY_ADDED_CONTENT_ENCODING);
} else if (1 == encoding_types->size()) {
SdchManager::SdchErrorRecovery(
SdchManager::BINARY_FIXED_CONTENT_ENCODING);
} else {
SdchManager::SdchErrorRecovery(
SdchManager::BINARY_FIXED_CONTENT_ENCODINGS);
}
}
encoding_types->insert(encoding_types->begin(),
FILTER_TYPE_GZIP_HELPING_SDCH);
encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE);
return;
}
Filter::Filter()
: stream_buffer_(NULL),
stream_buffer_size_(0),
next_stream_data_(NULL),
stream_data_len_(0),
last_status_(FILTER_NEED_MORE_DATA) {}
Filter::FilterStatus Filter::CopyOut(char* dest_buffer, int* dest_len) {
int out_len;
int input_len = *dest_len;
*dest_len = 0;
if (0 == stream_data_len_)
return Filter::FILTER_NEED_MORE_DATA;
out_len = std::min(input_len, stream_data_len_);
memcpy(dest_buffer, next_stream_data_, out_len);
*dest_len += out_len;
stream_data_len_ -= out_len;
if (0 == stream_data_len_) {
next_stream_data_ = NULL;
return Filter::FILTER_NEED_MORE_DATA;
} else {
next_stream_data_ += out_len;
return Filter::FILTER_OK;
}
}
Filter* Filter::InitGZipFilter(FilterType type_id, int buffer_size) {
scoped_ptr<GZipFilter> gz_filter(new GZipFilter());
gz_filter->InitBuffer(buffer_size);
return gz_filter->InitDecoding(type_id) ? gz_filter.release() : NULL;
}
Filter* Filter::InitSdchFilter(FilterType type_id,
const FilterContext& filter_context,
int buffer_size) {
scoped_ptr<SdchFilter> sdch_filter(new SdchFilter(filter_context));
sdch_filter->InitBuffer(buffer_size);
return sdch_filter->InitDecoding(type_id) ? sdch_filter.release() : NULL;
}
Filter* Filter::PrependNewFilter(FilterType type_id,
const FilterContext& filter_context,
int buffer_size,
Filter* filter_list) {
scoped_ptr<Filter> first_filter;
switch (type_id) {
case FILTER_TYPE_GZIP_HELPING_SDCH:
case FILTER_TYPE_DEFLATE:
case FILTER_TYPE_GZIP:
first_filter.reset(InitGZipFilter(type_id, buffer_size));
break;
case FILTER_TYPE_SDCH:
case FILTER_TYPE_SDCH_POSSIBLE:
if (SdchManager::Global() && SdchManager::sdch_enabled()) {
first_filter.reset(
InitSdchFilter(type_id, filter_context, buffer_size));
}
break;
default:
break;
}
if (!first_filter.get())
return NULL;
first_filter->next_filter_.reset(filter_list);
return first_filter.release();
}
void Filter::InitBuffer(int buffer_size) {
DCHECK(!stream_buffer());
DCHECK_GT(buffer_size, 0);
stream_buffer_ = new IOBuffer(buffer_size);
stream_buffer_size_ = buffer_size;
}
void Filter::PushDataIntoNextFilter() {
IOBuffer* next_buffer = next_filter_->stream_buffer();
int next_size = next_filter_->stream_buffer_size();
last_status_ = ReadFilteredData(next_buffer->data(), &next_size);
if (FILTER_ERROR != last_status_)
next_filter_->FlushStreamBuffer(next_size);
}
}