nsURLHelperUnix.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* vim:set ts=4 sw=4 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* Unix-specific local file uri parsing */
  7. #include "nsURLHelper.h"
  8. #include "nsEscape.h"
  9. #include "nsIFile.h"
  10. #include "nsNativeCharsetUtils.h"
  11. nsresult
  12. net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
  13. {
  14. nsresult rv;
  15. nsAutoCString nativePath, ePath;
  16. nsAutoString path;
  17. rv = aFile->GetNativePath(nativePath);
  18. if (NS_FAILED(rv)) return rv;
  19. // Convert to unicode and back to check correct conversion to native charset
  20. NS_CopyNativeToUnicode(nativePath, path);
  21. NS_CopyUnicodeToNative(path, ePath);
  22. // Use UTF8 version if conversion was successful
  23. if (nativePath == ePath)
  24. CopyUTF16toUTF8(path, ePath);
  25. else
  26. ePath = nativePath;
  27. nsAutoCString escPath;
  28. NS_NAMED_LITERAL_CSTRING(prefix, "file://");
  29. // Escape the path with the directory mask
  30. if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
  31. escPath.Insert(prefix, 0);
  32. else
  33. escPath.Assign(prefix + ePath);
  34. // esc_Directory does not escape the semicolons, so if a filename
  35. // contains semicolons we need to manually escape them.
  36. // This replacement should be removed in bug #473280
  37. escPath.ReplaceSubstring(";", "%3b");
  38. result = escPath;
  39. return NS_OK;
  40. }
  41. nsresult
  42. net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
  43. {
  44. // NOTE: See also the implementation in nsURLHelperOSX.cpp,
  45. // which is based on this.
  46. nsresult rv;
  47. nsCOMPtr<nsIFile> localFile;
  48. rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile));
  49. if (NS_FAILED(rv))
  50. return rv;
  51. nsAutoCString directory, fileBaseName, fileExtension, path;
  52. rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
  53. if (NS_FAILED(rv)) return rv;
  54. if (!directory.IsEmpty()) {
  55. rv = NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path,
  56. mozilla::fallible);
  57. if (NS_FAILED(rv))
  58. return rv;
  59. }
  60. if (!fileBaseName.IsEmpty()) {
  61. rv = NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path,
  62. mozilla::fallible);
  63. if (NS_FAILED(rv))
  64. return rv;
  65. }
  66. if (!fileExtension.IsEmpty()) {
  67. path += '.';
  68. rv = NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path,
  69. mozilla::fallible);
  70. if (NS_FAILED(rv))
  71. return rv;
  72. }
  73. NS_UnescapeURL(path);
  74. if (path.Length() != strlen(path.get()))
  75. return NS_ERROR_FILE_INVALID_PATH;
  76. if (IsUTF8(path)) {
  77. // speed up the start-up where UTF-8 is the native charset
  78. // (e.g. on recent Linux distributions)
  79. if (NS_IsNativeUTF8())
  80. rv = localFile->InitWithNativePath(path);
  81. else
  82. rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
  83. // XXX In rare cases, a valid UTF-8 string can be valid as a native
  84. // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
  85. // However, the chance is very low that a meaningful word in a legacy
  86. // encoding is valid as UTF-8.
  87. }
  88. else
  89. // if path is not in UTF-8, assume it is encoded in the native charset
  90. rv = localFile->InitWithNativePath(path);
  91. if (NS_FAILED(rv)) return rv;
  92. localFile.forget(result);
  93. return NS_OK;
  94. }