OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 #include "net/spdy/hpack_huffman_aggregator.h" |
| 5 |
| 6 #include "base/metrics/bucket_ranges.h" |
| 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "base/metrics/sample_vector.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "net/base/load_flags.h" |
| 13 #include "net/http/http_request_headers.h" |
| 14 #include "net/http/http_request_info.h" |
| 15 #include "net/http/http_response_headers.h" |
| 16 #include "net/spdy/hpack_encoder.h" |
| 17 #include "net/spdy/spdy_http_utils.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char kHistogramName[] = "Net.SpdyHpackEncodedCharacterFrequency"; |
| 24 |
| 25 const size_t kTotalCountsPublishThreshold = 50000; |
| 26 |
| 27 // Each encoder uses the default dynamic table size of 4096 total bytes. |
| 28 const size_t kMaxEncoders = 20; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 HpackHuffmanAggregator::HpackHuffmanAggregator() |
| 33 : counts_(256, 0), |
| 34 total_counts_(0), |
| 35 max_encoders_(kMaxEncoders) { |
| 36 } |
| 37 |
| 38 HpackHuffmanAggregator::~HpackHuffmanAggregator() { |
| 39 } |
| 40 |
| 41 void HpackHuffmanAggregator::AggregateTransactionCharacterCounts( |
| 42 const HttpRequestInfo& request, |
| 43 const HttpRequestHeaders& request_headers, |
| 44 const ProxyServer& proxy, |
| 45 const HttpResponseHeaders& response_headers) { |
| 46 if (IsCrossOrigin(request)) { |
| 47 return; |
| 48 } |
| 49 HostPortPair endpoint = HostPortPair(request.url.HostNoBrackets(), |
| 50 request.url.EffectiveIntPort()); |
| 51 HpackEncoder* encoder = ObtainEncoder( |
| 52 SpdySessionKey(endpoint, proxy, request.privacy_mode)); |
| 53 |
| 54 // Convert and encode the request and response header sets. |
| 55 { |
| 56 SpdyHeaderBlock headers; |
| 57 CreateSpdyHeadersFromHttpRequest( |
| 58 request, request_headers, &headers, SPDY4, false); |
| 59 |
| 60 std::string tmp_out; |
| 61 encoder->EncodeHeaderSet(headers, &tmp_out); |
| 62 } |
| 63 { |
| 64 SpdyHeaderBlock headers; |
| 65 CreateSpdyHeadersFromHttpResponse(response_headers, &headers); |
| 66 |
| 67 std::string tmp_out; |
| 68 encoder->EncodeHeaderSet(headers, &tmp_out); |
| 69 } |
| 70 if (total_counts_ >= kTotalCountsPublishThreshold) { |
| 71 PublishCounts(); |
| 72 } |
| 73 } |
| 74 |
| 75 // static |
| 76 bool HpackHuffmanAggregator::UseAggregator() { |
| 77 const std::string group_name = |
| 78 base::FieldTrialList::FindFullName("HpackHuffmanAggregator"); |
| 79 if (group_name == "Enabled") { |
| 80 return true; |
| 81 } |
| 82 return false; |
| 83 } |
| 84 |
| 85 // static |
| 86 void HpackHuffmanAggregator::CreateSpdyHeadersFromHttpResponse( |
| 87 const HttpResponseHeaders& headers, |
| 88 SpdyHeaderBlock* headers_out) { |
| 89 // Lower-case header names, and coalesce multiple values delimited by \0. |
| 90 // Also add the fixed status header. |
| 91 std::string name, value; |
| 92 void* it = NULL; |
| 93 while (headers.EnumerateHeaderLines(&it, &name, &value)) { |
| 94 StringToLowerASCII(&name); |
| 95 if (headers_out->find(name) == headers_out->end()) { |
| 96 (*headers_out)[name] = value; |
| 97 } else { |
| 98 (*headers_out)[name] += std::string(1, '\0') + value; |
| 99 } |
| 100 } |
| 101 (*headers_out)[":status"] = base::IntToString(headers.response_code()); |
| 102 } |
| 103 |
| 104 // static |
| 105 bool HpackHuffmanAggregator::IsCrossOrigin(const HttpRequestInfo& request) { |
| 106 // Require that the request is top-level, or that it shares |
| 107 // an origin with its referer. |
| 108 HostPortPair endpoint = HostPortPair(request.url.HostNoBrackets(), |
| 109 request.url.EffectiveIntPort()); |
| 110 if ((request.load_flags & LOAD_MAIN_FRAME) == 0) { |
| 111 std::string referer_str; |
| 112 if (!request.extra_headers.GetHeader(HttpRequestHeaders::kReferer, |
| 113 &referer_str)) { |
| 114 // Require a referer. |
| 115 return true; |
| 116 } |
| 117 GURL referer(referer_str); |
| 118 HostPortPair referer_endpoint = HostPortPair(referer.HostNoBrackets(), |
| 119 referer.EffectiveIntPort()); |
| 120 if (!endpoint.Equals(referer_endpoint)) { |
| 121 // Cross-origin request. |
| 122 return true; |
| 123 } |
| 124 } |
| 125 return false; |
| 126 } |
| 127 |
| 128 HpackEncoder* HpackHuffmanAggregator::ObtainEncoder(const SpdySessionKey& key) { |
| 129 for (OriginEncoders::iterator it = encoders_.begin(); |
| 130 it != encoders_.end(); ++it) { |
| 131 if (key.Equals(it->first)) { |
| 132 // Move to head of list and return. |
| 133 OriginEncoder origin_encoder = *it; |
| 134 encoders_.erase(it); |
| 135 encoders_.push_front(origin_encoder); |
| 136 return origin_encoder.second; |
| 137 } |
| 138 } |
| 139 // Not found. Create a new encoder, evicting one if needed. |
| 140 encoders_.push_front(std::make_pair( |
| 141 key, new HpackEncoder(ObtainHpackHuffmanTable()))); |
| 142 if (encoders_.size() > max_encoders_) { |
| 143 delete encoders_.back().second; |
| 144 encoders_.pop_back(); |
| 145 } |
| 146 encoders_.front().second->SetCharCountsStorage(&counts_, &total_counts_); |
| 147 return encoders_.front().second; |
| 148 } |
| 149 |
| 150 void HpackHuffmanAggregator::PublishCounts() { |
| 151 // base::Histogram requires that values be 1-indexed. |
| 152 const size_t kRangeMin = 1; |
| 153 const size_t kRangeMax = counts_.size() + 1; |
| 154 const size_t kBucketCount = kRangeMax + 1; |
| 155 |
| 156 base::BucketRanges ranges(kBucketCount + 1); |
| 157 for (size_t i = 0; i != ranges.size(); ++i) { |
| 158 ranges.set_range(i, i); |
| 159 } |
| 160 ranges.ResetChecksum(); |
| 161 |
| 162 // Copy |counts_| into a SampleVector. |
| 163 base::SampleVector samples(&ranges); |
| 164 for (size_t i = 0; i != counts_.size(); ++i) { |
| 165 samples.Accumulate(i + 1, counts_[i]); |
| 166 } |
| 167 |
| 168 STATIC_HISTOGRAM_POINTER_BLOCK( |
| 169 kHistogramName, |
| 170 AddSamples(samples), |
| 171 base::LinearHistogram::FactoryGet( |
| 172 kHistogramName, kRangeMin, kRangeMax, kBucketCount, |
| 173 base::HistogramBase::kUmaTargetedHistogramFlag)); |
| 174 |
| 175 // Clear counts. |
| 176 counts_.assign(counts_.size(), 0); |
| 177 total_counts_ = 0; |
| 178 } |
| 179 |
| 180 } // namespace net |
OLD | NEW |