view ui/downloader.cpp @ 10:fe39d93f1261

Start on Downloader component
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 13 Feb 2014 14:43:15 +0000
parents
children 7e2f14c7aba2
line wrap: on
line source
#include "downloader.h"

#ifndef MYVERSION
#define MYVERSION "1"
#endif

#ifndef DOWNLOAD_SERVER
#define DOWNLOAD_SERVER "https://www.intevation.de"
#endif

#ifdef Q_OS_WIN
#include <windows.h>
#include <winhttp.h>
#endif

#include <QFile>
#include <QDebug>

Downloader::Downloader(QObject* parent, const QString& url)
{
    Downloader (parent, url, QFile(":Certificates/https").readAll());
}

Downloader::Downloader(QObject* parent, const QString& url,
                       const QByteArray& certificate) :
    mUrl(url),
    mCert(certificate),
    QThread(parent)
{
}

void Downloader::run() {
#ifdef Q_OS_WIN
    // We use WinAPI here instead of Qt because we want to avoid
    // QtNetworks SSL stack which is based on OpenSSL and so
    // we might be incompatible with GPL code. Also using the
    // native API means that the security of the SSL implementation
    // is tied to the security of the system.
    BOOL bResults = FALSE;
    HINTERNET hSession = NULL,
              hConnect = NULL,
              hRequest = NULL;
    SYSTEMTIME lastModified;
    DWORD sizeOfSystemtime = sizeof (SYSTEMTIME);

    // Get a syncronous session handle
    hSession = WinHttpOpen(L"M13 "MYVERSION,
                           WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                           WINHTTP_NO_PROXY_NAME,
                           WINHTTP_NO_PROXY_BYPASS, 0);

    if (hSession) {
        // Initialize connection. No request is done here.
        hConnect = WinHttpConnect(hSession, L""DOWNLOAD_SERVER,
                                  INTERNET_DEFAULT_HTTPS_PORT, 0);

    }

    if (hConnect) {
        // Make a head request
        hRequest = WinHttpOpenRequest(hConnect, L"HEAD",
                                      L"/index.html",
                                      NULL, WINHTTP_NO_REFERER,
                                      WINHTTP_DEFAULT_ACCEPT_TYPES,
                                      0);
    }

    if (hRequest) {
        bResults = WinHttpSendRequest(hRequest,
                                      WINHTTP_NO_ADDITIONAL_HEADERS,
                                      0, WINHTTP_NO_REQUEST_DATA, 0, 
                                      0, 0);
    }

    if (bResults) {
        bResults = WinHttpReceiveResponse(hRequest, NULL);
    }



    if (bResults) {
        bResults = WinHttpQueryHeaders(hRequest,
                                       WINHTTP_QUERY_LAST_MODIFIED |
                                       WINHTTP_QUERY_FLAG_SYSTEMTIME,
                                       NULL,
                                       &lastModified,
                                       &sizeOfSystemtime,
                                       WINHTTP_NO_HEADER_INDEX );
    }

    qDebug() << "Last modified year: " << lastModified.wYear;


    if (!bResults) {
        // Report any errors.
        qDebug() << "Error" << GetLastError();
        emit error(tr("Unknown Problem when connecting"), Unknown);
    }

    // Cleanup
    if (hRequest) {
        WinHttpCloseHandle(hRequest);
    }

    if (hConnect) {
        WinHttpCloseHandle(hConnect);

    }

    if (hSession) {
        WinHttpCloseHandle(hSession);
    }
#endif

    for (int i=0; i< 10; i++) {
        qDebug("Going to sleep\n");
        sleep(10);
    }
}

http://wald.intevation.org/projects/trustbridge/