Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/base/nsURLHelperUnix.cpp
Line
Count
Source (jump to first uncovered line)
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
7
/* Unix-specific local file uri parsing */
8
#include "nsURLHelper.h"
9
#include "nsEscape.h"
10
#include "nsIFile.h"
11
#include "nsNativeCharsetUtils.h"
12
13
nsresult
14
net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
15
349
{
16
349
    nsresult rv;
17
349
    nsAutoCString nativePath, ePath;
18
349
    nsAutoString path;
19
349
20
349
    rv = aFile->GetNativePath(nativePath);
21
349
    if (NS_FAILED(rv)) return rv;
22
349
23
349
    // Convert to unicode and back to check correct conversion to native charset
24
349
    NS_CopyNativeToUnicode(nativePath, path);
25
349
    NS_CopyUnicodeToNative(path, ePath);
26
349
27
349
    // Use UTF8 version if conversion was successful
28
349
    if (nativePath == ePath)
29
349
        CopyUTF16toUTF8(path, ePath);
30
0
    else
31
0
        ePath = nativePath;
32
349
33
349
    nsAutoCString escPath;
34
349
    NS_NAMED_LITERAL_CSTRING(prefix, "file://");
35
349
36
349
    // Escape the path with the directory mask
37
349
    if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
38
0
        escPath.Insert(prefix, 0);
39
349
    else
40
349
        escPath.Assign(prefix + ePath);
41
349
42
349
    // esc_Directory does not escape the semicolons, so if a filename
43
349
    // contains semicolons we need to manually escape them.
44
349
    // This replacement should be removed in bug #473280
45
349
    escPath.ReplaceSubstring(";", "%3b");
46
349
    result = escPath;
47
349
    return NS_OK;
48
349
}
49
50
nsresult
51
net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
52
9
{
53
9
    // NOTE: See also the implementation in nsURLHelperOSX.cpp,
54
9
    // which is based on this.
55
9
56
9
    nsresult rv;
57
9
58
9
    nsCOMPtr<nsIFile> localFile;
59
9
    rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile));
60
9
    if (NS_FAILED(rv))
61
9
      return rv;
62
9
63
9
    nsAutoCString directory, fileBaseName, fileExtension, path;
64
9
65
9
    rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
66
9
    if (NS_FAILED(rv)) return rv;
67
9
68
9
    if (!directory.IsEmpty()) {
69
9
        rv = NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path,
70
9
                         mozilla::fallible);
71
9
        if (NS_FAILED(rv))
72
9
          return rv;
73
9
    }
74
9
    if (!fileBaseName.IsEmpty()) {
75
9
        rv = NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path,
76
9
                          mozilla::fallible);
77
9
        if (NS_FAILED(rv))
78
9
          return rv;
79
9
    }
80
9
    if (!fileExtension.IsEmpty()) {
81
9
        path += '.';
82
9
        rv = NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path,
83
9
                          mozilla::fallible);
84
9
        if (NS_FAILED(rv))
85
9
          return rv;
86
9
    }
87
9
88
9
    NS_UnescapeURL(path);
89
9
    if (path.Length() != strlen(path.get()))
90
0
        return NS_ERROR_FILE_INVALID_PATH;
91
9
92
9
    if (IsUTF8(path)) {
93
9
        // speed up the start-up where UTF-8 is the native charset
94
9
        // (e.g. on recent Linux distributions)
95
9
        if (NS_IsNativeUTF8())
96
9
            rv = localFile->InitWithNativePath(path);
97
0
        else
98
0
            rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
99
9
            // XXX In rare cases, a valid UTF-8 string can be valid as a native
100
9
            // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
101
9
            // However, the chance is very low that a meaningful word in a legacy
102
9
            // encoding is valid as UTF-8.
103
9
    }
104
0
    else
105
0
        // if path is not in UTF-8, assume it is encoded in the native charset
106
0
        rv = localFile->InitWithNativePath(path);
107
9
108
9
    if (NS_FAILED(rv)) return rv;
109
9
110
9
    localFile.forget(result);
111
9
    return NS_OK;
112
9
}