I was searching for a method to access & tamper the raw packet data sent and received by a Qt application but could not find anything. Is there any method?
Or if not the application at least a method to access the packet data from a QWebView.
Is there any method to achieve any of the above two?
I’m sorry, Sam, I cannot make it work. I’ve been trying everything I can imagine, but nothing goes as it would have been desired.
Let’s make a short list of options and what can and cannot be done:
You can access the retrieved web page by means of obtaining the
QWebFramewithwebView->page()->currentFrame()and then accessing to methods likesetContents(),setHtml(),toHtml()ortoPlainText(). You can, by those functions, change dynamically the web page shown in the widget.You can access the raw response (as an array of bytes) received for each request, by means of the method
reply->readAll()in thereplyFinished()slot.It’s possible to intercept the requests, by overriding the
QNetworkAccessManager::createRequest()method in a network access manager of your own. There you can access the URL and the headers sent in the request and change them or cancel the request.But… you want to access to the request and replied data for each request.
Access to the request is easy, it’s already been explained by the override to the
createRequest()method in a customQNetworkAccessManager. But let’s talk about the other part:For what I’ve seen, the reply obtained from the request is read-only and IT CAN’T BE CHANGED, as it’s hard-coded in the source code for
QNetworkReply.You cannot use the
writeData()function ofQNetworkReply, as it is hardcoded to simply doreturn -1!!!You can try to subclass a
QNetworkReplyof your own and return it in thecreateRequest()method of a customQNetworkAccessManager. You must override the functionsreadAll(),bytesAvailable(), andabort(). Curiously, this method works only if the request is made to a non-HTTP destination. I think the internals ofQNetworkAccessManagerinvokes different implementations of theQNetworkReplydepending of the protocol used (HTTP, FTP, …). So this worked in a sense, but not in the way we want. This method can be used to return a predefined web page under certain conditions, for example.Considering the fact that
QNetworkAccessManager::createRequest()must return aQNetworkReplyobject, I cannot see any combination of subclassing that permits tampering in the replied byte array. It’s really well protected and you must reimplement everything on those classes to be able to achieve your goal. Almost copy-paste the source classes of all theQtNetworkpart and make your own implementation.So I think that the answer would be: “No, it seems that it cannot be done exactly as you ask; only partially“.
Sorry.