]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
02761f6c | 2 | // Name: src/common/fs_inet.cpp |
5526e819 VS |
3 | // Purpose: HTTP and FTP file system |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
02761f6c | 6 | // RCS-ID: $Id$ |
65571936 | 7 | // Licence: wxWindows licence |
5526e819 VS |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
3096bd2f | 10 | #include "wx/wxprec.h" |
5526e819 | 11 | |
2b5f62a0 | 12 | #ifdef __BORLANDC__ |
02761f6c | 13 | #pragma hdrstop |
5526e819 VS |
14 | #endif |
15 | ||
31528cd3 VZ |
16 | #if !wxUSE_SOCKETS |
17 | #undef wxUSE_FS_INET | |
18 | #define wxUSE_FS_INET 0 | |
19 | #endif | |
20 | ||
24528b0c | 21 | #if wxUSE_FILESYSTEM && wxUSE_FS_INET |
e3e717ec | 22 | |
b4f4d3dd | 23 | #ifndef WX_PRECOMP |
02761f6c | 24 | #include "wx/module.h" |
5526e819 VS |
25 | #endif |
26 | ||
27 | #include "wx/wfstream.h" | |
28 | #include "wx/url.h" | |
29 | #include "wx/filesys.h" | |
30 | #include "wx/fs_inet.h" | |
31 | ||
3caa34b7 VS |
32 | // ---------------------------------------------------------------------------- |
33 | // Helper classes | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | // This stream deletes the file when destroyed | |
37 | class wxTemporaryFileInputStream : public wxFileInputStream | |
5526e819 | 38 | { |
3caa34b7 VS |
39 | public: |
40 | wxTemporaryFileInputStream(const wxString& filename) : | |
41 | wxFileInputStream(filename), m_filename(filename) {} | |
a62848fd | 42 | |
d3c7fc99 | 43 | virtual ~wxTemporaryFileInputStream() |
3caa34b7 VS |
44 | { |
45 | // NB: copied from wxFileInputStream dtor, we need to do it before | |
46 | // wxRemoveFile | |
47 | if (m_file_destroy) | |
48 | { | |
49 | delete m_file; | |
50 | m_file_destroy = false; | |
51 | } | |
a62848fd | 52 | wxRemoveFile(m_filename); |
3caa34b7 | 53 | } |
5526e819 | 54 | |
3caa34b7 VS |
55 | protected: |
56 | wxString m_filename; | |
5526e819 VS |
57 | }; |
58 | ||
59 | ||
3caa34b7 | 60 | // ---------------------------------------------------------------------------- |
5526e819 | 61 | // wxInternetFSHandler |
3caa34b7 | 62 | // ---------------------------------------------------------------------------- |
5526e819 | 63 | |
c3a1c7d2 VS |
64 | static wxString StripProtocolAnchor(const wxString& location) |
65 | { | |
66 | wxString myloc(location.BeforeLast(wxT('#'))); | |
489f6cf7 | 67 | if (myloc.empty()) myloc = location.AfterFirst(wxT(':')); |
c3a1c7d2 VS |
68 | else myloc = myloc.AfterFirst(wxT(':')); |
69 | ||
70 | // fix malformed url: | |
489f6cf7 | 71 | if (!myloc.Left(2).IsSameAs(wxT("//"))) |
c3a1c7d2 VS |
72 | { |
73 | if (myloc.GetChar(0) != wxT('/')) myloc = wxT("//") + myloc; | |
74 | else myloc = wxT("/") + myloc; | |
75 | } | |
76 | if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/'); | |
77 | ||
78 | return myloc; | |
79 | } | |
80 | ||
81 | ||
5526e819 VS |
82 | bool wxInternetFSHandler::CanOpen(const wxString& location) |
83 | { | |
34e0d9f8 | 84 | #if wxUSE_URL |
5526e819 | 85 | wxString p = GetProtocol(location); |
04dbb646 | 86 | if ((p == wxT("http")) || (p == wxT("ftp"))) |
81915974 | 87 | { |
c3a1c7d2 | 88 | wxURL url(p + wxT(":") + StripProtocolAnchor(location)); |
81915974 VS |
89 | return (url.GetError() == wxURL_NOERR); |
90 | } | |
34e0d9f8 | 91 | #endif |
a62848fd | 92 | return false; |
5526e819 VS |
93 | } |
94 | ||
95 | ||
3caa34b7 VS |
96 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), |
97 | const wxString& location) | |
5526e819 | 98 | { |
34e0d9f8 JS |
99 | #if !wxUSE_URL |
100 | return NULL; | |
101 | #else | |
3caa34b7 VS |
102 | wxString right = |
103 | GetProtocol(location) + wxT(":") + StripProtocolAnchor(location); | |
5526e819 | 104 | |
3caa34b7 VS |
105 | wxURL url(right); |
106 | if (url.GetError() == wxURL_NOERR) | |
324dbfec | 107 | { |
3caa34b7 VS |
108 | wxInputStream *s = url.GetInputStream(); |
109 | wxString content = url.GetProtocol().GetContentType(); | |
3caa34b7 | 110 | if (s) |
07d4b94c | 111 | { |
3caa34b7 VS |
112 | wxString tmpfile = |
113 | wxFileName::CreateTempFileName(wxT("wxhtml")); | |
5526e819 | 114 | |
3caa34b7 VS |
115 | { // now copy streams content to temporary file: |
116 | wxFileOutputStream sout(tmpfile); | |
117 | s->Read(sout); | |
118 | } | |
119 | delete s; | |
a62848fd | 120 | |
3caa34b7 VS |
121 | return new wxFSFile(new wxTemporaryFileInputStream(tmpfile), |
122 | right, | |
123 | content, | |
124 | GetAnchor(location) | |
e2b87f38 | 125 | #if wxUSE_DATETIME |
3caa34b7 | 126 | , wxDateTime::Now() |
e2b87f38 VZ |
127 | #endif // wxUSE_DATETIME |
128 | ); | |
3caa34b7 | 129 | } |
5526e819 | 130 | } |
3caa34b7 | 131 | |
d3b9f782 | 132 | return NULL; // incorrect URL |
34e0d9f8 | 133 | #endif |
5526e819 VS |
134 | } |
135 | ||
3caa34b7 | 136 | |
f61815af GL |
137 | class wxFileSystemInternetModule : public wxModule |
138 | { | |
139 | DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule) | |
140 | ||
141 | public: | |
5949d307 RR |
142 | wxFileSystemInternetModule() : |
143 | wxModule(), | |
144 | m_handler(NULL) | |
145 | { | |
146 | } | |
147 | ||
f61815af GL |
148 | virtual bool OnInit() |
149 | { | |
5949d307 RR |
150 | m_handler = new wxInternetFSHandler; |
151 | wxFileSystem::AddHandler(m_handler); | |
a62848fd | 152 | return true; |
f61815af | 153 | } |
5949d307 | 154 | |
03647350 | 155 | virtual void OnExit() |
5949d307 RR |
156 | { |
157 | delete wxFileSystem::RemoveHandler(m_handler); | |
158 | } | |
159 | ||
160 | private: | |
161 | wxFileSystemHandler* m_handler; | |
f61815af GL |
162 | }; |
163 | ||
164 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule) | |
165 | ||
24528b0c | 166 | #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET |