This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnsGnomeVFSProtocolHandler.cpp
988 lines (838 loc) · 29.7 KB
/
nsGnomeVFSProtocolHandler.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/* vim:set ts=2 sw=2 et cindent: */
/* 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/. */
// GnomeVFS v2.2.2 is missing G_BEGIN_DECLS in gnome-vfs-module-callback.h
extern "C" {
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
}
#include <algorithm>
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
#include "mozilla/ModuleUtils.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIObserver.h"
#include "nsThreadUtils.h"
#include "nsProxyRelease.h"
#include "nsIAuthPrompt.h"
#include "nsIStringBundle.h"
#include "nsIStandardURL.h"
#include "nsIURL.h"
#include "nsMimeTypes.h"
#include "nsNetUtil.h"
#include "nsINetUtil.h"
#include "nsAutoPtr.h"
#include "nsError.h"
#include "prlog.h"
#include "prtime.h"
#include "prprf.h"
#include "plstr.h"
#include "mozilla/Attributes.h"
#define MOZ_GNOMEVFS_SCHEME "moz-gnomevfs"
#define MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS "network.gnomevfs.supported-protocols"
//-----------------------------------------------------------------------------
// NSPR_LOG_MODULES=gnomevfs:5
#ifdef PR_LOGGING
static PRLogModuleInfo *sGnomeVFSLog;
#define LOG(args) PR_LOG(sGnomeVFSLog, PR_LOG_DEBUG, args)
#else
#define LOG(args)
#endif
//-----------------------------------------------------------------------------
static nsresult
MapGnomeVFSResult(GnomeVFSResult result)
{
switch (result)
{
case GNOME_VFS_OK: return NS_OK;
case GNOME_VFS_ERROR_NOT_FOUND: return NS_ERROR_FILE_NOT_FOUND;
case GNOME_VFS_ERROR_INTERNAL: return NS_ERROR_UNEXPECTED;
case GNOME_VFS_ERROR_BAD_PARAMETERS: return NS_ERROR_INVALID_ARG;
case GNOME_VFS_ERROR_NOT_SUPPORTED: return NS_ERROR_NOT_AVAILABLE;
case GNOME_VFS_ERROR_CORRUPTED_DATA: return NS_ERROR_FILE_CORRUPTED;
case GNOME_VFS_ERROR_TOO_BIG: return NS_ERROR_FILE_TOO_BIG;
case GNOME_VFS_ERROR_NO_SPACE: return NS_ERROR_FILE_NO_DEVICE_SPACE;
case GNOME_VFS_ERROR_READ_ONLY:
case GNOME_VFS_ERROR_READ_ONLY_FILE_SYSTEM: return NS_ERROR_FILE_READ_ONLY;
case GNOME_VFS_ERROR_INVALID_URI:
case GNOME_VFS_ERROR_INVALID_HOST_NAME: return NS_ERROR_MALFORMED_URI;
case GNOME_VFS_ERROR_ACCESS_DENIED:
case GNOME_VFS_ERROR_NOT_PERMITTED:
case GNOME_VFS_ERROR_LOGIN_FAILED: return NS_ERROR_FILE_ACCESS_DENIED;
case GNOME_VFS_ERROR_EOF: return NS_BASE_STREAM_CLOSED;
case GNOME_VFS_ERROR_NOT_A_DIRECTORY: return NS_ERROR_FILE_NOT_DIRECTORY;
case GNOME_VFS_ERROR_IN_PROGRESS: return NS_ERROR_IN_PROGRESS;
case GNOME_VFS_ERROR_FILE_EXISTS: return NS_ERROR_FILE_ALREADY_EXISTS;
case GNOME_VFS_ERROR_IS_DIRECTORY: return NS_ERROR_FILE_IS_DIRECTORY;
case GNOME_VFS_ERROR_NO_MEMORY: return NS_ERROR_OUT_OF_MEMORY;
case GNOME_VFS_ERROR_HOST_NOT_FOUND:
case GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS: return NS_ERROR_UNKNOWN_HOST;
case GNOME_VFS_ERROR_CANCELLED:
case GNOME_VFS_ERROR_INTERRUPTED: return NS_ERROR_ABORT;
case GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY: return NS_ERROR_FILE_DIR_NOT_EMPTY;
case GNOME_VFS_ERROR_NAME_TOO_LONG: return NS_ERROR_FILE_NAME_TOO_LONG;
case GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE: return NS_ERROR_UNKNOWN_PROTOCOL;
/* No special mapping for these error codes...
case GNOME_VFS_ERROR_GENERIC:
case GNOME_VFS_ERROR_IO:
case GNOME_VFS_ERROR_WRONG_FORMAT:
case GNOME_VFS_ERROR_BAD_FILE:
case GNOME_VFS_ERROR_NOT_OPEN:
case GNOME_VFS_ERROR_INVALID_OPEN_MODE:
case GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES:
case GNOME_VFS_ERROR_LOOP:
case GNOME_VFS_ERROR_DIRECTORY_BUSY:
case GNOME_VFS_ERROR_TOO_MANY_LINKS:
case GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM:
case GNOME_VFS_ERROR_SERVICE_OBSOLETE:
case GNOME_VFS_ERROR_PROTOCOL_ERROR:
case GNOME_VFS_ERROR_NO_MASTER_BROWSER:
*/
// Make GCC happy
default:
return NS_ERROR_FAILURE;
}
return NS_ERROR_FAILURE;
}
//-----------------------------------------------------------------------------
static void
ProxiedAuthCallback(gconstpointer in,
gsize in_size,
gpointer out,
gsize out_size,
gpointer callback_data)
{
GnomeVFSModuleCallbackAuthenticationIn *authIn =
(GnomeVFSModuleCallbackAuthenticationIn *) in;
GnomeVFSModuleCallbackAuthenticationOut *authOut =
(GnomeVFSModuleCallbackAuthenticationOut *) out;
LOG(("gnomevfs: ProxiedAuthCallback [uri=%s]\n", authIn->uri));
// Without a channel, we have no way of getting a prompter.
nsIChannel *channel = (nsIChannel *) callback_data;
if (!channel)
return;
nsCOMPtr<nsIAuthPrompt> prompt;
NS_QueryNotificationCallbacks(channel, prompt);
// If no auth prompt, then give up. We could failover to using the
// WindowWatcher service, but that might defeat a consumer's purposeful
// attempt to disable authentication (for whatever reason).
if (!prompt)
return;
// Parse out the host and port...
nsCOMPtr<nsIURI> uri;
channel->GetURI(getter_AddRefs(uri));
if (!uri)
return;
#ifdef DEBUG
{
//
// Make sure authIn->uri is consistent with the channel's URI.
//
// XXX This check is probably not IDN safe, and it might incorrectly
// fire as a result of escaping differences. It's unclear what
// kind of transforms GnomeVFS might have applied to the URI spec
// that we originally gave to it. In spite of the likelihood of
// false hits, this check is probably still valuable.
//
nsAutoCString spec;
uri->GetSpec(spec);
int uriLen = strlen(authIn->uri);
if (!StringHead(spec, uriLen).Equals(nsDependentCString(authIn->uri, uriLen)))
{
LOG(("gnomevfs: [spec=%s authIn->uri=%s]\n", spec.get(), authIn->uri));
NS_ERROR("URI mismatch");
}
}
#endif
nsAutoCString scheme, hostPort;
uri->GetScheme(scheme);
uri->GetHostPort(hostPort);
// It doesn't make sense for either of these strings to be empty. What kind
// of funky URI is this?
if (scheme.IsEmpty() || hostPort.IsEmpty())
return;
// Construct the single signon key. Altering the value of this key will
// cause people's remembered passwords to be forgotten. Think carefully
// before changing the way this key is constructed.
nsAutoString key, realm;
NS_ConvertUTF8toUTF16 dispHost(scheme);
dispHost.AppendLiteral("://");
dispHost.Append(NS_ConvertUTF8toUTF16(hostPort));
key = dispHost;
if (authIn->realm)
{
// We assume the realm string is ASCII. That might be a bogus assumption,
// but we have no idea what encoding GnomeVFS is using, so for now we'll
// limit ourselves to ISO-Latin-1. XXX What is a better solution?
realm.Append('"');
realm.Append(NS_ConvertASCIItoUTF16(authIn->realm));
realm.Append('"');
key.Append(' ');
key.Append(realm);
}
// Construct the message string...
//
// We use Necko's string bundle here. This code really should be encapsulated
// behind some Necko API, after all this code is based closely on the code in
// nsHttpChannel.cpp.
nsCOMPtr<nsIStringBundleService> bundleSvc =
do_GetService(NS_STRINGBUNDLE_CONTRACTID);
if (!bundleSvc)
return;
nsCOMPtr<nsIStringBundle> bundle;
bundleSvc->CreateBundle("chrome://global/locale/commonDialogs.properties",
getter_AddRefs(bundle));
if (!bundle)
return;
nsString message;
if (!realm.IsEmpty())
{
const char16_t *strings[] = { realm.get(), dispHost.get() };
bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordForRealm"),
strings, 2, getter_Copies(message));
}
else
{
const char16_t *strings[] = { dispHost.get() };
bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordFor"),
strings, 1, getter_Copies(message));
}
if (message.IsEmpty())
return;
// Prompt the user...
nsresult rv;
bool retval = false;
char16_t *user = nullptr, *pass = nullptr;
rv = prompt->PromptUsernameAndPassword(nullptr, message.get(),
key.get(),
nsIAuthPrompt::SAVE_PASSWORD_PERMANENTLY,
&user, &pass, &retval);
if (NS_FAILED(rv))
return;
if (!retval || !user || !pass)
return;
// XXX We need to convert the UTF-16 username and password from our dialog to
// strings that GnomeVFS can understand. It's unclear what encoding GnomeVFS
// expects, so for now we assume 7-bit ASCII. Hopefully, we can get a better
// solution at some point.
// One copy is never enough...
authOut->username = g_strdup(NS_LossyConvertUTF16toASCII(user).get());
authOut->password = g_strdup(NS_LossyConvertUTF16toASCII(pass).get());
nsMemory::Free(user);
nsMemory::Free(pass);
}
struct nsGnomeVFSAuthCallbackEvent : public nsRunnable
{
gconstpointer in;
gsize in_size;
gpointer out;
gsize out_size;
gpointer callback_data;
NS_IMETHOD Run() {
ProxiedAuthCallback(in, in_size, out, out_size, callback_data);
return NS_OK;
}
};
static void
AuthCallback(gconstpointer in,
gsize in_size,
gpointer out,
gsize out_size,
gpointer callback_data)
{
// Need to proxy this callback over to the main thread. Synchronous dispatch
// is required in order to provide data to the GnomeVFS callback.
nsRefPtr<nsGnomeVFSAuthCallbackEvent> ev = new nsGnomeVFSAuthCallbackEvent();
if (!ev)
return; // OOM
ev->in = in;
ev->in_size = in_size;
ev->out = out;
ev->out_size = out_size;
ev->callback_data = callback_data;
NS_DispatchToMainThread(ev, NS_DISPATCH_SYNC);
}
//-----------------------------------------------------------------------------
static gint
FileInfoComparator(gconstpointer a, gconstpointer b)
{
const GnomeVFSFileInfo *ia = (const GnomeVFSFileInfo *) a;
const GnomeVFSFileInfo *ib = (const GnomeVFSFileInfo *) b;
return strcasecmp(ia->name, ib->name);
}
//-----------------------------------------------------------------------------
class nsGnomeVFSInputStream MOZ_FINAL : public nsIInputStream
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINPUTSTREAM
nsGnomeVFSInputStream(const nsCString &uriSpec)
: mSpec(uriSpec)
, mChannel(nullptr)
, mHandle(nullptr)
, mBytesRemaining(UINT64_MAX)
, mStatus(NS_OK)
, mDirList(nullptr)
, mDirListPtr(nullptr)
, mDirBufCursor(0)
, mDirOpen(false) {}
~nsGnomeVFSInputStream() { Close(); }
void SetChannel(nsIChannel *channel)
{
// We need to hold an owning reference to our channel. This is done
// so we can access the channel's notification callbacks to acquire
// a reference to a nsIAuthPrompt if we need to handle a GnomeVFS
// authentication callback.
//
// However, the channel can only be accessed on the main thread, so
// we have to be very careful with ownership. Moreover, it doesn't
// support threadsafe addref/release, so proxying is the answer.
//
// Also, it's important to note that this likely creates a reference
// cycle since the channel likely owns this stream. This reference
// cycle is broken in our Close method.
NS_ADDREF(mChannel = channel);
}
private:
GnomeVFSResult DoOpen();
GnomeVFSResult DoRead(char *aBuf, uint32_t aCount, uint32_t *aCountRead);
nsresult SetContentTypeOfChannel(const char *contentType);
private:
nsCString mSpec;
nsIChannel *mChannel; // manually refcounted
GnomeVFSHandle *mHandle;
uint64_t mBytesRemaining;
nsresult mStatus;
GList *mDirList;
GList *mDirListPtr;
nsCString mDirBuf;
uint32_t mDirBufCursor;
bool mDirOpen;
};
GnomeVFSResult
nsGnomeVFSInputStream::DoOpen()
{
GnomeVFSResult rv;
NS_ASSERTION(mHandle == nullptr, "already open");
// Push a callback handler on the stack for this thread, so we can intercept
// authentication requests from GnomeVFS. We'll use the channel to get a
// nsIAuthPrompt instance.
gnome_vfs_module_callback_push(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
AuthCallback, mChannel, nullptr);
// Query the mime type first (this could return nullptr).
//
// XXX We need to do this up-front in order to determine how to open the URI.
// Unfortunately, the error code GNOME_VFS_ERROR_IS_DIRECTORY is not
// always returned by gnome_vfs_open when we pass it a URI to a directory!
// Otherwise, we could have used that as a way to failover to opening the
// URI as a directory. Also, it would have been ideal if
// gnome_vfs_get_file_info_from_handle were actually implemented by the
// smb:// module, since that would have allowed us to potentially save a
// round trip to the server to discover the mime type of the document in
// the case where gnome_vfs_open would have been used. (Oh well! /me
// throws hands up in the air and moves on...)
GnomeVFSFileInfo info = {0};
rv = gnome_vfs_get_file_info(mSpec.get(), &info, GnomeVFSFileInfoOptions(
GNOME_VFS_FILE_INFO_DEFAULT |
GNOME_VFS_FILE_INFO_FOLLOW_LINKS));
if (rv == GNOME_VFS_OK)
{
if (info.type == GNOME_VFS_FILE_TYPE_DIRECTORY)
{
rv = gnome_vfs_directory_list_load(&mDirList, mSpec.get(),
GNOME_VFS_FILE_INFO_DEFAULT);
LOG(("gnomevfs: gnome_vfs_directory_list_load returned %d (%s) [spec=\"%s\"]\n",
rv, gnome_vfs_result_to_string(rv), mSpec.get()));
}
else
{
rv = gnome_vfs_open(&mHandle, mSpec.get(), GNOME_VFS_OPEN_READ);
LOG(("gnomevfs: gnome_vfs_open returned %d (%s) [spec=\"%s\"]\n",
rv, gnome_vfs_result_to_string(rv), mSpec.get()));
}
}
gnome_vfs_module_callback_pop(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION);
if (rv == GNOME_VFS_OK)
{
if (mHandle)
{
// Here we set the content type of the channel to the value of the mime
// type determined by GnomeVFS. However, if GnomeVFS is telling us that
// the document is binary, we'll ignore that and keep the channel's
// content type unspecified. That will enable our content type sniffing
// algorithms. This should provide more consistent mime type handling.
if (info.mime_type && (strcmp(info.mime_type, APPLICATION_OCTET_STREAM) != 0))
SetContentTypeOfChannel(info.mime_type);
mBytesRemaining = info.size;
// Update the content length attribute on the channel. We do this
// synchronously without proxying. This hack is not as bad as it looks!
if (mBytesRemaining > INT64_MAX) {
mChannel->SetContentLength(-1);
} else {
mChannel->SetContentLength(mBytesRemaining);
}
}
else
{
mDirOpen = true;
// Sort mDirList
mDirList = g_list_sort(mDirList, FileInfoComparator);
mDirListPtr = mDirList;
// Write base URL (make sure it ends with a '/')
mDirBuf.AppendLiteral("300: ");
mDirBuf.Append(mSpec);
if (mSpec.get()[mSpec.Length() - 1] != '/')
mDirBuf.Append('/');
mDirBuf.Append('\n');
// Write column names
mDirBuf.AppendLiteral("200: filename content-length last-modified file-type\n");
// Write charset (assume UTF-8)
// XXX is this correct?
mDirBuf.AppendLiteral("301: UTF-8\n");
SetContentTypeOfChannel(APPLICATION_HTTP_INDEX_FORMAT);
}
}
gnome_vfs_file_info_clear(&info);
return rv;
}
GnomeVFSResult
nsGnomeVFSInputStream::DoRead(char *aBuf, uint32_t aCount, uint32_t *aCountRead)
{
GnomeVFSResult rv;
if (mHandle)
{
GnomeVFSFileSize bytesRead;
rv = gnome_vfs_read(mHandle, aBuf, aCount, &bytesRead);
if (rv == GNOME_VFS_OK)
{
// aCount is 32-bit, so aCountRead is under 32-bit value.
*aCountRead = (uint32_t) bytesRead;
mBytesRemaining -= *aCountRead;
}
}
else if (mDirOpen)
{
rv = GNOME_VFS_OK;
while (aCount && rv != GNOME_VFS_ERROR_EOF)
{
// Copy data out of our buffer
uint32_t bufLen = mDirBuf.Length() - mDirBufCursor;
if (bufLen)
{
uint32_t n = std::min(bufLen, aCount);
memcpy(aBuf, mDirBuf.get() + mDirBufCursor, n);
*aCountRead += n;
aBuf += n;
aCount -= n;
mDirBufCursor += n;
}
if (!mDirListPtr) // Are we at the end of the directory list?
{
rv = GNOME_VFS_ERROR_EOF;
}
else if (aCount) // Do we need more data?
{
GnomeVFSFileInfo *info = (GnomeVFSFileInfo *) mDirListPtr->data;
// Prune '.' and '..' from directory listing.
if (info->name[0] == '.' &&
(info->name[1] == '\0' ||
(info->name[1] == '.' && info->name[2] == '\0')))
{
mDirListPtr = mDirListPtr->next;
continue;
}
mDirBuf.AssignLiteral("201: ");
// The "filename" field
nsCString escName;
nsCOMPtr<nsINetUtil> nu = do_GetService(NS_NETUTIL_CONTRACTID);
if (nu) {
nu->EscapeString(nsDependentCString(info->name),
nsINetUtil::ESCAPE_URL_PATH, escName);
mDirBuf.Append(escName);
mDirBuf.Append(' ');
}
// The "content-length" field
// XXX truncates size from 64-bit to 32-bit
mDirBuf.AppendInt(int32_t(info->size));
mDirBuf.Append(' ');
// The "last-modified" field
//
// NSPR promises: PRTime is compatible with time_t
// we just need to convert from seconds to microseconds
PRExplodedTime tm;
PRTime pt = ((PRTime) info->mtime) * 1000000;
PR_ExplodeTime(pt, PR_GMTParameters, &tm);
{
char buf[64];
PR_FormatTimeUSEnglish(buf, sizeof(buf),
"%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm);
mDirBuf.Append(buf);
}
// The "file-type" field
switch (info->type)
{
case GNOME_VFS_FILE_TYPE_REGULAR:
mDirBuf.AppendLiteral("FILE ");
break;
case GNOME_VFS_FILE_TYPE_DIRECTORY:
mDirBuf.AppendLiteral("DIRECTORY ");
break;
case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
mDirBuf.AppendLiteral("SYMBOLIC-LINK ");
break;
default:
break;
}
mDirBuf.Append('\n');
mDirBufCursor = 0;
mDirListPtr = mDirListPtr->next;
}
}
}
else
{
NS_NOTREACHED("reading from what?");
rv = GNOME_VFS_ERROR_GENERIC;
}
return rv;
}
// This class is used to implement SetContentTypeOfChannel.
class nsGnomeVFSSetContentTypeEvent : public nsRunnable
{
public:
nsGnomeVFSSetContentTypeEvent(nsIChannel *channel, const char *contentType)
: mChannel(channel), mContentType(contentType)
{
// stash channel reference in mChannel. no AddRef here! see note
// in SetContentTypeOfchannel.
}
NS_IMETHOD Run()
{
mChannel->SetContentType(mContentType);
return NS_OK;
}
private:
nsIChannel *mChannel;
nsCString mContentType;
};
nsresult
nsGnomeVFSInputStream::SetContentTypeOfChannel(const char *contentType)
{
// We need to proxy this call over to the main thread. We post an
// asynchronous event in this case so that we don't delay reading data, and
// we know that this is safe to do since the channel's reference will be
// released asynchronously as well. We trust the ordering of the main
// thread's event queue to protect us against memory corruption.
nsresult rv;
nsCOMPtr<nsIRunnable> ev =
new nsGnomeVFSSetContentTypeEvent(mChannel, contentType);
if (!ev)
{
rv = NS_ERROR_OUT_OF_MEMORY;
}
else
{
rv = NS_DispatchToMainThread(ev);
}
return rv;
}
NS_IMPL_ISUPPORTS(nsGnomeVFSInputStream, nsIInputStream)
NS_IMETHODIMP
nsGnomeVFSInputStream::Close()
{
if (mHandle)
{
gnome_vfs_close(mHandle);
mHandle = nullptr;
}
if (mDirList)
{
// Destroy the list of GnomeVFSFileInfo objects...
g_list_foreach(mDirList, (GFunc) gnome_vfs_file_info_unref, nullptr);
g_list_free(mDirList);
mDirList = nullptr;
mDirListPtr = nullptr;
}
if (mChannel)
{
nsresult rv = NS_OK;
nsCOMPtr<nsIThread> thread = do_GetMainThread();
if (thread)
rv = NS_ProxyRelease(thread, mChannel);
NS_ASSERTION(thread && NS_SUCCEEDED(rv), "leaking channel reference");
mChannel = nullptr;
}
mSpec.Truncate(); // free memory
// Prevent future reads from re-opening the handle.
if (NS_SUCCEEDED(mStatus))
mStatus = NS_BASE_STREAM_CLOSED;
return NS_OK;
}
NS_IMETHODIMP
nsGnomeVFSInputStream::Available(uint64_t *aResult)
{
if (NS_FAILED(mStatus))
return mStatus;
*aResult = mBytesRemaining;
return NS_OK;
}
NS_IMETHODIMP
nsGnomeVFSInputStream::Read(char *aBuf,
uint32_t aCount,
uint32_t *aCountRead)
{
*aCountRead = 0;
if (mStatus == NS_BASE_STREAM_CLOSED)
return NS_OK;
if (NS_FAILED(mStatus))
return mStatus;
GnomeVFSResult rv = GNOME_VFS_OK;
// If this is our first-time through here, then open the URI.
if (!mHandle && !mDirOpen)
rv = DoOpen();
if (rv == GNOME_VFS_OK)
rv = DoRead(aBuf, aCount, aCountRead);
if (rv != GNOME_VFS_OK)
{
// If we reach here, we hit some kind of error. EOF is not an error.
mStatus = MapGnomeVFSResult(rv);
if (mStatus == NS_BASE_STREAM_CLOSED)
return NS_OK;
LOG(("gnomevfs: result %d [%s] mapped to 0x%x\n",
rv, gnome_vfs_result_to_string(rv), mStatus));
}
return mStatus;
}
NS_IMETHODIMP
nsGnomeVFSInputStream::ReadSegments(nsWriteSegmentFun aWriter,
void *aClosure,
uint32_t aCount,
uint32_t *aResult)
{
// There is no way to implement this using GnomeVFS, but fortunately
// that doesn't matter. Because we are a blocking input stream, Necko
// isn't going to call our ReadSegments method.
NS_NOTREACHED("nsGnomeVFSInputStream::ReadSegments");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsGnomeVFSInputStream::IsNonBlocking(bool *aResult)
{
*aResult = false;
return NS_OK;
}
//-----------------------------------------------------------------------------
class nsGnomeVFSProtocolHandler MOZ_FINAL : public nsIProtocolHandler
, public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIPROTOCOLHANDLER
NS_DECL_NSIOBSERVER
nsresult Init();
private:
void InitSupportedProtocolsPref(nsIPrefBranch *prefs);
bool IsSupportedProtocol(const nsCString &spec);
nsCString mSupportedProtocols;
};
NS_IMPL_ISUPPORTS(nsGnomeVFSProtocolHandler, nsIProtocolHandler, nsIObserver)
nsresult
nsGnomeVFSProtocolHandler::Init()
{
#ifdef PR_LOGGING
sGnomeVFSLog = PR_NewLogModule("gnomevfs");
#endif
if (!gnome_vfs_initialized())
{
if (!gnome_vfs_init())
{
NS_WARNING("gnome_vfs_init failed");
return NS_ERROR_UNEXPECTED;
}
}
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (prefs)
{
InitSupportedProtocolsPref(prefs);
prefs->AddObserver(MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS, this, false);
}
return NS_OK;
}
void
nsGnomeVFSProtocolHandler::InitSupportedProtocolsPref(nsIPrefBranch *prefs)
{
// read preferences
nsresult rv = prefs->GetCharPref(MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS,
getter_Copies(mSupportedProtocols));
if (NS_SUCCEEDED(rv)) {
mSupportedProtocols.StripWhitespace();
ToLowerCase(mSupportedProtocols);
}
else
mSupportedProtocols.AssignLiteral("smb:,sftp:"); // use defaults
LOG(("gnomevfs: supported protocols \"%s\"\n", mSupportedProtocols.get()));
}
bool
nsGnomeVFSProtocolHandler::IsSupportedProtocol(const nsCString &aSpec)
{
const char *specString = aSpec.get();
const char *colon = strchr(specString, ':');
if (!colon)
return false;
uint32_t length = colon - specString + 1;
// <scheme> + ':'
nsCString scheme(specString, length);
char *found = PL_strcasestr(mSupportedProtocols.get(), scheme.get());
if (!found)
return false;
if (found[length] != ',' && found[length] != '\0')
return false;
return true;
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::GetScheme(nsACString &aScheme)
{
aScheme.Assign(MOZ_GNOMEVFS_SCHEME);
return NS_OK;
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::GetDefaultPort(int32_t *aDefaultPort)
{
*aDefaultPort = -1;
return NS_OK;
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::GetProtocolFlags(uint32_t *aProtocolFlags)
{
// Is URI_STD true of all GnomeVFS URI types?
*aProtocolFlags = URI_STD | URI_DANGEROUS_TO_LOAD;
return NS_OK;
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::NewURI(const nsACString &aSpec,
const char *aOriginCharset,
nsIURI *aBaseURI,
nsIURI **aResult)
{
const nsCString flatSpec(aSpec);
LOG(("gnomevfs: NewURI [spec=%s]\n", flatSpec.get()));
if (!aBaseURI)
{
//
// XXX This check is used to limit the gnome-vfs protocols we support. For
// security reasons, it is best that we limit the protocols we support to
// those with known characteristics. We might want to lessen this
// restriction if it proves to be too heavy handed. A black list of
// protocols we don't want to support might be better. For example, we
// probably don't want to try to load "start-here:" inside the browser.
// There are others that fall into this category, which are best handled
// externally by Nautilus (or another app like it).
//
if (!IsSupportedProtocol(flatSpec))
return NS_ERROR_UNKNOWN_PROTOCOL;
// Verify that GnomeVFS supports this URI scheme.
GnomeVFSURI *uri = gnome_vfs_uri_new(flatSpec.get());
if (!uri)
return NS_ERROR_UNKNOWN_PROTOCOL;
}
//
// XXX Can we really assume that all gnome-vfs URIs can be parsed using
// nsStandardURL? We probably really need to implement nsIURI/nsIURL
// in terms of the gnome_vfs_uri_XXX methods, but at least this works
// correctly for smb:// URLs ;-)
//
// Also, it might not be possible to fully implement nsIURI/nsIURL in
// terms of GnomeVFSURI since some Necko methods have no GnomeVFS
// equivalent.
//
nsresult rv;
nsCOMPtr<nsIStandardURL> url =
do_CreateInstance(NS_STANDARDURL_CONTRACTID, &rv);
if (NS_FAILED(rv))
return rv;
rv = url->Init(nsIStandardURL::URLTYPE_STANDARD, -1, flatSpec,
aOriginCharset, aBaseURI);
if (NS_SUCCEEDED(rv))
rv = CallQueryInterface(url, aResult);
return rv;
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::NewChannel2(nsIURI* aURI,
nsILoadInfo* aLoadInfo,
nsIChannel** aResult)
{
NS_ENSURE_ARG_POINTER(aURI);
nsresult rv;
nsAutoCString spec;
rv = aURI->GetSpec(spec);
if (NS_FAILED(rv))
return rv;
nsRefPtr<nsGnomeVFSInputStream> stream = new nsGnomeVFSInputStream(spec);
if (!stream)
{
rv = NS_ERROR_OUT_OF_MEMORY;
}
else
{
// start out assuming an unknown content-type. we'll set the content-type
// to something better once we open the URI.
rv = NS_NewInputStreamChannel(aResult, aURI, stream,
NS_LITERAL_CSTRING(UNKNOWN_CONTENT_TYPE));
if (NS_SUCCEEDED(rv))
stream->SetChannel(*aResult);
}
return rv;
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::NewChannel(nsIURI *aURI, nsIChannel **aResult)
{
return NewChannel2(aURI, nullptr, aResult);
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::AllowPort(int32_t aPort,
const char *aScheme,
bool *aResult)
{
// Don't override anything.
*aResult = false;
return NS_OK;
}
NS_IMETHODIMP
nsGnomeVFSProtocolHandler::Observe(nsISupports *aSubject,
const char *aTopic,
const char16_t *aData)
{
if (strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0) {
nsCOMPtr<nsIPrefBranch> prefs = do_QueryInterface(aSubject);
InitSupportedProtocolsPref(prefs);
}
return NS_OK;
}
//-----------------------------------------------------------------------------
#define NS_GNOMEVFSPROTOCOLHANDLER_CID \
{ /* 9b6dc177-a2e4-49e1-9c98-0a8384de7f6c */ \
0x9b6dc177, \
0xa2e4, \
0x49e1, \
{0x9c, 0x98, 0x0a, 0x83, 0x84, 0xde, 0x7f, 0x6c} \
}
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsGnomeVFSProtocolHandler, Init)
NS_DEFINE_NAMED_CID(NS_GNOMEVFSPROTOCOLHANDLER_CID);
static const mozilla::Module::CIDEntry kVFSCIDs[] = {
{ &kNS_GNOMEVFSPROTOCOLHANDLER_CID, false, nullptr, nsGnomeVFSProtocolHandlerConstructor },
{ nullptr }
};
static const mozilla::Module::ContractIDEntry kVFSContracts[] = {
{ NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX MOZ_GNOMEVFS_SCHEME, &kNS_GNOMEVFSPROTOCOLHANDLER_CID },
{ nullptr }
};
static const mozilla::Module kVFSModule = {
mozilla::Module::kVersion,
kVFSCIDs,
kVFSContracts
};
NSMODULE_DEFN(nsGnomeVFSModule) = &kVFSModule;