1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HTTP and FTP file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
10 #pragma implementation "fs_inet.h"
13 #include "wx/wxprec.h"
21 #define wxUSE_FS_INET 0
24 #if wxUSE_FILESYSTEM && wxUSE_FS_INET
29 #include "wx/wfstream.h"
31 #include "wx/filesys.h"
32 #include "wx/fs_inet.h"
33 #include "wx/module.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // This stream deletes the file when destroyed
40 class wxTemporaryFileInputStream
: public wxFileInputStream
43 wxTemporaryFileInputStream(const wxString
& filename
) :
44 wxFileInputStream(filename
), m_filename(filename
) {}
46 ~wxTemporaryFileInputStream()
48 // NB: copied from wxFileInputStream dtor, we need to do it before
53 m_file_destroy
= false;
55 wxRemoveFile(m_filename
);
63 // ----------------------------------------------------------------------------
64 // wxInternetFSHandler
65 // ----------------------------------------------------------------------------
67 static wxString
StripProtocolAnchor(const wxString
& location
)
69 wxString
myloc(location
.BeforeLast(wxT('#')));
70 if (myloc
.empty()) myloc
= location
.AfterFirst(wxT(':'));
71 else myloc
= myloc
.AfterFirst(wxT(':'));
74 if (!myloc
.Left(2).IsSameAs(wxT("//")))
76 if (myloc
.GetChar(0) != wxT('/')) myloc
= wxT("//") + myloc
;
77 else myloc
= wxT("/") + myloc
;
79 if (myloc
.Mid(2).Find(wxT('/')) == wxNOT_FOUND
) myloc
<< wxT('/');
85 bool wxInternetFSHandler::CanOpen(const wxString
& location
)
88 wxString p
= GetProtocol(location
);
89 if ((p
== wxT("http")) || (p
== wxT("ftp")))
91 wxURL
url(p
+ wxT(":") + StripProtocolAnchor(location
));
92 return (url
.GetError() == wxURL_NOERR
);
99 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
),
100 const wxString
& location
)
106 GetProtocol(location
) + wxT(":") + StripProtocolAnchor(location
);
109 if (url
.GetError() == wxURL_NOERR
)
111 wxInputStream
*s
= url
.GetInputStream();
112 wxString content
= url
.GetProtocol().GetContentType();
113 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
117 wxFileName::CreateTempFileName(wxT("wxhtml"));
119 { // now copy streams content to temporary file:
120 wxFileOutputStream
sout(tmpfile
);
125 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile
),
131 #endif // wxUSE_DATETIME
136 return (wxFSFile
*) NULL
; // incorrect URL
141 class wxFileSystemInternetModule
: public wxModule
143 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
146 virtual bool OnInit()
148 wxFileSystem::AddHandler(new wxInternetFSHandler
);
151 virtual void OnExit() {}
154 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
156 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET