Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / netwerk / base / nsURLHelperWin.cpp
blob7a4f53978e0b36ece73cf4c33a5b701e23ea357c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sw=2 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 "nsComponentManagerUtils.h"
9 #include "nsURLHelper.h"
10 #include "nsEscape.h"
11 #include "nsIFile.h"
12 #include <windows.h>
13 #include "mozilla/StaticPrefs_network.h"
14 #include "mozilla/Utf8.h"
16 using namespace mozilla;
18 nsresult net_GetURLSpecFromActualFile(nsIFile* aFile, nsACString& result) {
19 nsresult rv;
20 nsAutoString path;
22 // construct URL spec from file path
23 rv = aFile->GetPath(path);
24 if (NS_FAILED(rv)) return rv;
26 // Replace \ with / to convert to an url
27 path.ReplaceChar(char16_t(0x5Cu), char16_t(0x2Fu));
29 nsAutoCString escPath;
31 // Windows Desktop paths begin with a drive letter, so need an 'extra'
32 // slash at the begining
33 // C:\Windows => file:///C:/Windows
34 constexpr auto prefix = "file:///"_ns;
36 // Escape the path with the directory mask
37 NS_ConvertUTF16toUTF8 ePath(path);
38 if (NS_EscapeURL(ePath.get(), -1, esc_Directory + esc_Forced, escPath))
39 escPath.Insert(prefix, 0);
40 else
41 escPath.Assign(prefix + ePath);
43 // esc_Directory does not escape the semicolons, so if a filename
44 // contains semicolons we need to manually escape them.
45 // This replacement should be removed in bug #473280
46 escPath.ReplaceSubstring(";", "%3b");
48 result = escPath;
49 return NS_OK;
52 nsresult net_GetFileFromURLSpec(const nsACString& aURL, nsIFile** result) {
53 nsresult rv;
55 if (aURL.Length() > StaticPrefs::network_standard_url_max_length()) {
56 return NS_ERROR_MALFORMED_URI;
59 nsCOMPtr<nsIFile> localFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
60 if (NS_FAILED(rv)) {
61 NS_ERROR("Only nsIFile supported right now");
62 return rv;
65 const nsACString* specPtr;
67 nsAutoCString buf;
68 if (net_NormalizeFileURL(aURL, buf))
69 specPtr = &buf;
70 else
71 specPtr = &aURL;
73 nsAutoCString directory, fileBaseName, fileExtension;
75 rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension);
76 if (NS_FAILED(rv)) return rv;
78 nsAutoCString path;
80 if (!directory.IsEmpty()) {
81 NS_EscapeURL(directory, esc_Directory | esc_AlwaysCopy, path);
82 if (path.Length() > 2 && path.CharAt(2) == '|') path.SetCharAt(':', 2);
83 path.ReplaceChar('/', '\\');
85 if (!fileBaseName.IsEmpty())
86 NS_EscapeURL(fileBaseName, esc_FileBaseName | esc_AlwaysCopy, path);
87 if (!fileExtension.IsEmpty()) {
88 path += '.';
89 NS_EscapeURL(fileExtension, esc_FileExtension | esc_AlwaysCopy, path);
92 NS_UnescapeURL(path);
93 if (path.Length() != strlen(path.get())) return NS_ERROR_FILE_INVALID_PATH;
95 // remove leading '\'
96 if (path.CharAt(0) == '\\') path.Cut(0, 1);
98 if (IsUtf8(path)) rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
99 // XXX In rare cases, a valid UTF-8 string can be valid as a native
100 // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
101 // However, the chance is very low that a meaningful word in a legacy
102 // encoding is valid as UTF-8.
103 else
104 // if path is not in UTF-8, assume it is encoded in the native charset
105 rv = localFile->InitWithNativePath(path);
107 if (NS_FAILED(rv)) return rv;
109 localFile.forget(result);
110 return NS_OK;