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