Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: net/http/http_response_headers.cc

Issue 8511063: Improve merging of header modifications in webRequest.OnHeadersReceived (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Disabled browser tests again Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/http/http_response_headers.h ('k') | net/http/http_response_headers_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // The rules for header parsing were borrowed from Firefox: 5 // The rules for header parsing were borrowed from Firefox:
6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo nseHead.cpp 6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo nseHead.cpp
7 // The rules for parsing content-types were also borrowed from Firefox: 7 // The rules for parsing content-types were also borrowed from Firefox:
8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834
9 9
10 #include "net/http/http_response_headers.h" 10 #include "net/http/http_response_headers.h"
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 i = k; 287 i = k;
288 } 288 }
289 new_raw_headers.push_back('\0'); 289 new_raw_headers.push_back('\0');
290 290
291 // Make this object hold the new data. 291 // Make this object hold the new data.
292 raw_headers_.clear(); 292 raw_headers_.clear();
293 parsed_.clear(); 293 parsed_.clear();
294 Parse(new_raw_headers); 294 Parse(new_raw_headers);
295 } 295 }
296 296
297 void HttpResponseHeaders::MergeWithHeadersWithValue(
298 const std::string& raw_headers,
299 const std::string& header_to_remove_name,
300 const std::string& header_to_remove_value) {
301 std::string header_to_remove_name_lowercase(header_to_remove_name);
302 StringToLowerASCII(&header_to_remove_name_lowercase);
303
304 std::string new_raw_headers(raw_headers);
305 for (size_t i = 0; i < parsed_.size(); ++i) {
306 DCHECK(!parsed_[i].is_continuation());
307
308 // Locate the start of the next header.
309 size_t k = i;
310 while (++k < parsed_.size() && parsed_[k].is_continuation()) {}
311 --k;
312
313 std::string name(parsed_[i].name_begin, parsed_[i].name_end);
314 StringToLowerASCII(&name);
315 std::string value(parsed_[i].value_begin, parsed_[i].value_end);
316 if (name != header_to_remove_name_lowercase ||
317 value != header_to_remove_value) {
318 // It's ok to preserve this header in the final result.
319 new_raw_headers.append(parsed_[i].name_begin, parsed_[k].value_end);
320 new_raw_headers.push_back('\0');
321 }
322
323 i = k;
324 }
325 new_raw_headers.push_back('\0');
326
327 // Make this object hold the new data.
328 raw_headers_.clear();
329 parsed_.clear();
330 Parse(new_raw_headers);
331 }
332
297 void HttpResponseHeaders::RemoveHeader(const std::string& name) { 333 void HttpResponseHeaders::RemoveHeader(const std::string& name) {
298 // Copy up to the null byte. This just copies the status line. 334 // Copy up to the null byte. This just copies the status line.
299 std::string new_raw_headers(raw_headers_.c_str()); 335 std::string new_raw_headers(raw_headers_.c_str());
300 new_raw_headers.push_back('\0'); 336 new_raw_headers.push_back('\0');
301 337
302 std::string lowercase_name(name); 338 std::string lowercase_name(name);
303 StringToLowerASCII(&lowercase_name); 339 StringToLowerASCII(&lowercase_name);
304 HeaderSet to_remove; 340 HeaderSet to_remove;
305 to_remove.insert(lowercase_name); 341 to_remove.insert(lowercase_name);
306 MergeWithHeaders(new_raw_headers, to_remove); 342 MergeWithHeaders(new_raw_headers, to_remove);
307 } 343 }
308 344
345 void HttpResponseHeaders::RemoveHeaderWithValue(const std::string& name,
346 const std::string& value) {
347 // Copy up to the null byte. This just copies the status line.
348 std::string new_raw_headers(raw_headers_.c_str());
349 new_raw_headers.push_back('\0');
350
351 MergeWithHeadersWithValue(new_raw_headers, name, value);
352 }
353
309 void HttpResponseHeaders::AddHeader(const std::string& header) { 354 void HttpResponseHeaders::AddHeader(const std::string& header) {
310 CheckDoesNotHaveEmbededNulls(header); 355 CheckDoesNotHaveEmbededNulls(header);
311 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 2]); 356 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 2]);
312 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]); 357 DCHECK_EQ('\0', raw_headers_[raw_headers_.size() - 1]);
313 // Don't copy the last null. 358 // Don't copy the last null.
314 std::string new_raw_headers(raw_headers_, 0, raw_headers_.size() - 1); 359 std::string new_raw_headers(raw_headers_, 0, raw_headers_.size() - 1);
315 new_raw_headers.append(header); 360 new_raw_headers.append(header);
316 new_raw_headers.push_back('\0'); 361 new_raw_headers.push_back('\0');
317 new_raw_headers.push_back('\0'); 362 new_raw_headers.push_back('\0');
318 363
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 // We have all the values; let's verify that they make sense for a 206 1285 // We have all the values; let's verify that they make sense for a 206
1241 // response. 1286 // response.
1242 if (*first_byte_position < 0 || *last_byte_position < 0 || 1287 if (*first_byte_position < 0 || *last_byte_position < 0 ||
1243 *instance_length < 0 || *instance_length - 1 < *last_byte_position) 1288 *instance_length < 0 || *instance_length - 1 < *last_byte_position)
1244 return false; 1289 return false;
1245 1290
1246 return true; 1291 return true;
1247 } 1292 }
1248 1293
1249 } // namespace net 1294 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_response_headers.h ('k') | net/http/http_response_headers_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698