Back to index

lightning-sunbird  0.9+nobinonly
nsURLHelperUnix.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 /* Unix-specific local file uri parsing */
00042 #include "nsURLHelper.h"
00043 #include "nsEscape.h"
00044 #include "nsILocalFile.h"
00045 #include "nsNativeCharsetUtils.h"
00046 
00047 nsresult
00048 net_GetURLSpecFromFile(nsIFile *aFile, nsACString &result)
00049 {
00050     // NOTE: This is identical to the implementation in nsURLHelperOSX.cpp
00051 
00052     nsresult rv;
00053     nsAutoString path;
00054 
00055     // construct URL spec from  file path
00056     rv = aFile->GetPath(path);
00057     if (NS_FAILED(rv)) return rv;
00058 
00059     nsCAutoString escPath;
00060     NS_NAMED_LITERAL_CSTRING(prefix, "file://");
00061         
00062     // Escape the path with the directory mask
00063     NS_ConvertUTF16toUTF8 ePath(path);
00064     if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
00065         escPath.Insert(prefix, 0);
00066     else
00067         escPath.Assign(prefix + ePath);
00068 
00069     // esc_Directory does not escape the semicolons, so if a filename 
00070     // contains semicolons we need to manually escape them.
00071     escPath.ReplaceSubstring(";", "%3b");
00072 
00073     // if this file references a directory, then we need to ensure that the
00074     // URL ends with a slash.  this is important since it affects the rules
00075     // for relative URL resolution when this URL is used as a base URL.
00076     // if the file does not exist, then we make no assumption about its type,
00077     // and simply leave the URL unmodified.
00078     if (escPath.Last() != '/') {
00079         PRBool dir;
00080         rv = aFile->IsDirectory(&dir);
00081         if (NS_SUCCEEDED(rv) && dir)
00082             escPath += '/';
00083     }
00084     
00085     result = escPath;
00086     return NS_OK;
00087 }
00088 
00089 nsresult
00090 net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
00091 {
00092     // NOTE: See also the implementation in nsURLHelperOSX.cpp,
00093     // which is based on this.
00094 
00095     nsresult rv;
00096 
00097     nsCOMPtr<nsILocalFile> localFile;
00098     rv = NS_NewNativeLocalFile(EmptyCString(), PR_TRUE, getter_AddRefs(localFile));
00099     if (NS_FAILED(rv))
00100       return rv;
00101     
00102     nsCAutoString directory, fileBaseName, fileExtension, path;
00103 
00104     rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
00105     if (NS_FAILED(rv)) return rv;
00106 
00107     if (!directory.IsEmpty())
00108         NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
00109     if (!fileBaseName.IsEmpty())
00110         NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
00111     if (!fileExtension.IsEmpty()) {
00112         path += '.';
00113         NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
00114     }
00115     
00116     NS_UnescapeURL(path);
00117     if (path.Length() != strlen(path.get()))
00118         return NS_ERROR_FILE_INVALID_PATH;
00119 
00120     if (IsUTF8(path)) {
00121         // speed up the start-up where UTF-8 is the native charset
00122         // (e.g. on recent Linux distributions)
00123         if (NS_IsNativeUTF8())
00124             rv = localFile->InitWithNativePath(path);
00125         else
00126             rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
00127             // XXX In rare cases, a valid UTF-8 string can be valid as a native 
00128             // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
00129             // However, the chance is very low that a meaningful word in a legacy
00130             // encoding is valid as UTF-8.
00131     }
00132     else 
00133         // if path is not in UTF-8, assume it is encoded in the native charset
00134         rv = localFile->InitWithNativePath(path);
00135 
00136     if (NS_FAILED(rv)) return rv;
00137 
00138     NS_ADDREF(*result = localFile);
00139     return NS_OK;
00140 }