]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_inet.cpp
added conversion to mac native filenames from local filesystem handler
[wxWidgets.git] / src / common / fs_inet.cpp
CommitLineData
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 11REMARKS :
5526e819
VS
12
13This FS creates local cache (in /tmp directory). The cache is freed
14on program exit.
15
16Size of cache is limited to cca 1000 items (due to GetTempFileName
17limitation)
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
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
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
48class 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
69static 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
88bool 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
101wxFSFile* 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(),
148 GetAnchor(location),
149 wxDateTime::Now());
5526e819
VS
150}
151
152
153
154wxInternetFSHandler::~wxInternetFSHandler()
155{
156 wxNode *n;
157 wxInetCacheNode *n2;
158
159 m_Cache.BeginFind();
913df6f2 160 while ((n = m_Cache.Next()) != NULL)
324dbfec
RR
161 {
162 n2 = (wxInetCacheNode*) n->GetData();
163 wxRemoveFile(n2->GetTemp());
5526e819
VS
164 delete n2;
165 }
166}
167
f61815af
GL
168class 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
181IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
182
24528b0c 183#endif // wxUSE_FILESYSTEM && wxUSE_FS_INET