1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
24 * Alec Flett <alecf@netscape.com>
25 * Darin Fisher <darin@netscape.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 /* Unix-specific local file uri parsing */
42 #include "nsURLHelper.h"
44 #include "nsILocalFile.h"
45 #include "nsNativeCharsetUtils.h"
48 net_GetURLSpecFromFile(nsIFile
*aFile
, nsACString
&result
)
51 nsCAutoString nativePath
, ePath
;
54 rv
= aFile
->GetNativePath(nativePath
);
55 if (NS_FAILED(rv
)) return rv
;
57 // Convert to unicode and back to check correct conversion to native charset
58 NS_CopyNativeToUnicode(nativePath
, path
);
59 NS_CopyUnicodeToNative(path
, ePath
);
61 // Use UTF8 version if conversion was successful
62 if (nativePath
== ePath
)
63 CopyUTF16toUTF8(path
, ePath
);
67 nsCAutoString escPath
;
68 NS_NAMED_LITERAL_CSTRING(prefix
, "file://");
70 // Escape the path with the directory mask
71 if (NS_EscapeURL(ePath
.get(), -1, esc_Directory
+esc_Forced
, escPath
))
72 escPath
.Insert(prefix
, 0);
74 escPath
.Assign(prefix
+ ePath
);
76 // esc_Directory does not escape the semicolons, so if a filename
77 // contains semicolons we need to manually escape them.
78 escPath
.ReplaceSubstring(";", "%3b");
80 // if this file references a directory, then we need to ensure that the
81 // URL ends with a slash. this is important since it affects the rules
82 // for relative URL resolution when this URL is used as a base URL.
83 // if the file does not exist, then we make no assumption about its type,
84 // and simply leave the URL unmodified.
85 if (escPath
.Last() != '/') {
87 rv
= aFile
->IsDirectory(&dir
);
88 if (NS_SUCCEEDED(rv
) && dir
)
97 net_GetFileFromURLSpec(const nsACString
&aURL
, nsIFile
**result
)
99 // NOTE: See also the implementation in nsURLHelperOSX.cpp,
100 // which is based on this.
104 nsCOMPtr
<nsILocalFile
> localFile
;
105 rv
= NS_NewNativeLocalFile(EmptyCString(), PR_TRUE
, getter_AddRefs(localFile
));
109 nsCAutoString directory
, fileBaseName
, fileExtension
, path
;
111 rv
= net_ParseFileURL(aURL
, directory
, fileBaseName
, fileExtension
);
112 if (NS_FAILED(rv
)) return rv
;
114 if (!directory
.IsEmpty())
115 NS_EscapeURL(directory
, esc_Directory
|esc_AlwaysCopy
, path
);
116 if (!fileBaseName
.IsEmpty())
117 NS_EscapeURL(fileBaseName
, esc_FileBaseName
|esc_AlwaysCopy
, path
);
118 if (!fileExtension
.IsEmpty()) {
120 NS_EscapeURL(fileExtension
, esc_FileExtension
|esc_AlwaysCopy
, path
);
123 NS_UnescapeURL(path
);
124 if (path
.Length() != strlen(path
.get()))
125 return NS_ERROR_FILE_INVALID_PATH
;
128 // speed up the start-up where UTF-8 is the native charset
129 // (e.g. on recent Linux distributions)
130 if (NS_IsNativeUTF8())
131 rv
= localFile
->InitWithNativePath(path
);
133 rv
= localFile
->InitWithPath(NS_ConvertUTF8toUTF16(path
));
134 // XXX In rare cases, a valid UTF-8 string can be valid as a native
135 // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
136 // However, the chance is very low that a meaningful word in a legacy
137 // encoding is valid as UTF-8.
140 // if path is not in UTF-8, assume it is encoded in the native charset
141 rv
= localFile
->InitWithNativePath(path
);
143 if (NS_FAILED(rv
)) return rv
;
145 NS_ADDREF(*result
= localFile
);