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 if ((p
== wxT("http")) || (p
== wxT("ftp")))
75 wxURL
url(GetProtocol(location
) + wxT(":") + GetRightLocation(location
));
76 return (url
.GetError() == wxURL_NOERR
);
83 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
85 wxString right
= GetProtocol(location
) + wxT(":") + GetRightLocation(location
);
88 wxInetCacheNode
*info
;
90 info
= (wxInetCacheNode
*) m_Cache
.Get(right
);
92 // Add item into cache:
96 if (url
.GetError() == wxURL_NOERR
)
98 s
= url
.GetInputStream();
99 content
= url
.GetProtocol().GetContentType();
100 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
105 wxGetTempFileName( wxT("wxhtml"), buf
);
106 info
= new wxInetCacheNode(buf
, content
);
107 m_Cache
.Put(right
, info
);
109 { // ok, now copy it:
110 wxFileOutputStream
sout((wxString
)buf
);
111 s
-> Read(sout
); // copy the stream
116 return (wxFSFile
*) NULL
; // we can't open the URL
119 return (wxFSFile
*) NULL
; // incorrect URL
122 // Load item from cache:
123 s
= new wxFileInputStream(info
->GetTemp());
126 return new wxFSFile(s
,
129 GetAnchor(location
));
131 else return (wxFSFile
*) NULL
;
136 wxInternetFSHandler::~wxInternetFSHandler()
142 while ((n
= m_Cache
.Next()) != NULL
)
144 n2
= (wxInetCacheNode
*) n
->GetData();
145 wxRemoveFile(n2
->GetTemp());
150 class wxFileSystemInternetModule
: public wxModule
152 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
155 virtual bool OnInit()
157 wxFileSystem::AddHandler(new wxInternetFSHandler
);
160 virtual void OnExit() {}
163 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
165 #endif // wxUSE_FS_INET