1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HTTP and FTP file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
17 #define wxUSE_FS_INET 0
20 #if wxUSE_FILESYSTEM && wxUSE_FS_INET
25 #include "wx/wfstream.h"
27 #include "wx/filesys.h"
28 #include "wx/fs_inet.h"
29 #include "wx/module.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // This stream deletes the file when destroyed
36 class wxTemporaryFileInputStream
: public wxFileInputStream
39 wxTemporaryFileInputStream(const wxString
& filename
) :
40 wxFileInputStream(filename
), m_filename(filename
) {}
42 ~wxTemporaryFileInputStream()
44 // NB: copied from wxFileInputStream dtor, we need to do it before
49 m_file_destroy
= false;
51 wxRemoveFile(m_filename
);
59 // ----------------------------------------------------------------------------
60 // wxInternetFSHandler
61 // ----------------------------------------------------------------------------
63 static wxString
StripProtocolAnchor(const wxString
& location
)
65 wxString
myloc(location
.BeforeLast(wxT('#')));
66 if (myloc
.empty()) myloc
= location
.AfterFirst(wxT(':'));
67 else myloc
= myloc
.AfterFirst(wxT(':'));
70 if (!myloc
.Left(2).IsSameAs(wxT("//")))
72 if (myloc
.GetChar(0) != wxT('/')) myloc
= wxT("//") + myloc
;
73 else myloc
= wxT("/") + myloc
;
75 if (myloc
.Mid(2).Find(wxT('/')) == wxNOT_FOUND
) myloc
<< wxT('/');
81 bool wxInternetFSHandler::CanOpen(const wxString
& location
)
84 wxString p
= GetProtocol(location
);
85 if ((p
== wxT("http")) || (p
== wxT("ftp")))
87 wxURL
url(p
+ wxT(":") + StripProtocolAnchor(location
));
88 return (url
.GetError() == wxURL_NOERR
);
95 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
),
96 const wxString
& location
)
102 GetProtocol(location
) + wxT(":") + StripProtocolAnchor(location
);
105 if (url
.GetError() == wxURL_NOERR
)
107 wxInputStream
*s
= url
.GetInputStream();
108 wxString content
= url
.GetProtocol().GetContentType();
109 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
113 wxFileName::CreateTempFileName(wxT("wxhtml"));
115 { // now copy streams content to temporary file:
116 wxFileOutputStream
sout(tmpfile
);
121 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile
),
127 #endif // wxUSE_DATETIME
132 return (wxFSFile
*) NULL
; // incorrect URL
137 class wxFileSystemInternetModule
: public wxModule
139 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
142 virtual bool OnInit()
144 wxFileSystem::AddHandler(new wxInternetFSHandler
);
147 virtual void OnExit() {}
150 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
152 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET