1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fs_inet.cpp
3 // Purpose: HTTP and FTP file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
18 #define wxUSE_FS_INET 0
21 #if wxUSE_FILESYSTEM && wxUSE_FS_INET
24 #include "wx/module.h"
27 #include "wx/wfstream.h"
29 #include "wx/filesys.h"
30 #include "wx/fs_inet.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // This stream deletes the file when destroyed
37 class wxTemporaryFileInputStream
: public wxFileInputStream
40 wxTemporaryFileInputStream(const wxString
& filename
) :
41 wxFileInputStream(filename
), m_filename(filename
) {}
43 virtual ~wxTemporaryFileInputStream()
45 // NB: copied from wxFileInputStream dtor, we need to do it before
50 m_file_destroy
= false;
52 wxRemoveFile(m_filename
);
60 // ----------------------------------------------------------------------------
61 // wxInternetFSHandler
62 // ----------------------------------------------------------------------------
64 static wxString
StripProtocolAnchor(const wxString
& location
)
66 wxString
myloc(location
.BeforeLast(wxT('#')));
67 if (myloc
.empty()) myloc
= location
.AfterFirst(wxT(':'));
68 else myloc
= myloc
.AfterFirst(wxT(':'));
71 if (!myloc
.Left(2).IsSameAs(wxT("//")))
73 if (myloc
.GetChar(0) != wxT('/')) myloc
= wxT("//") + myloc
;
74 else myloc
= wxT("/") + myloc
;
76 if (myloc
.Mid(2).Find(wxT('/')) == wxNOT_FOUND
) myloc
<< wxT('/');
82 bool wxInternetFSHandler::CanOpen(const wxString
& location
)
85 wxString p
= GetProtocol(location
);
86 if ((p
== wxT("http")) || (p
== wxT("ftp")))
88 wxURL
url(p
+ wxT(":") + StripProtocolAnchor(location
));
89 return (url
.GetError() == wxURL_NOERR
);
96 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
),
97 const wxString
& location
)
103 GetProtocol(location
) + wxT(":") + StripProtocolAnchor(location
);
106 if (url
.GetError() == wxURL_NOERR
)
108 wxInputStream
*s
= url
.GetInputStream();
109 wxString content
= url
.GetProtocol().GetContentType();
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 NULL
; // incorrect URL
137 class wxFileSystemInternetModule
: public wxModule
139 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
142 wxFileSystemInternetModule() :
148 virtual bool OnInit()
150 m_handler
= new wxInternetFSHandler
;
151 wxFileSystem::AddHandler(m_handler
);
155 virtual void OnExit()
157 delete wxFileSystem::RemoveHandler(m_handler
);
161 wxFileSystemHandler
* m_handler
;
164 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
166 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET