简体   繁体   中英

Custom QSslSocket for QNetworkAccessManager

In my project I need to use a specific version of OpenSSL. I'm using both Qt 4.8.6 and Qt 5.4.0. I'd like to create a custom QSslSocket to be passed to QNetworkAccessManager, which will be used for a QWebView.

I noticed that in Qt 4.8.6 only TLS 1.0 is supported, newer protocol versions aren't.

Is there a way to pass a subclassed QSslSocket (with a TLS 1.2 version) to QNetworkAccessManager in an easy way? Looking at the source code, it is hidden from public usage (QSslSocket is a friend of private implementation)?

Note: I don't want to use QHttp because it's not public anymore in newer Qt libraries, making it hard to be portable.

Edit: There's a similar question ( QNetworkAccessManager/QNetworkReply with custom QTcpSocket? ), made 5 years ago, but it still cannot be possible to modify the QSslSocket directly. The answer given back then is too generic

I think I've found a solution. In createRequest I can use my custom socket/ssl class and then pass the read data to a new custom QNetworkReply object which will set these data in Qt format (from char* to QByteArray). So far I've tested it and it works.

//see http://code.woboq.org/kde/qt4/src/network/access/qnetworkreplydataimpl_p.h.html
class SubclassedNetworkReply : public QNetworkReply
{
  public:
    SubclassedNetworkReply(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op, char* data);

    void abort() override;
    void close() override;
    qint64 bytesAvailable() const override;
    qint64 readData(char *data, qint64 maxlen) override;
    bool isSequential () const override;
};

QNetworkReply* SubclassedNetworkAccessManager::createRequest(...)
{
  if(url.scheme().contains("https"))
  {
    //Here you can use your custom QSslSocket/SSL Class to get the char* data

    //Here you create your custom reply, which will acquire the char* data and convert it to a QByteArray which will shown in the QWebView
    QNetworkReply *reply = new SubclassedNetworkReply(this, request, operation, data);
    return reply;  
  }
  return QNetworkAccessManager::createRequest(...);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM