[haiku-commits] haiku: hrev49515 - src/apps/haikudepot/model

  • From: waddlesplash@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 8 Aug 2015 03:18:10 +0200 (CEST)

hrev49515 adds 1 changeset to branch 'master'
old head: 3749038b534a5948026624805adf1ee14ff4caa6
new head: b8d10c6dc62727003c7d9533879987d521138630
overview:
http://cgit.haiku-os.org/haiku/log/?qt=range&q=b8d10c6dc627+%5E3749038b534a

----------------------------------------------------------------------------

b8d10c6dc627: HaikuDepot: Enhancements to the User-Agent header to include the
version.

Signed-off-by: Augustin Cavalier <waddlesplash@xxxxxxxxx>
Fixes #12262.

[ Andrew Lindesay <apl@xxxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision: hrev49515
Commit: b8d10c6dc62727003c7d9533879987d521138630
URL: http://cgit.haiku-os.org/haiku/commit/?id=b8d10c6dc627
Author: Andrew Lindesay <apl@xxxxxxxxxxxxxx>
Date: Thu Jul 30 10:21:46 2015 UTC
Committer: Augustin Cavalier <waddlesplash@xxxxxxxxx>
Commit-Date: Sat Aug 8 01:17:14 2015 UTC

Ticket: https://dev.haiku-os.org/ticket/12262

----------------------------------------------------------------------------

2 files changed, 70 insertions(+), 7 deletions(-)
src/apps/haikudepot/model/WebAppInterface.cpp | 73 ++++++++++++++++++++---
src/apps/haikudepot/model/WebAppInterface.h | 4 ++

----------------------------------------------------------------------------

diff --git a/src/apps/haikudepot/model/WebAppInterface.cpp
b/src/apps/haikudepot/model/WebAppInterface.cpp
index 78ed19e..921e7dd 100644
--- a/src/apps/haikudepot/model/WebAppInterface.cpp
+++ b/src/apps/haikudepot/model/WebAppInterface.cpp
@@ -7,22 +7,28 @@

#include <stdio.h>

+#include <AppFileInfo.h>
+#include <Application.h>
+#include <Autolock.h>
#include <File.h>
#include <HttpHeaders.h>
#include <HttpRequest.h>
#include <Json.h>
#include <Message.h>
+#include <Roster.h>
#include <Url.h>
#include <UrlContext.h>
#include <UrlProtocolListener.h>
#include <UrlProtocolRoster.h>

+#include "AutoLocker.h"
#include "List.h"
#include "PackageInfo.h"


#define CODE_REPOSITORY_DEFAULT "haikuports"
#define BASEURL_DEFAULT "https://depot.haiku-os.org";
+#define USERAGENT_FALLBACK_VERSION "0.0.0"


class JsonBuilder {
@@ -274,7 +280,8 @@ enum {


BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT);
-
+BString WebAppInterface::fUserAgent = BString();
+BLocker WebAppInterface::fUserAgentLocker = BLocker();

WebAppInterface::WebAppInterface()
:
@@ -337,7 +344,7 @@ arguments_is_url_valid(const BString& value)

BString scheme;
value.CopyInto(scheme, 0, schemeEnd);
-
+
if (scheme != "http" && scheme != "https") {
fprintf(stderr, "the url scheme should be 'http' or 'https'\n");
return false;
@@ -356,7 +363,7 @@ arguments_is_url_valid(const BString& value)
indicate if the URL was acceptable.
\return B_OK if the base URL was valid and B_BAD_VALUE if not.
*/
-status_t
+status_t
WebAppInterface::SetBaseUrl(const BString& 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
WebAppInterface::SetPreferredLanguage(const BString& language)
{
@@ -592,7 +653,7 @@ WebAppInterface::RetrieveScreenshot(const BString& code,
listener.SetDownloadIO(stream);

BHttpHeaders headers;
- headers.AddHeader("User-Agent", "X-HDS-Client");
+ headers.AddHeader("User-Agent", _GetUserAgent());

BHttpRequest request(url, isSecure, "HTTP", &listener);
request.SetMethod(B_HTTP_GET);
@@ -715,7 +776,7 @@ WebAppInterface::_SendJsonRequest(const char* domain,
BString jsonString,

BHttpHeaders headers;
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);
request.SetMethod(B_HTTP_POST);
@@ -764,5 +825,3 @@ WebAppInterface::_SendJsonRequest(const char* domain,
BString jsonString,
}
return status;
}
-
-
diff --git a/src/apps/haikudepot/model/WebAppInterface.h
b/src/apps/haikudepot/model/WebAppInterface.h
index 8cf8779..f5fffec 100644
--- a/src/apps/haikudepot/model/WebAppInterface.h
+++ b/src/apps/haikudepot/model/WebAppInterface.h
@@ -101,6 +101,8 @@ public:

BMessage& message);

private:
+ static const BString _GetUserAgentVersionString();
+ static const BString _GetUserAgent();
BString _FormFullUrl(const
BString& suffix) const;
status_t _SendJsonRequest(const
char* domain,
BString
jsonString, uint32 flags,
@@ -108,6 +110,8 @@ private:

private:
static BString fBaseUrl;
+ static BString fUserAgent;
+ static BLocker fUserAgentLocker;
BString fUsername;
BString fPassword;
BString fLanguage;


Other related posts:

  • » [haiku-commits] haiku: hrev49515 - src/apps/haikudepot/model - waddlesplash