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 ~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();
110 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
114 wxFileName::CreateTempFileName(wxT("wxhtml"));
116 { // now copy streams content to temporary file:
117 wxFileOutputStream
sout(tmpfile
);
122 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile
),
128 #endif // wxUSE_DATETIME
133 return (wxFSFile
*) NULL
; // incorrect URL
138 class wxFileSystemInternetModule
: public wxModule
140 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
143 virtual bool OnInit()
145 wxFileSystem::AddHandler(new wxInternetFSHandler
);
148 virtual void OnExit() {}
151 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
153 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET