Back to index

lightning-sunbird  0.9+nobinonly
nsURLHelperWin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:set ts=4 sw=4 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Alec Flett <alecf@netscape.com>
00025  *   Darin Fisher <darin@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 /* Windows-specific local file uri parsing */
00042 #include "nsURLHelper.h"
00043 #include "nsEscape.h"
00044 #include "nsILocalFile.h"
00045 #include <windows.h>
00046 
00047 nsresult
00048 net_GetURLSpecFromFile(nsIFile *aFile, nsACString &result)
00049 {
00050     nsresult rv;
00051     nsAutoString path;
00052   
00053     // construct URL spec from file path
00054     rv = aFile->GetPath(path);
00055     if (NS_FAILED(rv)) return rv;
00056   
00057     // Replace \ with / to convert to an url
00058     path.ReplaceChar(PRUnichar(0x5Cu), PRUnichar(0x2Fu));
00059 
00060     nsCAutoString escPath;
00061     NS_NAMED_LITERAL_CSTRING(prefix, "file:///");
00062   
00063     // Escape the path with the directory mask
00064     NS_ConvertUTF16toUTF8 ePath(path);
00065     if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
00066         escPath.Insert(prefix, 0);
00067     else
00068         escPath.Assign(prefix + ePath);
00069 
00070     // esc_Directory does not escape the semicolons, so if a filename 
00071     // contains semicolons we need to manually escape them.
00072     escPath.ReplaceSubstring(";", "%3b");
00073 
00074     // if this file references a directory, then we need to ensure that the
00075     // URL ends with a slash.  this is important since it affects the rules
00076     // for relative URL resolution when this URL is used as a base URL.
00077     // if the file does not exist, then we make no assumption about its type,
00078     // and simply leave the URL unmodified.
00079     if (escPath.Last() != '/') {
00080         PRBool dir;
00081         rv = aFile->IsDirectory(&dir);
00082         if (NS_SUCCEEDED(rv) && dir)
00083             escPath += '/';
00084     }
00085     
00086     result = escPath;
00087     return NS_OK;
00088 }
00089 
00090 nsresult
00091 net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
00092 {
00093     nsresult rv;
00094     
00095     nsCOMPtr<nsILocalFile> localFile(
00096             do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00097     if (NS_FAILED(rv)) {
00098         NS_ERROR("Only nsILocalFile supported right now");
00099         return rv;
00100     }
00101 
00102     const nsACString *specPtr;
00103 
00104     nsCAutoString buf;
00105     if (net_NormalizeFileURL(aURL, buf))
00106         specPtr = &buf;
00107     else
00108         specPtr = &aURL;
00109     
00110     nsCAutoString directory, fileBaseName, fileExtension;
00111     
00112     rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension);
00113     if (NS_FAILED(rv)) return rv;
00114 
00115     nsCAutoString path;
00116 
00117     if (!directory.IsEmpty()) {
00118         NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
00119         if (path.Length() > 2 && path.CharAt(2) == '|')
00120             path.SetCharAt(':', 2);
00121         path.ReplaceChar('/', '\\');
00122     }    
00123     if (!fileBaseName.IsEmpty())
00124         NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
00125     if (!fileExtension.IsEmpty()) {
00126         path += '.';
00127         NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
00128     }
00129     
00130     NS_UnescapeURL(path);
00131     if (path.Length() != strlen(path.get()))
00132         return NS_ERROR_FILE_INVALID_PATH;
00133 
00134     // remove leading '\'
00135     if (path.CharAt(0) == '\\')
00136         path.Cut(0, 1);
00137 
00138     if (IsUTF8(path))
00139         rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
00140         // XXX In rare cases, a valid UTF-8 string can be valid as a native 
00141         // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
00142         // However, the chance is very low that a meaningful word in a legacy
00143         // encoding is valid as UTF-8.
00144     else 
00145         // if path is not in UTF-8, assume it is encoded in the native charset
00146         rv = localFile->InitWithNativePath(path);
00147 
00148     if (NS_FAILED(rv)) return rv;
00149 
00150     NS_ADDREF(*result = localFile);
00151     return NS_OK;
00152 }