From b45e8b1ef93ed1d30769f08cd4db01c7a0c344f6 Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Mon, 19 Mar 2018 20:42:22 +0000 Subject: [PATCH] HaikuDepot : additional debugging for json-rpc invocations --- headers/private/shared/Json.h | 2 + .../haikudepot/server/WebAppInterface.cpp | 65 ++++++++++++++----- src/apps/haikudepot/server/WebAppInterface.h | 6 +- src/kits/shared/Json.cpp | 13 +++- 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/headers/private/shared/Json.h b/headers/private/shared/Json.h index 73746f2177..3ec9c4dee8 100644 --- a/headers/private/shared/Json.h +++ b/headers/private/shared/Json.h @@ -21,6 +21,8 @@ class BJson { public: static status_t Parse(const char* JSON, BMessage& message); + static status_t Parse(const char* JSON, size_t length, + BMessage& message); static status_t Parse(const BString& JSON, BMessage& message); static void Parse(BDataIO* data, BJsonEventListener* listener); diff --git a/src/apps/haikudepot/server/WebAppInterface.cpp b/src/apps/haikudepot/server/WebAppInterface.cpp index b4e7c0ec09..3d77e79517 100644 --- a/src/apps/haikudepot/server/WebAppInterface.cpp +++ b/src/apps/haikudepot/server/WebAppInterface.cpp @@ -559,21 +559,33 @@ WebAppInterface::AuthenticateUser(const BString& nickName, status_t -WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, - uint32 flags, BMessage& reply) const +WebAppInterface::_SendJsonRequest(const char* domain, BDataIO* requestData, + size_t requestDataSize, uint32 flags, BMessage& reply) const { - if (!ServerHelper::IsNetworkAvailable()) + if (!ServerHelper::IsNetworkAvailable()) { + if (Logger::IsDebugEnabled()) { + printf("dropping json-rpc request to ...[%s] as network is not " + "available\n", domain); + } return HD_NETWORK_INACCESSIBLE; + } - if (!ServerSettings::IsClientTooOld()) + if (ServerSettings::IsClientTooOld()) { + if (Logger::IsDebugEnabled()) { + printf("dropping json-rpc request to ...[%s] as client is too " + "old\n", domain); + } return HD_CLIENT_TOO_OLD; - - if (Logger::IsTraceEnabled()) - printf("_SendJsonRequest(%s)\n", jsonString.String()); + } BUrl url = ServerSettings::CreateFullUrl(BString("/__api/v1/") << domain); bool isSecure = url.Protocol() == "https"; + if (Logger::IsDebugEnabled()) { + printf("will make json-rpc request to [%s]\n", + url.UrlString().String()); + } + ProtocolListener listener(Logger::IsTraceEnabled()); BUrlContext context; @@ -595,10 +607,7 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, context.AddAuthentication(url, authentication); } - BMemoryIO* data = new BMemoryIO( - jsonString.String(), jsonString.Length() - 1); - - request.AdoptInputData(data, jsonString.Length() - 1); + request.AdoptInputData(requestData, requestDataSize); BMallocIO replyData; listener.SetDownloadIO(&replyData); @@ -611,6 +620,11 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, int32 statusCode = result.StatusCode(); + if (Logger::IsDebugEnabled()) { + printf("did receive json-rpc response http status [%" B_PRId32 "] " + "from [%s]\n", statusCode, url.UrlString().String()); + } + switch (statusCode) { case B_HTTP_STATUS_OK: break; @@ -625,14 +639,29 @@ WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, return B_ERROR; } - jsonString.SetTo(static_cast(replyData.Buffer()), - replyData.BufferLength()); - if (jsonString.Length() == 0) - return B_ERROR; - - status_t status = BJson::Parse(jsonString, reply); + status_t status = BJson::Parse( + static_cast(replyData.Buffer()), replyData.BufferLength(), + reply); if (Logger::IsTraceEnabled() && status == B_BAD_DATA) { - printf("Parser choked on JSON:\n%s\n", jsonString.String()); + BString resultString(static_cast(replyData.Buffer()), + replyData.BufferLength()); + printf("Parser choked on JSON:\n%s\n", resultString.String()); } return status; } + + +status_t +WebAppInterface::_SendJsonRequest(const char* domain, BString jsonString, + uint32 flags, BMessage& reply) const +{ + if (Logger::IsTraceEnabled()) + printf("_SendJsonRequest(%s)\n", jsonString.String()); + + BMemoryIO* data = new BMemoryIO( + jsonString.String(), jsonString.Length() - 1); + + return _SendJsonRequest(domain, data, jsonString.Length() - 1, flags, + reply); +} + diff --git a/src/apps/haikudepot/server/WebAppInterface.h b/src/apps/haikudepot/server/WebAppInterface.h index 62b46b3f40..09690a78db 100644 --- a/src/apps/haikudepot/server/WebAppInterface.h +++ b/src/apps/haikudepot/server/WebAppInterface.h @@ -96,7 +96,11 @@ private: status_t _SendJsonRequest(const char* domain, BString jsonString, uint32 flags, BMessage& reply) const; - + status_t _SendJsonRequest(const char* domain, + BDataIO* requestData, + size_t requestDataSize, uint32 flags, + BMessage& reply) const; + private: BString fUsername; BString fPassword; diff --git a/src/kits/shared/Json.cpp b/src/kits/shared/Json.cpp index 9e34d234da..14c4bcf950 100644 --- a/src/kits/shared/Json.cpp +++ b/src/kits/shared/Json.cpp @@ -126,9 +126,9 @@ BJson::Parse(const BString& JSON, BMessage& message) status_t -BJson::Parse(const char* JSON, BMessage& message) +BJson::Parse(const char* JSON, size_t length, BMessage& message) { - BMemoryIO* input = new BMemoryIO(JSON, strlen(JSON)); + BMemoryIO* input = new BMemoryIO(JSON, length); ObjectDeleter inputDeleter(input); BJsonMessageWriter* writer = new BJsonMessageWriter(message); ObjectDeleter writerDeleter(writer); @@ -140,6 +140,13 @@ BJson::Parse(const char* JSON, BMessage& message) } +status_t +BJson::Parse(const char* JSON, BMessage& message) +{ + return Parse(JSON, strlen(JSON), message); +} + + /*! The data is read as a stream of JSON data. As the JSON is read, events are raised such as; - string @@ -768,4 +775,4 @@ BJson::ParseNumber(JsonParseContext& jsonParseContext) } } -} // namespace BPrivate \ No newline at end of file +} // namespace BPrivate