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