netwerk/base/src/nsURLHelperUnix.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/base/src/nsURLHelperUnix.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim:set ts=4 sw=4 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* Unix-specific local file uri parsing */
    1.11 +#include "nsURLHelper.h"
    1.12 +#include "nsEscape.h"
    1.13 +#include "nsIFile.h"
    1.14 +#include "nsNativeCharsetUtils.h"
    1.15 +
    1.16 +nsresult 
    1.17 +net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
    1.18 +{
    1.19 +    nsresult rv;
    1.20 +    nsAutoCString nativePath, ePath;
    1.21 +    nsAutoString path;
    1.22 +
    1.23 +    rv = aFile->GetNativePath(nativePath);
    1.24 +    if (NS_FAILED(rv)) return rv;
    1.25 +
    1.26 +    // Convert to unicode and back to check correct conversion to native charset
    1.27 +    NS_CopyNativeToUnicode(nativePath, path);
    1.28 +    NS_CopyUnicodeToNative(path, ePath);
    1.29 +
    1.30 +    // Use UTF8 version if conversion was successful
    1.31 +    if (nativePath == ePath)
    1.32 +        CopyUTF16toUTF8(path, ePath);
    1.33 +    else
    1.34 +        ePath = nativePath;
    1.35 +    
    1.36 +    nsAutoCString escPath;
    1.37 +    NS_NAMED_LITERAL_CSTRING(prefix, "file://");
    1.38 +        
    1.39 +    // Escape the path with the directory mask
    1.40 +    if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
    1.41 +        escPath.Insert(prefix, 0);
    1.42 +    else
    1.43 +        escPath.Assign(prefix + ePath);
    1.44 +
    1.45 +    // esc_Directory does not escape the semicolons, so if a filename 
    1.46 +    // contains semicolons we need to manually escape them.
    1.47 +    // This replacement should be removed in bug #473280
    1.48 +    escPath.ReplaceSubstring(";", "%3b");
    1.49 +    result = escPath;
    1.50 +    return NS_OK;
    1.51 +}
    1.52 +
    1.53 +nsresult
    1.54 +net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
    1.55 +{
    1.56 +    // NOTE: See also the implementation in nsURLHelperOSX.cpp,
    1.57 +    // which is based on this.
    1.58 +
    1.59 +    nsresult rv;
    1.60 +
    1.61 +    nsCOMPtr<nsIFile> localFile;
    1.62 +    rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile));
    1.63 +    if (NS_FAILED(rv))
    1.64 +      return rv;
    1.65 +    
    1.66 +    nsAutoCString directory, fileBaseName, fileExtension, path;
    1.67 +
    1.68 +    rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
    1.69 +    if (NS_FAILED(rv)) return rv;
    1.70 +
    1.71 +    if (!directory.IsEmpty())
    1.72 +        NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
    1.73 +    if (!fileBaseName.IsEmpty())
    1.74 +        NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
    1.75 +    if (!fileExtension.IsEmpty()) {
    1.76 +        path += '.';
    1.77 +        NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
    1.78 +    }
    1.79 +    
    1.80 +    NS_UnescapeURL(path);
    1.81 +    if (path.Length() != strlen(path.get()))
    1.82 +        return NS_ERROR_FILE_INVALID_PATH;
    1.83 +
    1.84 +    if (IsUTF8(path)) {
    1.85 +        // speed up the start-up where UTF-8 is the native charset
    1.86 +        // (e.g. on recent Linux distributions)
    1.87 +        if (NS_IsNativeUTF8())
    1.88 +            rv = localFile->InitWithNativePath(path);
    1.89 +        else
    1.90 +            rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
    1.91 +            // XXX In rare cases, a valid UTF-8 string can be valid as a native 
    1.92 +            // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
    1.93 +            // However, the chance is very low that a meaningful word in a legacy
    1.94 +            // encoding is valid as UTF-8.
    1.95 +    }
    1.96 +    else 
    1.97 +        // if path is not in UTF-8, assume it is encoded in the native charset
    1.98 +        rv = localFile->InitWithNativePath(path);
    1.99 +
   1.100 +    if (NS_FAILED(rv)) return rv;
   1.101 +
   1.102 +    NS_ADDREF(*result = localFile);
   1.103 +    return NS_OK;
   1.104 +}

mercurial