]> git.saurik.com Git - wxWidgets.git/blob - src/common/webviewfilehandler.cpp
Fix history in wxWebViewIE when using a custom file scheme.
[wxWidgets.git] / src / common / webviewfilehandler.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webviewfilehandler.cpp
3 // Purpose: Custom handler for the file scheme to allow archive browsing
4 // Author: Steven Lamerton
5 // Id: $Id$
6 // Copyright: (c) 2011 Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_WEB
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 #include "wx/webviewfilehandler.h"
20 #include "wx/filesys.h"
21
22 //Taken from wx/filesys.cpp
23 static wxString EscapeFileNameCharsInURL(const char *in)
24 {
25 wxString s;
26
27 for ( const unsigned char *p = (const unsigned char*)in; *p; ++p )
28 {
29 const unsigned char c = *p;
30
31 if ( c == '/' || c == '-' || c == '.' || c == '_' || c == '~' ||
32 (c >= '0' && c <= '9') ||
33 (c >= 'a' && c <= 'z') ||
34 (c >= 'A' && c <= 'Z') )
35 {
36 s << c;
37 }
38 else
39 {
40 s << wxString::Format("%%%02x", c);
41 }
42 }
43
44 return s;
45 }
46
47 wxWebFileHandler::wxWebFileHandler()
48 {
49 m_name = "file";
50 m_fileSystem = new wxFileSystem();
51 }
52
53 wxFSFile* wxWebFileHandler::GetFile(const wxString &uri)
54 {
55 //If there is a fragment at the end of the path then we need to strip it
56 //off as not all backends do this for us
57 wxString path = uri;
58 size_t hashloc = uri.find('#');
59 if(hashloc != wxString::npos)
60 {
61 path = uri.substr(0, hashloc);
62 }
63
64 //We iterate through the string to see if there is a protocol description
65 size_t start = wxString::npos;
66 for(size_t i = 0; i < path.length(); i++)
67 {
68 if(path[i] == ';' && path.substr(i, 10) == ";protocol=")
69 {
70 start = i;
71 break;
72 }
73 }
74
75 //We do not have a protocol string so we just pass the path withouth the
76 if(start == wxString::npos)
77 {
78 size_t doubleslash = path.find("//");
79 //The path is incorrectly formed without // after the scheme
80 if(doubleslash == wxString::npos)
81 return NULL;
82
83 wxString fspath = "file:" +
84 EscapeFileNameCharsInURL(path.substr(doubleslash + 2));
85 return m_fileSystem->OpenFile(fspath);
86 }
87 //Otherwise we need to extract the protocol
88 else
89 {
90 size_t end = path.find('/', start);
91 //For the path to be valid there must to a path after the protocol
92 if(end == wxString::npos)
93 {
94 return NULL;
95 }
96 wxString mainpath = path.substr(0, start);
97 wxString archivepath = path.substr(end);
98 wxString protstring = path.substr(start, end - start);
99 wxString protocol = protstring.substr(10);
100 //We can now construct the correct path
101 size_t doubleslash = path.find("//");
102 //The path is incorrectly formed without // after the first protocol
103 if(doubleslash == wxString::npos)
104 return NULL;
105
106 wxString fspath = "file:" +
107 EscapeFileNameCharsInURL(mainpath.substr(doubleslash + 2))
108 + "#" + protocol +":" + archivepath;
109 return m_fileSystem->OpenFile(fspath);
110 }
111 }
112
113 #endif // wxUSE_WEB