Source code

Revision control

Other Tools

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