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
== _T("http")) || (p
== _T("ftp"));
76 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
78 wxString right
= GetProtocol(location
) + _T(":") + 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( _T("wxhtml"), buf
);
97 info
= new wxInetCacheNode(buf
, content
);
98 m_Cache
.Put(right
, info
);
100 { // ok, now copy it:
101 #if defined(__VISAGECPP__)
102 // VA thinks this is an ambiguous call
103 wxFileOutputStream
sout((wxString
)buf
);
105 wxFileOutputStream
sout(buf
);
107 s
-> Read(sout
); // copy the stream
113 return (wxFSFile
*) NULL
; // we can't open the URL
117 // Load item from cache:
118 s
= new wxFileInputStream(info
->GetTemp());
121 return new wxFSFile(s
,
124 GetAnchor(location
));
126 else return (wxFSFile
*) NULL
;
131 wxInternetFSHandler::~wxInternetFSHandler()
137 while ((n
= m_Cache
.Next()) != NULL
)
139 n2
= (wxInetCacheNode
*) n
->GetData();
140 wxRemoveFile(n2
->GetTemp());
145 class wxFileSystemInternetModule
: public wxModule
147 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
150 virtual bool OnInit()
152 wxFileSystem::AddHandler(new wxInternetFSHandler
);
155 virtual void OnExit() {}
158 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
160 #endif // wxUSE_FS_INET