Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 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 
      5 #include "net/base/net_util.h"
      6 
      7 #include "base/file_path.h"
      8 #include "base/string_util.h"
      9 #include "googleurl/src/gurl.h"
     10 #include "net/base/escape.h"
     11 
     12 namespace net {
     13 
     14 bool FileURLToFilePath(const GURL& url, FilePath* path) {
     15   *path = FilePath();
     16   std::string& file_path_str = const_cast<std::string&>(path->value());
     17   file_path_str.clear();
     18 
     19   if (!url.is_valid())
     20     return false;
     21 
     22   // Firefox seems to ignore the "host" of a file url if there is one. That is,
     23   // file://foo/bar.txt maps to /bar.txt.
     24   // TODO(dhg): This should probably take into account UNCs which could
     25   // include a hostname other than localhost or blank
     26   std::string old_path = url.path();
     27 
     28   if (old_path.empty())
     29     return false;
     30 
     31   // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
     32   old_path = UnescapeURLComponent(old_path,
     33       UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
     34 
     35   // Collapse multiple path slashes into a single path slash.
     36   std::string new_path;
     37   do {
     38     new_path = old_path;
     39     ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/");
     40     old_path.swap(new_path);
     41   } while (new_path != old_path);
     42 
     43   file_path_str.assign(old_path);
     44 
     45   return !file_path_str.empty();
     46 }
     47 
     48 }  // namespace net
     49