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"
47 #include "wx/module.h"
49 class wxInetCacheNode
: public wxObject
56 wxInetCacheNode(const wxString
& l
, const wxString
& m
) : wxObject() {m_Temp
= l
; m_Mime
= m
;}
57 const wxString
& GetTemp() const {return m_Temp
;}
58 const wxString
& GetMime() const {return m_Mime
;}
65 //--------------------------------------------------------------------------------
66 // wxInternetFSHandler
67 //--------------------------------------------------------------------------------
70 bool wxInternetFSHandler::CanOpen(const wxString
& location
)
72 wxString p
= GetProtocol(location
);
73 return (p
== wxT("http")) || (p
== wxT("ftp"));
77 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
79 wxString right
= GetProtocol(location
) + wxT(":") + GetRightLocation(location
);
82 wxInetCacheNode
*info
;
84 info
= (wxInetCacheNode
*) m_Cache
.Get(right
);
86 // Add item into cache:
90 s
= url
.GetInputStream();
91 content
= url
.GetProtocol().GetContentType();
92 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
97 wxGetTempFileName( wxT("wxhtml"), buf
);
98 info
= new wxInetCacheNode(buf
, content
);
99 m_Cache
.Put(right
, info
);
101 { // ok, now copy it:
102 wxFileOutputStream
sout((wxString
)buf
);
103 s
-> Read(sout
); // copy the stream
109 return (wxFSFile
*) NULL
; // we can't open the URL
113 // Load item from cache:
114 s
= new wxFileInputStream(info
->GetTemp());
117 return new wxFSFile(s
,
120 GetAnchor(location
));
122 else return (wxFSFile
*) NULL
;
127 wxInternetFSHandler::~wxInternetFSHandler()
133 while ((n
= m_Cache
.Next()) != NULL
)
135 n2
= (wxInetCacheNode
*) n
->GetData();
136 wxRemoveFile(n2
->GetTemp());
141 class wxFileSystemInternetModule
: public wxModule
143 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
146 virtual bool OnInit()
148 wxFileSystem::AddHandler(new wxInternetFSHandler
);
151 virtual void OnExit() {}
154 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
156 #endif // wxUSE_FS_INET