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 apl-haiku, 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  
    77
    88#include <stdio.h>
    99
     10#include <AppFileInfo.h>
     11#include <Application.h>
     12#include <Autolock.h>
    1013#include <File.h>
    1114#include <HttpHeaders.h>
    1215#include <HttpRequest.h>
    1316#include <Json.h>
    1417#include <Message.h>
     18#include <Roster.h>
    1519#include <Url.h>
    1620#include <UrlContext.h>
    1721#include <UrlProtocolListener.h>
    1822#include <UrlProtocolRoster.h>
    1923
     24#include "AutoLocker.h"
    2025#include "List.h"
    2126#include "PackageInfo.h"
    2227
    2328
    2429#define CODE_REPOSITORY_DEFAULT "haikuports"
    2530#define BASEURL_DEFAULT "https://depot.haiku-os.org"
     31#define USERAGENT_FALLBACK_VERSION "0.0.0"
    2632
    2733
    2834class JsonBuilder {
    enum {  
    274280
    275281
    276282BString WebAppInterface::fBaseUrl = BString(BASEURL_DEFAULT);
    277 
     283BString WebAppInterface::fUserAgent = BString();
     284BLocker WebAppInterface::fUserAgentLocker = BLocker();
    278285
    279286WebAppInterface::WebAppInterface()
    280287    :
    arguments_is_url_valid(const BString& value)  
    337344
    338345    BString scheme;
    339346    value.CopyInto(scheme, 0, schemeEnd);
    340  
     347
    341348    if (scheme != "http" && scheme != "https") {
    342349        fprintf(stderr, "the url scheme should be 'http' or 'https'\n");
    343350        return false;
    arguments_is_url_valid(const BString& value)  
    356363    indicate if the URL was acceptable.
    357364    \return B_OK if the base URL was valid and B_BAD_VALUE if not.
    358365 */
    359 status_t 
     366status_t
    360367WebAppInterface::SetBaseUrl(const BString& url)
    361368{
    362369    if (!arguments_is_url_valid(url))
    WebAppInterface::SetBaseUrl(const BString& url)  
    368375}
    369376
    370377
     378const BString
     379WebAppInterface::_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 */
     418const BString
     419WebAppInterface::_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
    371432void
    372433WebAppInterface::SetPreferredLanguage(const BString& language)
    373434{
    WebAppInterface::RetrieveScreenshot(const BString& code,  
    592653    listener.SetDownloadIO(stream);
    593654
    594655    BHttpHeaders headers;
    595     headers.AddHeader("User-Agent", "X-HDS-Client");
     656    headers.AddHeader("User-Agent", _GetUserAgent());
    596657
    597658    BHttpRequest request(url, isSecure, "HTTP", &listener);
    598659    request.SetMethod(B_HTTP_GET);
    WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,  
    715776
    716777    BHttpHeaders headers;
    717778    headers.AddHeader("Content-Type", "application/json");
    718     headers.AddHeader("User-Agent", "X-HDS-Client");
     779    headers.AddHeader("User-Agent", _GetUserAgent());
    719780
    720781    BHttpRequest request(url, isSecure, "HTTP", &listener, &context);
    721782    request.SetMethod(B_HTTP_POST);
    WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString,  
    764825    }
    765826    return status;
    766827}
    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:  
    101101                                    BMessage& message);
    102102
    103103private:
     104    static  const BString       _GetUserAgentVersionString();
     105    static  const BString       _GetUserAgent();
    104106            BString             _FormFullUrl(const BString& suffix) const;
    105107            status_t            _SendJsonRequest(const char* domain,
    106108                                    BString jsonString, uint32 flags,
    private:  
    108110
    109111private:
    110112    static  BString             fBaseUrl;
     113    static  BString             fUserAgent;
     114    static  BLocker             fUserAgentLocker;
    111115            BString             fUsername;
    112116            BString             fPassword;
    113117            BString             fLanguage;