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 /* Windows-specific local file uri parsing */
42 #include "nsURLHelper.h"
44 #include "nsILocalFile.h"
48 net_GetURLSpecFromFile(nsIFile
*aFile
, nsACString
&result
)
53 // construct URL spec from file path
54 rv
= aFile
->GetPath(path
);
55 if (NS_FAILED(rv
)) return rv
;
57 // Replace \ with / to convert to an url
58 path
.ReplaceChar(PRUnichar(0x5Cu
), PRUnichar(0x2Fu
));
60 nsCAutoString escPath
;
61 NS_NAMED_LITERAL_CSTRING(prefix
, "file:///");
63 // Escape the path with the directory mask
64 NS_ConvertUTF16toUTF8
ePath(path
);
65 if (NS_EscapeURL(ePath
.get(), -1, esc_Directory
+esc_Forced
, escPath
))
66 escPath
.Insert(prefix
, 0);
68 escPath
.Assign(prefix
+ ePath
);
70 // esc_Directory does not escape the semicolons, so if a filename
71 // contains semicolons we need to manually escape them.
72 escPath
.ReplaceSubstring(";", "%3b");
74 // if this file references a directory, then we need to ensure that the
75 // URL ends with a slash. this is important since it affects the rules
76 // for relative URL resolution when this URL is used as a base URL.
77 // if the file does not exist, then we make no assumption about its type,
78 // and simply leave the URL unmodified.
79 if (escPath
.Last() != '/') {
81 rv
= aFile
->IsDirectory(&dir
);
82 if (NS_SUCCEEDED(rv
) && dir
)
91 net_GetFileFromURLSpec(const nsACString
&aURL
, nsIFile
**result
)
95 nsCOMPtr
<nsILocalFile
> localFile(
96 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
));
98 NS_ERROR("Only nsILocalFile supported right now");
102 localFile
->SetFollowLinks(PR_TRUE
);
104 const nsACString
*specPtr
;
107 if (net_NormalizeFileURL(aURL
, buf
))
112 nsCAutoString directory
, fileBaseName
, fileExtension
;
114 rv
= net_ParseFileURL(*specPtr
, directory
, fileBaseName
, fileExtension
);
115 if (NS_FAILED(rv
)) return rv
;
119 if (!directory
.IsEmpty()) {
120 NS_EscapeURL(directory
, esc_Directory
|esc_AlwaysCopy
, path
);
121 if (path
.Length() > 2 && path
.CharAt(2) == '|')
122 path
.SetCharAt(':', 2);
123 path
.ReplaceChar('/', '\\');
125 if (!fileBaseName
.IsEmpty())
126 NS_EscapeURL(fileBaseName
, esc_FileBaseName
|esc_AlwaysCopy
, path
);
127 if (!fileExtension
.IsEmpty()) {
129 NS_EscapeURL(fileExtension
, esc_FileExtension
|esc_AlwaysCopy
, path
);
132 NS_UnescapeURL(path
);
133 if (path
.Length() != strlen(path
.get()))
134 return NS_ERROR_FILE_INVALID_PATH
;
136 // remove leading '\'
137 if (path
.CharAt(0) == '\\')
141 rv
= localFile
->InitWithPath(NS_ConvertUTF8toUTF16(path
));
142 // XXX In rare cases, a valid UTF-8 string can be valid as a native
143 // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
144 // However, the chance is very low that a meaningful word in a legacy
145 // encoding is valid as UTF-8.
147 // if path is not in UTF-8, assume it is encoded in the native charset
148 rv
= localFile
->InitWithNativePath(path
);
150 if (NS_FAILED(rv
)) return rv
;
152 NS_ADDREF(*result
= localFile
);