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 "fs_inet.h"
26 #include "wx/wxprec.h"
34 #define wxUSE_FS_INET 0
37 #if wxUSE_FILESYSTEM && wxUSE_FS_INET
42 #include "wx/wfstream.h"
44 #include "wx/filesys.h"
45 #include "wx/fs_inet.h"
46 #include "wx/module.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 static wxString
StripProtocolAnchor(const wxString
& location
)
71 wxString
myloc(location
.BeforeLast(wxT('#')));
72 if (myloc
.IsEmpty()) myloc
= location
.AfterFirst(wxT(':'));
73 else myloc
= myloc
.AfterFirst(wxT(':'));
76 if (myloc
.Left(2) != wxT("//"))
78 if (myloc
.GetChar(0) != wxT('/')) myloc
= wxT("//") + myloc
;
79 else myloc
= wxT("/") + myloc
;
81 if (myloc
.Mid(2).Find(wxT('/')) == wxNOT_FOUND
) myloc
<< wxT('/');
88 bool wxInternetFSHandler::CanOpen(const wxString
& location
)
90 wxString p
= GetProtocol(location
);
91 if ((p
== wxT("http")) || (p
== wxT("ftp")))
93 wxURL
url(p
+ wxT(":") + StripProtocolAnchor(location
));
94 return (url
.GetError() == wxURL_NOERR
);
101 wxFSFile
* wxInternetFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
103 wxString right
= GetProtocol(location
) + wxT(":") + StripProtocolAnchor(location
);
106 wxInetCacheNode
*info
;
108 info
= (wxInetCacheNode
*) m_Cache
.Get(right
);
110 // Add item into cache:
114 if (url
.GetError() == wxURL_NOERR
)
116 s
= url
.GetInputStream();
117 content
= url
.GetProtocol().GetContentType();
118 if (content
== wxEmptyString
) content
= GetMimeTypeFromExt(location
);
123 wxGetTempFileName( wxT("wxhtml"), buf
);
124 info
= new wxInetCacheNode(buf
, content
);
125 m_Cache
.Put(right
, info
);
127 { // ok, now copy it:
128 wxFileOutputStream
sout((wxString
)buf
);
129 s
-> Read(sout
); // copy the stream
134 return (wxFSFile
*) NULL
; // we can't open the URL
137 return (wxFSFile
*) NULL
; // incorrect URL
140 // Load item from cache:
141 s
= new wxFileInputStream(info
->GetTemp());
143 return (wxFSFile
*) NULL
;
145 return new wxFSFile(s
,
154 wxInternetFSHandler::~wxInternetFSHandler()
160 while ((n
= m_Cache
.Next()) != NULL
)
162 n2
= (wxInetCacheNode
*) n
->GetData();
163 wxRemoveFile(n2
->GetTemp());
168 class wxFileSystemInternetModule
: public wxModule
170 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule
)
173 virtual bool OnInit()
175 wxFileSystem::AddHandler(new wxInternetFSHandler
);
178 virtual void OnExit() {}
181 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule
, wxModule
)
183 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET