summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/file/nsFileProtocolHandler.cpp
blob: edaed5daeb4022dbf786054017685f2eaaf795b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:ts=4 sw=2 sts=2 et cin:
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsIFile.h"
#include "nsFileProtocolHandler.h"
#include "nsFileChannel.h"
#include "nsStandardURL.h"
#include "nsURLHelper.h"
#include "nsIURIMutator.h"

#include "nsNetUtil.h"

#include "FileChannelChild.h"

#include "mozilla/ResultExtensions.h"

// URL file handling, copied and modified from
// xpfe/components/bookmarks/src/nsBookmarksService.cpp
#ifdef XP_WIN
#  include <shlobj.h>
#  include <intshcut.h>
#  include "nsIFileURL.h"
#  ifdef CompareString
#    undef CompareString
#  endif
#endif

// URL file handling for freedesktop.org
#ifdef XP_UNIX
#  include "nsINIParser.h"
#  define DESKTOP_ENTRY_SECTION "Desktop Entry"
#endif

//-----------------------------------------------------------------------------

nsresult nsFileProtocolHandler::Init() { return NS_OK; }

NS_IMPL_ISUPPORTS(nsFileProtocolHandler, nsIFileProtocolHandler,
                  nsIProtocolHandler, nsISupportsWeakReference)

//-----------------------------------------------------------------------------
// nsIProtocolHandler methods:

#if defined(XP_WIN)
NS_IMETHODIMP
nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) {
  nsAutoString path;
  nsresult rv = aFile->GetPath(path);
  if (NS_FAILED(rv)) return rv;

  if (path.Length() < 4) return NS_ERROR_NOT_AVAILABLE;
  if (!StringTail(path, 4).LowerCaseEqualsLiteral(".url"))
    return NS_ERROR_NOT_AVAILABLE;

  HRESULT result;

  rv = NS_ERROR_NOT_AVAILABLE;

  IUniformResourceLocatorW* urlLink = nullptr;
  result =
      ::CoCreateInstance(CLSID_InternetShortcut, nullptr, CLSCTX_INPROC_SERVER,
                         IID_IUniformResourceLocatorW, (void**)&urlLink);
  if (SUCCEEDED(result) && urlLink) {
    IPersistFile* urlFile = nullptr;
    result = urlLink->QueryInterface(IID_IPersistFile, (void**)&urlFile);
    if (SUCCEEDED(result) && urlFile) {
      result = urlFile->Load(path.get(), STGM_READ);
      if (SUCCEEDED(result)) {
        LPWSTR lpTemp = nullptr;

        // The URL this method will give us back seems to be already
        // escaped. Hence, do not do escaping of our own.
        result = urlLink->GetURL(&lpTemp);
        if (SUCCEEDED(result) && lpTemp) {
          rv = NS_NewURI(aURI, nsDependentString(lpTemp));
          // free the string that GetURL alloc'd
          CoTaskMemFree(lpTemp);
        }
      }
      urlFile->Release();
    }
    urlLink->Release();
  }
  return rv;
}

#elif defined(XP_UNIX)
NS_IMETHODIMP
nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) {
  // We only support desktop files that end in ".desktop" like the spec says:
  // http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s02.html
  nsAutoCString leafName;
  nsresult rv = aFile->GetNativeLeafName(leafName);
  if (NS_FAILED(rv) || !StringEndsWith(leafName, ".desktop"_ns)) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  bool isFile = false;
  rv = aFile->IsFile(&isFile);
  if (NS_FAILED(rv) || !isFile) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  nsINIParser parser;
  rv = parser.Init(aFile);
  if (NS_FAILED(rv)) return rv;

  nsAutoCString type;
  parser.GetString(DESKTOP_ENTRY_SECTION, "Type", type);
  if (!type.EqualsLiteral("Link")) return NS_ERROR_NOT_AVAILABLE;

  nsAutoCString url;
  rv = parser.GetString(DESKTOP_ENTRY_SECTION, "URL", url);
  if (NS_FAILED(rv) || url.IsEmpty()) return NS_ERROR_NOT_AVAILABLE;

  return NS_NewURI(aURI, url);
}

#else   // other platforms
NS_IMETHODIMP
nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) {
  return NS_ERROR_NOT_AVAILABLE;
}
#endif  // ReadURLFile()

