Code:  std::unique_ptr<HttpResponse> HandleFileRequest(
     const base::FilePath& server_root,
    const HttpRequest& request) {
  base::ScopedAllowBlockingForTesting allow_blocking;

  GURL request_url = request.GetURL();
  std::string relative_path(request_url.path());

  std::string post_prefix("/post/");
  if (base::StartsWith(relative_path, post_prefix,
                       base::CompareCase::SENSITIVE)) {
    if (request.method != METHOD_POST)
      return nullptr;
    relative_path = relative_path.substr(post_prefix.size() - 1);
  }

  RequestQuery query = ParseQuery(request_url);

  std::unique_ptr<BasicHttpResponse> failed_response(new BasicHttpResponse);
  failed_response->set_code(HTTP_NOT_FOUND);

  if (query.find("expected_body") != query.end()) {
    if (request.content.find(query["expected_body"].front()) ==
        std::string::npos) {
      return std::move(failed_response);
    }
  }

  if (query.find("expected_headers") != query.end()) {
    for (const auto& header : query["expected_headers"]) {
      if (header.find(":") == std::string::npos)
        return std::move(failed_response);
      std::string key = header.substr(0, header.find(":"));
      std::string value = header.substr(header.find(":") + 1);
      if (request.headers.find(key) == request.headers.end() ||
          request.headers.at(key) != value) {
        return std::move(failed_response);
      }
    }
  }

  DCHECK(base::StartsWith(relative_path, "/", base::CompareCase::SENSITIVE));
  std::string request_path = relative_path.substr(1);
  base::FilePath file_path(server_root.AppendASCII(request_path));
  std::string file_contents;
  if (!base::ReadFileToString(file_path, &file_contents)) {
    file_path = file_path.AppendASCII("index.html");
    if (!base::ReadFileToString(file_path, &file_contents))
      return nullptr;
  }

   if (request.method == METHOD_HEAD)
     file_contents = "";
 
  if (query.find("replace_text") != query.end()) {
    for (const auto& replacement : query["replace_text"]) {
      if (replacement.find(":") == std::string::npos)
        return std::move(failed_response);
      std::string find;
      std::string with;
      base::Base64Decode(replacement.substr(0, replacement.find(":")), &find);
      base::Base64Decode(replacement.substr(replacement.find(":") + 1), &with);
      base::ReplaceSubstringsAfterOffset(&file_contents, 0, find, with);
    }
  }
 
   base::FilePath::StringPieceType mock_headers_extension;
 #if defined(OS_WIN)
  base::string16 temp = base::ASCIIToUTF16(kMockHttpHeadersExtension);
  mock_headers_extension = temp;
#else
  mock_headers_extension = kMockHttpHeadersExtension;
#endif

  base::FilePath headers_path(file_path.AddExtension(mock_headers_extension));

   if (base::PathExists(headers_path)) {
     std::string headers_contents;
 
    if (!base::ReadFileToString(headers_path, &headers_contents))
       return nullptr;
 
     return std::make_unique<RawHttpResponse>(headers_contents, file_contents);
   }

  std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
  http_response->set_code(HTTP_OK);

  if (request.headers.find("Range") != request.headers.end()) {
    std::vector<HttpByteRange> ranges;

    if (HttpUtil::ParseRangeHeader(request.headers.at("Range"), &ranges) &&
        ranges.size() == 1) {
      ranges[0].ComputeBounds(file_contents.size());
      size_t start = ranges[0].first_byte_position();
      size_t end = ranges[0].last_byte_position();

      http_response->set_code(HTTP_PARTIAL_CONTENT);
      http_response->AddCustomHeader(
          "Content-Range",
          base::StringPrintf("bytes %" PRIuS "-%" PRIuS "/%" PRIuS, start, end,
                             file_contents.size()));

      file_contents = file_contents.substr(start, end - start + 1);
    }
  }

  http_response->set_content_type(GetContentType(file_path));
  http_response->AddCustomHeader("Accept-Ranges", "bytes");
  http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'");
  http_response->set_content(file_contents);
  return std::move(http_response);
}

Vulnerability Type: DoS 
CWE ID: 
Summary: Multiple unspecified vulnerabilities in Google Chrome before 50.0.2661.94 allow attackers to cause a denial of service or possibly have other impact via unknown vectors.
Commit Message: Fix ChromeResourceDispatcherHostDelegateMirrorBrowserTest.MirrorRequestHeader with network service.

The functionality worked, as part of converting DICE, however the test code didn't work since it
depended on accessing the net objects directly. Switch the tests to use the EmbeddedTestServer, to
better match production, which removes the dependency on net/.

Also:
-make GetFilePathWithReplacements replace strings in the mock headers if they're present
-add a global to google_util to ignore ports; that way other tests can be converted without having
  to modify each callsite to google_util

Bug: 881976
Change-Id: Ic52023495c1c98c1248025c11cdf37f433fef058
Reviewed-on: https://chromium-review.googlesource.com/c/1328142
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: Ramin Halavati <rhalavati@chromium.org>
Reviewed-by: Maks Orlovich <morlovich@chromium.org>
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607652}