]> git.saurik.com Git - wxWidgets.git/blob - src/common/webviewfilehandler.cpp
Fix recursion problems when loading pages from a virtual file system using the WebKit...
[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 //We iterate through the string to see if there is a protocol description
56 int start = -1;
57 for(int i = 0; i < uri.length(); i++)
58 {
59 if(uri[i] == ';' && uri.substr(i, 10) == ";protocol=")
60 {
61 start = i;
62 break;
63 }
64 }
65
66 //We do not have a protocol string so we just pass the path withouth the
67 if(start == -1)
68 {
69 size_t doubleslash = uri.find("//");
70 //The path is incorrectly formed without // after the scheme
71 if(doubleslash == wxString::npos)
72 return NULL;
73
74 wxString fspath = "file:" +
75 EscapeFileNameCharsInURL(uri.substr(doubleslash + 2));
76 return m_fileSystem->OpenFile(fspath);
77 }
78 //Otherwise we need to extract the protocol
79 else
80 {
81 int end = uri.find('/', start);
82 //For the path to be valid there must to a path after the protocol
83 if(end == wxString::npos)
84 {
85 return NULL;
86 }
87 wxString mainpath = uri.substr(0, start);
88 wxString archivepath = uri.substr(end);
89 wxString protstring = uri.substr(start, end - start);
90 wxString protocol = protstring.substr(10);
91 //We can now construct the correct path
92 size_t doubleslash = uri.find("//");
93 //The path is incorrectly formed without // after the first protocol
94 if(doubleslash == wxString::npos)
95 return NULL;
96
97 wxString fspath = "file:" +
98 EscapeFileNameCharsInURL(mainpath.substr(doubleslash + 2))
99 + "#" + protocol +":" + archivepath;
100 return m_fileSystem->OpenFile(fspath);
101 }
102 }
103
104 #endif // wxUSE_WEB