Skip to content

Commit

Permalink
Merge branch 'custom-http-headers'
Browse files Browse the repository at this point in the history
  • Loading branch information
vslavik committed Apr 14, 2019
2 parents 309709e + cc790f0 commit 506c0b2
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 7 deletions.
10 changes: 10 additions & 0 deletions examples/psdk/app_psdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ int APIENTRY WinMain(HINSTANCE hInstance,

ShowWindow(g_hwndMain, nCmdShow);

/*
Configure WinSparkle using win_sparkle_set_* functions *before*
calling win_sparkle_init(). This is optional and the following example
of doing that is just that, an example.
See e.g. https://github.com/vslavik/poedit/blob/master/src/edapp.cpp
for another, real-life example of use.
*/
win_sparkle_set_http_header("X-Product-License", "Pro");

/* Initialize WinSparkle as soon as the app itself is initialized, right
before entering the event loop. This also checks for updates and
shows update UI if there any. */
Expand Down
2 changes: 1 addition & 1 deletion include/winsparkle-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*--------------------------------------------------------------------------*/

#define WIN_SPARKLE_VERSION_MAJOR 0
#define WIN_SPARKLE_VERSION_MINOR 6
#define WIN_SPARKLE_VERSION_MINOR 7
#define WIN_SPARKLE_VERSION_MICRO 0

/**
Expand Down
19 changes: 19 additions & 0 deletions include/winsparkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ WIN_SPARKLE_API void __cdecl win_sparkle_set_app_details(const wchar_t *company_
*/
WIN_SPARKLE_API void __cdecl win_sparkle_set_app_build_version(const wchar_t *build);

/**
Set custom HTTP header for appcast checks.
@since 0.7
@see win_sparkle_clear_http_headers()
*/
WIN_SPARKLE_API void __cdecl win_sparkle_set_http_header(const char *name, const char *value);

/**
Clears all custom HTTP headers previously added using
win_sparkle_set_http_header().
@since 0.7
@see win_sparkle_set_http_header()
*/
WIN_SPARKLE_API void __cdecl win_sparkle_clear_http_headers();

/**
Set the registry path where settings will be stored.
Expand Down
18 changes: 18 additions & 0 deletions src/dll_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ WIN_SPARKLE_API void __cdecl win_sparkle_set_app_details(const wchar_t *company_
CATCH_ALL_EXCEPTIONS
}

WIN_SPARKLE_API void __cdecl win_sparkle_set_http_header(const char *name, const char *value)
{
try
{
Settings::SetHttpHeader(name, value);
}
CATCH_ALL_EXCEPTIONS
}

WIN_SPARKLE_API void __cdecl win_sparkle_clear_http_headers()
{
try
{
Settings::ClearHttpHeaders();
}
CATCH_ALL_EXCEPTIONS
}

WIN_SPARKLE_API void __cdecl win_sparkle_set_app_build_version(const wchar_t *build)
{
try
Expand Down
6 changes: 3 additions & 3 deletions src/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void WaitUntilSignaledWithTerminationCheck(Event& event, Thread *thread)
public functions
*--------------------------------------------------------------------------*/

void DownloadFile(const std::string& url, IDownloadSink *sink, Thread *onThread, int flags)
void DownloadFile(const std::string& url, IDownloadSink *sink, Thread *onThread, const std::string &headers, int flags)
{
char url_path[2048];
URL_COMPONENTSA urlc;
Expand Down Expand Up @@ -234,8 +234,8 @@ void DownloadFile(const std::string& url, IDownloadSink *sink, Thread *onThread,
(
inet,
url.c_str(),
NULL, // lpszHeaders
-1, // dwHeadersLength
headers.c_str(),
(DWORD)headers.length(),
dwFlags,
(DWORD_PTR)&context // dwContext
);
Expand Down
2 changes: 1 addition & 1 deletion src/download.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ enum DownloadFlag
@see CheckConnection()
*/
void DownloadFile(const std::string& url, IDownloadSink *sink, Thread *onThread, int flags = 0);
void DownloadFile(const std::string& url, IDownloadSink *sink, Thread *onThread, const std::string &headers = "", int flags = 0);

} // namespace winsparkle

Expand Down
3 changes: 2 additions & 1 deletion src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ std::wstring Settings::ms_companyName;
std::wstring Settings::ms_appName;
std::wstring Settings::ms_appVersion;
std::wstring Settings::ms_appBuildVersion;
std::string Settings::ms_DSAPubKey;
std::string Settings::ms_DSAPubKey;
std::map<std::string, std::string> Settings::ms_httpHeaders;


/*--------------------------------------------------------------------------*
Expand Down
26 changes: 26 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "threads.h"
#include "utils.h"

#include <map>
#include <string>
#include <sstream>

Expand Down Expand Up @@ -196,6 +197,30 @@ class Settings
ms_appVersion = version;
}

/// Add a custom HTT header to requests
static void SetHttpHeader(const char *name, const char *value)
{
CriticalSectionLocker lock(ms_csVars);
ms_httpHeaders[name] = value;
}

/// Get a string containing all custom HTTP headers
static std::string GetHttpHeadersString()
{
CriticalSectionLocker lock(ms_csVars);
std::string out;
for (auto i = ms_httpHeaders.begin(); i != ms_httpHeaders.end(); ++i)
out += i->first + ": " + i->second + "\r\n";
return out;
}

/// Clear previously set HTTP headers
static void ClearHttpHeaders()
{
CriticalSectionLocker lock(ms_csVars);
ms_httpHeaders.clear();
}

/// Set application's build version number
static void SetAppBuildVersion(const wchar_t *version)
{
Expand Down Expand Up @@ -323,6 +348,7 @@ class Settings
static std::wstring ms_appVersion;
static std::wstring ms_appBuildVersion;
static std::string ms_DSAPubKey;
static std::map<std::string, std::string> ms_httpHeaders;
};

} // namespace winsparkle
Expand Down
2 changes: 1 addition & 1 deletion src/updatechecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void UpdateChecker::PerformUpdateCheck()
CheckForInsecureURL(url, "appcast feed");

StringDownloadSink appcast_xml;
DownloadFile(url, &appcast_xml, this, Download_BypassProxies);
DownloadFile(url, &appcast_xml, this, Settings::GetHttpHeadersString(), Download_BypassProxies);

Appcast appcast = Appcast::Load(appcast_xml.data);
if (!appcast.ReleaseNotesURL.empty())
Expand Down

0 comments on commit 506c0b2

Please sign in to comment.