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