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
;
62 // Windows Desktop paths beging with a drive letter, so need an 'extra'
63 // slash at the begining
64 #ifdef WINCE // /Windows => file:///Windows
65 NS_NAMED_LITERAL_CSTRING(prefix
, "file://");
66 #else // C:\Windows => file:///C:/Windows
67 NS_NAMED_LITERAL_CSTRING(prefix
, "file:///");
69 // Escape the path with the directory mask
70 NS_ConvertUTF16toUTF8
ePath(path
);
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
)
101 nsCOMPtr
<nsILocalFile
> localFile(
102 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
));
104 NS_ERROR("Only nsILocalFile supported right now");
108 localFile
->SetFollowLinks(PR_TRUE
);
110 const nsACString
*specPtr
;
113 if (net_NormalizeFileURL(aURL
, buf
))
118 nsCAutoString directory
, fileBaseName
, fileExtension
;
120 rv
= net_ParseFileURL(*specPtr
, directory
, fileBaseName
, fileExtension
);
121 if (NS_FAILED(rv
)) return rv
;
125 if (!directory
.IsEmpty()) {
126 NS_EscapeURL(directory
, esc_Directory
|esc_AlwaysCopy
, path
);
127 if (path
.Length() > 2 && path
.CharAt(2) == '|')
128 path
.SetCharAt(':', 2);
129 path
.ReplaceChar('/', '\\');
131 if (!fileBaseName
.IsEmpty())
132 NS_EscapeURL(fileBaseName
, esc_FileBaseName
|esc_AlwaysCopy
, path
);
133 if (!fileExtension
.IsEmpty()) {
135 NS_EscapeURL(fileExtension
, esc_FileExtension
|esc_AlwaysCopy
, path
);
138 NS_UnescapeURL(path
);
139 if (path
.Length() != strlen(path
.get()))
140 return NS_ERROR_FILE_INVALID_PATH
;
143 // remove leading '\'
144 if (path
.CharAt(0) == '\\')
149 rv
= localFile
->InitWithPath(NS_ConvertUTF8toUTF16(path
));
150 // XXX In rare cases, a valid UTF-8 string can be valid as a native
151 // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
152 // However, the chance is very low that a meaningful word in a legacy
153 // encoding is valid as UTF-8.
155 // if path is not in UTF-8, assume it is encoded in the native charset
156 rv
= localFile
->InitWithNativePath(path
);
158 if (NS_FAILED(rv
)) return rv
;
160 NS_ADDREF(*result
= localFile
);