Abort http requests when we quit the program

This commit is contained in:
David Capello 2016-05-17 12:14:01 -03:00
parent cf380ab1a2
commit 61a933bee2
4 changed files with 23 additions and 43 deletions

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
// Copyright (C) 2001-2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
@ -27,50 +27,33 @@ static const int kMonitoringPeriod = 100;
namespace app {
class CheckUpdateBackgroundJob : public updater::CheckUpdateDelegate
{
class CheckUpdateBackgroundJob : public updater::CheckUpdateDelegate {
public:
CheckUpdateBackgroundJob()
: m_canceled(false)
, m_received(false) { }
: m_received(false) { }
virtual ~CheckUpdateBackgroundJob() { }
void cancel()
{
m_canceled = true;
void abort() {
m_checker.abort();
}
bool isCanceled() const
{
return m_canceled;
}
bool isReceived() const
{
bool isReceived() const {
return m_received;
}
void sendRequest(const updater::Uuid& uuid, const std::string& extraParams)
{
void sendRequest(const updater::Uuid& uuid, const std::string& extraParams) {
m_checker.checkNewVersion(uuid, extraParams, this);
}
const updater::CheckUpdateResponse& getResponse() const
{
const updater::CheckUpdateResponse& getResponse() const {
return m_response;
}
private:
// CheckUpdateDelegate implementation
virtual void onResponse(updater::CheckUpdateResponse& data)
{
void onResponse(updater::CheckUpdateResponse& data) override {
m_response = data;
m_received = true;
}
bool m_canceled;
bool m_received;
updater::CheckUpdate m_checker;
updater::CheckUpdateResponse m_response;
@ -117,7 +100,7 @@ CheckUpdateThreadLauncher::~CheckUpdateThreadLauncher()
if (m_thread) {
if (m_bgJob)
m_bgJob->cancel();
m_bgJob->abort();
m_thread->join();
}

View File

@ -71,7 +71,7 @@ public:
void abort()
{
// TODO
curl_easy_cleanup(m_curl);
}
private:

View File

@ -12,8 +12,9 @@
#include "updater/check_update.h"
#include "base/bind.h"
#include "base/debug.h"
#include "base/convert_to.h"
#include "base/debug.h"
#include "base/unique_ptr.h"
#include "net/http_headers.h"
#include "net/http_request.h"
#include "net/http_response.h"
@ -91,14 +92,12 @@ public:
void abort()
{
// TODO impl
if (m_request)
m_request->abort();
}
void checkNewVersion(const Uuid& uuid, const std::string& extraParams, CheckUpdateDelegate* delegate)
{
using namespace base;
using namespace net;
#ifndef UPDATE_URL
#define UPDATE_URL ""
#pragma message("warning: Define UPDATE_URL macro")
@ -114,14 +113,14 @@ public:
url += extraParams;
}
HttpRequest request(url);
HttpHeaders headers;
m_request.reset(new net::HttpRequest(url));
net::HttpHeaders headers;
headers.setHeader("User-Agent", getUserAgent());
request.setHeaders(headers);
m_request->setHeaders(headers);
std::stringstream body;
HttpResponse response(&body);
request.send(response);
net::HttpResponse response(&body);
m_request->send(response);
TRACE("Checking updates: %s (User-Agent: %s)\n", url.c_str(), getUserAgent().c_str());
TRACE("Response:\n--\n%s--\n", body.str().c_str());
@ -130,6 +129,8 @@ public:
delegate->onResponse(data);
}
private:
base::UniquePtr<net::HttpRequest> m_request;
};
CheckUpdate::CheckUpdate()

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
// Copyright (C) 2001-2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
@ -63,10 +63,6 @@ namespace updater {
public:
virtual ~CheckUpdateDelegate() { }
// Returns true when the user is quitting the application so he does
// not want to continue checking for updates.
// virtual bool abortRequest() = 0;
// Called by CheckUpdate::checkNewVersion() when the response from
// the "updates server" is received.
virtual void onResponse(CheckUpdateResponse& data) = 0;