]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_inet.cpp
compilation fix for DJGPP
[wxWidgets.git] / src / common / fs_inet.cpp
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
11 REMARKS :
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 "fs_inet.h"
24 #endif
25
26 #include "wx/wxprec.h"
27
28 #ifdef __BORDLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if !wxUSE_SOCKETS
33 #undef wxUSE_FS_INET
34 #define wxUSE_FS_INET 0
35 #endif
36
37 #if wxUSE_FILESYSTEM && wxUSE_FS_INET
38
39 #ifndef WXPRECOMP
40 #endif
41
42 #include "wx/wfstream.h"
43 #include "wx/url.h"
44 #include "wx/filesys.h"
45 #include "wx/fs_inet.h"
46 #include "wx/module.h"
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
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:
76 if (myloc.Left(2) != wxT("//"))
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
88 bool wxInternetFSHandler::CanOpen(const wxString& location)
89 {
90 wxString p = GetProtocol(location);
91 if ((p == wxT("http")) || (p == wxT("ftp")))
92 {
93 wxURL url(p + wxT(":") + StripProtocolAnchor(location));
94 return (url.GetError() == wxURL_NOERR);
95 }
96
97 return FALSE;
98 }
99
100
101 wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
102 {
103 wxString right = GetProtocol(location) + wxT(":") + StripProtocolAnchor(location);
104 wxInputStream *s;
105 wxString content;
106 wxInetCacheNode *info;
107
108 info = (wxInetCacheNode*) m_Cache.Get(right);
109
110 // Add item into cache:
111 if (info == NULL)
112 {
113 wxURL url(right);
114 if (url.GetError() == wxURL_NOERR)
115 {
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;
132 }
133 else
134 return (wxFSFile*) NULL; // we can't open the URL
135 }
136 else
137 return (wxFSFile*) NULL; // incorrect URL
138 }
139
140 // Load item from cache:
141 s = new wxFileInputStream(info->GetTemp());
142 if (!s)
143 return (wxFSFile*) NULL;
144
145 return new wxFSFile(s,
146 right,
147 info->GetMime(),
148 GetAnchor(location),
149 wxDateTime::Now());
150 }
151
152
153
154 wxInternetFSHandler::~wxInternetFSHandler()
155 {
156 wxNode *n;
157 wxInetCacheNode *n2;
158
159 m_Cache.BeginFind();
160 while ((n = m_Cache.Next()) != NULL)
161 {
162 n2 = (wxInetCacheNode*) n->GetData();
163 wxRemoveFile(n2->GetTemp());
164 delete n2;
165 }
166 }
167
168 class wxFileSystemInternetModule : public wxModule
169 {
170 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule)
171
172 public:
173 virtual bool OnInit()
174 {
175 wxFileSystem::AddHandler(new wxInternetFSHandler);
176 return TRUE;
177 }
178 virtual void OnExit() {}
179 };
180
181 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
182
183 #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET