netwerk/base/src/nsURLHelperWin.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     2 /* vim:set ts=4 sw=4 et cindent: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 /* Windows-specific local file uri parsing */
     8 #include "nsURLHelper.h"
     9 #include "nsEscape.h"
    10 #include "nsIFile.h"
    11 #include <windows.h>
    13 nsresult
    14 net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
    15 {
    16     nsresult rv;
    17     nsAutoString path;
    19     // construct URL spec from file path
    20     rv = aFile->GetPath(path);
    21     if (NS_FAILED(rv)) return rv;
    23     // Replace \ with / to convert to an url
    24     path.ReplaceChar(char16_t(0x5Cu), char16_t(0x2Fu));
    26     nsAutoCString escPath;
    28     // Windows Desktop paths begin with a drive letter, so need an 'extra'
    29     // slash at the begining
    30     // C:\Windows =>  file:///C:/Windows
    31     NS_NAMED_LITERAL_CSTRING(prefix, "file:///");
    33     // Escape the path with the directory mask
    34     NS_ConvertUTF16toUTF8 ePath(path);
    35     if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
    36         escPath.Insert(prefix, 0);
    37     else
    38         escPath.Assign(prefix + ePath);
    40     // esc_Directory does not escape the semicolons, so if a filename 
    41     // contains semicolons we need to manually escape them.
    42     // This replacement should be removed in bug #473280
    43     escPath.ReplaceSubstring(";", "%3b");
    45     result = escPath;
    46     return NS_OK;
    47 }
    49 nsresult
    50 net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
    51 {
    52     nsresult rv;
    54     nsCOMPtr<nsIFile> localFile(
    55             do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
    56     if (NS_FAILED(rv)) {
    57         NS_ERROR("Only nsIFile supported right now");
    58         return rv;
    59     }
    61     localFile->SetFollowLinks(true);
    63     const nsACString *specPtr;
    65     nsAutoCString buf;
    66     if (net_NormalizeFileURL(aURL, buf))
    67         specPtr = &buf;
    68     else
    69         specPtr = &aURL;
    71     nsAutoCString directory, fileBaseName, fileExtension;
    73     rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension);
    74     if (NS_FAILED(rv)) return rv;
    76     nsAutoCString path;
    78     if (!directory.IsEmpty()) {
    79         NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
    80         if (path.Length() > 2 && path.CharAt(2) == '|')
    81             path.SetCharAt(':', 2);
    82         path.ReplaceChar('/', '\\');
    83     }    
    84     if (!fileBaseName.IsEmpty())
    85         NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
    86     if (!fileExtension.IsEmpty()) {
    87         path += '.';
    88         NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
    89     }
    91     NS_UnescapeURL(path);
    92     if (path.Length() != strlen(path.get()))
    93         return NS_ERROR_FILE_INVALID_PATH;
    95     // remove leading '\'
    96     if (path.CharAt(0) == '\\')
    97         path.Cut(0, 1);
    99     if (IsUTF8(path))
   100         rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
   101         // XXX In rare cases, a valid UTF-8 string can be valid as a native 
   102         // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
   103         // However, the chance is very low that a meaningful word in a legacy
   104         // encoding is valid as UTF-8.
   105     else 
   106         // if path is not in UTF-8, assume it is encoded in the native charset
   107         rv = localFile->InitWithNativePath(path);
   109     if (NS_FAILED(rv)) return rv;
   111     NS_ADDREF(*result = localFile);
   112     return NS_OK;
   113 }

mercurial