From 45e2887ce81a0de8069b7795eb2f55b5cebccc56 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 3 Aug 2019 11:20:19 +0200 Subject: [PATCH] Set our own custom User-Agent on network requests. Format: chatterino/VERSION (COMMIT HASH) Fixes #1169 --- chatterino.pro | 17 +++++++++++++++++ src/common/NetworkRequest.cpp | 14 ++++++++++++++ src/common/NetworkRequest.hpp | 2 ++ 3 files changed, 33 insertions(+) diff --git a/chatterino.pro b/chatterino.pro index 8c1479c71..e0d4234ae 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -446,3 +446,20 @@ linux { INSTALLS += desktop build_icons icon target } + +git_commit=$$(GIT_COMMIT) +git_release=$$(GIT_RELEASE) +# Git data +isEmpty(git_commit) { +git_commit=$$system(git rev-parse HEAD) +} +isEmpty(git_release) { +git_release=$$system(git describe) +} +git_hash = $$str_member($$git_commit, 0, 8) + +# Passing strings as defines requires you to use this weird triple-escape then quotation mark syntax. +# https://stackoverflow.com/questions/3348711/add-a-define-to-qmake-with-a-value/18343449#18343449 +DEFINES += CHATTERINO_GIT_COMMIT=\\\"$$git_commit\\\" +DEFINES += CHATTERINO_GIT_RELEASE=\\\"$$git_release\\\" +DEFINES += CHATTERINO_GIT_HASH=\\\"$$git_hash\\\" diff --git a/src/common/NetworkRequest.cpp b/src/common/NetworkRequest.cpp index b4192e8e6..e813794c7 100644 --- a/src/common/NetworkRequest.cpp +++ b/src/common/NetworkRequest.cpp @@ -3,6 +3,7 @@ #include "common/NetworkData.hpp" #include "common/NetworkManager.hpp" #include "common/Outcome.hpp" +#include "common/Version.hpp" #include "debug/Log.hpp" #include "providers/twitch/TwitchCommon.hpp" #include "singletons/Paths.hpp" @@ -22,6 +23,8 @@ NetworkRequest::NetworkRequest(const std::string &url, { this->data->request_.setUrl(QUrl(QString::fromStdString(url))); this->data->requestType_ = requestType; + + this->initializeDefaultValues(); } NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType) @@ -30,6 +33,8 @@ NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType) { this->data->request_.setUrl(url); this->data->requestType_ = requestType; + + this->initializeDefaultValues(); } NetworkRequest::~NetworkRequest() @@ -166,6 +171,15 @@ QString NetworkRequest::urlString() const return this->data->request_.url().toString(); } +void NetworkRequest::initializeDefaultValues() +{ + const auto userAgent = QString("chatterino/%1 (%2)") + .arg(CHATTERINO_VERSION, CHATTERINO_GIT_HASH) + .toUtf8(); + + this->data->request_.setRawHeader("User-Agent", userAgent); +} + Outcome NetworkRequest::tryLoadCachedFile() { QFile cachedFile(getPaths()->cacheDirectory() + "/" + diff --git a/src/common/NetworkRequest.hpp b/src/common/NetworkRequest.hpp index cb6407686..d0179847a 100644 --- a/src/common/NetworkRequest.hpp +++ b/src/common/NetworkRequest.hpp @@ -60,6 +60,8 @@ public: QString urlString() const; private: + void initializeDefaultValues(); + // "invalid" data "invalid" is specified by the onSuccess callback Outcome tryLoadCachedFile();