1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HTTP and FTP file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
13 This FS creates local cache (in /tmp directory). The cache is freed
16 Size of cache is limited to cca 1000 items (due to GetTempFileName
23 #pragma implementation
26 #include <wx/wxprec.h>
34 #define wxUSE_FS_INET 0
43 #include "wx/wfstream.h"
45 #include "wx/filesys.h"
46 #include "wx/fs_inet.h"
48 class wxInetCacheNode
: public wxObject
55 wxInetCacheNode(const wxString
& l
, const wxString
& m
) : wxObject() {m_Temp
= l
; m_Mime
= m
;}
56 const wxString
& GetTemp() const {return m_Temp
;}
57 const wxString
& GetMime() const {return m_Mime
;}
64 //--------------------------------------------------------------------------------
65 // wxInternetFSHandler
66 //--------------------------------------------------------------------------------
69 bool wxInternetFSHandler::CanOpen(const wxString
& location
)
71 wxString p
= GetProtocol(location
);
72 return (p
== wxT("http")) || (p
== wxT("ftp"));
76 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
78 wxString right
= GetProtocol(location
) + wxT(":") + GetRightLocation(location
);
81 wxInetCacheNode
*info
;
83 info
= (wxInetCacheNode
*) m_Cache
.Get(right
);
85 // Add item into cache:
89 s
= url
.GetInputStream();
90 content
= url
.GetProtocol().GetContentType();
91 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
96 wxGetTempFileName( wxT("wxhtml"), buf
);
97 info
= new wxInetCacheNode(buf
, content
);
98 m_Cache
.Put(right
, info
);
100 { // ok, now copy it:
101 wxFileOutputStream
sout((wxString
)buf
);
102 s
-> Read(sout
); // copy the stream
108 return (wxFSFile
*) NULL
; // we can't open the URL
112 // Load item from cache:
113 s
= new wxFileInputStream(info
->GetTemp());
116 return new wxFSFile(s
,
119 GetAnchor(location
));
121 else return (wxFSFile
*) NULL
;
126 wxInternetFSHandler::~wxInternetFSHandler()
132 while ((n
= m_Cache
.Next()) != NULL
)
134 n2
= (wxInetCacheNode
*) n
->GetData();
135 wxRemoveFile(n2
->GetTemp());
140 class wxFileSystemInternetModule
: public wxModule
142 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
145 virtual bool OnInit()
147 wxFileSystem::AddHandler(new wxInternetFSHandler
);
150 virtual void OnExit() {}
153 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
155 #endif // wxUSE_FS_INET