]>
Commit | Line | Data |
---|---|---|
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 | |
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_FS_INET | |
38 | ||
39 | #ifndef WXPRECOMP | |
40 | #include "wx/wx.h" | |
41 | #endif | |
42 | ||
43 | #include "wx/wfstream.h" | |
44 | #include "wx/url.h" | |
45 | #include "wx/filesys.h" | |
46 | #include "wx/fs_inet.h" | |
47 | #include "wx/module.h" | |
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 | ||
70 | bool wxInternetFSHandler::CanOpen(const wxString& location) | |
71 | { | |
72 | wxString p = GetProtocol(location); | |
73 | if ((p == wxT("http")) || (p == wxT("ftp"))) | |
74 | { | |
75 | wxURL url(GetProtocol(location) + wxT(":") + GetRightLocation(location)); | |
76 | return (url.GetError() == wxURL_NOERR); | |
77 | } | |
78 | else | |
79 | return FALSE; | |
80 | } | |
81 | ||
82 | ||
83 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
84 | { | |
85 | wxString myloc(GetRightLocation(location)); | |
86 | ||
87 | // fix malformed url: | |
88 | if (myloc.Left(2) != wxT("//")) | |
89 | { | |
90 | if (myloc.GetChar(0) != wxT('/')) myloc = wxT("//") + myloc; | |
91 | else myloc = wxT("/") + myloc; | |
92 | } | |
93 | if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/'); | |
94 | ||
95 | wxString right = GetProtocol(location) + wxT(":") + myloc; | |
96 | wxInputStream *s; | |
97 | wxString content; | |
98 | wxInetCacheNode *info; | |
99 | ||
100 | info = (wxInetCacheNode*) m_Cache.Get(right); | |
101 | ||
102 | // Add item into cache: | |
103 | if (info == NULL) | |
104 | { | |
105 | wxURL url(right); | |
106 | if (url.GetError() == wxURL_NOERR) | |
107 | { | |
108 | s = url.GetInputStream(); | |
109 | content = url.GetProtocol().GetContentType(); | |
110 | if (content == wxEmptyString) content = GetMimeTypeFromExt(location); | |
111 | if (s) | |
112 | { | |
113 | wxChar buf[256]; | |
114 | ||
115 | wxGetTempFileName( wxT("wxhtml"), buf); | |
116 | info = new wxInetCacheNode(buf, content); | |
117 | m_Cache.Put(right, info); | |
118 | ||
119 | { // ok, now copy it: | |
120 | wxFileOutputStream sout((wxString)buf); | |
121 | s -> Read(sout); // copy the stream | |
122 | } | |
123 | delete s; | |
124 | } | |
125 | else | |
126 | return (wxFSFile*) NULL; // we can't open the URL | |
127 | } | |
128 | else | |
129 | return (wxFSFile*) NULL; // incorrect URL | |
130 | } | |
131 | ||
132 | // Load item from cache: | |
133 | s = new wxFileInputStream(info->GetTemp()); | |
134 | if (s) | |
135 | { | |
136 | return new wxFSFile(s, | |
137 | right, | |
138 | info->GetMime(), | |
139 | GetAnchor(location), | |
140 | wxDateTime::Today()); | |
141 | } | |
142 | else return (wxFSFile*) NULL; | |
143 | } | |
144 | ||
145 | ||
146 | ||
147 | wxInternetFSHandler::~wxInternetFSHandler() | |
148 | { | |
149 | wxNode *n; | |
150 | wxInetCacheNode *n2; | |
151 | ||
152 | m_Cache.BeginFind(); | |
153 | while ((n = m_Cache.Next()) != NULL) | |
154 | { | |
155 | n2 = (wxInetCacheNode*) n->GetData(); | |
156 | wxRemoveFile(n2->GetTemp()); | |
157 | delete n2; | |
158 | } | |
159 | } | |
160 | ||
161 | class wxFileSystemInternetModule : public wxModule | |
162 | { | |
163 | DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule) | |
164 | ||
165 | public: | |
166 | virtual bool OnInit() | |
167 | { | |
168 | wxFileSystem::AddHandler(new wxInternetFSHandler); | |
169 | return TRUE; | |
170 | } | |
171 | virtual void OnExit() {} | |
172 | }; | |
173 | ||
174 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule) | |
175 | ||
176 | #endif // wxUSE_FS_INET |