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.

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

mercurial