comparison ui/sslconnection_bare.cpp @ 910:eaed02defe6a

More SSLConnection refactoring. Fixes curl downloader.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 18 Aug 2014 18:51:33 +0200
parents d1c951b3012d
children 9bcd8ed36f4b
comparison
equal deleted inserted replaced
909:18e3ad073b38 910:eaed02defe6a
9 * see programs/ssl/ssl_client2.c for example of session reuse */ 9 * see programs/ssl/ssl_client2.c for example of session reuse */
10 #include "sslconnection_bare.h" 10 #include "sslconnection_bare.h"
11 #include "sslhelp.h" 11 #include "sslhelp.h"
12 12
13 #include <QFile> 13 #include <QFile>
14 #include <QSaveFile>
14 #include <QUuid> 15 #include <QUuid>
15 #include <QApplication> 16 #include <QApplication>
16 17
17 #define MAX_IO_TRIES 10 18 #define MAX_IO_TRIES 10
18 #define MAX_RESETS 10 19 #define MAX_RESETS 10
357 } while (len > 0 && tries < MAX_IO_TRIES); 358 } while (len > 0 && tries < MAX_IO_TRIES);
358 359
359 return retval; 360 return retval;
360 } 361 }
361 362
362 363 QMap<QString, QString> SSLConnectionBare::parseHeaders(QByteArray *data)
364 {
365 int bodyStart = data->indexOf("\r\n\r\n");
366 QMap<QString, QString> retval;
367 QByteArray headers;
368 QString response(*data);
369 if (bodyStart == -1) {
370 qDebug() << "Could not find header end.";
371 return retval;
372 }
373
374 /* Take the headers with one additional line break */
375 headers = data->left(bodyStart + 2);
376 /* Chop off the head */
377
378 foreach (const QString& line, response.split("\r\n")) {
379 int sepPos = -1;
380 sepPos = line.indexOf(": ");
381 if (sepPos == -1) {
382 continue;
383 }
384 QString key = line.left(sepPos);
385 QString value = line.right(line.size() - sepPos - 2);
386
387 retval.insert(key, value);
388 }
389
390 *data = data->right(data->size() - bodyStart - 4);
391 return retval;
392 }
393
394 QDateTime SSLConnectionBare::getLastModifiedHeader(const QString &resource) {
395 int ret = -1;
396 QByteArray response;
397 QLocale cLocale = QLocale::c();
398 QMap<QString, QString> headers;
399 QString headRequest =
400 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource);
401
402 ret = write(headRequest.toUtf8());
403 if (ret != 0) {
404 return QDateTime();
405 }
406
407 response = read(1024);
408
409 qDebug() << "Response from server was: " << response;
410
411 if (response.isNull()) {
412 qDebug() << "No response";
413 return QDateTime();
414 }
415
416 headers = parseHeaders(&response);
417 const QString lastModified = headers.value("Last-Modified");
418 if (!lastModified.isEmpty()) {
419 QDateTime candidate = cLocale.toDateTime(lastModified,
420 "ddd, dd MMM yyyy HH:mm:ss' GMT'");
421 if (candidate.isValid()) {
422 return candidate;
423 }
424 }
425 return QDateTime();
426 }
427
428 bool SSLConnectionBare::downloadFile(const QString &resource,
429 const QString &fileName,
430 size_t maxSize)
431 {
432 int ret = -1;
433 size_t bytesRead = 0;
434 QString getRequest =
435 QString::fromLatin1("GET %1 HTTP/1.0\r\n\r\n").arg(resource);
436
437 QSaveFile outputFile(fileName);
438
439 ret = write(getRequest.toUtf8());
440
441 // Open / Create the file to write to.
442 if (!outputFile.open(QIODevice::WriteOnly)) {
443 qDebug() << "Failed to open file";
444 return false;
445 }
446
447 bool inBody = false;
448 QMap <QString, QString> headers;
449 do {
450 /* Read the response in 8KiB chunks */
451 int responseSize = 0;
452 QByteArray response = read(8192);
453 if (response.isNull()) {
454 qDebug() << "Error reading response";
455 return false;
456 }
457 responseSize = response.size();
458 if (!inBody) {
459 headers = parseHeaders(&response);
460 inBody = true;
461 }
462 outputFile.write(response);
463 bytesRead += responseSize;
464 if (responseSize < 8192) {
465 /* Nothing more to read */
466 break;
467 }
468 } while (bytesRead < maxSize);
469
470 return outputFile.commit();
471 }
472

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