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
myloc(GetRightLocation(location
));
88 if (myloc
.Left(2) != wxT("//"))
90 if (myloc
.GetChar(0) != wxT('/')) myloc
= wxT("//") + myloc
;
91 else myloc
= wxT("/") + myloc
;
93 if (myloc
.Mid(2).Find(wxT('/')) == wxNOT_FOUND
) myloc
<< wxT('/');
95 wxString right
= GetProtocol(location
) + wxT(":") + myloc
;
98 wxInetCacheNode
*info
;
100 info
= (wxInetCacheNode
*) m_Cache
.Get(right
);
102 // Add item into cache:
106 if (url
.GetError() == wxURL_NOERR
)
108 s
= url
.GetInputStream();
109 content
= url
.GetProtocol().GetContentType();
110 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
115 wxGetTempFileName( wxT("wxhtml"), buf
);
116 info
= new wxInetCacheNode(buf
, content
);
117 m_Cache
.Put(right
, info
);
119 { // ok, now copy it:
120 wxFileOutputStream
sout((wxString
)buf
);
121 s
-> Read(sout
); // copy the stream
126 return (wxFSFile
*) NULL
; // we can't open the URL
129 return (wxFSFile
*) NULL
; // incorrect URL
132 // Load item from cache:
133 s
= new wxFileInputStream(info
->GetTemp());
136 return new wxFSFile(s
,
140 wxDateTime::Today());
142 else return (wxFSFile
*) NULL
;
147 wxInternetFSHandler::~wxInternetFSHandler()
153 while ((n
= m_Cache
.Next()) != NULL
)
155 n2
= (wxInetCacheNode
*) n
->GetData();
156 wxRemoveFile(n2
->GetTemp());
161 class wxFileSystemInternetModule
: public wxModule
163 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
166 virtual bool OnInit()
168 wxFileSystem::AddHandler(new wxInternetFSHandler
);
171 virtual void OnExit() {}
174 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
176 #endif // wxUSE_FS_INET