netwerk/base/src/nsURLHelperUnix.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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 /* Unix-specific local file uri parsing */
     8 #include "nsURLHelper.h"
     9 #include "nsEscape.h"
    10 #include "nsIFile.h"
    11 #include "nsNativeCharsetUtils.h"
    13 nsresult 
    14 net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
    15 {
    16     nsresult rv;
    17     nsAutoCString nativePath, ePath;
    18     nsAutoString path;
    20     rv = aFile->GetNativePath(nativePath);
    21     if (NS_FAILED(rv)) return rv;
    23     // Convert to unicode and back to check correct conversion to native charset
    24     NS_CopyNativeToUnicode(nativePath, path);
    25     NS_CopyUnicodeToNative(path, ePath);
    27     // Use UTF8 version if conversion was successful
    28     if (nativePath == ePath)
    29         CopyUTF16toUTF8(path, ePath);
    30     else
    31         ePath = nativePath;
    33     nsAutoCString escPath;
    34     NS_NAMED_LITERAL_CSTRING(prefix, "file://");
    36     // Escape the path with the directory mask
    37     if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
    38         escPath.Insert(prefix, 0);
    39     else
    40         escPath.Assign(prefix + ePath);
    42     // esc_Directory does not escape the semicolons, so if a filename 
    43     // contains semicolons we need to manually escape them.
    44     // This replacement should be removed in bug #473280
    45     escPath.ReplaceSubstring(";", "%3b");
    46     result = escPath;
    47     return NS_OK;
    48 }
    50 nsresult
    51 net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
    52 {
    53     // NOTE: See also the implementation in nsURLHelperOSX.cpp,
    54     // which is based on this.
    56     nsresult rv;
    58     nsCOMPtr<nsIFile> localFile;
    59     rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile));
    60     if (NS_FAILED(rv))
    61       return rv;
    63     nsAutoCString directory, fileBaseName, fileExtension, path;
    65     rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
    66     if (NS_FAILED(rv)) return rv;
    68     if (!directory.IsEmpty())
    69         NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
    70     if (!fileBaseName.IsEmpty())
    71         NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
    72     if (!fileExtension.IsEmpty()) {
    73         path += '.';
    74         NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
    75     }
    77     NS_UnescapeURL(path);
    78     if (path.Length() != strlen(path.get()))
    79         return NS_ERROR_FILE_INVALID_PATH;
    81     if (IsUTF8(path)) {
    82         // speed up the start-up where UTF-8 is the native charset
    83         // (e.g. on recent Linux distributions)
    84         if (NS_IsNativeUTF8())
    85             rv = localFile->InitWithNativePath(path);
    86         else
    87             rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
    88             // XXX In rare cases, a valid UTF-8 string can be valid as a native 
    89             // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
    90             // However, the chance is very low that a meaningful word in a legacy
    91             // encoding is valid as UTF-8.
    92     }
    93     else 
    94         // if path is not in UTF-8, assume it is encoded in the native charset
    95         rv = localFile->InitWithNativePath(path);
    97     if (NS_FAILED(rv)) return rv;
    99     NS_ADDREF(*result = localFile);
   100     return NS_OK;
   101 }

mercurial