]>
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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
10 | #pragma implementation "fs_inet.h" | |
11 | #endif | |
12 | ||
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if !wxUSE_SOCKETS | |
20 | #undef wxUSE_FS_INET | |
21 | #define wxUSE_FS_INET 0 | |
22 | #endif | |
23 | ||
24 | #if wxUSE_FILESYSTEM && wxUSE_FS_INET | |
25 | ||
26 | #ifndef WXPRECOMP | |
27 | #endif | |
28 | ||
29 | #include "wx/wfstream.h" | |
30 | #include "wx/url.h" | |
31 | #include "wx/filesys.h" | |
32 | #include "wx/fs_inet.h" | |
33 | #include "wx/module.h" | |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // Helper classes | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | // This stream deletes the file when destroyed | |
40 | class wxTemporaryFileInputStream : public wxFileInputStream | |
41 | { | |
42 | public: | |
43 | wxTemporaryFileInputStream(const wxString& filename) : | |
44 | wxFileInputStream(filename), m_filename(filename) {} | |
45 | ||
46 | ~wxTemporaryFileInputStream() | |
47 | { | |
48 | // NB: copied from wxFileInputStream dtor, we need to do it before | |
49 | // wxRemoveFile | |
50 | if (m_file_destroy) | |
51 | { | |
52 | delete m_file; | |
53 | m_file_destroy = false; | |
54 | } | |
55 | wxRemoveFile(m_filename); | |
56 | } | |
57 | ||
58 | protected: | |
59 | wxString m_filename; | |
60 | }; | |
61 | ||
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // wxInternetFSHandler | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | static wxString StripProtocolAnchor(const wxString& location) | |
68 | { | |
69 | wxString myloc(location.BeforeLast(wxT('#'))); | |
70 | if (myloc.IsEmpty()) myloc = location.AfterFirst(wxT(':')); | |
71 | else myloc = myloc.AfterFirst(wxT(':')); | |
72 | ||
73 | // fix malformed url: | |
74 | if (myloc.Left(2) != wxT("//")) | |
75 | { | |
76 | if (myloc.GetChar(0) != wxT('/')) myloc = wxT("//") + myloc; | |
77 | else myloc = wxT("/") + myloc; | |
78 | } | |
79 | if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/'); | |
80 | ||
81 | return myloc; | |
82 | } | |
83 | ||
84 | ||
85 | bool wxInternetFSHandler::CanOpen(const wxString& location) | |
86 | { | |
87 | wxString p = GetProtocol(location); | |
88 | if ((p == wxT("http")) || (p == wxT("ftp"))) | |
89 | { | |
90 | wxURL url(p + wxT(":") + StripProtocolAnchor(location)); | |
91 | return (url.GetError() == wxURL_NOERR); | |
92 | } | |
93 | ||
94 | return FALSE; | |
95 | } | |
96 | ||
97 | ||
98 | wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), | |
99 | const wxString& location) | |
100 | { | |
101 | wxString right = | |
102 | GetProtocol(location) + wxT(":") + StripProtocolAnchor(location); | |
103 | ||
104 | wxURL url(right); | |
105 | if (url.GetError() == wxURL_NOERR) | |
106 | { | |
107 | wxInputStream *s = url.GetInputStream(); | |
108 | wxString content = url.GetProtocol().GetContentType(); | |
109 | if (content == wxEmptyString) content = GetMimeTypeFromExt(location); | |
110 | if (s) | |
111 | { | |
112 | wxString tmpfile = | |
113 | wxFileName::CreateTempFileName(wxT("wxhtml")); | |
114 | ||
115 | { // now copy streams content to temporary file: | |
116 | wxFileOutputStream sout(tmpfile); | |
117 | s->Read(sout); | |
118 | } | |
119 | delete s; | |
120 | ||
121 | return new wxFSFile(new wxTemporaryFileInputStream(tmpfile), | |
122 | right, | |
123 | content, | |
124 | GetAnchor(location) | |
125 | #if wxUSE_DATETIME | |
126 | , wxDateTime::Now() | |
127 | #endif // wxUSE_DATETIME | |
128 | ); | |
129 | } | |
130 | } | |
131 | ||
132 | return (wxFSFile*) NULL; // incorrect URL | |
133 | } | |
134 | ||
135 | ||
136 | class wxFileSystemInternetModule : public wxModule | |
137 | { | |
138 | DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule) | |
139 | ||
140 | public: | |
141 | virtual bool OnInit() | |
142 | { | |
143 | wxFileSystem::AddHandler(new wxInternetFSHandler); | |
144 | return TRUE; | |
145 | } | |
146 | virtual void OnExit() {} | |
147 | }; | |
148 | ||
149 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule) | |
150 | ||
151 | #endif // wxUSE_FILESYSTEM && wxUSE_FS_INET |