]>
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__ | |
23 | #pragma implementation | |
24 | #endif | |
25 | ||
3096bd2f | 26 | #include "wx/wxprec.h" |
5526e819 VS |
27 | |
28 | #ifdef __BORDLANDC__ | |
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 | ||
e3e717ec VZ |
37 | #if wxUSE_FS_INET |
38 | ||
5526e819 | 39 | #ifndef WXPRECOMP |
3096bd2f | 40 | #include "wx/wx.h" |
5526e819 VS |
41 | #endif |
42 | ||
43 | #include "wx/wfstream.h" | |
44 | #include "wx/url.h" | |
45 | #include "wx/filesys.h" | |
46 | #include "wx/fs_inet.h" | |
cf1f0870 | 47 | #include "wx/module.h" |
5526e819 VS |
48 | |
49 | class wxInetCacheNode : public wxObject | |
50 | { | |
51 | private: | |
52 | wxString m_Temp; | |
53 | wxString m_Mime; | |
54 | ||
55 | public: | |
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;} | |
59 | }; | |
60 | ||
61 | ||
62 | ||
63 | ||
64 | ||
65 | //-------------------------------------------------------------------------------- | |
66 | // wxInternetFSHandler | |
67 | //-------------------------------------------------------------------------------- | |
68 | ||
69 | ||
c3a1c7d2 VS |
70 | static wxString StripProtocolAnchor(const wxString& location) |
71 | { | |
72 | wxString myloc(location.BeforeLast(wxT('#'))); | |
73 | if (myloc.IsEmpty()) myloc = location.AfterFirst(wxT(':')); | |
74 | else myloc = myloc.AfterFirst(wxT(':')); | |
75 | ||
76 | // fix malformed url: | |
77 | if (myloc.Left(2) != wxT("//")) | |
78 | { | |
79 | if (myloc.GetChar(0) != wxT('/')) myloc = wxT("//") + myloc; | |
80 | else myloc = wxT("/") + myloc; | |
81 | } | |
82 | if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/'); | |
83 | ||
84 | return myloc; | |
85 | } | |
86 | ||
87 | ||
88 | ||
5526e819 VS |
89 | bool wxInternetFSHandler::CanOpen(const wxString& location) |
90 | { | |
91 | wxString p = GetProtocol(location); | |
81915974 VS |
92 | if ((p == wxT("http")) || (p == wxT("ftp"))) |
93 | { | |
c3a1c7d2 | 94 | wxURL url(p + wxT(":") + StripProtocolAnchor(location)); |
81915974 VS |
95 | return (url.GetError() == wxURL_NOERR); |
96 | } | |
97 | else | |
98 | return FALSE; | |
5526e819 VS |
99 | } |
100 | ||
101 | ||
5526e819 VS |
102 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
103 | { | |
c3a1c7d2 | 104 | wxString right = GetProtocol(location) + wxT(":") + StripProtocolAnchor(location); |
5526e819 VS |
105 | wxInputStream *s; |
106 | wxString content; | |
107 | wxInetCacheNode *info; | |
108 | ||
109 | info = (wxInetCacheNode*) m_Cache.Get(right); | |
110 | ||
111 | // Add item into cache: | |
f61815af | 112 | if (info == NULL) |
324dbfec | 113 | { |
5526e819 | 114 | wxURL url(right); |
81915974 | 115 | if (url.GetError() == wxURL_NOERR) |
07d4b94c | 116 | { |
81915974 VS |
117 | s = url.GetInputStream(); |
118 | content = url.GetProtocol().GetContentType(); | |
119 | if (content == wxEmptyString) content = GetMimeTypeFromExt(location); | |
120 | if (s) | |
121 | { | |
122 | wxChar buf[256]; | |
123 | ||
124 | wxGetTempFileName( wxT("wxhtml"), buf); | |
125 | info = new wxInetCacheNode(buf, content); | |
126 | m_Cache.Put(right, info); | |
127 | ||
128 | { // ok, now copy it: | |
129 | wxFileOutputStream sout((wxString)buf); | |
130 | s -> Read(sout); // copy the stream | |
131 | } | |
132 | delete s; | |
5526e819 | 133 | } |
81915974 VS |
134 | else |
135 | return (wxFSFile*) NULL; // we can't open the URL | |
5526e819 | 136 | } |
324dbfec | 137 | else |
81915974 | 138 | return (wxFSFile*) NULL; // incorrect URL |
5526e819 VS |
139 | } |
140 | ||
141 | // Load item from cache: | |
324dbfec | 142 | s = new wxFileInputStream(info->GetTemp()); |
913df6f2 | 143 | if (s) |
324dbfec | 144 | { |
5526e819 VS |
145 | return new wxFSFile(s, |
146 | right, | |
324dbfec | 147 | info->GetMime(), |
6ee654e6 | 148 | GetAnchor(location), |
5f8525bf | 149 | wxDateTime::Now()); |
5526e819 | 150 | } |
324dbfec | 151 | else return (wxFSFile*) NULL; |
5526e819 VS |
152 | } |
153 | ||
154 | ||
155 | ||
156 | wxInternetFSHandler::~wxInternetFSHandler() | |
157 | { | |
158 | wxNode *n; | |
159 | wxInetCacheNode *n2; | |
160 | ||
161 | m_Cache.BeginFind(); | |
913df6f2 | 162 | while ((n = m_Cache.Next()) != NULL) |
324dbfec RR |
163 | { |
164 | n2 = (wxInetCacheNode*) n->GetData(); | |
165 | wxRemoveFile(n2->GetTemp()); | |
5526e819 VS |
166 | delete n2; |
167 | } | |
168 | } | |
169 | ||
f61815af GL |
170 | class wxFileSystemInternetModule : public wxModule |
171 | { | |
172 | DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule) | |
173 | ||
174 | public: | |
175 | virtual bool OnInit() | |
176 | { | |
177 | wxFileSystem::AddHandler(new wxInternetFSHandler); | |
178 | return TRUE; | |
179 | } | |
180 | virtual void OnExit() {} | |
181 | }; | |
182 | ||
183 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule) | |
184 | ||
e3e717ec | 185 | #endif // wxUSE_FS_INET |