VirtualBox

Ignore:
Timestamp:
Oct 14, 2015 1:53:49 PM (9 years ago)
Author:
vboxsync
Message:

FE/Qt: Networking cleanup/rework (part 4): Support for our own HEAD request type based on RTHttp stuff.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/net
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkDefs.h

    r55401 r58242  
    2323{
    2424    UINetworkRequestType_HEAD,
     25    UINetworkRequestType_HEAD_Our,
    2526    UINetworkRequestType_GET,
    2627    UINetworkRequestType_GET_Our
  • trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkReply.cpp

    r58238 r58242  
    6363public:
    6464
    65     /* Constructor: */
    66     UINetworkReplyPrivateThread(const QNetworkRequest &request);
     65    /** Constructs network reply thread for the passed @a request of the passed @a type. */
     66    UINetworkReplyPrivateThread(const QNetworkRequest &request, UINetworkRequestType type);
    6767
    6868    /** Returns short descriptive context of thread's current operation. */
     
    7777    /** Abort HTTP request. */
    7878    void abort();
     79    /** Returns value for the cached reply header of the passed @a type. */
     80    QString header(QNetworkRequest::KnownHeaders type) const
     81    {
     82        /* Look for known header type: */
     83        switch (type)
     84        {
     85            case QNetworkRequest::ContentTypeHeader:   return m_headers.value("Content-Type");
     86            case QNetworkRequest::ContentLengthHeader: return m_headers.value("Content-Length");
     87            case QNetworkRequest::LastModifiedHeader:  return m_headers.value("Last-Modified");
     88            default: break;
     89        }
     90        /* Return null-string by default: */
     91        return QString();
     92    }
    7993    /** @} */
    8094
     
    119133    /* Variables: */
    120134    QNetworkRequest m_request;
     135    /** Holds the request type. */
     136    UINetworkRequestType m_type;
    121137    int m_iError;
    122138    /** IPRT HTTP client instance handle. */
    123139    RTHTTP m_hHttp;
    124140    QByteArray m_reply;
     141    /* Holds the cached reply headers. */
     142    QMap<QString, QString> m_headers;
    125143
    126144    static const char * const s_apszRootsZipUrls[];
     
    201219
    202220
    203 UINetworkReplyPrivateThread::UINetworkReplyPrivateThread(const QNetworkRequest &request)
     221UINetworkReplyPrivateThread::UINetworkReplyPrivateThread(const QNetworkRequest &request, UINetworkRequestType type)
    204222    : m_request(request)
     223    , m_type(type)
    205224    , m_iError(VINF_SUCCESS)
    206225    , m_hHttp(NIL_RTHTTP)
     
    359378    int rc = 0;
    360379
    361     /* Perform blocking HTTP GET request: */
    362     void   *pvResponse = 0;
    363     size_t  cbResponse = 0;
    364     rc = RTHttpGetBinary(m_hHttp, m_request.url().toString().toUtf8().constData(), &pvResponse, &cbResponse);
    365     if (RT_SUCCESS(rc))
    366     {
    367         m_reply = QByteArray((char*)pvResponse, cbResponse);
    368         RTHttpFreeResponse(pvResponse);
     380    /* Depending on request type: */
     381    switch (m_type)
     382    {
     383        case UINetworkRequestType_HEAD_Our:
     384        {
     385            /* Perform blocking HTTP HEAD request: */
     386            void   *pvResponse = 0;
     387            size_t  cbResponse = 0;
     388            rc = RTHttpGetHeaderBinary(m_hHttp, m_request.url().toString().toUtf8().constData(), &pvResponse, &cbResponse);
     389            if (RT_SUCCESS(rc))
     390            {
     391                m_reply = QByteArray((char*)pvResponse, cbResponse);
     392                RTHttpFreeResponse(pvResponse);
     393            }
     394
     395            /* Paranoia: */
     396            m_headers.clear();
     397
     398            /* Parse header contents: */
     399            const QString strHeaders = QString(m_reply);
     400            const QStringList headers = strHeaders.split("\n", QString::SkipEmptyParts);
     401            foreach (const QString &strHeader, headers)
     402            {
     403                const QStringList values = strHeader.split(": ", QString::SkipEmptyParts);
     404                if (values.size() > 1)
     405                    m_headers[values.at(0)] = values.at(1);
     406            }
     407
     408            break;
     409        }
     410        case UINetworkRequestType_GET_Our:
     411        {
     412            /* Perform blocking HTTP GET request: */
     413            void   *pvResponse = 0;
     414            size_t  cbResponse = 0;
     415            rc = RTHttpGetBinary(m_hHttp, m_request.url().toString().toUtf8().constData(), &pvResponse, &cbResponse);
     416            if (RT_SUCCESS(rc))
     417            {
     418                m_reply = QByteArray((char*)pvResponse, cbResponse);
     419                RTHttpFreeResponse(pvResponse);
     420            }
     421
     422            break;
     423        }
     424        default:
     425            break;
    369426    }
    370427
     
    749806
    750807    /* Constructor: */
    751     UINetworkReplyPrivate(const QNetworkRequest &request)
     808    UINetworkReplyPrivate(const QNetworkRequest &request, UINetworkRequestType type)
    752809        : m_error(QNetworkReply::NoError)
    753810        , m_pThread(0)
     
    756813        m_strErrorTemplate = tr("%1: %2", "Context description: Error description");
    757814        /* Create and run network-reply thread: */
    758         m_pThread = new UINetworkReplyPrivateThread(request);
     815        m_pThread = new UINetworkReplyPrivateThread(request, type);
    759816        connect(m_pThread, SIGNAL(finished()), this, SLOT(sltFinished()));
    760817        m_pThread->start();
     
    802859    /* API: Reply getter: */
    803860    QByteArray readAll() { return m_pThread->readAll(); }
     861
     862    /** Returns value for the cached reply header of the passed @a type. */
     863    QString header(QNetworkRequest::KnownHeaders type) const { return m_pThread->header(type); }
    804864
    805865private slots:
     
    854914            m_pReply = gNetworkManager->head(request);
    855915            break;
     916        /* Prepare our network-reply (HEAD): */
     917        case UINetworkRequestType_HEAD_Our:
     918            m_replyType = UINetworkReplyType_Our;
     919            m_pReply = new UINetworkReplyPrivate(request, UINetworkRequestType_HEAD_Our);
     920            break;
    856921        /* Prepare Qt network-reply (GET): */
    857922        case UINetworkRequestType_GET:
     
    862927        case UINetworkRequestType_GET_Our:
    863928            m_replyType = UINetworkReplyType_Our;
    864             m_pReply = new UINetworkReplyPrivate(request);
     929            m_pReply = new UINetworkReplyPrivate(request, UINetworkRequestType_GET_Our);
    865930            break;
    866931    }
     
    887952    {
    888953        case UINetworkReplyType_Qt: result = qobject_cast<QNetworkReply*>(m_pReply)->header(header); break;
    889         case UINetworkReplyType_Our: /* TODO: header() */ break;
     954        case UINetworkReplyType_Our: result = qobject_cast<UINetworkReplyPrivate*>(m_pReply)->header(header); break;
    890955    }
    891956    return result;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette