HaikuDepot: Enhancements to the User-Agent header to include the version.
Signed-off-by: Augustin Cavalier <waddlesplash@gmail.com> Fixes #12262.
This commit is contained in:
parent
3749038b53
commit
b8d10c6dc6
@ -7,22 +7,28 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <AppFileInfo.h>
|
||||||
|
#include <Application.h>
|
||||||
|
#include <Autolock.h>
|
||||||
#include <File.h>
|
#include <File.h>
|
||||||
#include <HttpHeaders.h>
|
#include <HttpHeaders.h>
|
||||||
#include <HttpRequest.h>
|
#include <HttpRequest.h>
|
||||||
#include <Json.h>
|
#include <Json.h>
|
||||||
#include <Message.h>
|
#include <Message.h>
|
||||||
|
#include <Roster.h>
|
||||||
#include <Url.h>
|
#include <Url.h>
|
||||||
#include <UrlContext.h>
|
#include <UrlContext.h>
|
||||||
#include <UrlProtocolListener.h>
|
#include <UrlProtocolListener.h>
|
||||||
#include <UrlProtocolRoster.h>
|
#include <UrlProtocolRoster.h>
|
||||||
|
|
||||||
|
#include "AutoLocker.h"
|
||||||
#include "List.h"
|
#include "List.h"
|
||||||
#include "PackageInfo.h"
|
#include "PackageInfo.h"
|
||||||
|
|
||||||
|
|
||||||
#define CODE_REPOSITORY_DEFAULT "haikuports"
|
#define CODE_REPOSITORY_DEFAULT "haikuports"
|
||||||
#define BASEURL_DEFAULT "https://depot.haiku-os.org"
|
#define BASEURL_DEFAULT "https://depot.haiku-os.org"
|
||||||
|
#define USERAGENT_FALLBACK_VERSION "0.0.0"
|
||||||
|
|
||||||
|
|
||||||
class JsonBuilder {
|
class JsonBuilder {
|
||||||
@ -274,7 +280,8 @@ enum {
|
|||||||
|
|
||||||
|
|
||||||
BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT);
|
BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT);
|
||||||
|
BString WebAppInterface::fUserAgent = BString();
|
||||||
|
BLocker WebAppInterface::fUserAgentLocker = BLocker();
|
||||||
|
|
||||||
WebAppInterface::WebAppInterface()
|
WebAppInterface::WebAppInterface()
|
||||||
:
|
:
|
||||||
@ -337,7 +344,7 @@ arguments_is_url_valid(const BString& value)
|
|||||||
|
|
||||||
BString scheme;
|
BString scheme;
|
||||||
value.CopyInto(scheme, 0, schemeEnd);
|
value.CopyInto(scheme, 0, schemeEnd);
|
||||||
|
|
||||||
if (scheme != "http" && scheme != "https") {
|
if (scheme != "http" && scheme != "https") {
|
||||||
fprintf(stderr, "the url scheme should be 'http' or 'https'\n");
|
fprintf(stderr, "the url scheme should be 'http' or 'https'\n");
|
||||||
return false;
|
return false;
|
||||||
@ -356,7 +363,7 @@ arguments_is_url_valid(const BString& value)
|
|||||||
indicate if the URL was acceptable.
|
indicate if the URL was acceptable.
|
||||||
\return B_OK if the base URL was valid and B_BAD_VALUE if not.
|
\return B_OK if the base URL was valid and B_BAD_VALUE if not.
|
||||||
*/
|
*/
|
||||||
status_t
|
status_t
|
||||||
WebAppInterface::SetBaseUrl(const BString& url)
|
WebAppInterface::SetBaseUrl(const BString& url)
|
||||||
{
|
{
|
||||||
if (!arguments_is_url_valid(url))
|
if (!arguments_is_url_valid(url))
|
||||||
@ -368,6 +375,60 @@ WebAppInterface::SetBaseUrl(const BString& url)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const BString
|
||||||
|
WebAppInterface::_GetUserAgentVersionString()
|
||||||
|
{
|
||||||
|
app_info info;
|
||||||
|
|
||||||
|
if (be_app->GetAppInfo(&info) != B_OK) {
|
||||||
|
fprintf(stderr, "Unable to get the application info\n");
|
||||||
|
be_app->Quit();
|
||||||
|
return BString(USERAGENT_FALLBACK_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
BFile file(&info.ref, B_READ_ONLY);
|
||||||
|
|
||||||
|
if (file.InitCheck() != B_OK) {
|
||||||
|
fprintf(stderr, "Unable to access the application info file\n");
|
||||||
|
be_app->Quit();
|
||||||
|
return BString(USERAGENT_FALLBACK_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
BAppFileInfo appFileInfo(&file);
|
||||||
|
version_info versionInfo;
|
||||||
|
|
||||||
|
if (appFileInfo.GetVersionInfo(
|
||||||
|
&versionInfo, B_APP_VERSION_KIND) != B_OK) {
|
||||||
|
fprintf(stderr, "Unable to establish the application version\n");
|
||||||
|
be_app->Quit();
|
||||||
|
return BString(USERAGENT_FALLBACK_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
BString result;
|
||||||
|
result.SetToFormat("%ld.%ld.%ld", versionInfo.major, versionInfo.middle,
|
||||||
|
versionInfo.minor);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*! This method will devise a suitable User-Agent header value that
|
||||||
|
can be transmitted with HTTP requests to the server in order
|
||||||
|
to identify this client.
|
||||||
|
*/
|
||||||
|
const BString
|
||||||
|
WebAppInterface::_GetUserAgent()
|
||||||
|
{
|
||||||
|
AutoLocker<BLocker> lock(&fUserAgentLocker);
|
||||||
|
|
||||||
|
if (fUserAgent.IsEmpty()) {
|
||||||
|
fUserAgent.SetTo("HaikuDepot/");
|
||||||
|
fUserAgent.Append(_GetUserAgentVersionString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return fUserAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
WebAppInterface::SetPreferredLanguage(const BString& language)
|
WebAppInterface::SetPreferredLanguage(const BString& language)
|
||||||
{
|
{
|
||||||
@ -592,7 +653,7 @@ WebAppInterface::RetrieveScreenshot(const BString& code,
|
|||||||
listener.SetDownloadIO(stream);
|
listener.SetDownloadIO(stream);
|
||||||
|
|
||||||
BHttpHeaders headers;
|
BHttpHeaders headers;
|
||||||
headers.AddHeader("User-Agent", "X-HDS-Client");
|
headers.AddHeader("User-Agent", _GetUserAgent());
|
||||||
|
|
||||||
BHttpRequest request(url, isSecure, "HTTP", &listener);
|
BHttpRequest request(url, isSecure, "HTTP", &listener);
|
||||||
request.SetMethod(B_HTTP_GET);
|
request.SetMethod(B_HTTP_GET);
|
||||||
@ -715,7 +776,7 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
|||||||
|
|
||||||
BHttpHeaders headers;
|
BHttpHeaders headers;
|
||||||
headers.AddHeader("Content-Type", "application/json");
|
headers.AddHeader("Content-Type", "application/json");
|
||||||
headers.AddHeader("User-Agent", "X-HDS-Client");
|
headers.AddHeader("User-Agent", _GetUserAgent());
|
||||||
|
|
||||||
BHttpRequest request(url, isSecure, "HTTP", &listener, &context);
|
BHttpRequest request(url, isSecure, "HTTP", &listener, &context);
|
||||||
request.SetMethod(B_HTTP_POST);
|
request.SetMethod(B_HTTP_POST);
|
||||||
@ -764,5 +825,3 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,
|
|||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -101,6 +101,8 @@ public:
|
|||||||
BMessage& message);
|
BMessage& message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static const BString _GetUserAgentVersionString();
|
||||||
|
static const BString _GetUserAgent();
|
||||||
BString _FormFullUrl(const BString& suffix) const;
|
BString _FormFullUrl(const BString& suffix) const;
|
||||||
status_t _SendJsonRequest(const char* domain,
|
status_t _SendJsonRequest(const char* domain,
|
||||||
BString jsonString, uint32 flags,
|
BString jsonString, uint32 flags,
|
||||||
@ -108,6 +110,8 @@ private:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static BString fBaseUrl;
|
static BString fBaseUrl;
|
||||||
|
static BString fUserAgent;
|
||||||
|
static BLocker fUserAgentLocker;
|
||||||
BString fUsername;
|
BString fUsername;
|
||||||
BString fPassword;
|
BString fPassword;
|
||||||
BString fLanguage;
|
BString fLanguage;
|
||||||
|
Loading…
Reference in New Issue
Block a user