1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/nsURLHelperWin.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 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 +/* Windows-specific local file uri parsing */ 1.11 +#include "nsURLHelper.h" 1.12 +#include "nsEscape.h" 1.13 +#include "nsIFile.h" 1.14 +#include <windows.h> 1.15 + 1.16 +nsresult 1.17 +net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result) 1.18 +{ 1.19 + nsresult rv; 1.20 + nsAutoString path; 1.21 + 1.22 + // construct URL spec from file path 1.23 + rv = aFile->GetPath(path); 1.24 + if (NS_FAILED(rv)) return rv; 1.25 + 1.26 + // Replace \ with / to convert to an url 1.27 + path.ReplaceChar(char16_t(0x5Cu), char16_t(0x2Fu)); 1.28 + 1.29 + nsAutoCString escPath; 1.30 + 1.31 + // Windows Desktop paths begin with a drive letter, so need an 'extra' 1.32 + // slash at the begining 1.33 + // C:\Windows => file:///C:/Windows 1.34 + NS_NAMED_LITERAL_CSTRING(prefix, "file:///"); 1.35 + 1.36 + // Escape the path with the directory mask 1.37 + NS_ConvertUTF16toUTF8 ePath(path); 1.38 + if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath)) 1.39 + escPath.Insert(prefix, 0); 1.40 + else 1.41 + escPath.Assign(prefix + ePath); 1.42 + 1.43 + // esc_Directory does not escape the semicolons, so if a filename 1.44 + // contains semicolons we need to manually escape them. 1.45 + // This replacement should be removed in bug #473280 1.46 + escPath.ReplaceSubstring(";", "%3b"); 1.47 + 1.48 + result = escPath; 1.49 + return NS_OK; 1.50 +} 1.51 + 1.52 +nsresult 1.53 +net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) 1.54 +{ 1.55 + nsresult rv; 1.56 + 1.57 + nsCOMPtr<nsIFile> localFile( 1.58 + do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); 1.59 + if (NS_FAILED(rv)) { 1.60 + NS_ERROR("Only nsIFile supported right now"); 1.61 + return rv; 1.62 + } 1.63 + 1.64 + localFile->SetFollowLinks(true); 1.65 + 1.66 + const nsACString *specPtr; 1.67 + 1.68 + nsAutoCString buf; 1.69 + if (net_NormalizeFileURL(aURL, buf)) 1.70 + specPtr = &buf; 1.71 + else 1.72 + specPtr = &aURL; 1.73 + 1.74 + nsAutoCString directory, fileBaseName, fileExtension; 1.75 + 1.76 + rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension); 1.77 + if (NS_FAILED(rv)) return rv; 1.78 + 1.79 + nsAutoCString path; 1.80 + 1.81 + if (!directory.IsEmpty()) { 1.82 + NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path); 1.83 + if (path.Length() > 2 && path.CharAt(2) == '|') 1.84 + path.SetCharAt(':', 2); 1.85 + path.ReplaceChar('/', '\\'); 1.86 + } 1.87 + if (!fileBaseName.IsEmpty()) 1.88 + NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path); 1.89 + if (!fileExtension.IsEmpty()) { 1.90 + path += '.'; 1.91 + NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path); 1.92 + } 1.93 + 1.94 + NS_UnescapeURL(path); 1.95 + if (path.Length() != strlen(path.get())) 1.96 + return NS_ERROR_FILE_INVALID_PATH; 1.97 + 1.98 + // remove leading '\' 1.99 + if (path.CharAt(0) == '\\') 1.100 + path.Cut(0, 1); 1.101 + 1.102 + if (IsUTF8(path)) 1.103 + rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path)); 1.104 + // XXX In rare cases, a valid UTF-8 string can be valid as a native 1.105 + // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x). 1.106 + // However, the chance is very low that a meaningful word in a legacy 1.107 + // encoding is valid as UTF-8. 1.108 + else 1.109 + // if path is not in UTF-8, assume it is encoded in the native charset 1.110 + rv = localFile->InitWithNativePath(path); 1.111 + 1.112 + if (NS_FAILED(rv)) return rv; 1.113 + 1.114 + NS_ADDREF(*result = localFile); 1.115 + return NS_OK; 1.116 +}