LCOV - code coverage report
Current view: top level - src/utilities - NetworkUtilities.cpp (source / functions) Coverage Total Hit
Test: coverage.info.cleaned Lines: 66.0 % 50 33
Test Date: 2026-01-27 22:31:25 Functions: 33.3 % 6 2

            Line data    Source code
       1              : #include "NetworkUtilities.h"
       2              : 
       3              : #include <QNetworkDiskCache>
       4              : #include <QStandardPaths>
       5              : 
       6              : 
       7            0 : NetworkUtilities::NetworkUtilities()
       8              : {
       9            0 : }
      10              : 
      11            8 : QUrl NetworkUtilities::getHost(const QUrl& url) {
      12            8 :     QUrl ret = url;
      13            8 :     ret.setPath("");
      14            8 :     ret.setQuery("");
      15            8 :     QString sUrl = ret.toString();
      16            8 :     if (sUrl.endsWith("?"))
      17            8 :         sUrl = sUrl.remove(sUrl.length() - 1, 1);
      18              :     
      19            8 :     ret.setUrl(sUrl);
      20           16 :     return ret;
      21            8 : }
      22              : 
      23            0 : void NetworkUtilities::addCache(QNetworkAccessManager *manager)
      24              : {
      25              :     // Set default cache dir.
      26            0 :     QNetworkDiskCache* diskCache = new QNetworkDiskCache(manager);
      27            0 :     QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
      28            0 :     diskCache->setCacheDirectory(cacheDir);
      29            0 :     if (!cacheDir.isEmpty())
      30            0 :         manager->setCache(diskCache);
      31            0 : }
      32              : 
      33            0 : void NetworkUtilities::useCache(QNetworkRequest* request)
      34              : {
      35              :     // This SHOULD be the default, but just in case.
      36            0 :     request->setAttribute(QNetworkRequest::CacheLoadControlAttribute,
      37              :                           QNetworkRequest::PreferNetwork);
      38            0 : }
      39              : 
      40            0 : void NetworkUtilities::fakeBrowserHeaders(QNetworkRequest* request)
      41              : {
      42              :     // We have to pretend to be Firefox in order for some stupid servers to speak with us.
      43            0 :     request->setHeader(QNetworkRequest::UserAgentHeader,
      44              :                       "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0");
      45              :     
      46              :     // Required for blogs.gnome.org.
      47            0 :     request->setRawHeader("Accept",
      48              :                           "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
      49            0 : }
      50              : 
      51           47 : QString NetworkUtilities::urlFixup(const QString &url, QUrl baseURL)
      52              : {
      53              : 
      54           47 :     QUrl qURL(url);
      55              : 
      56              :     // If the URL is valid and not relative, there's nothing more to do.
      57           47 :     if (!qURL.isRelative() && qURL.isValid()) {
      58           37 :         return url;
      59              :     }
      60              : 
      61              :     // If the URL is relative, try prepending the base URL.
      62           10 :     if (qURL.isRelative() && !baseURL.isRelative()) {
      63            4 :         QUrl urlRet(baseURL);
      64            4 :         if (url.startsWith("/")) {
      65              :             // Absolute path
      66            2 :             urlRet.setPath(url);
      67              :         } else {
      68              :             // Relative path
      69            2 :             QString urlPath = baseURL.path() + "/" + url; // Build new path
      70            2 :             urlPath = urlPath.replace("//", "/");  // Remove redudant slashes
      71            2 :             urlRet.setPath(urlPath);
      72            2 :         }
      73              : 
      74            4 :         return urlRet.toString();
      75            4 :     }
      76              : 
      77              :     // Attempt to correct basic protocol errors.
      78            6 :     if (url.startsWith("//")) {
      79              :         // Just assume it's http.
      80            1 :         return "http:" + url;
      81            5 :     } else if (url.startsWith("/")) {
      82              :         // Same as above, but we have to add an extra slash.
      83            2 :         return "http:/" + url;
      84            3 :     } else if (url.startsWith(":")) {
      85              :         // Starts with a :, assume everything after it is valid.
      86            1 :         return "http" + url;
      87              :     }
      88              : 
      89            2 :     if (!url.contains(':') && url.size() && url.at(0).isLetterOrNumber()) {
      90              :         // Also assume it's http.
      91            2 :         return "http://" + url;
      92              :     }
      93              : 
      94              :     // Hopefully it's correct!
      95            0 :     return url;
      96           47 : }
        

Generated by: LCOV version 2.0-1