Ticket #12262: 0001-HaikuDepot-enhancements-to-the-User-Agent-header-to-.patch
File 0001-HaikuDepot-enhancements-to-the-User-Agent-header-to-.patch, 5.0 KB (added by , 9 years ago) |
---|
-
src/apps/haikudepot/model/WebAppInterface.cpp
From 41187693afa592f86ff8003e434e6d0504183d28 Mon Sep 17 00:00:00 2001 From: Andrew Lindesay <apl@lindesay.co.nz> Date: Thu, 30 Jul 2015 22:21:46 +1200 Subject: [PATCH] HaikuDepot: enhancements to the User-Agent header to include the version --- src/apps/haikudepot/model/WebAppInterface.cpp | 73 ++++++++++++++++++++++++--- src/apps/haikudepot/model/WebAppInterface.h | 4 ++ 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/apps/haikudepot/model/WebAppInterface.cpp b/src/apps/haikudepot/model/WebAppInterface.cpp index 78ed19e..5567e99 100644
a b 7 7 8 8 #include <stdio.h> 9 9 10 #include <AppFileInfo.h> 11 #include <Application.h> 12 #include <Autolock.h> 10 13 #include <File.h> 11 14 #include <HttpHeaders.h> 12 15 #include <HttpRequest.h> 13 16 #include <Json.h> 14 17 #include <Message.h> 18 #include <Roster.h> 15 19 #include <Url.h> 16 20 #include <UrlContext.h> 17 21 #include <UrlProtocolListener.h> 18 22 #include <UrlProtocolRoster.h> 19 23 24 #include "AutoLocker.h" 20 25 #include "List.h" 21 26 #include "PackageInfo.h" 22 27 23 28 24 29 #define CODE_REPOSITORY_DEFAULT "haikuports" 25 30 #define BASEURL_DEFAULT "https://depot.haiku-os.org" 31 #define USERAGENT_FALLBACK_VERSION "0.0.0" 26 32 27 33 28 34 class JsonBuilder { … … enum { 274 280 275 281 276 282 BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT); 277 283 BString WebAppInterface::fUserAgent = BString(); 284 BLocker WebAppInterface::fUserAgentLocker = BLocker(); 278 285 279 286 WebAppInterface::WebAppInterface() 280 287 : … … arguments_is_url_valid(const BString& value) 337 344 338 345 BString scheme; 339 346 value.CopyInto(scheme, 0, schemeEnd); 340 347 341 348 if (scheme != "http" && scheme != "https") { 342 349 fprintf(stderr, "the url scheme should be 'http' or 'https'\n"); 343 350 return false; … … arguments_is_url_valid(const BString& value) 356 363 indicate if the URL was acceptable. 357 364 \return B_OK if the base URL was valid and B_BAD_VALUE if not. 358 365 */ 359 status_t 366 status_t 360 367 WebAppInterface::SetBaseUrl(const BString& url) 361 368 { 362 369 if (!arguments_is_url_valid(url)) … … WebAppInterface::SetBaseUrl(const BString& url) 368 375 } 369 376 370 377 378 const BString 379 WebAppInterface::_GetUserAgentVersionString() 380 { 381 app_info info; 382 383 if (be_app->GetAppInfo(&info) != B_OK) { 384 fprintf(stderr,"unable to get the application info\n"); 385 be_app->Quit(); 386 return BString(USERAGENT_FALLBACK_VERSION); 387 } 388 389 BFile file(&info.ref, B_READ_ONLY); 390 391 if (file.InitCheck() != B_OK) { 392 fprintf(stderr,"unable to access the application info file\n"); 393 be_app->Quit(); 394 return BString(USERAGENT_FALLBACK_VERSION); 395 } 396 397 BAppFileInfo appFileInfo(&file); 398 version_info versionInfo; 399 400 if (appFileInfo.GetVersionInfo( 401 &versionInfo, B_APP_VERSION_KIND) != B_OK) { 402 fprintf(stderr,"unable to establish the application version\n"); 403 be_app->Quit(); 404 return BString(USERAGENT_FALLBACK_VERSION); 405 } 406 407 BString result; 408 result.SetToFormat("%ld.%ld.%ld", versionInfo.major, versionInfo.middle, 409 versionInfo.minor); 410 return result; 411 } 412 413 414 /*! This method will devise a suitable User-Agent header value that 415 can be transmitted with HTTP requests to the server in order 416 to identify this client. 417 */ 418 const BString 419 WebAppInterface::_GetUserAgent() 420 { 421 AutoLocker<BLocker> lock(&fUserAgentLocker); 422 423 if (fUserAgent.IsEmpty()) { 424 fUserAgent.SetTo("HaikuDepot/"); 425 fUserAgent.Append(_GetUserAgentVersionString()); 426 } 427 428 return fUserAgent; 429 } 430 431 371 432 void 372 433 WebAppInterface::SetPreferredLanguage(const BString& language) 373 434 { … … WebAppInterface::RetrieveScreenshot(const BString& code, 592 653 listener.SetDownloadIO(stream); 593 654 594 655 BHttpHeaders headers; 595 headers.AddHeader("User-Agent", "X-HDS-Client");656 headers.AddHeader("User-Agent", _GetUserAgent()); 596 657 597 658 BHttpRequest request(url, isSecure, "HTTP", &listener); 598 659 request.SetMethod(B_HTTP_GET); … … WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, 715 776 716 777 BHttpHeaders headers; 717 778 headers.AddHeader("Content-Type", "application/json"); 718 headers.AddHeader("User-Agent", "X-HDS-Client");779 headers.AddHeader("User-Agent", _GetUserAgent()); 719 780 720 781 BHttpRequest request(url, isSecure, "HTTP", &listener, &context); 721 782 request.SetMethod(B_HTTP_POST); … … WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, 764 825 } 765 826 return status; 766 827 } 767 768 -
src/apps/haikudepot/model/WebAppInterface.h
diff --git a/src/apps/haikudepot/model/WebAppInterface.h b/src/apps/haikudepot/model/WebAppInterface.h index 8cf8779..f5fffec 100644
a b public: 101 101 BMessage& message); 102 102 103 103 private: 104 static const BString _GetUserAgentVersionString(); 105 static const BString _GetUserAgent(); 104 106 BString _FormFullUrl(const BString& suffix) const; 105 107 status_t _SendJsonRequest(const char* domain, 106 108 BString jsonString, uint32 flags, … … private: 108 110 109 111 private: 110 112 static BString fBaseUrl; 113 static BString fUserAgent; 114 static BLocker fUserAgentLocker; 111 115 BString fUsername; 112 116 BString fPassword; 113 117 BString fLanguage;