使用QTWebKit显示内存中存储的网站
目前,我将 HTML、JS、CSS、图形等存储在本地硬盘上,并使用 QWebFrame::SetUrl( QUrl::fromLocalFile( "appFolder\html\index.html" )) 访问它们 。在某些时候,我需要加密本地存储的文件,因此我正在寻找一种方法来根据请求解密它们,或者将它们全部解密到内存中并以这种方式访问它们。
我知道我可以使用 QWebFrame::setContent( htmlData ) 从内存加载 HTML,这样我就可以加载加密的 HTML 文件,在内存中解密,然后以这种方式显示它,但是我该怎么做关于当前存储在子文件夹中的其他数据(JS、CSS、图形等)?
或者,有没有一种方法可以拦截访问所有 HTML、JS、CSS 等文件的请求,并在加载它们时对其进行解密?
通过使用我自己的 NetworkAccessManager,我可以拦截对 createRequest 的调用,这样我就可以看到每个文件何时被加载,但我看不到如何使用它来解密数据在飞行中。我还可以将槽函数连接到 finished(QNetworkReply*)
信号,但此时数据已被读取 - QIODevice
的当前位置指向文件末尾。
我将非常感谢任何正确方向的建议或指示。
Currently I have my HTML, JS, CSS, graphics, etc stored locally on hard disk and access them using QWebFrame::SetUrl( QUrl::fromLocalFile( "appFolder\html\index.html" ))
. At some point I am going to need to encrypt the locally stored files so I'm looking for a way to either decrypt them as they're requested or to decrypt them all into memory and access them that way.
I know I can use QWebFrame::setContent( htmlData )
to load the HTML from memory so I can load the encrypted HTML file, decrypt it in memory and then display it that way, but how would I go about the other data (JS, CSS, graphics, etc) which is currently stored in subfolders?
Alternatively, is there a way I can intercept requests for access to all the HTML, JS, CSS, etc files and decrypt them as they're loaded?
By using my own NetworkAccessManager
I can intercept calls to createRequest
so I can see when each file is being loaded, but I can't see how to use this to decrypt the data on the fly. I can also connect a slot function to the finished(QNetworkReply*)
signal, but at that point the data has already been read - the QIODevice
's current position is pointing to the end of the file.
I'd be very grateful for any advice or pointers in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在您的情况下,最好的解决方案是继承 QNetworkReply 类并在重新实现的 QNetworkAccessManager::createRequest() 函数中使用这个新类。
一般来说,您应该重新实现 QNetworkReply 的下一个虚拟函数:
bytesAvailable()
、readData(char *data, qint64 maxSize)
、close()
、abort()
。例如,
readData
应如下所示:其中
m_buffer
是已解密的数据。此外,您还需要在此类中添加所有必要的逻辑来获取加密数据、解密该数据......
最后,您应该在新类中手动发出
finished()
信号,这样QWebView
或其他相关类将获得解密的 html。I think in your case the best solution is to inherit QNetworkReply class and use this new class in reimplemented
QNetworkAccessManager::createRequest()
function.In general, you should reimplement next virtual functions of QNetworkReply:
bytesAvailable()
,readData(char *data, qint64 maxSize)
,close()
,abort()
.For example,
readData
should be the folowing:where
m_buffer
is already decrypted data.Also you need to add all necessary logic in this class to get encrypted data, decrypt this data...
In the end you should manually emit
finished()
signal inside new class, soQWebView
or other related class will get decrypted html.