NS_IMETHODIMP
nsFileProtocolHandler::ReadShellLink(nsIFile* aFile, nsIURI** aURI) {
#if defined(XP_WIN)
  nsAutoString path;
  MOZ_TRY(aFile->GetPath(path));

  if (path.Length() < 4 ||
      !StringTail(path, 4).LowerCaseEqualsLiteral(".lnk")) {
    return NS_ERROR_NOT_AVAILABLE;
  }
  RefPtr<IPersistFile> persistFile;
  RefPtr<IShellLinkW> shellLink;
  WCHAR lpTemp[MAX_PATH];
  // Get a pointer to the IPersistFile interface.
  if (FAILED(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
                              IID_IShellLinkW, getter_AddRefs(shellLink))) ||
      FAILED(shellLink->QueryInterface(IID_IPersistFile,
                                       getter_AddRefs(persistFile))) ||
      FAILED(persistFile->Load(path.get(), STGM_READ)) ||
      FAILED(shellLink->Resolve(nullptr, SLR_NO_UI)) ||
      FAILED(shellLink->GetPath(lpTemp, MAX_PATH, nullptr, SLGP_UNCPRIORITY))) {
    return NS_ERROR_FAILURE;
  }
  nsCOMPtr<nsIFile> linkedFile;
  MOZ_TRY(NS_NewLocalFile(nsDependentString(lpTemp), false,
                          getter_AddRefs(linkedFile)));
  return NS_NewFileURI(aURI, linkedFile);
#else
  return NS_ERROR_NOT_AVAILABLE;
#endif
}

NS_IMETHODIMP
nsFileProtocolHandler::GetScheme(nsACString& result) {
  result.AssignLiteral("file");
  return NS_OK;
}

NS_IMETHODIMP
nsFileProtocolHandler::NewChannel(nsIURI* uri, nsILoadInfo* aLoadInfo,
                                  nsIChannel** result) {
  nsresult rv;

  RefPtr<nsFileChannel> chan;
  if (IsNeckoChild()) {
    chan = new mozilla::net::FileChannelChild(uri);
  } else {
    chan = new nsFileChannel(uri);
  }

  // set the loadInfo on the new channel ; must do this
  // before calling Init() on it, since it needs the load
  // info be already set.
  rv = chan->SetLoadInfo(aLoadInfo);
  if (NS_FAILED(rv)) {
    return rv;
  }

  rv = chan->Init();
  if (NS_FAILED(rv)) {
    return rv;
  }

  chan.forget(result);
  return NS_OK;
}

NS_IMETHODIMP
nsFileProtocolHandler::AllowPort(int32_t port, const char* scheme,
                                 bool* result) {
  // don't override anything.
  *result = false;
  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsIFileProtocolHandler methods:

NS_IMETHODIMP
nsFileProtocolHandler::NewFileURI(nsIFile* aFile, nsIURI** aResult) {
  NS_ENSURE_ARG_POINTER(aFile);

  RefPtr<nsIFile> file(aFile);
  // NOTE: the origin charset is assigned the value of the platform
  // charset by the SetFile method.
  return NS_MutateURI(new nsStandardURL::Mutator())
      .Apply(&nsIFileURLMutator::SetFile, file)
      .Finalize(aResult);
}

NS_IMETHODIMP
nsFileProtocolHandler::NewFileURIMutator(nsIFile* aFile,
                                         nsIURIMutator** aResult) {
  NS_ENSURE_ARG_POINTER(aFile);
  nsresult rv;

  nsCOMPtr<nsIURIMutator> mutator = new nsStandardURL::Mutator();
  nsCOMPtr<nsIFileURLMutator> fileMutator = do_QueryInterface(mutator, &rv);
  if (NS_FAILED(rv)) {
    return rv;
  }

  // NOTE: the origin charset is assigned the value of the platform
  // charset by the SetFile method.
  rv = fileMutator->SetFile(aFile);
  if (NS_FAILED(rv)) {
    return rv;
  }

  mutator.forget(aResult);
  return NS_OK;
}

NS_IMETHODIMP
nsFileProtocolHandler::GetURLSpecFromFile(nsIFile* file, nsACString& result) {
  NS_ENSURE_ARG_POINTER(file);
  return net_GetURLSpecFromFile(file, result);
}

NS_IMETHODIMP
nsFileProtocolHandler::GetURLSpecFromActualFile(nsIFile* file,
                                                nsACString& result) {
  NS_ENSURE_ARG_POINTER(file);
  return net_GetURLSpecFromActualFile(file, result);
}

NS_IMETHODIMP
nsFileProtocolHandler::GetURLSpecFromDir(nsIFile* file, nsACString& result) {
  NS_ENSURE_ARG_POINTER(file);
  return net_GetURLSpecFromDir(file, result);
}

NS_IMETHODIMP
nsFileProtocolHandler::GetFileFromURLSpec(const nsACString& spec,
                                          nsIFile** result) {
  return net_GetFileFromURLSpec(spec, result);
}