]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: fs_inet.cpp | |
3 | // Purpose: HTTP and FTP file system | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
6 | // Licence: wxWindows Licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /* | |
10 | ||
e3e717ec | 11 | REMARKS : |
5526e819 VS |
12 | |
13 | This FS creates local cache (in /tmp directory). The cache is freed | |
14 | on program exit. | |
15 | ||
16 | Size of cache is limited to cca 1000 items (due to GetTempFileName | |
17 | limitation) | |
18 | ||
19 | ||
20 | */ | |
21 | ||
22 | #ifdef __GNUG__ | |
c3f4609e | 23 | #pragma implementation "fs_inet.h" |
5526e819 VS |
24 | #endif |
25 | ||
3096bd2f | 26 | #include "wx/wxprec.h" |
5526e819 | 27 | |
2b5f62a0 | 28 | #ifdef __BORLANDC__ |
5526e819 VS |
29 | #pragma hdrstop |
30 | #endif | |
31 | ||
31528cd3 VZ |
32 | #if !wxUSE_SOCKETS |
33 | #undef wxUSE_FS_INET | |
34 | #define wxUSE_FS_INET 0 | |
35 | #endif | |
36 | ||
24528b0c | 37 | #if wxUSE_FILESYSTEM && wxUSE_FS_INET |
e3e717ec | 38 | |
5526e819 | 39 | #ifndef WXPRECOMP |
5526e819 VS |
40 | #endif |
41 | ||
42 | #include "wx/wfstream.h" | |
43 | #include "wx/url.h" | |
44 | #include "wx/filesys.h" | |
45 | #include "wx/fs_inet.h" | |
cf1f0870 | 46 | #include "wx/module.h" |
5526e819 VS |
47 | |
48 | class wxInetCacheNode : public wxObject | |
49 | { | |
50 | private: | |
51 | wxString m_Temp; | |
52 | wxString m_Mime; | |
53 | ||
54 | public: | |
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;} | |
58 | }; | |
59 | ||
60 | ||
61 | ||
62 | ||
63 | ||
64 | //-------------------------------------------------------------------------------- | |
65 | // wxInternetFSHandler | |
66 | //-------------------------------------------------------------------------------- | |
67 | ||
68 | ||
c3a1c7d2 VS |
69 | static wxString StripProtocolAnchor(const wxString& location) |
70 | { | |
71 | wxString myloc(location.BeforeLast(wxT('#'))); | |
72 | if (myloc.IsEmpty()) myloc = location.AfterFirst(wxT(':')); | |
73 | else myloc = myloc.AfterFirst(wxT(':')); | |
74 | ||
75 | // fix malformed url: | |
04dbb646 | 76 | if (myloc.Left(2) != wxT("//")) |
c3a1c7d2 VS |
77 | { |
78 | if (myloc.GetChar(0) != wxT('/')) myloc = wxT("//") + myloc; | |
79 | else myloc = wxT("/") + myloc; | |
80 | } | |
81 | if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/'); | |
82 | ||
83 | return myloc; | |
84 | } | |
85 | ||
86 | ||
87 | ||
5526e819 VS |
88 | bool wxInternetFSHandler::CanOpen(const wxString& location) |
89 | { | |
90 | wxString p = GetProtocol(location); | |
04dbb646 | 91 | if ((p == wxT("http")) || (p == wxT("ftp"))) |
81915974 | 92 | { |
c3a1c7d2 | 93 | wxURL url(p + wxT(":") + StripProtocolAnchor(location)); |
81915974 VS |
94 | return (url.GetError() == wxURL_NOERR); |
95 | } | |
f6bcfd97 BP |
96 | |
97 | return FALSE; | |
5526e819 VS |
98 | } |
99 | ||
100 | ||
5526e819 VS |
101 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
102 | { | |
c3a1c7d2 | 103 | wxString right = GetProtocol(location) + wxT(":") + StripProtocolAnchor(location); |
5526e819 VS |
104 | wxInputStream *s; |
105 | wxString content; | |
106 | wxInetCacheNode *info; | |
107 | ||
108 | info = (wxInetCacheNode*) m_Cache.Get(right); | |
109 | ||
110 | // Add item into cache: | |
f61815af | 111 | if (info == NULL) |
324dbfec | 112 | { |
5526e819 | 113 | wxURL url(right); |
04dbb646 | 114 | if (url.GetError() == wxURL_NOERR) |
07d4b94c | 115 | { |
81915974 VS |
116 | s = url.GetInputStream(); |
117 | content = url.GetProtocol().GetContentType(); | |
118 | if (content == wxEmptyString) content = GetMimeTypeFromExt(location); | |
119 | if (s) | |
120 | { | |
121 | wxChar buf[256]; | |
122 | ||
123 | wxGetTempFileName( wxT("wxhtml"), buf); | |
124 | info = new wxInetCacheNode(buf, content); | |
125 | m_Cache.Put(right, info); | |
126 | ||
127 | { // ok, now copy it: | |
128 | wxFileOutputStream sout((wxString)buf); | |
129 | s -> Read(sout); // copy the stream | |
130 | } | |
131 | delete s; | |
5526e819 | 132 | } |
81915974 VS |
133 | else |
134 | return (wxFSFile*) NULL; // we can't open the URL | |
5526e819 | 135 | } |
324dbfec | 136 | else |
81915974 | 137 | return (wxFSFile*) NULL; // incorrect URL |
5526e819 VS |
138 | } |
139 | ||
140 | // Load item from cache: | |
324dbfec | 141 | s = new wxFileInputStream(info->GetTemp()); |
f6bcfd97 BP |
142 | if (!s) |
143 | return (wxFSFile*) NULL; | |
144 | ||
145 | return new wxFSFile(s, | |
146 | right, | |
147 | info->GetMime(), | |
e2b87f38 VZ |
148 | GetAnchor(location) |
149 | #if wxUSE_DATETIME | |
150 | , wxDateTime::Now() | |
151 | #endif // wxUSE_DATETIME | |
152 | ); | |
5526e819 VS |
153 | } |
154 | ||
155 | ||
156 | ||
157 | wxInternetFSHandler::~wxInternetFSHandler() | |
158 | { | |
df5168c4 | 159 | wxHashTable::compatibility_iterator n; |
5526e819 VS |
160 | wxInetCacheNode *n2; |
161 | ||
162 | m_Cache.BeginFind(); | |
b0cb0158 | 163 | while ((n = m_Cache.Next()) != 0) |
324dbfec RR |
164 | { |
165 | n2 = (wxInetCacheNode*) n->GetData(); | |
166 | wxRemoveFile(n2->GetTemp()); | |
5526e819 VS |
167 | delete n2; |
168 | } | |
169 | } | |
170 | ||
f61815af GL |
171 | class wxFileSystemInternetModule : public wxModule |
172 | { | |
173 | DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule) | |
174 | ||
175 | public: | |
176 | virtual bool OnInit() | |
177 | { | |
178 | wxFileSystem::AddHandler(new wxInternetFSHandler); | |
179 | return TRUE; | |
180 | } | |
181 | virtual void OnExit() {} | |
182 | }; | |
183 | ||
184 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule) | |
185 | ||
24528b0c | 186 | #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET |