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
37 #if wxUSE_FILESYSTEM && wxUSE_FS_INET
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 static wxString
StripProtocolAnchor(const wxString
& location
)
72 wxString
myloc(location
.BeforeLast(wxT('#')));
73 if (myloc
.IsEmpty()) myloc
= location
.AfterFirst(wxT(':'));
74 else myloc
= myloc
.AfterFirst(wxT(':'));
77 if (myloc
.Left(2) != wxT("//"))
79 if (myloc
.GetChar(0) != wxT('/')) myloc
= wxT("//") + myloc
;
80 else myloc
= wxT("/") + myloc
;
82 if (myloc
.Mid(2).Find(wxT('/')) == wxNOT_FOUND
) myloc
<< wxT('/');
89 bool wxInternetFSHandler::CanOpen(const wxString
& location
)
91 wxString p
= GetProtocol(location
);
92 if ((p
== wxT("http")) || (p
== wxT("ftp")))
94 wxURL
url(p
+ wxT(":") + StripProtocolAnchor(location
));
95 return (url
.GetError() == wxURL_NOERR
);
102 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
104 wxString right
= GetProtocol(location
) + wxT(":") + StripProtocolAnchor(location
);
107 wxInetCacheNode
*info
;
109 info
= (wxInetCacheNode
*) m_Cache
.Get(right
);
111 // Add item into cache:
115 if (url
.GetError() == wxURL_NOERR
)
117 s
= url
.GetInputStream();
118 content
= url
.GetProtocol().GetContentType();
119 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
124 wxGetTempFileName( wxT("wxhtml"), buf
);
125 info
= new wxInetCacheNode(buf
, content
);
126 m_Cache
.Put(right
, info
);
128 { // ok, now copy it:
129 wxFileOutputStream
sout((wxString
)buf
);
130 s
-> Read(sout
); // copy the stream
135 return (wxFSFile
*) NULL
; // we can't open the URL
138 return (wxFSFile
*) NULL
; // incorrect URL
141 // Load item from cache:
142 s
= new wxFileInputStream(info
->GetTemp());
145 return new wxFSFile(s
,
151 else return (wxFSFile
*) NULL
;
156 wxInternetFSHandler::~wxInternetFSHandler()
162 while ((n
= m_Cache
.Next()) != NULL
)
164 n2
= (wxInetCacheNode
*) n
->GetData();
165 wxRemoveFile(n2
->GetTemp());
170 class wxFileSystemInternetModule
: public wxModule
172 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
175 virtual bool OnInit()
177 wxFileSystem::AddHandler(new wxInternetFSHandler
);
180 virtual void OnExit() {}
183 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
185 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET